Skip to main content
11 events
when toggle format what by license comment
Aug 15, 2014 at 10:50 comment added Aviv Cohn My answer about the arg is that it's most probably the info object in the 'push' model. I don't know why you'd need it when pulling.
Aug 15, 2014 at 10:49 comment added USer22999299 Ok , cool i did not get yet my answer about the arg but you saved me many other questions , thank you!
Aug 15, 2014 at 10:47 vote accept USer22999299
Aug 15, 2014 at 10:45 comment added Aviv Cohn In OO code, in general down-casting is a bad sign. 90% of the time you can replace it with a cooler, more elegant polymorphic solution.
Aug 15, 2014 at 10:44 comment added Aviv Cohn So if you want to implement the 'pull' model - don't do down-casting! Instead, make a more specific Observable interface with getters. Like in my example: AgeObservable with a getAge() method. Then your observer code can talk to this interface without down-casting. This way it won't need to change with every new AgeObservable added to the program, allowing for extensibility and flexibility. The whole point of patterns such as Observer is acheving this flexibility.
Aug 15, 2014 at 10:42 comment added Aviv Cohn If your observer code relies on down-casting, it will need to change with every new class implementing the Observable interface, thus defeating the whole purpose of the pattern. This is an example of why 'programming to an interface, not a concrete implementation' is a good thing - code that works against an interface will work unchanged with every new class that implements the interface. If you rely on the concrete implementation - e.g. by doing downcasting, your code will need to change with every new Observable class. Beating the whole purpose.
Aug 15, 2014 at 10:40 comment added Aviv Cohn Are you talking about 'push' or 'pull'? The idea in 'push' is that the observer doesn't need to know about the type of the subject. Thus you avoid down-casting. Down-casting is mostly bad becasue it creates tighter coupling between the two sides - the observer now knows the exact type of the subject, which beats the whole purpose. What if later a new class implements the Observable interface? If you don't rely on the concrete type of the subject - i.e. by not doing down-casting - the current observer code will work just as well, polymorphically, with the new subject.
Aug 15, 2014 at 10:35 comment added USer22999299 Yeah this is what i was thinking as well , but then why do we send the obs ? :) if we down casting we can just pull the information that we need.
Aug 15, 2014 at 10:33 comment added Aviv Cohn I'm not sure, but I think the Object arg is meant for the 'push' model, where arg is an 'information bundle' sent to the observer.
Aug 15, 2014 at 10:14 comment added USer22999299 Thank you for this explanation. Regrading to my question , as you said while using pull , i need to down casting and using the getters , but what is the arg for in the observer update method? - public void update(Observable obs, Object arg) ?
Aug 15, 2014 at 10:07 history answered Aviv Cohn CC BY-SA 3.0