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
So whenever I use an Observable object I am going to use something like this
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:
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?
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.
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
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