• Post Reply Bookmark Topic Watch Topic
  • New Topic

Observer implementation question  RSS feed

 
Brett Phagan
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a USB Observable that notify's observers when one of our devices is connected or disconnected.
Currently, in the Update method, I check a field in USB to see if connected or disconnected.
If connected, I get the vendorID and productID fields from the USB class.
I then connect to the USB device and access data.

However, I feel like this is tightly coupled since I need to check fields and pass info back and forth.

My thought is to send the 3 pieces of info from the USB so I can create a new object to access the data to be more loosely coupled.

My issue is how to pass the 3 pieces of info to observer.
Boolean connectUSB
int vendorID
int productID

from what I can tell, only one Object argument can be sent to Observers.
Should I create a new Object that contains this info and pass that?
Or is there some other way to pass the info.
I use getters now, but this makes it seem like it's tightly coupled.

I could possibly pass the hid object, but I plan to add serial UART option instead of USB down the road.

I have my app running. but it is very tightly coupled and I am trying to loosen it up.

I am very much a greenhorn so please be gentle.

Regards,
Brett
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use the Observer/Observable types, they're horribly designed. Instead, you should make your own listener interfaces to solve this problem.

Create an interface UsbDeviceListener that contains methods for the different kinds of events that can occur, for instance:

Your event class should be an immutable class that holds all the data related to the event, such as vendorID and productID.

Now all objects that want to listen to your USB class can just register their listeners with some addUsbDeviceListener() method defined on your USB class.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are 2 methods really required? What info can possibly be contained in the deviceDisconnected();?

Why not have a connectionStatusChanged(UsbDeviceEvent event); instead? The UsbDeviceEvent can then encapsulate an USBDevice object which in turn encapsulates the required info like vendorId, productId etc. In case of disconnection, the encapsulated USBDevice object will of course be null (This is presuming it is NOT possible to sense which device disconnected, so info like vendorId etc are not available)
 
Stephan van Hulst
Saloon Keeper
Posts: 7817
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, two methods are not required. I just like that different events trigger different method calls, because it allows you to implement very different actions without using an if-statement. It also allows you to easily react to only one of the two cases if you have an adapter class:

 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!