Spring yang
Overview
The combination mode is sometimes called the partial-overall mode, which makes the concept of simple elements and complex elements blurred in the tree structure problem, A customer program can process complex elements like a simple element, decoupling the customer program from the internal structure of the Complex Element.
The best way to describe the Composite mode is tree structure. The abstract class or interface is the root node, and then the branches sprout to form the branches and leaf nodes. Therefore, the Composite mode is usually used to describe the relationship between the part and the whole, and abstract the structure through the root node, this allows the client to treat a single-Element Node and a composite-Element Node as the same object.
Intention
Combine objects into a tree structure to represent the "part-whole" hierarchy. The Composite mode ensures consistency between the use of a single object and a Composite object. [GOF design patterns]
Structure chart
1. Structure of safe synthesis Mode
The safe synthesis mode requires that the management clustering method only appears in the tree component class, but not in the leaf component.
This form involves three roles:
- Abstract Component role: This is an abstract role that defines public interfaces and default actions for the objects involved in the combination and can be used to manage all sub-objects. In a safe synthesis mode, the component role does not define a method for managing sub-objects. This definition is provided by the Branch component object.
- Leaf component (Leaf) Role: A Leaf object is an object without sub-child objects and defines the behavior of the original objects in the combination.
- Composite role: indicates the object with sub-object in the combination. The branches object provides all methods for managing sub-objects, such as add (), remove (), and getChild.
2. Transparent synthesis mode structure
Different from the safe synthesis mode, the transparent synthesis mode requires that all specific component classes, regardless of the branches or leaves, comply with a fixed interface.
This form involves three roles:
- Abstract Component role: This is an abstract role that specifies an interface for the objects to be combined to standardize common interfaces and default behaviors.
- Leaf component (Leaf) Role: this role represents the Leaf objects that participate in the combination and defines the behavior of the original objects that participate in the combination. The leaf class provides mediocre implementation of methods used to manage sub-classes on objects, such as add (), remove (), and getChild.
- Composite role: represents the object with sub-objects in the combination and defines the behavior of such objects.
Examples in life
The combination mode combines objects into a tree structure to represent the "part-whole" hierarchy. Allows users to use a single object and composite object in a consistent manner. Although examples are abstract, arithmetic expressions are examples of combinations. Arithmetic expressions include operands, operators, and other operands. The operand can be a number or another expression. In this way, both 2 + 3 and (2 + 3) + (4*6) are valid expressions.
Figure 2 Composite mode object graph using arithmetic expressions
Example
Building a house is a combination model. First, the structure of the house is built, and then the window and door are a house. Let's see the example:
Code Design
First create the interface IBuild. cs:
public interface IBuild { string Action { get; set; } string Create(); string Finish(); }
Create Build. cs again:
public abstract class Build : IBuild { private string _Action; public string Action { get { return _Action; } set { _Action = value; } } public Build(string action) { this.Action = action; } public abstract string Create(); public abstract string Finish(); }
Create House. cs again:
Public class House: Build {protected List <IBuild> houseBuild = new List <IBuild> (); public House (string action): base (action) {} public override string Create () {StringBuilder strBuilder = new StringBuilder (); strBuilder. appendFormat ("start building {0 }. ", Action); foreach (IBuild build in houseBuild) {strBuilder. appendLine (build. create (); strBuilder. appendLine (build. finish ();} return strBuilder. toString ();} public void Add (IBuild build) {houseBuild. add (build);} public void Remove (IBuild build) {houseBuild. remove (build);} public override string Finish () {return string. format ("{0} built successfully. ", Action );}}
Create Construct. cs again:
Public class Construct: Build {public Construct (string Action): base (Action) {} public override string Create () {return string. format ("Building {0}", Action);} public override string Finish () {return string. format ("{0} built successfully. ", Action );}}
Create Window. cs again:
Public class Window: Build {public Window (string Action): base (Action) {} public override string Create () {return string. format ("installing {0 }. ", Action);} public override string Finish () {return string. format ("{0} built successfully. ", Action );}}
Create Door. cs again:
Public class Door: Build {public Door (string Action): base (Action) {} public override string Create () {return string. format ("installing {0}", Action);} public override string Finish () {return string. format ("{0} built successfully. ", Action );}}
Last call:
Public partial class Run: Form {public Run () {InitializeComponent ();} private void btnRun_Click (object sender, EventArgs e) {// --------------------------------------- House buildhouse = new House ("House"); buildhouse. add (new Construct ("House Structure"); buildhouse. add (new Window ("Window"); buildh