Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1742800
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Java

2017-02-10 11:36:16

Composite pattern 在你需要tree 的时候,会是选择之一。
The Composite Pattern allows you to compose object into a tree structure to represent the 
part-whole hierarchy which means you can create a tree of objects that is made of different 
parts.but that can be treated as a whole one big thing.Composite lets clients to treat individual 
objects and compositions of objects uniformly,that is the intent of the Composite Pattern. 

Composite Pattern consists of following objects:
Base Component: if the interface for all objects in the composition, client program uses 
base component to work with the objects in the compositions, it can be an interface or an abstract class with some methods common to all the objects.
Leaf: Defines the behavior for the elements in the composition, it is the build block for the composition and implements base component, it does not have references to other components.
Composite: it consists of leaf elements and implements the operations in base component. 

Usage in JDK:
java.awt.Container #add(Component) is a great example of Composite pattern in java and used 
in a lot in Swing.


点击(此处)折叠或打开

  1. package CompositePattern;

  2. import java.util.ArrayList;
  3. import java.util.Iterator;
  4. import java.util.List;

  5. /*
  6.  * First we will create component interface
  7.  * It represents object in composition
  8.  * It has all common operation that will be applicable to
  9.  * both manager and developer
  10.  */

  11. /*
  12.  * Manager(Composite)
  13.  * Developer(Leaf)
  14.  * Employee(Component)
  15.  *
  16.  */


  17. interface IEmployee{
  18.     public void add(IEmployee emp);
  19.     public void remove(IEmployee emp);
  20.     public IEmployee getChild(int i);
  21.     public String getName();
  22.     public double getSalary();
  23.     public void print();
  24. }

  25. /* Now we create manager(composite class)
  26.  * Key point here is that all common method delegates its operations
  27.  * its operations to child objects, It has method to access
  28.  * and modify its children.
  29.  */

  30. class Manager implements IEmployee{
  31.     private String name;
  32.     private double salary;
  33.     List<IEmployee> employees = new ArrayList<IEmployee>();

  34.     public Manager(String name, double salary){
  35.         this.name = name;
  36.         this.salary = salary;
  37.     }

  38.     @Override
  39.     public void add(IEmployee emp){
  40.         employees.add(emp);
  41.     }

  42.     @Override
  43.     public void remove(IEmployee emp){
  44.         employees.remove(emp);
  45.     }

  46.     @Override
  47.     public IEmployee getChild(int i){
  48.         return employees.get(i);
  49.     }

  50.     @Override
  51.     public String getName(){
  52.         return name;
  53.     }

  54.     @Override
  55.     public double getSalary(){
  56.         return salary;
  57.     }

  58.     @Override
  59.     public void print(){
  60.         System.out.println("-----------");
  61.         System.out.println("Name ="+getName());
  62.         System.out.println("Salary ="+getSalary());
  63.         System.out.println("-----------");
  64.         Iterator<IEmployee> empIterator = employees.iterator();
  65.         while(empIterator.hasNext()){
  66.             IEmployee emp = empIterator.next();
  67.             emp.print();
  68.         }
  69.     }
  70. }

  71. /*In this class , there are many methods which are not
  72.  * applicable to developer because it is a leaf node
  73.  *
  74.  */

  75. class Developer implements IEmployee{
  76.     private String name;
  77.     private double salary;

  78.     public Developer(String name, double salary){
  79.         this.name = name;
  80.         this.salary = salary;
  81.     }

  82.     @Override
  83.     public void add(IEmployee emp){
  84.         //this is leaf node so this method is not applicable to this class
  85.     }

  86.     @Override
  87.     public void remove(IEmployee emp){
  88.         //this is leaf node so this method is not applicable to this class
  89.     }

  90.     @Override
  91.     public IEmployee getChild(int i){
  92.         //this is leaf node so this method is not applicable to this class
  93.         return null;
  94.     }

  95.     @Override
  96.     public String getName(){
  97.         return name;
  98.     }

  99.     @Override
  100.     public double getSalary(){
  101.         return salary;
  102.     }

  103.     @Override
  104.     public void print(){
  105.         System.out.println("-----------");
  106.         System.out.println("Name ="+getName());
  107.         System.out.println("Salary ="+getSalary());
  108.         System.out.println("-----------");
  109.     }

  110. }
  111. public class CompositePatternDemo {
  112.     public static void main(String[] args){
  113.         IEmployee emp1 = new Developer("John", 10000);
  114.         IEmployee emp2 = new Developer("David", 15000);
  115.         IEmployee manager1 = new Manager("Daniel", 25000);
  116.         manager1.add(emp1);
  117.         manager1.add(emp2);
  118.         IEmployee emp3 = new Developer("Michael", 20000);
  119.         Manager generalManager = new Manager("Mark", 50000);
  120.         generalManager.add(emp3);
  121.         generalManager.add(manager1);
  122.         generalManager.print();
  123.     }
  124. }

阅读(429) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~