• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Observer implementation question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 14010
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14010
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic