设计模式——访问者模式

顾客在超市中将选择的商品,如苹果、图书等放在购物车中,然后到收银员处付款。在购物过程中,顾客需要对这些商品进行访问,以便确认这些商品的质量,之后收银员计算价格时也需要访问购物车内顾客所选择的商品。此时,购物车作为一个ObjectStructure(对象结构)用于存储各种类型的商品,而顾客和收银员作为访问这些商品的访问者,他们需要对商品进行检查和计价。不同类型的商品其访问形式也可能不同,如苹果需要过秤之后再计价,而图书不需要。使用访问者模式来设计该购物过程。

[实验任务一]:打包员

在我们课堂上的“购物车”的例子中,增加一个新的访问者:打包员,负责对购物车中货物装包。

 

java实现

package visitor;

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);
}
package visitor;

public interface Product {
    void accept(Visitor visitor);
}
package visitor;

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+"直接计算书的价格");
    }
}
package visitor;

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+"付钱买书");
    }
}
package visitor;

public class Packer extends Visitor{
    @Override
    public void visit(Apple apple) {
        System.out.println("装包员"+name+"打包苹果");
    }

    @Override
    public void visit(Book book) {
        System.out.println("装包员"+name+"打包书");
    }
}
package visitor;

public class Apple implements Product{
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
package visitor;

public class Book implements Product{
    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
package visitor;

import java.util.ArrayList;
import java.util.Iterator;

public class BuyBasket {
    private ArrayList list = new ArrayList();

    public void accept(Visitor visitor){
        Iterator i=list.iterator();
        while (i.hasNext()){
            ((Product)i.next()).accept(visitor);
        }
    }

    public void addProduct(Product p){
        list.add(p);
    }

    public void removeProduct(Product p){
        list.remove(p);
    }
}
package visitor;

public class Client {
    public static void main(String[] args) {
        Product p1,p2;
        Visitor v1,v2,v3;
        p1=new Apple();
        p2=new Book();
        v1=new Saler();
        v1.setName("张三");
        v2=new Customer();
        v2.setName("李四");
        v3=new Packer();
        v3.setName("王五");
        BuyBasket basket = new BuyBasket();
        basket.addProduct(p1);
        basket.addProduct(p2);
        basket.accept(v1);
        basket.accept(v2);
        basket.accept(v3);
    }
}

 

 

c++实现

#include<iostream>
#include<list>
#include<string>
using namespace std;

class Product;
class Book;
class Apple;
class Visitor{
protected:
    string name;
public:
    void setName(string name){
        this->name=name;
    }
    virtual void visitor(Apple *apple)=0;
    virtual void visitor(Book *book)=0;
};
class Saler:public Visitor{
public:
    void visitor(Apple *apple){
        cout<<"收银员"<<name<<"计算苹果的价格"<<endl;
    }
    void visitor(Book *book){
        cout<<"收银员"<<name<<"计算书的价格"<<endl;
    }
};
class Customer:public Visitor{
public:
    void visitor(Apple *apple){
        cout<<"顾客"<<name<<"买苹果"<<endl;
    }
    void visitor(Book *book){
        cout<<"顾客"<<name<<"买些书"<<endl;
    }
};
class Packer:public Visitor{
public:
    void visitor(Apple *apple){
        cout<<"打包员"<<name<<"打包苹果"<<endl;
    }
    void visitor(Book *book){
        cout<<"打包员"<<name<<"把书装好"<<endl;
    }
};
class Product{
public:
    virtual void accept(Visitor *visitor)=0;
};
class Apple:public Product{
public:
    void accept(Visitor *visitor){
        visitor->visitor(this);
    }
};
class Book:public Product{
public:
    void accept(Visitor *visitor){
        visitor->visitor(this);
    }
};
class BuyBasket{
protected:
    list<Product*> list;
public:
    void accept(Visitor *visitor){
        std::list<Product*>::iterator iter = list.begin();
        for(;iter!=list.end();iter++){
            (*iter)->accept(visitor);
        }
    }
    void addProduct(Product *product){
        list.push_back(product);
    }
    void removeProduct(Product *product){
        list.remove(product);
    }
};

int main(){
    Product *p1,*p2;
    Visitor *v1,*v2,*v3;
    p1=new Apple();
    p2=new Book();
    v1=new Saler();
    v1->setName("张三");
    v2=new Customer();
    v2->setName("李四");
    v3=new Packer();
    v3->setName("马六");
    BuyBasket *basket = new BuyBasket();
    basket->addProduct(p1);
    basket->addProduct(p2);
    basket->accept(v1);
    basket->accept(v2);
    basket->accept(v3);
    return 0;
}

 

posted on 2022-11-25 16:52  跨越&尘世  阅读(46)  评论(0编辑  收藏  举报