行为型模式-访问者模式
Posted liunianfeiyu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了行为型模式-访问者模式相关的知识,希望对你有一定的参考价值。
行为型模式又可以分成以下四类:第一类:通过父类与子类的关系进行实现。第二类:两个类之间。第三类:类的状态。第四类:通过中间类
访问者模式属于第四类,通过中间类
分为访问者和被访问者,被访问者接受访问者,访问者就可以操作被访问者
package behavior.pattern.intermediator.visitor; public interface Element { public void accept(Visitor visitor); }
package behavior.pattern.intermediator.visitor; public class ConcreteElementA implements Element { private String name; public ConcreteElementA(String name) { this.name = name; } public String getName() { return name; } @Override public void accept(Visitor visitor) { visitor.visitConcreteElementA(this); } }
package behavior.pattern.intermediator.visitor; public class ConcreteElementB implements Element{ private String name; public ConcreteElementB(String name) { this.name = name; } public String getName() { return name; } @Override public void accept(Visitor visitor) { visitor.visitConcreteElementB(this); } }
package behavior.pattern.intermediator.visitor; public interface Visitor { public void visitConcreteElementA(ConcreteElementA concreteElementA); public void visitConcreteElementB(ConcreteElementB concreteElementB); }
package behavior.pattern.intermediator.visitor; public class ConcreteVisitor1 implements Visitor { public void visitConcreteElementA(ConcreteElementA concreteElementA){ System.out.println(concreteElementA.getName()); } public void visitConcreteElementB(ConcreteElementB concreteElementB){ System.out.println(concreteElementB.getName()); } }
package behavior.pattern.intermediator.visitor; public class ConcreteVisitor2 implements Visitor { @Override public void visitConcreteElementA(ConcreteElementA concreteElementA) { System.out.println(concreteElementA.getName()); } @Override public void visitConcreteElementB(ConcreteElementB concreteElementB) { System.out.println(concreteElementB.getName()); } }
package behavior.pattern.intermediator.visitor; import java.util.ArrayList; import java.util.Collection; public class ObjectStructure { private Collection<Element> elements; public ObjectStructure(Collection<Element> elements) { this.elements = elements; } public ObjectStructure() { elements = new ArrayList<Element>(); } public void attach(Element element) { elements.add(element); } public void detach(Element element) { elements.remove(element); } public void accept(Visitor visitor) { for(Element element: elements) { element.accept(visitor); } } }
定义了两个访问者和两个被访问者,通过一个中间类ObjectStructure,将两者联系起来
测试代码
package behavior.pattern.intermediator.visitor; public class Client { public static void main(String[] args) { ConcreteElementA elementA = new ConcreteElementA("ElementA"); ConcreteElementB elementB = new ConcreteElementB("ElementB"); ConcreteElementB elementA2 = new ConcreteElementB("ElementA2"); ConcreteVisitor1 visitor1 = new ConcreteVisitor1(); ConcreteVisitor2 visitor2 = new ConcreteVisitor2(); ObjectStructure os = new ObjectStructure(); os.attach(elementA); os.attach(elementA2); os.attach(elementB); os.detach(elementA2); os.accept(visitor1); System.out.println("-------------------------------------------"); os.accept(visitor2); } }
运行结果
以上是关于行为型模式-访问者模式的主要内容,如果未能解决你的问题,请参考以下文章
设计模式 行为型模式 -- 观察者模式(发布-订阅(Publish/Subscribe)模式)