软件设计-访问者模式
打包员
在我们课堂上的“购物车”的例子中,增加一个新的访问者:打包员,负责对购物车中货物装包。
类图
源代码
Java
package rjsj.no25; public class Client { public static void main(String[] args) { Product b1 = new Book(); Product a1 = new Apple(); Visitor visitor; BuyBasket basket = new BuyBasket(); basket.addProduct(b1); basket.addProduct(a1); visitor = new Customer(); visitor.setName("张三"); basket.accept(visitor); System.out.println("**************************************************"); visitor = new Packer(); visitor.setName("李四"); basket.accept(visitor); System.out.println("**************************************************"); visitor = new Saler(); visitor.setName("王五"); basket.accept(visitor); } }
package rjsj.no25; 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 rjsj.no25; 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 rjsj.no25; public interface Product { void accept(Visitor visitor); }
package rjsj.no25; 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 rjsj.no25; 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 rjsj.no25; public class Apple implements Product{ @Override public void accept(Visitor visitor) { visitor.visit(this); } }
package rjsj.no25; public class Book implements Product{ @Override public void accept(Visitor visitor) { visitor.visit(this); } }
package rjsj.no25; 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 product) { list.add(product); } public void removeProduct(Product product) { list.remove(product); } }
C++
#include<iostream> #include <vector> #include <algorithm> using namespace std; class Apple; class Book; class Visitor { protected: string name; public: void setName(string name) { this->name=name; } virtual void visit(Apple *apple)=0; virtual void visit(Book *book)=0; }; class Customer :public Visitor{ public: void visit(Apple *apple) { cout<<"顾客->"<<name<<"买苹果"<<endl; } void visit(Book *book) { cout<<"顾客->"<<name<<"买书"<<endl; } }; class Packer: public Visitor{ public: void visit(Apple *apple) { cout<<"打包员->"<<name<<"给苹果打包"<<endl; } void visit(Book *book) { cout<<"打包员->"<<name<<"给书打包"<<endl; } }; class Saler:public Visitor{ public: void visit(Apple *apple) { cout<<"收银员->"<<name<<"计算苹果价格"<<endl; } void visit(Book *book) { cout<<"收银员->"<<name<<"计算书的价格"<<endl; } }; class Product { public: virtual void accept(Visitor *visitor)=0; }; class Apple: public Product{ public: void accept(Visitor *visitor) { visitor->visit(this); } }; class Book: public Product{ public: void accept(Visitor *visitor) { visitor->visit(this); } }; class BuyBasket { private: vector <Product *> products; public: void accept(Visitor *visitor) { vector <Product *>::iterator iProduct = products.begin(); while(iProduct != products.end()) { (*iProduct)->accept(visitor); iProduct++; } } void addProduct(Product *product) { products.push_back(product); } void removeProduct(Product *product) { vector<Product*>::iterator product_rm = find(products.begin(), products.end(), product); products.erase(product_rm); } }; int main(){ Product *b1=new Book(); Product *a1=new Apple(); Product *a2=new Apple(); Visitor *visitor; BuyBasket *basket=new BuyBasket(); basket->addProduct(b1); basket->addProduct(a1); basket->addProduct(a2); visitor=new Customer(); visitor->setName("张三"); basket->accept(visitor); cout<<"########################################"<<endl; visitor=new Packer(); visitor->setName("李四"); basket->accept(visitor); cout<<"########################################"<<endl; visitor=new Saler(); visitor->setName("王五"); basket->accept(visitor); }
运行结果