Overview
For a tree structure, when a method of a container object (such as a folder) is called, the entire tree is traversed, looking for a member object that also contains the method (which can be a container object or a leaf object) and invoke execution, holding a move hundred, in which the mechanism of recursive invocation is used to process the entire structure. Because of the functional differences between the container object and the leaf object, the container object and the leaf object must be treated differently in the code that uses the objects, but in most cases we want to deal with them consistently, because the difference in treatment of these objects will make the program very complex. The combination pattern is created to solve such problems, which can make the use of leaf objects and container objects consistent
Defined
Combined mode (Composite pattern): Combines multiple objects to form a tree structure to represent a hierarchy with a "whole-part" relationship. The combined pattern is consistent with the use of a single object (that is, the leaf object) and the combined object (that is, the container object), which can also be called the "whole-part" (part-whole) pattern, which is an object-structured pattern.
Realize
Top-Level base class
Public Abstract classFile { Public stringName {Get;Set; } PublicFile (stringname) { This. Name =name; } /// <summary> ///Show file Methods/// </summary> Public Virtual voidDisplay () {Console.WriteLine (name); } }
Container class
Public classFolder:file {/// <summary> ///File Collection/// </summary> PrivateList<file>files; PublicFolder (stringName):Base(name) {Files=NewList<file>(); } /// <summary> ///New File Method/// </summary> /// <param name= "file" ></param> Public voidAdd (file file) {files. ADD (file); } Public voidRemove (file file) {files. Remove (file); } Public Override voidDisplay () {Base. Display (); Files. ForEach (P={p.display (); }); } }
Leaf Node class
public class Imagefile:file { public imagefile (string name): base (name) {} public void Display () {Console.WriteLine ( base .name); } }
Public class Textfile:file { public textfile (stringbase(name) {} public Override void Display () { Console.WriteLine (base. Name); } }
Client
Static voidMain (string[] args) {Folder Folder=NewFolder ("Total Folder"); Folder. ADD (NewTextfile (string. Format ("{0} Journal file. txt", Getlevelstring (1)))); Folder. ADD (NewImageFile (string. Format ("{0} graduation photo. JPG", Getlevelstring (1)))); Folder Level_one_folder=NewFolder (string. Format ("{0}1-level folders", Getlevelstring (1))); Level_one_folder.add (NewTextfile (string. Format ("{0} Grave robbers note. txt", Getlevelstring (2)))); Level_one_folder.add (NewImageFile (string. Format ("{0} profanity. txt", Getlevelstring (2)))); Folder. ADD (Level_one_folder); Folder Level_two_folder=NewFolder (string. Format ("{0}2-level folders", Getlevelstring (2))); Level_two_folder.add (NewTextfile (string. Format ("{0} Bridges of Dreams .", Getlevelstring (3)))); Level_two_folder.add (NewImageFile (string. Format ("{0} jon. JPG", Getlevelstring (3)))); Folder. ADD (Level_two_folder); Folder. Display (); Console.ReadLine (); } Static stringGetlevelstring (intLevel ) {StringBuilder SB=NewStringBuilder (); for(vari =0; I < level; i++) {sb. Append ("\ t"); } returnsb. ToString (); }
Summarize
Key Benefits
1, the combination mode can clearly define the hierarchical complex objects, representing all or part of the object level, it allows the client to ignore the hierarchy of differences, easy to control the entire hierarchy.
2, the client can consistently use a composite structure or a single object, do not care whether to deal with a single object or the entire composition structure, simplifying the client code.
3, adding new container components and leaf components in the combined mode is very convenient, no need to modify the existing class library, in line with the "open and closed principle."
4. The combination mode provides a flexible solution for the object-oriented implementation of tree structure, which can form complex tree structure through the recursive combination of leaf object and container object, but the control of tree structure is very simple.
Main disadvantages
It is difficult to limit the types of artifacts in a container when adding new artifacts. Sometimes we want a container to have only certain types of objects, such as only text files in a folder, and when using composite mode, you cannot rely on the type system to impose these constraints, because they all come from the same abstraction layer, in which case it must be done by typing at run time, This implementation process is more complex
The combination mode of structural pattern