访问者模式---打包员

今天做了访问者模式---打包员的实验,用了差不多两个小时的时间,理解了访问者模式的原理,下面是访问者模式的实验要求和实现。

实验要求:

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

截图:

 

 代码:

public class Apple implements Product {
public Apple() {
}

public void accept(Visitor visitor) {
visitor.visit(this);
}
}

 

public class Book implements Product {
public Book() {
}

public void accept(Visitor visitor) {
visitor.visit(this);
}
}

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

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

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

public BuyBasket() {
}

public void accept(Visitor visitor) {
Iterator i = this.list.iterator();

while(i.hasNext()) {
((Product)i.next()).accept(visitor);
}

}

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

public void removeProduct(Product product) {
this.list.remove(product);
}
}

 

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

public class Client {
public Client() {
}

public static void main(String[] a) {
Product b1 = new Book();
Product b2 = new Book();
Product a1 = new Apple();

Packer packer = new Packer();

BuyBasket basket = new BuyBasket();
basket.addProduct(b1);
basket.addProduct(b2);
basket.addProduct(a1);

Visitor visitor = (Visitor)XMLUtil.getBean();
visitor.setName("张三");
basket.accept(visitor);
}
}

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

public class Customer extends Visitor {
public Customer() {
}

public void visit(Apple apple) {
System.out.println("顾客" + this.name + "选苹果。");
}

public void visit(Book book) {
System.out.println("顾客" + this.name + "买书。");
}
}

 

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

@Override
public void visit(Book var1) {
System.out.println("打包员"+name+"包装书籍!");

}
}

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

public interface Product {
void accept(Visitor var1);
}

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

public class Saler extends Visitor {
public Saler() {
}

public void visit(Apple apple) {
System.out.println("收银员" + this.name + "给苹果过秤,然后计算其价格。");
}

public void visit(Book book) {
System.out.println("收银员" + this.name + "直接计算书的价格。");
}
}

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

public abstract class Visitor {
protected String name;

public Visitor() {
}

public void setName(String name) {
this.name = name;
}

public abstract void visit(Apple var1);

public abstract void visit(Book var1);
}

 

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLUtil {
public XMLUtil() {
}

public static Object getBean() {
try {
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc = builder.parse(new File("config.xml"));
NodeList nl = doc.getElementsByTagName("className");
Node classNode = nl.item(0).getFirstChild();
String cName = classNode.getNodeValue();
Class c = Class.forName(cName);
Object obj = c.newInstance();
return obj;
} catch (Exception var8) {
var8.printStackTrace();
return null;
}
}
}

posted @ 2021-10-03 21:10  潘福龙  阅读(47)  评论(0编辑  收藏  举报