Design Pattern series-combination pattern

Source: Internet
Author: User

Today, the lights in the living room are indeed lit up a lot. Looking in the mirror, I feel like I am a lot older. It seems that the light is better than dark, and I cannot see myself. Haha, as a matter of fact, my hair is long and I decided to cut it out.

When I entered the hairdresser, a warm waiter greeted me and showed me how to wash my hair, select a stylist, and cut my hair. In the haircut, the stylist recommended a variety of billing items. I only chose to soften my hair. After that, I read the list. Wow, it's so expensive. But I still like the hair style that the stylist cut for me. I decided to cut it for him later, but it's too expensive, the hair stylist recommended their membership card to me, with a 8.6 discount for all the projects in the audience. Recently, due to the fact that they are still engaged in Christmas activities, they can enjoy a discount for cards, I thought it would be too easy, and they can use all their stores and branches, so I did not hesitate to run a membership card... ....

When I got home, I felt a little regretful. I spent so much money on the scissors membership card, but I did it too. I had to comfort myself and thought, if you have a card, you can fold it up. If you want to go to which branch you want to go to, you feel better. Suddenly you think of a model, which is very suitable for the current scenario, the name is "Combination Mode". Why is it similar to the combination mode?

Let's take a look at the definition of the combination mode. in the book "big talk design mode", the combination mode is defined as:"The object is combined into a tree structure to represent the 'partial-uniyun' hierarchy. The combination mode ensures consistency between the use of a single object and a composite object."

Let's take a look at my hair clipper card operations.

First, a card can be used at the Headquarters, branches, and franchisees. The headquarters can be swiped, the branches can be swiped, And the franchisees can also swipe their cards. The hierarchical relationship between the stores is clear.

Therefore, the same principle is true for the credit card consumption of a primary store and the credit card consumption of a branch, so the use of membership cards by the primary store and the branch is consistent.

1. Example of the Combination Mode

Combined Mode Structure:

The following is an example of the Combined Mode:

// Abstract Part class description the behavior of all parts in the future
Public abstract class component
{
Protected string name;
Public string name
{
Get
{
Return name;
}
Set
{
Name = value;
}
}
// Add part
Public abstract void add (Component component );
// Delete part
Public abstract void remove (Component component );
// Traverse all child parts
Public abstract void eachchild ();
}

// Component combination
Public class leaf: Component
{
// Leaf nodes cannot be added.
Public override void add (Component component)
{
Throw new notimplementedexception ();
}

// The leaf node cannot be deleted because it cannot be added.
Public override void remove (Component component)
{
Throw new notimplementedexception ();
}

// The execution result is displayed because no child node exists on the leaf node.
Public override void eachchild ()
{
Console. writeline ("{0} executed...", name );
}
}

// Combination
Public class composite: Component
{
// The component used to save the combination
List <component> mylist = new list <component> ();

// Add a node and add a part
Public override void add (Component component)
{
Mylist. Add (component );
}

// Delete a node to delete a part
Public override void remove (Component component)
{
Mylist. Remove (component );
}

// Traverse subnodes
Public override void eachchild ()
{
Console. writeline ("{0} executed...", name );
Foreach (component C in mylist)
{
C. eachchild ();
}
}
}
Static void main (string [] ARGs)
{
// Construct the root node
Composite rootcomponent = new composite ();
Rootcomponent. Name = "Root Node ";

// Add two leaves, that is, child parts
Leaf L = new leaf ();
L. Name = "leaf node 1 ";
Leaf L1 = new leaf ();
L1.name = "leaf node 2 ";

Rootcomponent. Add (L );
Rootcomponent. Add (L1 );

// Traverse Components
Rootcomponent. eachchild ();
}

The running result is as follows:

2. Membership card consumption in Combination Mode

Then, let's simulate the implementation of the combination mode based on our membership card consumption! Let's go!

First:

1. Our components include, main stores, branches, and franchise stores!

2. The common behavior of our components is: Membership Card swiping.

3. the hierarchical relationship between components, that is, the hierarchical relationship between stores. A main store can have a branch and a franchise store.

With these necessary conditions, my requirement is that the store is currently engaged in activities. When I swipe my card at the main store, I can accumulate the credits equivalent to the total credits for card swiping at all lower-level stores, the Code is as follows:

/// <Summary>
/// Abstract storefront Components
/// </Summary>
Public abstract class storefront
{
// Store name
Protected string storename = string. empty;
Public String storename
{
Get
{
Return storename;
}
}

// Add a storefront
Public abstract void add (storefront store );
// Delete a store
Public abstract void remove (storefront store );

// Define the common behavior of all parts.
Public abstract void paybycard ();
}

Public class storeorbranch: storefront
{
// Constructor
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} credits have been added to this membership card", storename );
Foreach (storefront SF in mystorelist)
{
SF. paybycard ();
}
}

// Add a storefront
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
Public joininstore (){}
Public joininstore (string storename)
{
This. storename = storename;
}
// Credit card consumption
Public override void paybycard ()
{
Console. writeline ("store {0} credits have been added to this 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 headquarters ");
Storeorbranch Brach = new storeorbranch ("Dongcheng branch ");
Joininstore jstore = new joininstore ("Haidian franchisee 1 ");
Joininstore jstore1 = new joinstore ("Shangdi franchisee 2 ");

Brach. Add (jstore );
Brach. Add (jstore1 );
Store. Add (Brach );

Store. paybycard ();
}

The running result is as follows:

In this way, when accumulating all the sub-store points, you do not need to worry about the number of sub-stores, or whether it is a leaf node or a combination node, that is, whether it is a master store card, you can calculate the activity points correctly and effectively by swiping the card at the franchise stores.

3. When to use the Combination Mode

The snippet that references the big talk design pattern:"When you find that the requirement is partial and overall hierarchies, and you want users to ignore the differences between a composite object and a single object and use all objects in the composite structure in a unified manner, the combination mode should be considered."

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.