DesignPattern_Java: Composite Pattern
Composite Pattern combination modeCompose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.Combine objects into a tree structure to represent the "part-whole" hierarchy, so that users can use a single object and a combination object in a consistent manner. Abstract Component role: this role defines the common methods and attributes of the object to be combined, and standardizes some default behavior interfaces.
Package com. DesignPattern. Structural. Composite;
// Define the abstract build Interface
Public interface Component {
Public void operation ();
}
Leaf component role (Leaf): this role is a Leaf object and has no other branches under it. It defines the behavior of the original object that participates in the combination.
Package com. designPattern. structural. composite; // define the Leaf Component public class Leaf implements Component {@ Override public void operation () {// business logic code System. out. println (Leaf operation );}}
Composite: this role represents the branch object of the branch that participates in the combination. Its role is to combine the branches and leaves into a tree structure, define methods for managing sub-objects, such as add () and remove.
Package com. DesignPattern. Structural. Composite; import java. util. ArrayList; // defines the public class Composite implements Component of the Component container. {// private ArrayList
ComponentList = new ArrayList
(); // Add the public void add (Component component) {this. componentList. add (component);} // Delete the public void remove (Component component) {this. componentList. remove (component);} // obtain the sub-component public ArrayList
GetChild () {return this. componentList;} @ Override public void operation () {// business logic code System. out. println (Composite operation );}}
Client
Package com. designPattern. structural. composite; public class Client {public static void main (String [] args) {// create a root node Composite root = new Composite (); root. operation (); // create a branch node Composite branch = new Composite (); // create a Leaf node leaf Leaf = new Leaf (); // component tree structure root. add (branch); branch. add (leaf); display (root);} // traversal tree (recursion) public static void display (Composite root) {for (Component c: root. getChild () {if (c instanceof Leaf) {// if the node type is Leaf node c. operation ();} else {// tree branch node c. operation (); display (Composite) c); // recursive call }}}}
Instances in Combined Mode Company. java
Package com. DesignPattern. Structural. Composite; // abstract interface public interface Company {// obtain information public String getInfo ();}
ConcreteCompany. java
Package com. DesignPattern. Structural. Composite; import java. util. ArrayList; // public class ConcreteCompany implements Company {private ArrayList
CompanyList = new ArrayList
(); Private String name; private String position; private int salary; // ConcreteCompany (String name, String position, int salary) {this. name = name; // name this. position = position; // position this. salary = salary; // salary} public void add (Company company Company) {this. companyList. add (company);} public void remove (Company company) {this. companyList. remove (company);} public ArrayList
GetChild () {return this. companyList ;}@ Override public String getInfo () {String info =; info = Name: + this. name; info = info + position: + this. position; info = info + salary: + this. salary; return info ;}}
Employee. java
Package com. designPattern. structural. composite; // leaf node class 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 = Name: + this. name; info = info + position: + this. position; info = info + salary: + 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, R & D department manager, 12000); ConcreteCompany salesDep = new ConcreteCompany (salesDep, sales department manager, 12000); ConcreteCompany example = new ConcreteCompany, finance Department Manager, 12000); // Department Employee e1 = new Employee (A, R & D department, 3000); Employee e2 = new Employee (B, R & D 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); // generate 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) {for (Company c: root. getChild () {if (c instanceof Employee) {// if the node type is leaf node System. out. println (c. getInfo ();} else {// branchnode System. out. println (+ c. getInfo (); display (ConcreteCompany) c); // recursive call }}}}