The combination mode of C + + design pattern _c language

Source: Internet
Author: User

Problem description

Above, is a company's organization chart, the headquarters has several subsidiaries, at the same time headquarters also has various departments, under the subsidiary has several departments. If you are developing an OA system for such a company, how do you design this OA system as a programmer? Do not say how to design implementation, and then look down, read the following content, and then go back to how to design such an OA system.

What is a combination mode?

In Gof's "design pattern: The basics of reusable object-oriented software", the combination pattern is said to combine objects into a tree structure to represent a "partial-whole" hierarchy. The combined (composite) pattern enables users to have consistency in the use of a single object and a composite object.

Group mode (composite) combines small objects into a tree structure, allowing users to manipulate objects as if they were a single object. The combination pattern defines a "partial-whole" hierarchy, basic objects can be grouped into larger objects, and the operations are repeatable and can be repeated to get a very large group of objects, but these composite objects have the same interface as the base object, so the combination is transparent and consistent in usage.

In this way, we can simply understand the combinatorial pattern, the combination mode is to put some existing objects or elements, after the composition of the new object, the new object provides an internal method, it is convenient for us to complete these elements or internal objects access and operation. We can also interpret a composite object as a container that provides various APIs to access its internal objects or elements, and we just need to use these methods to manipulate it.

UML Class Diagram

Component:

1. Declare interfaces for objects in a combination;
2. To implement the default behavior of all classes in a common interface, where appropriate;
3. Declares an interface for accessing and managing component of a component.

Leaf:

1. The leaf node object is represented in the combination, and the leaf node has no child nodes;
2. Define the behavior of leaf nodes in the composition.

Composite:

1. Define the behavior of those parts that have child parts;
2. Storage child parts.

Client:

3. An object that operates a composite part through the component interface.

Code implementation

Copy Code code as follows:

/*
* * Filename:compositepatterndemo
* * author:jelly Young
* * date:2013/12/09
* * Description:more information, http://www.jb51.net
*/
#include <iostream>
#include <string>
#include <vector>
using namespace Std;
Abstract Part class describes behavior common to all future parts
Class Component
{
Public
Component (string name): M_strcompname (name) {}
Virtual ~component () {}
virtual void Operation () = 0;
virtual void Add (Component *) = 0;
virtual void Remove (Component *) = 0;
Virtual Component *getchild (int) = 0;
Virtual String GetName ()
{
return m_strcompname;
}
virtual void Print () = 0;
Protected
String M_strcompname;
};
Class Leaf:public Component
{
Public
Leaf (string name): Component (name)
{}
void Operation ()
{
cout<< "I ' m" <<m_strCompname<<endl;
}
void Add (Component *pcomponent) {}
void Remove (Component *pcomponent) {}
Component *getchild (int index)
{
return NULL;
}
void Print () {}
};
Class Composite:public Component
{
Public
Composite (string name): Component (name)
{}
~composite ()
{
Vector<component *>::iterator it = M_veccomp.begin ();
while (it!= m_veccomp.end ())
{
if (*it!= NULL)
{
cout<< "----Delete" << (*it)->getname () << "----" <<endl;
Delete *it;
*it = NULL;
}
M_veccomp.erase (IT);
it = M_veccomp.begin ();
}
}
void Operation ()
{
cout<< "I ' m" <<m_strCompname<<endl;
}
void Add (Component *pcomponent)
{
M_veccomp.push_back (pcomponent);
}
void Remove (Component *pcomponent)
{
For (vector<component *>::iterator it = M_veccomp.begin (); it!= m_veccomp.end (); ++it)
{
if ((*it)->getname () = = Pcomponent->getname ())
{
if (*it!= NULL)
{
Delete *it;
*it = NULL;
}
M_veccomp.erase (IT);
Break
}
}
}
Component *getchild (int index)
{
if (Index > M_veccomp.size ())
{
return NULL;
}
return m_veccomp[index-1];
}
void Print ()
{
For (vector<component *>::iterator it = M_veccomp.begin (); it!= m_veccomp.end (); ++it)
{
cout<< (*it)->getname () <<endl;
}
}
Private
Vector<component *> M_veccomp;
};
int main (int argc, char *argv[])
{
Component *pnode = new Composite ("Beijing head Office");
Component *pnodehr = new Leaf ("Beijing Human resources Department");
Component *psubnodesh = new Composite ("Shanghai Branch");
Component *PSUBNODECD = new Composite ("Chengdu Branch");
Component *psubnodebt = new Composite ("Baotou Branch");
Pnode->add (Pnodehr);
Pnode->add (Psubnodesh);
Pnode->add (PSUBNODECD);
Pnode->add (PSUBNODEBT);
Pnode->print ();
Component *PSUBNODESHHR = new Leaf ("Shanghai Human Resources Department");
Component *PSUBNODESHCG = new Leaf ("Shanghai Purchasing Department");
Component *psubnodeshxs = new Leaf ("Shanghai Sales Department");
Component *PSUBNODESHZB = new Leaf ("Shanghai Quality Supervision Department");
Psubnodesh->add (PSUBNODESHHR);
Psubnodesh->add (PSUBNODESHCG);
Psubnodesh->add (PSUBNODESHXS);
Psubnodesh->add (PSUBNODESHZB);
Pnode->print ();
The company is in recession and needs to close Shanghai Quality Supervision Department
Psubnodesh->remove (PSUBNODESHZB);
if (Pnode!= NULL)
{
Delete Pnode;
Pnode = NULL;
}
return 0;
}

Implementation Essentials

One of the keys to 1.Composite is an abstract class that can represent both the leaf and the composite, so you should maximize the component interface when you actually implement it. The component class should define as many common operations as possible for the leaf and composite classes. The component class typically provides default implementations for these operations, and the leaf and composite subclasses can redefine them;

2.Component should implement a component list, in the above code, I was maintained in the composite list, because in the leaf, there is no composite, So a component list is maintained in composite, which reduces the waste of memory;

3. Memory release; Because of the tree structure, when the parent node is destroyed, all of the child nodes must also be destroyed, so I am in the destructor of the maintenance of the component list of the unified destruction, so that the client can eliminate the frequent destruction of child nodes trouble;

4. Since the component interface provides a maximum interface definition, some operations do not apply to the leaf node, for example: the leaf node does not perform add and remove operations, because the composite mode masks the difference between the parts and the whole. In order to prevent customers from illegal add and remove operations on the leaf, in the actual development process, the Add and remove operations, the need for the corresponding judgement, to determine whether the current node is composite.

Advantages of Combined mode

Group objects into a tree structure to represent a "partial-whole" hierarchy. Combined mode enables users to have consistency in the use of individual objects and grouped objects.

Working with scenes

1. You want to represent the part of the object-the whole hierarchy;
2. If you want the user to ignore the difference between a grouped object and a single object, the user will uniformly use all the objects in the composite structure.

Quote: "When you discover that requirements are part and whole hierarchies, and you want users to be able to ignore the differences between the grouped objects and the individual objects, you should consider the combination mode when using all the objects in the composite structure uniformly." ”

Summarize

Through the simple explanation above, we know that the combination mode intention is through the relationship between the whole and the local, through the form of tree structure to organize complex objects, shielding the details of the object, the external display of a unified way to manipulate objects, is we deal with more complex objects of a means and means. Now combine the above code, think of the beginning of the article proposed the company OA system how to design.

Related Article

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.