combination mode Composite pattern synthesis mode
compose objects into the tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.Grouping objects into a tree structure to represent a "partial-whole" hierarchy, which makes the user consistent with the use of individual objects and composite objects. Abstract component Role (Component): This role defines common methods and properties that participate in composite objects and regulates some of the default behavior interfaces.
Package com.DesignPattern.Structural.Composite;
Defining an abstract Build interface
Public interface Component {
public void operation ();
}
Leaf component Role (leaf): The role is a leaf object with no other branches under it, defining the behavior of the original object that participates in the composition.
package com.DesignPattern.Structural.Composite;//定义叶子构件public class Leaf implements Component { @Override public void operation() { //业务逻辑代码 System.out.println("Leaf operation"); }}
Branch component Role (Composite): This role represents a branch object that participates in a grouped, branched branch, and his role is to combine branches and leaves into a tree structure and define methods for managing sub-objects, such as Add (), remove (), and so on.
package com.DesignPattern.Structural.Composite;import java.util.ArrayList;//定义树枝构件public class Composite implements Component { // 构件容器 private ArrayList<Component> componentList = new ArrayList<Component>(); // 添加构件 public void add(Component component) { this.componentList.add(component); } // 删除构件 public void remove(Component component) { this.componentList.remove(component); } // 获取子构件 public ArrayList<Component> getChild() { return this.componentList; } @Override public void operation() { // 业务逻辑代码 System.out.println("Composite operation"); }}
Client
package com.DesignPattern.Structural.Composite;public class Client { public static void main(String[] args) { // 创建一个根节点 Composite root = new Composite(); root.operation(); // 创建树枝节点 Composite branch = new Composite(); // 创建叶子节点 Leaf leaf = new Leaf(); // 构件树形结构 root.add(branch); branch.add(leaf); display(root); } // 遍历树(递归) public static void display(Composite root) { for (Component c : root.getChild()) { if (c instanceof Leaf) { // 如果节点类型是叶子节点 c.operation(); } else { // 树枝节点 c.operation(); display((Composite) c); // 递归调用 } } }}
example of a combined pattern
Company.java
package com.DesignPattern.Structural.Composite;//抽象接口public interface Company { //获取信息 public String getInfo();}
Concretecompany.java
package com.DesignPattern.Structural.Composite;import java.util.ArrayList;//树枝节点类public class ConcreteCompany implements Company { private ArrayList<Company> companyList = new ArrayList<Company>(); private String name; private String position; private int salary; //构造函数 public ConcreteCompany(String name, String position, int salary) { this.name = name; //姓名 this.position = position; //职位 this.salary = salary; //薪水 } public void add(Company company){ this.companyList.add(company); } public void remove(Company company){ this.companyList.remove(company); } public ArrayList<Company> getChild(){ return this.companyList; } @Override public String getInfo() { String info=""; info="名称:"+this.name; info=info+"\t职位:"+this.position; info=info+"\t薪水:"+this.salary; return info; }}
Employee.java
package com.DesignPattern.Structural.Composite;//叶子节点类public class Employee implements Company { private String name; private String position; private int salary; public Employee(String name, String position, int salary) { this.name = name; this.position = position; this.salary = salary; } @Override public String getInfo() { String info=""; info="名称:"+this.name; info=info+"\t职位:"+this.position; info=info+"\t薪水:"+this.salary; return info; }}
Clientdemo.java
Package com. Designpattern.structural.composite;public class Clientdemo {public static void main (string[] args) {//ceo Concretecompany root=new concretecompany ("Hello", "CEO", 100000); Department manager Concretecompany Developdep=new Concretecompany ("DEVELOPDEP", "research and development manager", 12000); Concretecompany salesdep=new concretecompany ("Salesdep", "Sales Manager", 12000); Concretecompany financedep=new concretecompany ("FINANCEDEP", "Finance department manager", 12000); Staff employee E1=new employee ("A", "Research and Development department", 3000); Employee E2=new Employee ("B", "Research and Development department", 3000); Employee E3=new Employee ("C", "Sales department", 3000); Employee E4=new Employee ("D", "Sales department", 3000); Employee E5=new Employee ("E", "Finance Department", 3000); Employee E6=new Employee ("F", "Finance Department", 3000); Spanning Tree Root.add (DEVELOPDEP); Root.add (SALESDEP); Root.add (FINANCEDEP); Developdep.add (E1); Developdep.add (E2); Salesdep.add (E3); Salesdep.add (E4); Financedep.add (E5); Financedep.add (E6); System.out.println (Root.getinfo ()); display (root); }//Traversal tree (recursion) public static void display (Concretecompany root) {in (company C:root.getchild ()) {if (c instanceof Employee) {//If the node type is a leaf node System.out.println (C.getinfo ()); }else{//Branch node System.out.println ("\ n" +c.getinfo ()); Display ((Concretecompany) c); Recursive Call}}}}
Copyright NOTICE: This article for Bo Master original article, without BO Master permission cannot reprint |copyright©2011-2015,supernatural, all rights Reserved.
Designpattern_java:composite Pattern