Visitor pattern

Definition


Define a new operation to deal with the classes of the elements without changing their structures.

Where to use & benefits

Add operations on a bunch of classes which have different interfaces.
Traverse the object structure to gather related operations
Easy to add new operations.
Crossing class hierarchies may break encapsulation.

Related patterns include

Composite, which may be applied in a visitor pattern.
Interpreter, which may be used to go through structure and define new operation in a visitor pattern.

Example


The following is a dummy program. Two interfaces involved: Visitor and Pizza. The Pizza system is completely independent. "How to get it" tries to add new operations to the Pizza system. It is done by adding another interface Visitor and parameterizing Pizza interface in the abstract method visit(composite pattern). The "how to get" classes implement Visitor interface and make a connection with Pizza system.
import java.util.*;
interface Visitor {
    public void visit(Pizza p);
}
interface Pizza {
    public String order();
}
class PopJohn implements Pizza {
    final String name = "PopJohn";
    public String order() {
        return name;
    }
}
class PizzaHut implements Pizza {
    final String name = "PizzaHut";
    public String order() {
        return name;
    }
}
class GodFather implements Pizza {
    final String name = "GodFather";
    public String order() {
        return name;
    }
}
class ByPickup implements Visitor {
    private String name;
    private final String method = "By pick up";
    public void visit(Pizza p) {
        name = p.order();
    }
   
    public String toString() {
         return name + " " + method;
    }
}
class ByEatin implements Visitor {
    private String name;
    private final String method = "By eat in";
   
    public void visit(Pizza p) {
        name = p.order();
    }
   
    public String toString() {
        return name + " " + method;
    }
}
class ByDelivery implements Visitor {
    private String name;
    private final String method = "By delivery";
   
    public void visit(Pizza p) {
        name = p.order();
    }
   
    public String toString() {
         return name + " " + method;
    }
}
class Dinner {
    public Pizza getDinner() {
        switch ((int)(Math.random()*3)){
            case 0: return new PopJohn();
            case 1: return new PizzaHut();
            case 2: return new GodFather();
            default: return null;
        }
    }
    public Visitor howto() {
        switch ((int)(Math.random()*3)){
            case 0: return new ByPickup();
            case 1: return new ByEatin();
            case 2: return new ByDelivery();
            default: return null;
        }
    }   
}
class Test {
   
    public static void main(String[] args) {
        List pizzaList = new ArrayList();
            pizzaList.add(new PopJohn());
            pizzaList.add(new PizzaHut());
            pizzaList.add(new GodFather());
        Iterator it = pizzaList.iterator();
        System.out.println("How many pizza restaurants in this area?");
        while (it.hasNext()) {
           System.out.println(((Pizza)it.next()).order());
        }
        Dinner d = new Dinner();
        Pizza pza = d.getDinner();
        Visitor v = d.howto();
        v.visit(pza);
        System.out.println("/nWhich store for dinner?");
        System.out.println(v);
    }
}
//run it several times.
java Test
How many pizza restaurants in this area?
PopJohn
PizzaHut
GodFather

Which restaurant for dinner?
GodFather By delivery

java Test
How many pizza restaurants in this area?
PopJohn
PizzaHut
GodFather

Which restaurant for dinner?
PizzaHut By pick up

java Test
How many pizza restaurants in this area?
PopJohn
PizzaHut
GodFather

Which restaurant for dinner?
PizzaHut By delivery
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值