C ++ Design Mode Composite

Source: Internet
Author: User

I. Functions

Indicates the "part-whole" relationship and allows users to use a single object and composite object in a consistent manner.

Ii. Structure Diagram

Can also be expanded, leaf and composite can be used as abstract base classes as needed, and child classes can be generated from them.

Iii. Advantages and Disadvantages

Advantage: for the composite mode, people may focus their attention on how it implements composite objects. However, the most important thing about composite is that the user does not care about the combination object or a single object, and the user will process it in a unified manner, therefore, the base class should be a public interface proposed from a single object and a composite object.

Disadvantage: the biggest problem with composite is that it is not easy to restrict components in a combination.

IV. Implementation

Sometimes you need to restrict components in the combination, that is, you want a composite to have only some specific leaf. I used multi-inheritance and dynamic type conversion to solve this problem. If composite1 can only contain a single object concreteleaf1, composite2 can contain a single object concreteleaf1 and concreteleaf2. As shown in:

There are many class hierarchies in it. They use abstractleaf1 and abstractleaf2, but they do not use abstractcomposite1 and abstractcomposite2. This is not important. You can also remove abstractleaf1 and abstractleaf2, you can decide whether or not to use it based on the actual situation.

Simple code implementation is as follows:

Namespace designpattern_composite
{
Class component
{
Public:
Virtual void operation () = 0;
Virtual void add (component *){}
};

Class abstractcomponent1: virtual public component {};

Class implements actleaf1: virtual public implements actcomponent1 {};

Class Composite1: public AbstractComponent1
{
Public:
Virtual void operation () {/* do operation */}
Virtual void Add (Component *);
};
Void Composite1: Add (Component * p)
{
AbstractComponent1 * pc1 = dynamic_cast <ABSTRACTCOMPONENT1 *> (p );
If (pc1 = NULL) return;
// Do add operation
}

Class AbstractComponent2: virtual public Component {};

Class AbstractLeaf2: virtual public AbstractComponent2 {};

Class Composite2: public AbstractComponent2
{
Public:
Virtual void operation () {/* do operation */}
Virtual void Add (Component *);
};
Void Composite2: Add (Component * p)
{
Abstractcomponent2 * PC2 = dynamic_cast <abstractcomponent2 *> (P );
If (PC2 = NULL) return;
// Do add operation
}

Class concreteleaf1: Public registractleaf1
{
Public:
Virtual void operation () {/* do operation */}
};

Class concreteleaf2: Public registractleaf1, public registractleaf2
{
Public:
Virtual void operation () {/* do operation */}
};
}

Client code:

{
Using namespace designpattern_composite;

Component * pC1 = new concreteleaf1 ();
Component * PC2 = new concreteleaf2 ();
Component * PC3 = new composite1 ();
Component * pc4 = new composite2 ();
PC3-> Add (pC1); // OK
PC3-> Add (PC2); // OK
PC3-> Add (PC3); // OK
PC3-> Add (pc4); // fail
Pc4-> Add (pC1); // fail
Pc4-> Add (PC2); // OK
Pc4-> Add (PC3); // fail
Pc4-> Add (pc4); // OK
}

There are two points to note: first, because multiple inheritance is used, virtual inheritance is required. The second is to use dynamic_cast to determine whether the component can be combined.

V. Sample Code

Namespace designpattern_composite
{
// Class component
Class component
{
Public:
Virtual void operation () = 0;
Virtual void add (component *){}
};
// Class leaf
Class leaf: public component
{
Public:
Virtual void operation (){}
};
// Class Composite
Class Composite: public component
{
Public:
Virtual void Add (Component * p) {_ list. push_back (p );}
Virtual void Operation ()
{
Vector <Component *>: const_iterator it;
For (it = _ list. begin (); it! = _ List. end (); it ++)
(* It)-> Operation ();
}
Private:
Vector <Component *> _ list;
};
}

Vi. Instances

(1) The Composite mode is used in JUnit.

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.