设计模式之访问模式18
来源:互联网 发布:python生成100个随机数 编辑:程序博客网 时间:2024/05/17 21:31
设计模式之访问模式18
背景
如果希望能打印出所有员工的信息(普通员工,经理)。普通的模板方法可以实现。现在尝试下访问模式。拆分对象:员工,普通员工,经理,访问类(集中访问信息的操作)
类图
代码实现、
定义一个员工抽象类,所有角色都继承该类。
public abstract class Employee { private String name; private Double salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getSalary() { return salary; } public void setSalary(Double salary) { this.salary = salary; } public abstract void accept(IVisitor visitor); }
定义一个普通员工
public class CommonEmployee extends Employee{ private String job; public String getJob() { return job; } public void setJob(String job) { this.job = job; } //设置允许访问 @Override public void accept(IVisitor visitor) { visitor.visit(this); } public CommonEmployee(String name, Double salary, String job) { super.setName(name); super.setSalary(salary); this.job = job; }}
定义经理
public class Manager extends Employee { private String performance; public String getPerformance() { return performance; } public void setPerformance(String performance) { this.performance = performance; } //设置允许访问者访问 @Override public void accept(IVisitor visitor) { visitor.visit(this); } public Manager(String name, Double salary, String performance) { super.setName(name); super.setSalary(salary); this.performance = performance; }}
定义访问类的接口,支持哪些类的访问
public interface IVisitor { public void visit(CommonEmployee commonEmployee); public void visit(Manager manager);}
具体实现访问类,将动作集中在这里
public class Visitor implements IVisitor { public void visit(CommonEmployee commonEmployee) { System.out.println(this.getCommonEmployeeInfo(commonEmployee)); } public void visit(Manager manager) { System.out.println(this.getManagerInfo(manager)); } public String getBasicInfo(Employee employee) { return employee.getName() + "\t" + employee.getSalary() + "\t"; } public String getManagerInfo(Manager manager) { return this.getBasicInfo(manager) + manager.getPerformance() + "\t"; } public String getCommonEmployeeInfo(CommonEmployee commonEmployee) { return this.getBasicInfo(commonEmployee) + commonEmployee.getJob() + "\t"; }}
客户端调用
public class Client { public static void main(String[] args) { Visitor visitor = new Visitor(); for(Employee employee : getData()) { employee.accept(visitor); } } public static List<Employee> getData() { List<Employee> list = new ArrayList<Employee>(); list.add(new CommonEmployee("张三", 1000.0, "撸Java")); list.add(new CommonEmployee("李四", 1100.0, "撸JS")); list.add(new Manager("王五", 11000.0, "吹牛比")); return list; }}
运行结果如下
张三 1000.0 撸Java 李四 1100.0 撸JS 王五 11000.0 吹牛比
1 0
- 设计模式之访问模式18
- 设计模式之访问模式
- PHP设计模式之适配器模式,建造者模式,数据访问对象模式
- 设计模式——访问设计模式
- 设计模式之18 - 备忘录模式Memento
- 设计模式(18)之职责链模式
- 【设计模式】设计模式之适配器模式
- 【设计模式】设计模式之外观模式
- 【设计模式】设计模式之工厂模式
- 从数据访问程序看设计模式之工厂模式、抽象工厂模式
- GOF23设计模式之解释器模式和访问器模式的理解
- 设计模式之禅18
- PHP设计模式--数据访问对象模式
- 设计模式——访问器模式
- Javascript设计模式-数据访问对象模式
- 设计模式【数据访问对象模式DataAccessObjectPattern】
- 访问控制:protected 之 单元素模式设计
- javascript 设计模式 - 成员访问
- Android webView点击图片放大效果之坑
- 51nod数字0-9的数量(数位dp)
- Sublime Text 3 配置 c/c++、Java编译运行环境
- 面向对象_抽象类练习学员案例
- Glide获取缓存大小并清除缓存图片
- 设计模式之访问模式18
- JDK自带日志工具Logger的研究使用
- 按钮被点击后屏蔽点击且倒计时60S能再次被点击-JS实现和JQuery实现
- iOS 自动释放池内存实现
- HDOJ2000
- 程序备忘_PI参数整定_LV41
- 6.restful
- 法语学习相关
- window程序设计---编写WM消息制贪吃蛇的代码与经过