Design Pattern C ++ description ---- 11. Composite pattern

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201205/132469.html

I. Example


This example is from the book. Assume that there is a company's grouping structure as follows:

 


 

Its structure is similar to a tree. The human resources department and the finance department have no subnodes, and the company has subnodes.

What's most important is that each layer has a similar structure.


The code is implemented as follows:
[Cpp] view plaincopyprint? // Company class, providing interfaces
Class Company
{
Public:
Company (string name)
{
M_name = name;
}

Virtual ~ Company ()
{}

Virtual void Add (Company * pCom)
{}

Virtual void Display (int depth)
{}
 
Protected:
String m_name;
};
 
// Specific company
Class ConcreteCompany: public Company
{
Public:
ConcreteCompany (string name): Company (name)
{}
 
Virtual ~ ConcreteCompany ()
{}
 
// Add subtree or leaf
Void Add (Company * pCom)
{
M_listCompany.push_back (pCom );
}

// Display
Void Display (int depth)
{
For (int I = 0; I <depth; I ++)
{
Cout <"-";

}

Cout <m_name <endl;

List <Company * >:: iterator iter = m_listCompany.begin ();

For (; iter! = M_listCompany.end (); iter ++) // display the lower Node
{
(* Iter)-> Display (depth + 2 );
}
}
 
Private:
List <Company *> m_listCompany;
};
 
// Specific department and Finance Department
Class FinanceDepartment: public Company
{
Public:
FinanceDepartment (string name): Company (name)
{}

Virtual ~ FinanceDepartment ()
{}

// Just display and add functions without limit, because it is already a leaf node
Virtual void Display (int depth)
{
For (int I = 0; I <depth; I ++)
Cout <"-";

Cout <m_name <endl;
}
};
 
// Specific department and Human Resources Department
Class HRDepartment: public Company
{
Public:
HRDepartment (string name): Company (name)
{}

Virtual ~ HRDepartment ()
{}

// Just display and add functions without limit, because it is already a leaf node
Virtual void Display (int depth)
{
For (int I = 0; I <depth; I ++)
{
Cout <"-";
}
Cout <m_name <endl;
}
};
 
//////////////////////////////////////// //////////////////////////////////
// Test code
Int main ()
{
Company * root = new ConcreteCompany ("Head Office ");
Company * leaf1 = new FinanceDepartment ("Finance Department ");
Company * leaf2 = new HRDepartment ("Human Resources Department ");
Root-> Add (leaf1 );
Root-> Add (leaf2 );

// China East Branch
Company * mid1 = new ConcreteCompany ("China East Branch ");
Company * leaf3 = new FinanceDepartment ("Finance Department of East China Branch ");
Company * leaf4 = new HRDepartment ("Human Resources Department of East China Branch ");
Mid1-> Add (leaf3 );
Mid1-> Add (leaf4 );
Root-> Add (mid1 );

// Nanjing Office
Company * mid2 = new ConcreteCompany ("Nanjing Office ");
FinanceDepartment * leaf5 = new FinanceDepartment ("Finance Department of Nanjing Office ");
HRDepartment * leaf6 = new HRDepartment ("Nanjing Office Human Resources Department ");
Mid2-> Add (leaf5 );
Mid2-> Add (leaf6 );
Root-> Add (mid2 );

// Hangzhou Office
Company * mid3 = new ConcreteCompany ("Hangzhou Office ");
FinanceDepartment * leaf7 = new FinanceDepartment ("Hangzhou Office Finance Department ");
HRDepartment * leaf8 = new HRDepartment ("Hangzhou Office Human Resources Department ");
Mid3-> Add (leaf7 );
Mid3-> Add (leaf8 );
Mid2-> Add (mid3 );
 
Root-> Display (0 );
 
Delete leaf1;
Delete leaf2;
Delete leaf3;
Delete leaf4;
Delete leaf5;
Delete leaf6;
Delete leaf7;
Delete leaf8;
Delete mid1;
Delete mid2;
Delete root;

Return 0;
}
// Company class, providing interfaces
Class Company
{
Public:
Company (string name)
{
M_name = name;
}

Virtual ~ Company ()
{}

Virtual void Add (Company * pCom)
{}

Virtual void Display (int depth)
{}

Protected:
String m_name;
};

