返回顶部

一缕半夏微光

温柔半两,从容一生

导航

软件设计25|访问者模式

 

一、Java版本

(1)效果如下:

(2)类图如下:

(3)目录如下:

(4)代码如下:

Apple.java

1 package visitor;
2 
3 public class Apple implements Product {
4     public void accept(Visitor visitor) {
5         visitor.visit(this);
6     }
7 }

Book.java

1 package visitor;
2 
3 public class Book implements Product {
4     public void accept(Visitor visitor) {
5         visitor.visit(this);
6     }
7 }

BuyBasket.java

 1 package visitor;
 2 
 3 import java.util.ArrayList;
 4 import java.util.Iterator;
 5 
 6 @SuppressWarnings("rawtypes")
 7 public class BuyBasket {
 8     
 9     private ArrayList list = new ArrayList();
10 
11     public void accept(Visitor visitor) {
12         Iterator i = list.iterator();
13         while (i.hasNext()) {
14             ((Product) i.next()).accept(visitor);
15         }
16     }
17 
18     @SuppressWarnings("unchecked")
19     public void addProduct(Product product) {
20         list.add(product);
21     }
22 
23     public void removeProduct(Product product) {
24         list.remove(product);
25     }
26 }

Client.java

 1 package visitor;
 2 
 3 public class Client {
 4     public static void main(String a[]) {
 5         Product b1 = new Book();
 6         Product b2 = new Book();
 7         Product a1 = new Apple();
 8         
 9         BuyBasket basket = new BuyBasket();
10         basket.addProduct(b1);
11         basket.addProduct(b2);
12         basket.addProduct(a1);
13 
14         //创建顾客
15         Customer customer = new Customer();
16         customer.setName("张三");
17 
18         basket.accept(customer);
19         
20         System.out.println("--------");
21         
22         Saler saler=new Saler();
23         saler.setName("李四");
24         
25         basket.accept(saler);
26         
27         System.out.println("--------");
28         
29         Packer packer=new Packer();
30         packer.setName("王五");
31         
32         basket.accept(packer);
33     }
34 }

Customer.java

 1 package visitor;
 2 
 3 public class Customer extends Visitor {
 4     public void visit(Apple apple) {
 5         System.out.println("顾客" + name + "选苹果。");
 6     }
 7 
 8     public void visit(Book book) {
 9         System.out.println("顾客" + name + "买书。");
10     }
11 }

Packer.java

 1 package visitor;
 2 
 3 public class Packer extends Visitor {
 4     public void visit(Apple apple) {
 5         System.out.println("打包员" + name + "将苹果打包。");
 6     }
 7 
 8     public void visit(Book book) {
 9         System.out.println("打包员" + name + "将书打包。");
10     }
11 }

Product.java

1 package visitor;
2 
3 public interface Product {
4     abstract void accept(Visitor visitor);
5 }

Saler.java

 1 package visitor;
 2 
 3 public class Saler extends Visitor {
 4     public void visit(Apple apple) {
 5         System.out.println("收银员" + name + "给苹果过秤,然后计算其价格。");
 6     }
 7 
 8     public void visit(Book book) {
 9         System.out.println("收银员" + name + "直接计算书的价格。");
10     }
11 }

Visitor.java

 1 package visitor;
 2 
 3 public abstract class Visitor {
 4     protected String name;
 5 
 6     public void setName(String name) {
 7         this.name = name;
 8     }
 9 
10     public abstract void visit(Apple apple);
11 
12     public abstract void visit(Book book);
13 }

二、C++版本

(1)效果如下:

(2)类图如下:

(3)代码如下:

  1 #include<iostream>
  2 #include<List>
  3 using namespace std;
  4 
  5 //抽象访问者Visitor(访问者类)
  6 class Apple;
  7 class Book;
  8 class Visitor {
  9 public:
 10     void setName(string name) {
 11         this->name = name;
 12     }
 13 
 14     virtual void visit(Apple* apple) = 0;
 15     virtual void visit(Book* book) = 0;
 16 protected:
 17     string name;
 18 };
 19 
 20 //具体访问者类Customer(顾客类) 
 21 class Customer : public Visitor {
 22 public:
 23     void visit(Apple* apple) {
 24         cout << "顾客" << name << "选苹果。" << endl;
 25     }
 26 
 27     void visit(Book* book) {
 28         cout << "顾客" << name << "买书。" << endl;
 29     }
 30 };
 31 
 32 //具体访问者类Saler(收银员类) 
 33 class Saler : public Visitor {
 34 public:
 35     void visit(Apple* apple) {
 36         cout << "收银员" << name << "给苹果过秤,然后计算价格。" << endl;
 37     }
 38 
 39     void visit(Book* book) {
 40         cout << "收银员" << name << "直接计算书的价格。" << endl;
 41     }
 42 };
 43 
 44 //抽象元素类Product(商品类)
 45 class Product {
 46 public:
 47     virtual void accept(Visitor* visitor) = 0;
 48 };
 49 
 50 //具体元素类Apple(苹果类) 
 51 class Apple : public Product {
 52 public:
 53     void accept(Visitor* visitor) {
 54         visitor->visit(this);
 55     }
 56 };
 57 
 58 //具体元素类Book(书籍类) 
 59 class Book : public Product {
 60 public:
 61     void accept(Visitor* visitor) {
 62         visitor->visit(this);
 63     }
 64 };
 65 
 66 //对象结构BuyBasket(购物车类)
 67 class BuyBasket {
 68 public:
 69     void accept(Visitor* visitor) {
 70         for (auto i : list) {
 71             i->accept(visitor);
 72         }
 73     }
 74 
 75     void addProduct(Product* product) {
 76         list.push_back(product);
 77     }
 78 
 79     void removeProduct(Product* product) {
 80         list.remove(product);
 81     }
 82 private:
 83     list<Product*> list;
 84 };
 85 
 86 //具体访问者类Packer(打包员类) 
 87 class Packer : public Visitor {
 88 public:
 89     void visit(Apple* apple) {
 90         cout << "打包员" << name << "将苹果打包。" << endl;
 91     }
 92 
 93     void visit(Book* book) {
 94         cout << "打包员" << name << "将书打包。" << endl;
 95     }
 96 };
 97 
 98 //客户端测试 
 99 int main(void)
100 {
101     //创建商品 
102     Book b1, b2;
103     Apple a1;
104 
105     //创建购物车 
106     BuyBasket basket;
107 
108     //购物车添加商品 
109     basket.addProduct(&b1);
110     basket.addProduct(&b2);
111     basket.addProduct(&a1);
112 
113     //创建顾客 
114     Customer customer;
115     customer.setName("张三");
116 
117     //访问操作 
118     basket.accept(&customer);
119 
120     //购物车移除商品 
121     basket.removeProduct(&b2);
122 
123     cout << "-----------------" << endl;
124 
125     //创建收银员 
126     Saler saler;
127     saler.setName("李四");
128 
129     //访问操作 
130     basket.accept(&saler);
131 
132     cout << "-----------------" << endl;
133 
134     //创建打包员
135     Packer packer;
136     packer.setName("王五");
137 
138     //访问操作 
139     basket.accept(&packer);
140 
141     return 0;
142 }

参考链接:设计模式之访问者模式实例(c++)

posted on 2021-12-01 21:20  一缕半夏微光  阅读(24)  评论(0编辑  收藏  举报