Design Mode-Combination Mode
Composite definition: combines objects into a tree structure to represent hierarchical relationships, and allows objects to be presented to client programs in the same way. For example, the compilation of family genealogy: Male: can be inherited, and has the right to remove some people from genealogy. Female: it is recorded in the family tree, but it cannot be inherited. Understanding: In every small family, both Mom and Dad are the masters of me. You can kick out any one of me and mom, or add any one. Components in the component mode can be composed of a single object or multiple components (Multi-Level continuation of a family or even a family). Common Code of the family members: copy the code //// <summary> //// the commonalities of the ethnic population abstracted by the ethnic population /// </summary> public abstract class Father {// The name of the ethnic group protected string name = string. empty; public string Name {get {return name ;}// Add the descendant public abstract void Add (Father boy); // evicted the family tree public abstract void Remove (Father boy ); // define all people and make an overview public abstract void Intro ();} copy the code family member code copy the code // male descendant public class Boy: father {// constructor public Boy () {} public Boy (string Name) {this. name = Name ;}list <Father> myFamily = new List <Father> (); // self-introduction public override void Intro () {Console. writeLine ("I am: {0};", Name); foreach (Father f in myFamily) {f. intro () ;}/// Add descendant public override void Add (Father boy) {myFamily. add (boy);} // eviction family tree public override void Remove (Father boy) {myFamily. remove (boy) ;}// female descendant public class Gril: Father {// constructor public Gril () {} public Gril (string Name) {this. name = Name;} // self-introduction public override void Intro () {Console. writeLine ("I am: {0};", Name) ;}// public override void Add (Father store) {throw new NotImplementedException ();} // public override void Remove (Father store) {throw new NotImplementedException () ;}} copy the code client code: copy the code public static void Main () {// Grandpa takes his wife Boy yeye = new Boy ("grandpa"); Gril nainai = new Gril ("grandma"); yeye. add (nainai); // grandpa wants children Boy baba = new Boy ("dad"); Gril gugu = new Gril ("aunt"); yeye. add (gugu); yeye. add (baba); // dad wants me Boy me = new Boy ("me"); baba. add (me); // I want a child Boy son = new Boy ("son"); me. add (son); // Grandpa's family, genealogy introduction yeye. intro (); Console. read ();} copy the code combination mode Summary