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

推荐订阅源

T
The Blog of Author Tim Ferriss
S
Securelist
D
Docker
The Register - Security
The Register - Security
GbyAI
GbyAI
Recorded Future
Recorded Future
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
云风的 BLOG
云风的 BLOG
P
Proofpoint News Feed
罗磊的独立博客
博客园 - 【当耐特】
F
Full Disclosure
WordPress大学
WordPress大学
腾讯CDC
小众软件
小众软件
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
SecWiki News
SecWiki News
L
Lohrmann on Cybersecurity
I
InfoQ
MyScale Blog
MyScale Blog
量子位
Cyberwarzone
Cyberwarzone
博客园 - 三生石上(FineUI控件)
The Hacker News
The Hacker News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
博客园_首页
H
Help Net Security
K
Kaspersky official blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Webroot Blog
Webroot Blog
Blog — PlanetScale
Blog — PlanetScale
V
Vulnerabilities – Threatpost
Y
Y Combinator Blog
The Cloudflare Blog
P
Proofpoint News Feed
V
Visual Studio Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tailwind CSS Blog
爱范儿
爱范儿
P
Privacy International News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
The GitHub Blog
The GitHub Blog
C
Cybersecurity and Infrastructure Security Agency CISA
B
Blog RSS Feed

博客园 - 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);
	}
}