Composite design template and empty overrides

In a composite design pattern, we process hierarchies in the manner shown below in UML.

Composite design pattern

But if you look at the Leaf class in the above figure, you will see that it has empty implementations of addComponent, removeComponent and getChild , since these methods do not apply to the Leaf node. What I really need to know is that we don’t need to implement empty / no-op methods?

: , , . , Leaf node , ? , , , , . Leaf node, parent-child, .

+4
1

addComponent() removeComponent() getChild() Composite. , API Component.

, Leaf , Leaf , Component. , .

, , java swing. java.awt.Component java.awt.Container.

, , empty/no-op?

Component no-op addComponent() removeComponent() getChild().

public abstract class Component {

     public abstract doOperation(); // still need to be implemented by subclasses


     /**
      * Empty stub method. Subclasses may override it.
      */
     public void addComponent(Component comp){
     }

     public void removeComponent(Component comp){
     }

     public Component getChild(int index){
         return null;
     }
}
+2

Source: https://habr.com/ru/post/1532093/


All Articles