Visitor Pattern

Get elements of Composite Patter </ font>

Design Pattarm MENU

Check with the following class structure

class Explanation
interface
face
Type and accept()To define
faceImple.class implement face
faceList.class implement face
Have a List
Has a method that issues a List iterator
Visitor.class Provide a Visitor Pattern
user(Main.class) Check the operation
  • User Includes the meaning that other developers use this pattern

interface_face


interface face{
  void accept(Visitor visit);
}

faceImple.class


class faceImple implements face{
  public void accept(Visitor visitor){
         System.out.println("."+this);}
}

faceList.class


class faceList implements face{
  List ls = new ArrayList();
  void      add( face fc){ls.add(fc);}
  public    void accept(Visitor visit){
            visit.visitor(this);}
  Iterator  iterate(){return ls.iterator();}
}

Visitor.class


class Visitor{
      void visitor( faceImple imple ){
           imple.accept(this);}
      void visitor( faceList list ){
           Iterator it = list.iterate();
           while(it.hasNext()){
                 face fc = (face) it.next();
                 System.out.println("/"+fc);
                 fc.accept(this);
           }
      }
}

user(Main.class)


public static void main(String[] args){
       faceList f1 = new faceList();
       faceList f2 = new faceList();
       faceList f3 = new faceList();
       f3.add(new  faceImple());
       f3.add(new  faceImple());
       f2.add(new  faceImple());
       f2.add(f3);
       f1.add(f2);
       f1.accept(new Visitor());
}

Recommended Posts

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