[JAVA] Composite Pattern

Call method recursively
Composite Pattern can represent a tree structure
(If it becomes a tree structure, you can consider Composite) </ font>

Design Pattarm MENU

Check with the following class structure

class Explanation
Handle different objects with face type
sam.class implement face
samList.class implement face
Hold List and call recursively
user(Main.class) Check the operation
  • User Includes the meaning that other developers use this pattern

Below is the sample code


interface face{void print();}


class sam implements face{
      public void print(){


class samList implements face{
      List   list = new ArrayList();
      void   add(face fc){list.add(fc);}
      public void print(){
             Iterator it = list.iterator();
                   face fit = (face) it.next();
                   fit.print(); //sam if fit is sam type.class print()Is called and this does not recurse
                                //print samList if fit is of type samList()Is called recursively


public static void main(String[] args){
  samList  sam1 = new samList();
  samList  sam2 = new samList();
  samList  sam3 = new samList();
  sam2.add(new  samList());
  sam2.add(new  sam());
  sam3.add(new  samList());
  sam3.add(new  sam());

Recommended Posts

Composite pattern
Composite Pattern
Design pattern ~ Composite ~
Memento Pattern
Mediator pattern
Iterator pattern
Observer Pattern
Builder pattern
Bridge Pattern
Command Pattern
Strategy pattern
Iterator Pattern
Adapter Pattern
Strategy Pattern
Singleton Pattern
Singleton pattern
Prototype Pattern
Facade Pattern
Decorator pattern
Flyweight Pattern
Decorator Pattern
Mediator Pattern
Facade pattern
Visitor Pattern
Bridge pattern
abstract Factory Pattern
Design pattern ~ Builder ~
[Java] Strategy pattern
Design pattern ~ Visitor ~
Java design pattern
java callback pattern
Design pattern ~ State ~
Design pattern ~ Strategy ~
Design pattern ~ Singleton ~
Design pattern (2): Builder
[Java] Singleton pattern
Design pattern ~ Command ~
Abstract Factory pattern
hibernate composite key
Design pattern ~ Iterator ~
Design pattern ~ Facade ~
Design pattern ~ Bridge ~
Design pattern ~ Mediator ~
Template Method pattern
Design pattern ~ Decorator ~
Template Method Pattern
Design pattern ~ Interpreter ~
Factory Method pattern
Design pattern ~ Observer ~
Design pattern ~ Prototype ~
[Java] Adapter pattern
Design pattern ~ Memento ~
Design pattern ~ Adapter ~
Design pattern ~ Flyweight ~
Java pattern memo