Question

I have a question about a class diagram in the book "Design Patterns: Elements of Reusable Object-Oriented Software" by the Group of Four.

Class diagram

In this diagram, there's an abstract class named Graphic with some abstract methods (Draw, Add, etc.). There's also a subclass named Picture witch overload the abstract methods of the Graphic class. I fully understand that.

My question is, in the other subclasses (Line, Rectangle and Text), there's no mention of overriding the abstract methods of the Graphic class. It is just ignored for simplicity ? If they have been ignored, I know there's no need to have the "add", "remove" and "getChild" methods in the Line, Rectangle or Text class. There's obvioustly something that I don't understand.

I know the book uses examples in c++ and Smalltalk, maybe that in one of these languages, the abstract methods don't act like in Java (subclasses must override parent abstract methods.).

Thanks for your help.

Was it helpful?

Solution 3

I found the answer many pages later in the book. They gives 2 ways to implement the Composite Pattern witch this UML refers to.

  1. Add the methods add, remove, getChild in all the leaves classes (Line, Rectangle and Text), and make them do nothing or throw exception if they are used (for exemple, if you want to add child to Text, you are forbidden).

  2. Remove the abstract methods add, remove, getChild from the Graphic class, and keep the concrete methods in the Picture class (composite).

I personally think the second one is the way to go. It's way more simpler to do and it don't make confusion.

Thanks for the replies

OTHER TIPS

Abstract methods need not be overridden in subclasses in Java; such subclasses are again considered abstract. So in this example Line, Rectangle, Text are abstract subclasses.

I too feel that it was ignored for some reasons or is a mistake. Because if the Line, Rectangle and Texts were abstract classes, class name should be represented in italic like Graphic. They may have omitted Add, Remove and GetChild operations of these classes for their reasons..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top