Design Mode-Combination Mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.