实验 25:访问者模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解访问者模式的动机,掌握该模式的结构;
2、能够利用访问者模式法解决实际问题。
在我们课堂上的“购物车”的例子中,增加一个新的访问者:打包员,负责对购物车中货物装包。
/**
* 抽象元素类 商品
*/
public interface Product
{
/**
* 用于接受访问者的访问
*/
void accept(Visitor visitor);
}
/**
* 抽象访问者类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);
}
/**
* 具体元素类 苹果类
*/
public class Apple implements Product
{
/**
* 用于接受访问者的访问
*/
@Override
public void accept(Visitor visitor)
{
visitor.visit(this);
}
}
/**
* 具体元素类 书籍类
*/
public class Book implements Product
{
@Override
public void accept(Visitor visitor)
{
visitor.visit(this);
}
}
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);
}
}
/**
* 具体访问者类 顾客
*/
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 + "买书");
}
}
/**
* 具体访问者类 打包员
*/
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 + "将书打包");
}
}
/**
* 具体访问者类 收银员
*/
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 + "直接计算书的价格。");
}
}
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
public class XMLUtil
{
public static Object getBean()
{
try
{
//创建DOM文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc = builder.parse(new File("src/main/resources/config25.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 e)
{
e.printStackTrace();
return null;
}
}
}
/**
* 测试类
*/
public class Client
{
public static void main(String[] args)
{
//实例化商品
Product b1 = new Book();
Product b2 = new Book();
Product a1 = new Apple();
//实例化购物车
BuyBasket basket = new BuyBasket();
//添加商品
basket.addProduct(b1);
basket.addProduct(b2);
basket.addProduct(a1);
//实例化访问者
Visitor visitor;
//根据XML文件动态获取访问者
visitor = (Visitor) XMLUtil.getBean();
visitor.setName("张三");
basket.accept(visitor);
}
}
config25.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<className>Tutorial_25.Packager</className>
</config>