Visitor pattern

本文介绍了一种使用访客模式为不同接口的多个类添加操作的方法,该方法无需改变原有类结构,通过添加访客接口实现操作扩展。示例中展示了如何为Pizza类添加获取方式的操作,包括自取、堂食和配送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值