Java Composite Pattern(组合模式)
来源:互联网 发布:youtube上的c语言 编辑:程序博客网 时间:2024/05/16 11:41
Java Composite Pattern(组合模式)
组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。
这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。
优点: 1、高层模块调用简单。 2、节点自由增加。
缺点:在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。
关键代码:树枝内部组合该接口,并且含有内部属性 List,里面放 Component。
- 创建 Employee 类,该类带有 Employee 对象的列表。
/** * 1. 创建 Employee 类,该类带有 Employee 对象的列表。 * @author mazaiting */public class Employee { /**姓名*/ private String name; /**职位*/ private String dept; /**工资*/ private int salary; private List<Employee> subordinates; public Employee(String name, String dept, int salary) { this.name = name; this.dept = dept; this.salary = salary; subordinates = new ArrayList<Employee>(); } /** * 添加 */ public void add(Employee e) { subordinates.add(e); } /** * 移除 */ public void remove(Employee e) { subordinates.remove(e); } /** * 获取数据 */ public List<Employee> getSubordinates() { return subordinates; } @Override public String toString() { return "Employee [name=" + name + ", dept=" + dept + ", salary=" + salary + "]"; } }
- 使用 Employee 类来创建和打印员工的层次结构。
/** * 2. 使用 Employee 类来创建和打印员工的层次结构。 * @author mazaiting */public class Client { public static void main(String[] args) { Employee CEO = new Employee("John", "CEO", 30000); Employee headSales = new Employee("Robert", "Head Sales", 20000); Employee headMarketing = new Employee("Michel", "Head Marketing", 20000); Employee clerk1 = new Employee("Laura", "Marketing", 10000); Employee clerk2 = new Employee("Bob", "Marketing", 10000); Employee salesExecutive1 = new Employee("Richard", "Sales", 10000); Employee salesExecutive2 = new Employee("Rob", "Sales", 10000); CEO.add(headSales); CEO.add(headMarketing); headSales.add(salesExecutive1); headSales.add(salesExecutive2); headMarketing.add(clerk1); headMarketing.add(clerk2); // 打印该组织的所有员工 System.out.println(CEO); for (Employee headEmployee : CEO.getSubordinates()) { System.out.println(headEmployee); for (Employee employee : headEmployee.getSubordinates()) { System.out.println(employee); } } }}
- 打印结果
Employee [name=John, dept=CEO, salary=30000]Employee [name=Robert, dept=Head Sales, salary=20000]Employee [name=Richard, dept=Sales, salary=10000]Employee [name=Rob, dept=Sales, salary=10000]Employee [name=Michel, dept=Head Marketing, salary=20000]Employee [name=Laura, dept=Marketing, salary=10000]Employee [name=Bob, dept=Marketing, salary=10000]
阅读全文
0 0
- Java Composite Pattern(组合模式)
- Java设计模式--组合模式【Composite Pattern】
- 组合模式-Composite Pattern
- Composite Pattern(组合模式)
- 【组合模式】Composite Pattern
- 组合模式 Composite Pattern
- 组合模式(Composite Pattern)
- 组合模式(Composite Pattern)
- 组合模式【Composite Pattern】
- 组合模式(Composite Pattern)
- composite pattern -- 组合模式
- 组合模式(Composite Pattern)
- 组合模式(Composite Pattern)
- Composite Pattern 组合模式
- 组合模式(Composite Pattern)
- 组合模式 (Composite Pattern)
- 组合模式【Composite Pattern】
- 组合模式【Composite Pattern】
- STM32学习笔记:adc采样得到的电压值用485发送给pc
- vim中对go的支持
- 两个有序链表序列的交集(20 分)
- STL的学习之( 三)
- Java Filter Pattern(过滤器模式)
- Java Composite Pattern(组合模式)
- Java Decorator Pattern(装饰器模式)
- Java Facade Pattern(外观模式)
- Java Flyweight Pattern(享元模式)
- Java Proxy Pattern(代理模式)
- Java Chain of Responsibility Pattern(责任链模式)
- Java Command Pattern(命令行模式)
- Java Interpreter Pattern(解释器模式)
- Java Iterator Pattern(迭代器模式)