码迷,mamicode.com
首页 > 其他好文 > 详细

行为型模式-访问者模式

时间:2018-12-21 01:02:08      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:term   media   int   通过   rem   访问者   col   之间   objects   

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

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

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

 

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

 

  运行结果

技术分享图片

行为型模式-访问者模式

标签:term   media   int   通过   rem   访问者   col   之间   objects   

原文地址:https://www.cnblogs.com/liunianfeiyu/p/10153089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!