Design pattern C + + implementation XV: Combined mode

Source: Internet
Author: User

Combined Mode (Composite): Combines objects into a tree structure to represent a partial-overall hierarchy. The combined mode makes the user consistent with the use of individual objects and composite objects.

The tree may have countless branches, and the tree structure can be realized only by repeatedly using composite. Declare all the methods used to manage child objects in component, including the Add,remove method, so that all subclasses that implement the component interface have add,remove methods. The advantage of this is that the leaf node and the minor points are not different from the outside world, they have a completely consistent behavior interface. Known as transparent mode.

Usage scenarios: When we find that a requirement is a hierarchy of parts and a whole, and we want users to be able to ignore the different combinations of objects from a single object, you can consider using the combined pattern when you use all the objects in the composite structure uniformly.


The following code uses a combination pattern to define a class hierarchy that contains the basic objects of the human resources and finance departments and the combined objects such as branch offices, office, and so on. Basic objects can be combined into more complex objects, and this combination of objects can be combined so that they are recursively recursive, and in the client code, any object used in the base object can be used together. Simply put, the combination pattern allows the customer to consistently use the composite structure and individual objects.

#ifndef composite_h#define composite_h#include<iostream> #include <string> #include <list>using Namespace Std;class Company{//friend class concretecompany;protected:string Name;public:company () {}Company (string St ): Name (St) {}virtual void Add (company * c) {};virtual void Remove (company * c) {};virtual void Display (int depth) {cout < < name; };virtual void Lineofduty () {};}; Class Concretecompany:p ublic company{list < company*> companys;public:concretecompany (string st); void Add ( Company * c), void Remove (company * c), void Display (int depth); void Lineofduty ();}; Concretecompany::concretecompany (String st) {name = st; Companys.push_back (new company (ST));} void Concretecompany::add (company * c) {companys.push_back (c);} void Concretecompany::remove (company * c) {companys.remove (c);} void Concretecompany::D isplay (int depth) {int n = depth;while (n) {cout << "-";--n;} For each (company* var in companys) {var->display (depth + 2); cout << Endl;}} void Concretecompany::lIneofduty () {for each (company * var in companys) {var->lineofduty (); cout << Endl;}} Class Hrdepartment:public Company{public:hrdepartment (String st), void Add (company * c), void Remove (company * c); void Dis Play (int depth); void Lineofduty ();}; void Hrdepartment::add (company * c) {}hrdepartment::hrdepartment (string st) {name = st;} void Hrdepartment::remove (company * c) {}void hrdepartment::D isplay (int depth) {int n = depth;while (n) {cout << "-";- -N;} cout << name;} void Hrdepartment::lineofduty () {cout << name << "Employee recruitment training management. \ n ";} Class Financedepartment:p ublic company{public:financedepartment (string st) {name = st;} void Add (company * c) {};void Remove (company * c) {};void Display (int depth); void Lineofduty ();}; void Financedepartment::D isplay (int depth) {int n = depth;while (n) {cout << "-";--n;} cout << name;} void Financedepartment::lineofduty () {cout << name << "Company financial revenue and expenditure management. \ n ";} #endif

#include "Composite.h" int main () {company* root=new concretecompany ("Beijing head Office"); Root->add (New Hrdepartment ("head Office HR Department ")); Root->add (New Financedepartment (" head Office Finance Department ")); Concretecompany Comp ("Shanghai Huadong Branch"); comp. ADD (New Hrdepartment ("East China Branch Human Resources department"); comp. ADD (New Financedepartment ("East China Branch Finance Department")); Root->add (&comp); Concretecompany comp1 ("Nanjing Office"); Comp1. ADD (New Hrdepartment ("Nanjing Office Human Resources Department"); Comp1. ADD (New Financedepartment ("Nanjing Office Finance Department"); comp. ADD (&COMP1); Concretecompany comp2 ("Hangzhou Office"); Comp2. ADD (New Hrdepartment ("Hangzhou Office Human Resources Department"); Comp2. ADD (New Financedepartment ("Hangzhou Office Finance Department"); comp. ADD (&COMP2); Root->display (1); root->lineofduty (); comp. Remove (&COMP1); Root->display (1); Root->lineofduty (); return 0;}


Design pattern C + + implementation XV: Combined mode

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.