When I was drinking milk, I found a big block of erie branch on the bag. Hey, today I learned the combination mode, and I am using examples to better understand the combination mode.
In Inner Mongolia, the shelf life for items like milk is not long. In order to let everyone drink really high-quality milk, Erie has added factories nationwide to meet their needs.
Both the total factory and the sub-factory have production and sales departments. In the combination mode, this can be said to be a problem that the whole and part can be treated in the same way.
Let's take a look at the code of the Erie factory combination mode:
Abstract class ErieFactory // Erie factory class {protected string name; public ErieFactory (string name) {this. name = name;} public abstract void Add (ErieFactory F); public abstract void Remove (ErieFactory F); public abstract void Display (int depth ); // display public abstract void LineOfDuty (); // perform duties} class ConcreteFactory: ErieFactory // specific factory class {private List
Children = new List
(); Public ConcreteFactory (string name): base (name) {} public override void Add (ErieFactory F) // Add {children. add (F);} public override void Remove (ErieFactory F) // Remove {children. remove (F);} public override void Display (int depth) // Display {Console. writeLine (new string ('-', depth) + name); foreach (ErieFactory componment in children) {componment. display (depth + 2) ;}}// perform public override void LineOfDuty () {foreach (ErieFactory componment in children) {componment. lineOfDuty () ;}}// Production Department class LineDepartment: ErieFactory {public LineDepartment (string name): base (name) {} public override void Add (ErieFactory F) // Add {} public override void Remove (ErieFactory F) // Remove {} public override void Display (int depth) // Display {Console. writeLine (new string ('-', depth) + name);} // perform the public override void LineOfDuty () {Console. writeLine ("{0} produce high quality milk", name) ;}// sales department class SaleDepartment: ErieFactory {public SaleDepartment (string name): base (name) {} public override void Add (ErieFactory F) // Add {} public override void Remove (ErieFactory F) // Remove {} public override void Display (int depth) // display {Console. writeLine (new string ('-', depth) + name);} // perform the public override void LineOfDuty () {Console. writeLine ("{0} only high quality milk", name );}}
In code implementation, we first define an abstract Erie factory class and define a specific factory class, which inherits all methods of the Erie factory.
Client code:
Static void Main (string [] args) {ConcreteFactory root = new ConcreteFactory ("Erie factory"); root. add (new LineDepartment ("total factory production Department"); root. add (new SaleDepartment ("total factory Sales Department"); ConcreteFactory comp = new ConcreteFactory ("Shanghai Branch"); comp. add (new LineDepartment ("Shanghai Branch Production Department"); comp. add (new SaleDepartment ("Shanghai Branch Sales Department"); root. add (comp); Console. writeLine ("\ n structure:"); root. display (1); Console. writeLine ("\ n responsibility:"); root. lineOfDuty (); Console. read ();}
Let's take a look at the code structure:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + small "http://www.2cto.com/uploadfile/Collfiles/20140502/20140502092230105.jpg" alt = "\">
Through the above instance, you should have a little understanding of the combination mode. Now let's take a look at the combination mode.
Combination Mode: combines objects into a tree structure to represent a "part-whole" hierarchy. The combination mode ensures consistency between the use of a single object and a composite object.
Let's take a look at the structure of the combination mode:
Add and Remove are added to the Leaf node of Leaf to enable all sub-classes of the Component interface to have Add and Remove. The advantage is that the Leaf node and branch node have no difference with the outside world, they have consistent behavior interfaces. However, there is also a problem. Leaf itself does not have these methods and implementation is meaningless. This method is calledTransparent.
If you do not want to do this, do not declare the Add and Remove methods in the Component interface, then Leaf in the subclass does not need to implement it. In Composite, declare all methods used to manage subclass objects, in this way, there will be no such problem, but because it is not transparent enough, the leaves and branches will not have the same interface, and the client needs to make the corresponding judgment for the call. This method is calledSecurity Mode.
When to use:When the requirements reflect the structure of parts and overall Layers
If you want to ignore the differences between a composite object and a single object
Benefits:The combination mode allows you to use the combination structure and a single object in a consistent manner.