Design Mode-Combination Mode

Source: Internet
Author: User

Definition:

The object is combined into a tree structure to represent the "part-whole" hierarchy. The combination structure makes the use of a single object and a combination object consistent.

Keywords:

Composite

Structure:

Application scenarios:

When the object is expressed as a "Whole-part" hierarchy.

That is, when the final result of an object is tree-shaped, the combination mode can be used.

For example:

China Mobile has three brands: Global connect, shenzhouxing, and dynamic zone.

At the same time, China Mobile's subsidiaries include Beijing Mobile, Tianjin mobile, Jiangsu mobile, and other subsidiaries. Each subsidiary has three brands.

Each branch also contains subordinate units and related brands.

The hierarchy of this tree structure uses the combination mode.

In fact, the brand and the company are not a class that can be described, but in order to make it easier for the customer to handle it, it can be designed without any complicated or simple elements.

Example:

Class diagram:

 
Code:
Public   Abstract   Class Mobile { Protected   String Name; Public Mobile ( String Name ){ This . Name = Name ;} Public   Abstract   Void Add (mobile m ); Public   Abstract   Void Remove (mobile m );Public   Abstract   Void Duty ( Int Depth );} Public   Class Easyown: Mobile { Public Easyown ( String Name ): Base (Name ){} Public   Override   Void Add (mobile m ){} Public   Override  Void Remove (mobile m ){} Public   Override   Void Duty ( Int Depth) {httpcontext. Current. response. Write ( New   String ('-', Depth) + name +" (It's easy for me, shenzhouxing !) <Br/> ");}} Public   Class M_zone: Mobile { Public M_zone ( String Name ):Base (Name ){} Public   Override   Void Add (mobile m ){} Public   Override   Void Remove (mobile m ){} Public   Override   Void Duty ( Int Depth) {httpcontext. Current. response. Write ( New   String ('-', Depth) + name +"(My website, listen to me !) <Br/> ");}} Public   Class Gotone: Mobile { Public Gotone ( String Name ): Base (Name ){} Public   Override   Void Add (mobile m ){} Public   Override   Void Remove (mobile m ){} Public  Override   Void Duty ( Int Depth) {httpcontext. Current. response. Write ( New   String ('-', Depth) + name +" (I can !) <Br/> ");}} Public   Class Mobilecompany: Mobile {list <mobile> Al = New List <mobile> (10 ); Public Mobilecompany ( String Name ): Base (Name ){} Public   Override   Void Add (mobile m) {Al. Add (m );} Public   Override   Void Remove (mobile m) {Al. Remove (m );} Public   Override   Void Duty ( Int Depth) {httpcontext. Current. response. Write ( New   String ('-', Depth) + name +"<Br/> "); Foreach (Mobile m In Al) {M. Duty (depth + 2 );}}}
 
Page call:
     Protected   Void Page_load ( Object Sender, eventargs e) {mobile M1 = New Mobilecompany (" China Mobile "); M1.add ( New Easyown (" Shenzhouxing "); M1.add ( New M_zone (" Dynamic Zone "); M1.add ( New Gotone (" Global connect "); Mobile m2 = New Mobilecompany (" Jiangsu mobile "); M2.add ( New Easyown (" Shenzhouxing "); M2.add ( New M_zone (" Dynamic Zone "); M2.add ( New Gotone (" Global connect "); Mobile m3 =New Mobilecompany (" Sichuan Mobile "); M3.add ( New Easyown (" Shenzhouxing "); M3.add ( New M_zone (" Dynamic Zone "); M3.add ( New Gotone (" Global connect "); Mobile m31 = New Mobilecompany (" Chengdu mobile "); M31.add ( New Easyown (" Shenzhouxing "); M31.add ( New M_zone (" Dynamic Zone "); M31.add ( New Gotone (" Global connect "); M3.add (m31); m1.add (m2); m1.add (m3); m1.duty (0 );}
 
Effect:
 

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.