Ext.: http://www.cnblogs.com/doubleliang/archive/2011/12/27/2304104.html
In short, let all the leaf nodes perform the same operation!!!!!!!!!!!!!!!
Combined mode, which combines objects into a tree structure to represent a "partial-whole" hierarchy, combining patterns makes the user consistent with the use of individual objects and composite objects.
Sometimes called part-overall mode, it blurs the concept of simple elements and complex elements in the problem of our tree structure, and the client program can deal with complex elements like simple elements, thus decoupling the client program from the internal structure of the complex elements.
The combined mode allows you to optimize the processing of recursive or hierarchical data structures. There are many examples of hierarchical data structures that make combinatorial patterns very useful. A universal example of hierarchical data structures is what you encounter every time you use a computer: the file system. The file system consists of directories and files. Each directory can be loaded with content. The contents of a directory can be either a file or a directory. In this way, the computer's file system is organized in a recursive structure. If you want to describe such a data structure, then you can use the combined mode composite.
Let's take a look at the definition of combinatorial mode, in the book of "Big Talk design pattern" The definition of combinatorial mode is: " combine objects into a tree structure to represent the ' part-whole ' hierarchy." The combined mode makes the user consistent with the use of individual objects and composite objects. "
Then take me to cut the card to analyze the matter.
First of all, a card can be at Headquarters, branch, franchise store use, then head office can swipe cards, stores can also swipe cards, franchise stores can also swipe, this attribute structure of the store hierarchy relationship is clear.
Then, the head Office card consumption and store credit card consumption is the same reason, then the main store and the use of the membership card is also consistent.
1. Examples of combinatorial patterns
Combo Mode structure diagram:
Then the example of the combined pattern is as follows:
Abstract Part class describes the behavior common to all parts in the future
Public abstract class Component
{
protected string name;
public string Name
{
Get
{
return name;
}
Set
{
name = value;
}
}
Add a part
public abstract void Add (Component Component);
Delete a part
public abstract void Remove (Component Component);
Traverse all Child Parts
public abstract void Eachchild ();
}
Composite Part class
public class Leaf:component
{
Leaf nodes do not have the ability to add, so do not implement
public override void Add (Component Component)
{
throw new NotImplementedException ();
}
The ability of a leaf node not to be added must not be removed.
public override void Remove (Component Component)
{
throw new NotImplementedException ();
}
Leaf nodes do not have child nodes, so display their own execution results
public override void Eachchild ()
{
Console.WriteLine ("{0} executed..", name);
}
}
Combination Class
public class Composite:component
{
Used to save the assembled parts
list<component> myList = new list<component> ();
Add a node Add part
public override void Add (Component Component)
{
Mylist.add (component);
}
Delete a node delete part
public override void Remove (Component Component)
{
Mylist.remove (component);
}
Traversing child nodes
public override void Eachchild ()
{
Console.WriteLine ("{0} executed..", name);
foreach (Component C in myList)
{
C.eachchild ();
}
}
}
static void Main (string[] args)
{
Constructing the root node
Composite rootcomponent = new Composite ();
Rootcomponent.name = "root node";
Add two leaves, which is a subassembly
Leaf L = new leaf ();
L.name = "leaf node One";
leaf L1 = new leaf ();
L1. Name = "leaf node two";
Rootcomponent.add (l);
Rootcomponent.add (L1);
Traversing combined parts
Rootcomponent.eachchild ();
}
The results of the operation are as follows:
2. Apply the combination mode of membership card consumption
Then we are based on our membership card consumption, to simulate the implementation of the combination mode! Let ' s go!
First of all:
1. Our parts are, head office, branch, franchise store!
2. The common behavior of our parts is: Swipe the membership card
3. The hierarchical relationship between the parts, that is, the level of the store is, the head office has branches, branches can have a franchise store.
With our necessary conditions, my request is that the current store activities when I swipe in the head office, you can accumulate the equivalent of the total amount of credit card in all subordinate stores, the design code is as follows:
<summary>
Store-type abstract storefront parts
</summary>
Public abstract class Storefront
{
Shop Name
Protected string storeName = String. Empty;
public string StoreName
{
Get
{
return storeName;
}
}
Add Storefront
public abstract void Add (Storefront store);
Delete Store
public abstract void Remove (Storefront store);
Define the behavior of all parts that are common to swipe behavior
public abstract void Paybycard ();
}
public class Storeorbranch:storefront
{
constructor function
Public Storeorbranch () {}
Public Storeorbranch (String storeName)
{
This.storename = StoreName;
}
list<storefront> mystorelist = new list<storefront> ();
Credit card consumption
public override void Paybycard ()
{
Console.WriteLine ("Store {0} points have been added to the membership card", storeName);
foreach (Storefront sf in mystorelist)
{
sf. Paybycard ();
}
}
Add Store
public override void Add (Storefront store)
{
Mystorelist.add (store);
}
Release the Store
public override void Remove (Storefront store)
{
Mystorelist.remove (store);
}
}
public class Joininstore:storefront
{
constructor function
Public Joininstore () {}
Public Joininstore (String storeName)
{
This.storename = StoreName;
}
Credit card consumption
public override void Paybycard ()
{
Console.WriteLine ("Store {0} points have been added to the membership card", storeName);
}
public override void Add (Storefront store)
{
throw new NotImplementedException ();
}
public override void Remove (Storefront store)
{
throw new NotImplementedException ();
}
}
static void Main (string[] args)
{
Storeorbranch store = new Storeorbranch ("Chaoyang Honten");
Storeorbranch Brach = new Storeorbranch ("Dongcheng branch");
Joininstore Jstore = new Joininstore ("Haidian Franchise Shop One");
Joininstore jstore1 = new Joininstore ("on the ground to join the store two");
Brach. ADD (Jstore);
Brach. ADD (Jstore1);
Store. ADD (Brach);
Store. Paybycard ();
}
The results of the operation are as follows:
In this way, in the accumulation of all sub-store points, there is no need to care about the number of sub-stores, or whether the relationship is a leaf node or a combination of nodes, that is, whether it is the head Office swipe card, or franchise store card, can be the correct and effective calculation of the activity points.
3. When to use the combo mode
Quote a fragment of the big Talk design pattern: "When you discover that a requirement is a part and a whole hierarchy, and you want users to be able to ignore the difference between a combined object and a single object, you should consider combining patterns when you use all the objects in a composite structure uniformly." "
Combination mode (Composite pattern)