53884

How do I know the generic object that the Observer class sends in Java?

Question:

I am implementing the Observer / Observable pattern using Java. However, I am encountering a problem in the Observer portion of my code.

<strong>Observable</strong>

public class Model extends Observable { public void notify() { this.setChanged(); this.notifyObservers(new ArrayList<A>()); this.notifyObservers(new ArrayList<B>()); } }

<strong>Observer</strong>

public class View implements Observer { @Override public void update(Observable observable, Object object) { // TODO: If object is ArrayList<A>? System.out.println("A"); // TODO: If object is ArrayList<B>? System.out.println("B"); } }

How would I fill in the TODO comments to check for the generic on the ArrayList? Is this even possible? (I would prefer to do it without implementing more classes, if possible.)

Answer1:

An Observable should send one and only one type of data.

public class ModelA extends Observable { public void notify() { this.setChanged(); this.notifyObservers(new ArrayList<A>()); } } public class ModelB extends Observable { public void notify() { this.setChanged(); this.notifyObservers(new ArrayList<B>()); } }

Your other alternative is to put ArrayList<A> and ArrayList<B> into a class. You can notify your observers with that class.

Answer2:

You could use instanceof to see what type is your object but you are misusing the pattern.<br /> The idea is that the Observable has a well defined interface and all the Observer needs to know is to use the API to pass the notification.<br /> Logic to know what exactly <em>is</em> the Observable should not be mixed in the code.<br /> From your question sounds to me you have 2 Observable and they should keep their own listeners <em>interested specifically in them</em> for notification. Not one combined

Recommend

  • drawing random circles, storing their coorindates in an array
  • Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop
  • Regarding starting the threads on a condition
  • WPF Visiblity Binding to Boolean Expression with multiple Variables
  • revitapi ironpython ToRoom returns “indexer # object”
  • Access variable of ScriptContext using Nashorn JavaScript Engine (Java 8)
  • Connect .sks to skscene.h
  • Debug.DrawLine not showing in the GameView
  • GridView breaks while scrolling
  • Jackson Parser: ignore deserializing for type mismatch
  • How to Cache Real-time Data?
  • How to use RequestBodyAdvice
  • Q promise. Difference between .when and .then
  • How to access EntityManager inside Entity class in EJB3
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Opengl-es onTouchEvents problem or a draw problem? [closed]
  • Incrementing object id automatically JS constructor (static method and variable)
  • Does CUDA 5 support STL or THRUST inside the device code?
  • When should I choose bucket sort over other sorting algorithms?
  • Hazelcast - OperationTimeoutException
  • To display the title for the current loaction in map in iphone
  • Akka Routing: Reply's send to router ends up as dead letters
  • AT Commands to Send SMS not working in Windows 8.1
  • Cannot Parse HTML Data Using Android / JSOUP
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Is it possible to post an object from jquery to bottle.py?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?