// Www.2cto.com
Class ConcreteCompany: public Company
{
Public:
ConcreteCompany (string name): Company (name)
{}

Virtual ~ ConcreteCompany ()
{}

// Add subtree or leaf
Void Add (Company * pCom)
{
M_listCompany.push_back (pCom );
}

// Display
Void Display (int depth)
{
For (int I = 0; I <depth; I ++)
{
Cout <"-";

}

Cout <m_name <endl;

List <Company * >:: iterator iter = m_listCompany.begin ();

For (; iter! = M_listCompany.end (); iter ++) // display the lower Node
{
(* Iter)-> Display (depth + 2 );
}
}

Private:
List <Company *> m_listCompany;
};

// Specific department and Finance Department
Class FinanceDepartment: public Company
{
Public:
FinanceDepartment (string name): Company (name)
{}

Virtual ~ FinanceDepartment ()
{}

// Just display and add functions without limit, because it is already a leaf node
Virtual void Display (int depth)
{
For (int I = 0; I <depth; I ++)
Cout <"-";

Cout <m_name <endl;
}
};

// Specific department and Human Resources Department
Class HRDepartment: public Company
{
Public:
HRDepartment (string name): Company (name)
{}

Virtual ~ HRDepartment ()
{}

// Just display and add functions without limit, because it is already a leaf node
Virtual void Display (int depth)
{
For (int I = 0; I <depth; I ++)
{
Cout <"-";
}
Cout <m_name <endl;
}
};

//////////////////////////////////////// //////////////////////////////////
// Test code
Int main ()
{
Company * root = new ConcreteCompany ("Head Office ");
Company * leaf1 = new FinanceDepartment ("Finance Department ");
Company * leaf2 = new HRDepartment ("Human Resources Department ");
Root-> Add (leaf1 );
Root-> Add (leaf2 );
 
// China East Branch
Company * mid1 = new ConcreteCompany ("China East Branch ");
Company * leaf3 = new FinanceDepartment ("Finance Department of East China Branch ");
Company * leaf4 = new HRDepartment ("Human Resources Department of East China Branch ");
Mid1-> Add (leaf3 );
Mid1-> Add (leaf4 );
Root-> Add (mid1 );

// Nanjing Office
Company * mid2 = new ConcreteCompany ("Nanjing Office ");
FinanceDepartment * leaf5 = new FinanceDepartment ("Finance Department of Nanjing Office ");
HRDepartment * leaf6 = new HRDepartment ("Nanjing Office Human Resources Department ");
Mid2-> Add (leaf5 );
Mid2-> Add (leaf6 );
Root-> Add (mid2 );
 
// Hangzhou Office
Company * mid3 = new ConcreteCompany ("Hangzhou Office ");
FinanceDepartment * leaf7 = new FinanceDepartment ("Hangzhou Office Finance Department ");
HRDepartment * leaf8 = new HRDepartment ("Hangzhou Office Human Resources Department ");
Mid3-> Add (leaf7 );
Mid3-> Add (leaf8 );
Mid2-> Add (mid3 );

Root-> Display (0 );

Delete leaf1;
Delete leaf2;
Delete leaf3;
Delete leaf4;
Delete leaf5;
Delete leaf6;
Delete leaf7;
Delete leaf8;
Delete mid1;
Delete mid2;
Delete root;

Return 0;
}
Ii. Description

1. The above company structure diagram is actually the relationship between the whole and the part, and the whole and the part can be treated in the same way, because there are many similarities.

2. This tree can be either a leaf or a child tree.


In fact, this mode is a combination mode.

Iii. Combination Mode

Definition: Combine objects into a tree structure to represent the "part-whole" hierarchy. Combination makes the use of a single object and a composite object consistent.

Pay attention to the following two points:

1. "Tree" must be a layered structure with branches that can be stretched down and leaves unchanged.

2. "consistency", that is, there must be many similarities.

The structure is as follows:

 

Component: defines a unified interface. To put it bluntly, similarity is extracted.

Composite: defines a branch node, that is, a subtree.

Leaf: defines leaf nodes. leaf nodes do not have subnodes.

 


Author lwbeyond

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.