惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - yufan27209

博文阅读密码验证 - 博客园 JVM调优 【转】中文分词之HMM模型详解 xwiki enterprise 8.4.5使用https步骤 博文阅读密码验证 - 博客园 dubbo和shiro的整合,在服务端做权限验证 电商课题:分布式锁 Export large data from Gridview and Datareader to an Excel file using C# 博文阅读密码验证 - 博客园 sap学习笔记 用户中心 - 博客园 .NET ActionFilterAttribute等 MYSQL实现上一条下一条功能 SVN基本配置文件 单表60亿记录等大数据场景的MySQL优化和运维之道 MSSQL镜像—无见证服务器 风险控制-防刷 开发中两款工具 Remote Desktop Organizer 和zabbix EasyUI datagrid 编辑框焦点
设计模式读书笔记-----访问者模式DEMO修改
yufan27209 · 2017-06-09 · via 博客园 - yufan27209

读访问者模式资料:http://blog.csdn.net/chenssy/article/details/12029633

直觉这写法有问题。访问的应该是药单,而不是药,特改写如下:

public abstract class Visitor {
	protected String name;

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

	public abstract void visitor(Presciption p);
}
public class Charger extends Visitor{
	public void visitor(Presciption p) {
		for(Medicine m : p.list){
			System.out.println("划价员:" + name + "给药" + m.getName() + "划价:" + m.getPrice());
		}
	}
}
public class WorkerOfPharmacy extends Visitor{
	public void visitor(Presciption p) {
		for(Medicine m : p.list){
			System.out.println("药房工作者:" + name + "拿药 :" + m.getName());
		}
	}

	/*public void visitor(MedicineB b) {
		System.out.println("药房工作者:" + name + "拿药 :" + b.getName());
	}*/
}
public class Medicine {
	protected String name;
	protected double price;

	public Medicine(String name, double price) {
		this.name = name;
		this.price = price;
	}

	public String getName() {
		return name;
	}

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

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	//public abstract void accept(Visitor visitor);
}
public class MedicineA extends Medicine{
	public MedicineA(String name, double price) {
		super(name, price);
	}
}
public class MedicineB extends Medicine {
	public MedicineB(String name, double price) {  
        super(name, price);  
    }
}
public class Presciption {
	List<Medicine> list = new ArrayList<Medicine>();  
    
    public void accept(Visitor visitor){
    	visitor.visitor(this);
    }
      
    public void addMedicine(Medicine medicine){  
        list.add(medicine);  
    }  
      
    public void removeMedicien(Medicine medicine){  
        list.remove(medicine);  
    }  
}
public class Client {
	public static void main(String[] args) {
		Medicine a = new MedicineA("板蓝根", 11.0);
		Medicine b = new MedicineB("感康", 14.3);

		Presciption presciption = new Presciption();
		presciption.addMedicine(a);
		presciption.addMedicine(b);

		Visitor charger = new Charger();
		charger.setName("张三");

		Visitor workerOfPharmacy = new WorkerOfPharmacy();
		workerOfPharmacy.setName("李四");

		presciption.accept(charger);
		System.out.println("-------------------------------------");
		presciption.accept(workerOfPharmacy);
	}
}