25.访问者模式
[实验任务一]:打包员
在我们课堂上的“购物车”的例子中,增加一个新的访问者:打包员,负责对购物车中货物装包。
代码
java
Apple.java
package test25;
public class Apple implements Product {
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
Book.java
package test25;
public class Book implements Product{
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
BuyBasket.java
package test25;
import java.util.ArrayList;
import java.util.Iterator;
public class BuyBasket{
private ArrayList list = new ArrayList();
public void accept(Visitor visitor){
Iterator iterator = list.iterator();
while (iterator.hasNext()){
((Product)iterator.next()).accept(visitor);
}
}
public void addProduce(Product product){
list.add(product);
}
public void removeProduce(Product product){
list.remove(product);
}
}
Client.java
package test25;
public class Client {
public static void main(String[] args) {
Product b1 = new Book();
Product b2 = new Book();
Product a1 = new Apple();
Visitor visitor;
BuyBasket basket = new BuyBasket();
basket.addProduce(b1);
basket.addProduce(b2);
basket.addProduce(a1);
visitor = new Packager();
visitor.setName("张三");
basket.accept(visitor);
}
}
Customer.java
package test25;
public class Customer extends Visitor{
@Override
public void visit(Apple apple) {
System.out.println("顾客"+name+"选苹果");
}
@Override
public void visit(Book book) {
System.out.println("顾客"+name+"买书");
}
}
Packager.java
package test25;
public class Packager extends Visitor{
@Override
public void visit(Apple apple) {
System.out.println("打包员"+name+"打包苹果");
}
@Override
public void visit(Book book) {
System.out.println("打包员"+name+"打包书籍");
}
}
Product.java
package test25;
public interface Product {
void accept(Visitor visitor);
}
Saler.java
package test25;
public class Saler extends Visitor{
@Override
public void visit(Apple apple) {
System.out.println("收银员"+
name+"给苹果过称,计算价格");
}
@Override
public void visit(Book book) {
System.out.println("收银员"+name+"直接计算书的价格");
}
}
Visitor.java
package test25;
public abstract class Visitor {
protected String name;
public void setName(String name){
this.name = name;
}
public abstract void visit(Apple apple);
public abstract void visit(Book book);
}
C++代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Visitor;
class Apple;
class Book;
//抽象产品类
class Product{
public:
virtual void accept(Visitor *visitor){};
};
//抽像访问者
class Visitor{
protected:
string name;
public:
void setName(string name){
this->name = name;
}
virtual void visit(Apple *apple){}
virtual void visit(Book *book){}
};
class Packager:public Visitor{
public:
void visit(Apple *apple) override {
cout<<"打包员"+name+"打包苹果"<<endl;
}
void visit(Book *book) override {
cout<<"打包员"+name+"打包书籍"<<endl;
}
};
//具体产品类Apple
class Apple: public Product{
public:
void accept(Visitor *visitor){
visitor->visit(this);
}
};
//具体产品类Book
class Book:public Product{
public:
void accept(Visitor *visitor){
visitor->visit(this);
}
};
//购物车类BuyBasket
class BuyBasket{
private:
vector<Product*> list;
public:
void accept(Visitor *visitor){
// vector<Product>::iterator i;
// for (i = list.begin(); i != list.end(); ++i) { //用迭代器遍历容器
// i->accept(visitor);
// }
for(int i=0;i<list.size();i++){
list[i]->accept(visitor);
}
}
void addProduce(Product *product){
list.push_back(product);
}
};
int main(){
Book *b1 = new Book();
Book *b2 = new Book();
Apple *a1 = new Apple();
Packager *visitor = new Packager();
BuyBasket basket;
basket.addProduce(b1);
basket.addProduce(b2);
basket.addProduce(a1);
string name = "abc";
visitor->setName(name);
basket.accept(visitor);
}