Github source address

An overview of 23 design patterns

  • Java Language Design – An overview of 23 design patterns

Creation pattern

  • Factory Method Pattern
  • Abstract Factory Pattern
  • Builder Mode
  • Prototype mode
  • Singleton

Structural mode

  • Facade Pattern
  • Adapter mode (Adapter)
  • Proxy mode
  • Composite mode
  • Flyweight Mode
  • Decorator pattern
  • Bridge mode (Bridge)

Behavioral pattern

  • Mediator Mode
  • Observer Model
  • Command mode
  • Iterator pattern (Iterator)
  • Template Method
  • Strategy Pattern
  • State mode
  • Memento Mode
  • Interpreter mode
  • Chain of Responsibility model
  • Visitor Pattern

define

The composite mode is also called the composite mode, sometimes also called the partial-whole mode. It is mainly used to describe the relationship between parts and the whole. Objects are combined into a tree structure to represent the partial-whole hierarchical structure, so that users can use a single object and a composite object consistently.

Introduction to the role of composite patterns (there are two implementations of composite patterns: secure and transparent)

  • Component Abstract Roles define common methods and properties that participate in composite objects, and can define some default behaviors or properties.
  • Leaf objects have no further branches, which are the smallest units to traverse.
  • Composite branch object, which combines branch nodes and leaf nodes to form a tree structure. The focus of combination mode is on the branch members.

Usage scenarios

  • Whenever you have a tree structure or whenever you want to represent a relationship between part and whole, and that relationship can be deep, you want to think about composition patterns.
  • A scenario in which some modules or functions can be separated from a whole.
  • Maintain and display scenarios for partial-whole relationships.

The concrete implementation of security mode and transparent mode

1. Safe mode

(1) Abstract components

public abstract class Component {
	// Both individual and whole
	public void operation(a){
		// Write business logic}}Copy the code

(2) Branch members

public class Composite extends Component {
	// The component container
	private List<Component> componentArrayList = new ArrayList<Component>();
	// Add a leaf member or a branch member
	public void add(Component component){
		this.componentArrayList.add(component);
	}
	// Delete a leaf or branch member
	public void remove(Component component){
		this.componentArrayList.remove(component);
	}
	// Get all the leaves and branches under the branch
	public List<Component> getChildren(a){
		return this.componentArrayList; }}Copy the code

(3) Leaf components

public class Leaf extends Component {
	Public void operation(){* *} */
}
Copy the code

(4)Client

public class Client {
	public static void main(String[] args) {
		// Create a root node
		Composite root = new Composite();
		root.operation();
		// Create a branch component
		Composite branch = new Composite();
		// Create a leaf node
		Leaf leaf = new Leaf();
		// Build the whole
		root.add(branch);
		branch.add(leaf);
	}

	// Iterate through the tree recursively
	public static void showTree(Composite root){
		for(Component c:root.getChildren()){
			if(c instanceof Leaf){ // Leaf node
				c.operation();
			}else{ // Branch nodeshowTree((Composite)c); }}}}Copy the code

2. Transparent mode

(1) Abstract components

public abstract class Component {
	// Both individual and whole
	public void operation(a){
		// Write business logic
	}
	// Add a leaf member or a branch member
	public abstract void add(Component component);
	// Delete a leaf or branch member
	public abstract void remove(Component component);
	// Get all the leaves and branches under the branch
	public abstract List<Component> getChildren(a);
}
Copy the code

(2) Branch members

public class Composite extends Component {
	// The component container
	private ArrayList<Component> componentArrayList = new ArrayList<Component>();
	// Add a leaf member or a branch member
	public void add(Component component){
		this.componentArrayList.add(component);
	}
	// Delete a leaf or branch member
	public void remove(Component component){
		this.componentArrayList.remove(component);
	}
	// Get all the leaves and branches under the branch
	public List<Component> getChildren(a){
		return this.componentArrayList; }}Copy the code

(3) Leaf components

public class Leaf extends Component {

	public void add(Component component){
		/ / empty implementation
	}

	public void remove(Component component){
		/ / empty implementation
	}

	public List<Component> getChildren(a){
		/ / empty implementation}}Copy the code

(4)Client

public class Client {

	public static void main(String[] args) {
		// Create a root node
		Composite root = new Composite();
		root.operation();
		// Create a branch component
		Composite branch = new Composite();
		// Create a leaf node
		Leaf leaf = new Leaf();
		// Build the whole
		root.add(branch);
		branch.add(leaf);
	}

	// Iterate through the tree recursively
	public static void showTree(Component root){
		for(Component c:root.getChildren()){
			if(c instanceof Leaf){ // Leaf node
				c.operation();
			}else{ // Branch nodeshowTree(c); }}}}Copy the code

4. The difference between secure mode and transparent mode

  • The security pattern defines only some default behaviors or properties in the abstract component, which is to completely separate the branch node from the leaf node. Transparent mode is to put the methods used for composition into an abstract class, whether the leaf object or the branch object has the same structure, by determining whether it is a leaf node or a branch node, if not handled properly, this can cause problems at runtime, not very recommended.
  • Conflict between safety model and dependency inversion principle; The advantage of transparent mode is that it basically follows the principle of dependency reversal, which facilitates system expansion.
  • Safe mode requires a cast when traversing the tree structure. In transparent mode, it is easier to traverse the entire tree structure without casting.