• Post Reply Bookmark Topic Watch Topic
  • New Topic

HeadFirst Design Pattern question  RSS feed

 
Kumar Jaya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to design patterns and am not able to understand the Observable pattern of java.util pattern.

In the first chapter I learned the second design principle saying "Program to interface not to implementation". That being said when I moved to the second design pattern the Observer Pattern chapter, I came across the dark side of java.util.Observable where the picture talks about violating the OO design principle of programming to interfaces and not implementation. I didnot get this point, can any one well-versed in this concept throw some light on it.

Suppose I have an class BASE and a subclass EXTENDED which extends BASE, isn't the whole point of the design principle
Program to interface not to implementation
mean


So whenever I use an Observable object I am going to use something like this


Regards
Kumaravel J
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is coding to implementation:

And here is coding to interface:
Do you now understand the difference?
Btw, I advise you to read Head First Object oriented Analysis and Design before Patterns. I think this order is better.
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kumar:

Programming against an interface means exactly that. For example, the ArrayList class is a concrete implementation of the List interface. So, to program against the List interface, what you would do is this:

If, later on, you found that ArrayList wasn't the right class, and another List implementation worked better (say, LinkedList), you could easily change the previous line to this:

And not have to change anything else.

John.
 
Kumar Jaya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to the replies.., I do understand what programming to an interface means, but my question was regarding the dark side of using Obervable pattern. I dont understand why we are violating the design principle of 'programming to interface and not to implementation' if we use java.util.Observable?



 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give us a code snippet where this principle is violated. I don't understand where have you seen that..
 
Kumar Jaya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stanislav, but the whole code in in Head First - Design Patterns (on page 71), it says generally that if we use java.util.Observable then we are violating the principle. So thats what I dont understand better. What I think is that even if we use util Observable class then we can program to interfaces instead of implementation. I was told this forum is overseen by the authors of that book, so I made my question here.

Thanks again
Regards
Kumar
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it says generally that if we use java.util.Observable then we are violating the principle
You understood wrong. The book says that java.util.Observer - is a class and we have some problems because of that. But it doesn't break the principle of coding to interface, this principle has no deals to Observer at all(well it does, but in the other way)! And further in the book they say that it is not a good idea to use java.util.Observer in your own projects. So don't waste your time on this problem
 
Kumar Jaya
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be Stanislav, The picture asks the question Dosen't java.util.Observable violate our OO design principle of programming to interfaces not implementation, for which the answer was Yes, good catch on the first line.

That made me think that I got the whole concept wrong principle of programming to interfaces not implementation

Thanks for the reply
 
stanislav bashkirtsev
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good catch is in the meaning of good question
Programming to interface means that you should use base classes in all places where it is possible.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!