Design mode structural mode combined mode

Traditional solutions to the problems of School Department display
1) Consider a college as a subclass of a school, and a department as a subclass of a college. In fact, this is based on the size of the organization
2) In fact, our requirements are: show the composition of the school's departments in a page. A school has multiple colleges and a college has multiple departments. Therefore, this kind of scheme can't well realize the management operation, such as adding, deleting, traversing, etc. to the college and department
3) Solution: regard schools, colleges and departments as organizational structures. There is no inherited relationship between them, but a tree structure, which can better achieve management operations. =>Combination mode

Basic introduction
1) Composite Pattern, also known as partial overall pattern, creates a tree structure of object groups and combines objects into a tree structure to represent the hierarchical relationship of "whole part".
2) The combination pattern combines objects according to the tree structure to represent the part and the whole level.
3) This type of design pattern belongs to structural pattern.
4) The combination mode enables users to have consistent access to individual objects and composite objects, that is, combination enables customers to handle individual objects and composite objects in a consistent way.

Description of the schematic diagram (i.e. (roles and responsibilities of the combined pattern)
1) Component: This is the object declaration interface in the composition. When appropriate, it implements the default behavior of the interface shared by all classes. It is used to access and manage component subassemblies. Component can be abstract class or interface
2) Leaf: represents a Leaf node in the combination, and the Leaf node has no child nodes.

1) The combination pattern solves the problem that when our object to be processed can generate a tree structure, and we want to operate on the nodes and leaves on the tree, it can provide a consistent way, regardless of whether it is a node or a leaf
2) Corresponding schematic diagram

The application of combinatorial model in school department display
1) Application instance requirements
Write a program to show the structure of a school's departments: the demand is like this, to show the composition of the school's departments in a page, a school has more than one college, a college has more than one department.
2) Thought analysis and diagram (class diagram)

public abstract class OrganizationComponent {

	private String name; // Name
	private String des; // Explain
	
	protected  void add(OrganizationComponent organizationComponent) {
		//Default implementation
		throw new UnsupportedOperationException();
	}
	
	protected  void remove(OrganizationComponent organizationComponent) {
		//Default implementation
		throw new UnsupportedOperationException();
	}

	//constructor
	public OrganizationComponent(String name, String des) {
		super();
		this.name = name;
		this.des = des;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDes() {
		return des;
	}

	public void setDes(String des) {
		this.des = des;
	}
	//Method print, make abstract, subclass all need to be implemented
	protected abstract void print();
}
//University is Composite, which can manage College
public class University extends OrganizationComponent {

	List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

	// constructor
	public University(String name, String des) {
		super(name, des);
		// TODO Auto-generated constructor stub
	}

	// Rewrite add
	@Override
	protected void add(OrganizationComponent organizationComponent) {
		// TODO Auto-generated method stub
		organizationComponents.add(organizationComponent);
	}

	// Rewrite remove
	@Override
	protected void remove(OrganizationComponent organizationComponent) {
		// TODO Auto-generated method stub
		organizationComponents.remove(organizationComponent);
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return super.getName();
	}

	@Override
	public String getDes() {
		// TODO Auto-generated method stub
		return super.getDes();
	}

	// The print method is to output the colleges included in the University
	@Override
	protected void print() {
		// TODO Auto-generated method stub
		System.out.println("--------------" + getName() + "--------------");
		//Traverse organizationComponents 
		for (OrganizationComponent organizationComponent : organizationComponents) {
			organizationComponent.print();
		}
	}

}
public class Department extends OrganizationComponent {

	//No set
	
	public Department(String name, String des) {
		super(name, des);
		// TODO Auto-generated constructor stub
	}

	
	//Add, remove don't need to write, because it's a leaf node
	
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return super.getName();
	}
	
	@Override
	public String getDes() {
		// TODO Auto-generated method stub
		return super.getDes();
	}
	
	@Override
	protected void print() {
		// TODO Auto-generated method stub
		System.out.println(getName());
	}

}
public class College extends OrganizationComponent {
	//Department stored in List
	List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
	// constructor
	public College(String name, String des) {
		super(name, des);
		// TODO Auto-generated constructor stub
	}
	// Rewrite add
	@Override
	protected void add(OrganizationComponent organizationComponent) {
		// TODO Auto-generated method stub
		//  In the future actual business, Colleage's add and university's add may not be exactly the same
		organizationComponents.add(organizationComponent);
	}
	// Rewrite remove
	@Override
	protected void remove(OrganizationComponent organizationComponent) {
		// TODO Auto-generated method stub
		organizationComponents.remove(organizationComponent);
	}
	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return super.getName();
	}

	@Override
	public String getDes() {
		// TODO Auto-generated method stub
		return super.getDes();
	}

	// The print method is to output the colleges included in the University
	@Override
	protected void print() {
		// TODO Auto-generated method stub
		System.out.println("--------------" + getName() + "--------------");
		//Traverse organizationComponents 
		for (OrganizationComponent organizationComponent : organizationComponents) {
			organizationComponent.print();
		}
	}
}
public class Client {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//Create an object school from large to small
		OrganizationComponent university = new University("Tsinghua University", " Top universities in China ");
		
		//Create a college
		OrganizationComponent computerCollege = new College("school of computing", " school of computing ");
		OrganizationComponent infoEngineercollege = new College("School of Information Engineering", " School of Information Engineering ");
		
		
		//Create departments (majors) under each college
		computerCollege.add(new Department("software engineering", " Good software engineering "));
		computerCollege.add(new Department("Network engineering", " Good network engineering "));
		computerCollege.add(new Department("Computer science and technology", " Computer science and technology is an old specialty "));
		
		//
		infoEngineercollege.add(new Department("communication engineering", " Communication engineering is not easy to learn "));
		infoEngineercollege.add(new Department("Information Engineering", " Information engineering is eager to learn "));
		
		//Add colleges to schools
		university.add(computerCollege);
		university.add(infoEngineercollege);
		
		//university.print();
		infoEngineercollege.print();
	}
}

Notes and details of combination mode
1) Simplify client operations. The client only needs to face the same object without considering the whole part or node leaf.
2) It has strong expansibility. When we want to change the composite object, we only need to adjust the internal hierarchical relationship, and the client does not need to make any changes
3) It is convenient to create complex hierarchies. The client does not need to pay attention to the composition details in the combination, so it is easy to add nodes or leaves to create a complex tree structure
4) When you need to traverse the organization, or the objects you are working on have a tree structure, the composite pattern is very suitable
5) It requires high abstraction. If there are many differences between nodes and leaves, for example, many methods and attributes are different, so it is not suitable to use combination mode

 

 

 

 

Published 100 original articles, won praise 8, visited 60000+
Private letter follow

Tags: network

Posted on Sun, 02 Feb 2020 23:20:17 -0800 by mattock