java23种设计模式-访问者模式

26 篇文章 0 订阅
22 篇文章 0 订阅
定义

封装一些作用于某种数据结构中各元素的操作,它可以在不改变这个数据结构的前提下定义作用于这些元素的新的操作。

UML

角色
  • Visitor : 抽象访问者接口
  • Element : 被访问元素接口
  • ElementA,ElementB : 具体元素,实现了Element接口
  • VisitorA,VisitorB : 具体访问者,实现了Visitor接口
  • ObjectStruct : 对象结构管理类,是一个较高层的结构
示例

/**
 * desc : 元素接口
 * Created by tiantian on 2018/9/14
 */
public interface Element {
    
    void accept(Visitor visitor);
}
/**
 * desc : 元素A
 * Created by tiantian on 2018/9/14
 */
public class ElementA implements Element {
    
    private String property1 = "Hello ";
    
    private String property2 = "World";
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
        System.out.println(visitor.getName() + " visited ElementA");
    }

    public String getProperty1() {
        return property1;
    }

    public String getProperty2() {
        return property2;
    }
}


/**
 * desc : 元素B
 * Created by tiantian on 2018/9/14
 */
public class ElementB implements Element {
    
    private String property1 = "hello ";

    private String property2 = "world";
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
        System.out.println(visitor.getName() + " visited ElementB");
    }

    public String getProperty1() {
        return property1;
    }

    public String getProperty2() {
        return property2;
    }
}
/**
 * desc : 访问者接口
 * Created by tiantian on 2018/9/14
 */
public interface Visitor {
    
    String getName();
    
    void visit(ElementA e);
    
    void visit(ElementB e);
}
/**
 * desc : 具体访问者A
 * Created by tiantian on 2018/9/14
 */
public class VisitorA implements Visitor {
    
    private String name = "visitorA";
    
    @Override
    public String getName() {
        return name;
    }

    @Override
    public void visit(ElementA e) {
        System.out.println(e.getProperty1() + e.getProperty2());
    }

    @Override
    public void visit(ElementB e) {
        System.out.println(e.getProperty1() + e.getProperty2());
    }
}

/**
 * desc : 访问者B
 * Created by tiantian on 2018/9/15
 */
public class VisitorB implements Visitor{
    private String name = "VisitorB";
    @Override
    public String getName() {
        return name;
    }

    @Override
    public void visit(ElementA e) {
        System.out.println(e.getProperty1() + e.getProperty2());
    }

    @Override
    public void visit(ElementB e) {
        System.out.println(e.getProperty1() + e.getProperty2());
    }
}
/**
 * desc : 对象结构
 * Created by tiantian on 2018/9/14
 */
public class ObjectStruct {
    
    private List<Element> elements = new ArrayList<>();
    
    public void add(Element e) {
        elements.add(e);
    }
    
    public void show(Visitor visitor) {
        for (Element e : elements) {
            e.accept(visitor);
        }
    }
}
总结

访问者模式使数据结构对象和操纵数据结构的对象分离,从而降低了它们之间的耦合性。可以灵活的添加不同的访问者,以不同的方式访问该数据结构的各个元素。缺点是数据结构必须稳定,不能随意增加元素类型。也就是在示例中,Element元素的具体结构类型数量是恒定的且稳定的;若不稳定,增加了元素类型时,访问者接口也要发生变化,不符合开闭原则。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值