软件设计 石家庄铁道大学信息学院
实验 25:访问者模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解访问者模式的动机,掌握该模式的结构;
2、能够利用访问者模式法解决实际问题。
[实验任务一]:打包员
在我们课堂上的“购物车”的例子中,增加一个新的访问者:打包员,负责对购物车中货物装包。
实验要求:
1. 提交源代码,不要提交类图;
2. 注意编程规范。
package test25; public abstract class AbstractGoods implements Goods{ private String name; private int num; private int price; public AbstractGoods(String name, int num,int price) { this.name = name; this.num = num; this.price = price; } @Override public abstract int accept(Visitor visitor); public String getName() { return name; } public int getNum() { return num; } public int getPrice() { return price; } }
package test25; public class Book extends AbstractGoods { public Book(String name, int num, int price) { super(name, num, price); } @Override public int accept(Visitor visitor) { return visitor.visit(this); } }
package test25; public class Client { public static void main(String[] args) { Shoppingcar shoppingCar = new Shoppingcar(); shoppingCar.add(new Book("西游记", 2, 25)); shoppingCar.add(new Book("活着", 2, 30)); shoppingCar.add(new Fruit("苹果", 2, 5)); shoppingCar.add(new Fruit("火龙果", 6, 8)); shoppingCar.getCosts(); } }
package test25; public class Fruit extends AbstractGoods { public Fruit(String name, int num, int price) { super(name, num, price); } @Override public int accept(Visitor visitor) { return visitor.visit(this); } }
package test25; public interface Goods { public int accept(Visitor visitor); public String getName(); public int getNum(); public int getPrice(); }
package test25; import java.util.ArrayList; import java.util.List; public class Shoppingcar { private static final List<Goods> goods = new ArrayList<>(); private static final Visitor visitor = new shouyinyuan(); public void add(Goods g) { goods.add(g); } public void remove(Goods g) { goods.remove(g); } public int getCosts() { int sum = 0; int i = 1; for (Goods g : goods) { int c = g.accept(visitor); System.out.println(g.getName() + ":" + c + "元"); sum = sum + c; } System.out.println("一共是:" + sum + " 元"); return sum; } }
package test25; public class shouyinyuan implements Visitor { @Override public int visit(Book book) { int cost = book.getNum() * book.getPrice(); return cost; } @Override public int visit(Fruit fruit) { int cost = fruit.getNum() * fruit.getPrice(); return cost; } }
package test25; public interface Visitor { int visit(Book book) ; int visit(Fruit fruit); }
#include<iostream> #include <list> using namespace std; class Apple; class Book; //抽象访问者 class Visitor { protected: string name; public: void setName(string name) { this->name = name; } string getname() { return name; } virtual void visit(Apple *apple)=0; virtual void visit(Book *book)=0; }; //抽象元素 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: list< Product*> product; public: void accept(Visitor* visitor) { for (std::list<Product*>::iterator it = product.begin(); it != product.end(); ++it) (*it)->accept(visitor); } void addProduct(Product* product2) { product.push_back(product2); } void removeProduct(Product* product2) { product.remove(product2); } }; class Saler :public Visitor { public: void visit(Apple *apple) { cout << "收银员" <<name.c_str() << "给苹果过秤,然后计算其价格。" << endl; } void visit(Book *book) { cout << "收银员" << name.c_str() << "直接计算书的价格。" << endl; } }; class Customer :public Visitor { public: void visit(Apple *apple) { cout << "顾客" << name.c_str() << "选苹果。" << endl; } void visit(Book *book) { cout << "顾客" << name.c_str() << "买书。" << endl; } }; class Pack { private: list< Product*> product; public: void accept(Visitor* visitor) { for (std::list<Product*>::iterator it = product.begin(); it != product.end(); ++it) (*it)->accept(visitor); } void addProduct(Product* product2) { product.push_back(product2); } void removeProduct(Product* product2) { product.remove(product2); } }; class Packer :public Visitor { public: void visit(Apple *apple) { cout << "打包员" << name.c_str() << "给购物车中的苹果进行打包。" << endl; } void visit(Book *book) { cout << "打包员" << name.c_str() << "给购物车中的书本进行打包。" << endl; } }; int main() { Product *b1 = new Book(); Product *a1 = new Apple(); Visitor* visitor1 = new Customer(); Visitor* visitor2 = new Packer(); Visitor* visitor3 = new Saler(); BuyBasket *basket = new BuyBasket(); basket->addProduct(b1); basket->addProduct(a1); visitor1->setName("小龚"); visitor2->setName("小张"); visitor3->setName("小橘"); basket->accept(visitor1); basket->accept(visitor3); basket->accept(visitor2); delete a1, b1,visitor1, visitor2, visitor3; return 0; }