Get elements of Composite Patter </ font>
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 |
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