• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Observer Design Pattern doubt: Source Head First Design Patterrn

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand the design patterns reading HeadFirst Design Pattern. I have a doubt while trying to understand Observer pattern.

The Subject interface:


The Observer interface:



Statement:
Any Observer implementing class would need to be passed the reference of the Subject interface, in order for it to be added/removed as an observer in the list of the Subject, or more importantly to be notified for the update.

Question:
Now since the observer has the reference to the class implementing Subject interface, the observer can wrongfully call the notifyobservers() method and all the observers will be notified wrongly.

Can anyone help if my understanding is correct or wrong? If correct why such an implementation. thanks in advance.
 
Marshal
Posts: 68066
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to its API documentation, one shouldn't use Observer any more.

Adding you to a more general forum in the hope of getting you more attention.
 
Sheriff
Posts: 15038
251
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dinkar Joshi wrote:
Question:
Now since the observer has the reference to the class implementing Subject interface, the observer can wrongfully call the notifyobservers() method and all the observers will be notified wrongly.


Valid question and concern. If you look at the example implementations on the Wikipedia article, you'll see the notify method is private, which it should be.
 
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:If you look at the example implementations on the Wikipedia article, you'll see the notify method is private, which it should be.



That's true. But the Java example there doesn't have a "Source" interface to be implemented. If it did, then it couldn't have a private "notify" method.

So yes, that "Source" interface is problematic.
 
Dinkar Joshi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paul, just don't understand why would such a famous book, include this example. I have just started , may be further down the book, there is some clarification or a better design pattern.
 
Dinkar Joshi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Junilu Lacar wrote:If you look at the example implementations on the Wikipedia article, you'll see the notify method is private, which it should be.



That's true. But the Java example there doesn't have a "Source" interface to be implemented. If it did, then it couldn't have a private "notify" method.

So yes, that "Source" interface is problematic.



Also the UML in the wiki page UML shows the Subject  to have a public method notifyObservers(). Since it has a + sign to it.
 
Paul Clapham
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I notice that it was published in 2004. People's understanding of system design has progressed a lot since then.

 
Dinkar Joshi
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt, since wikipedia also has the same Subject layout
 
Paul Clapham
Marshal
Posts: 25194
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And as Wikipedia says:

Typically, the observer pattern is implemented so the "subject" being "observed" is part of the object for which state changes are being observed (and communicated to the observers). This type of implementation is considered "tightly coupled", forcing both the observers and the subject to be aware of each other and have access to their internal parts...



and

The observer pattern, as described in the GoF book, is a very basic concept...



However you'll notice that the "Java" section in that article doesn't have the problem you found. It uses a private notifyObservers method, for example.
 
What a show! What atmosphere! What fun! What a tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!