通知 网站从因情语写改为晴雨,这个网站的模板也从calmlog_ex改为 whimurmur

行为型模式-访问者模式

5213人浏览 / 0人评论 / | 作者:因情语写  | 分类: 设计模式与算法  | 标签: 设计模式与算法  | 

作者:因情语写

链接:https://www.proprogrammar.com/article/456

声明:请尊重原作者的劳动,如需转载请注明出处


  行为型模式又可以分成以下四类:第一类:通过父类与子类的关系进行实现。第二类:两个类之间。第三类:类的状态。第四类:通过中间类

  访问者模式属于第四类,通过中间类

  分为访问者和被访问者,被访问者接受访问者,访问者就可以操作被访问

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);
    }
}

  运行结果


亲爱的读者:有时间可以点赞评论一下

点赞(0) 打赏

全部评论

还没有评论!
广告位-帮帮忙点下广告