mercredi 8 mai 2019

Why are composite methods declared in Component interface?

In the gang of four book, the Composite pattern is defined with the diagram enter image description here Why are methods only related to Composite part of the Component interface? If the methods (Add, Remove, Getchild) were part of Composite and not the general interface, Composites and Leaves would still both implement the Component interface and thus be used interchangably as is one of the purposes of the Composite pattern. By having both Leaf and Composite implement Component (that only requires Operation now), the client would still treat them the same like

Component anobject = new Leaf();
Component another = new Composite();
another.Operation();
anobject.Operation();

Aucun commentaire:

Enregistrer un commentaire