Design Mode learning-Combination Mode

Source: Internet
Author: User

Combination Mode

GOOD: The whole and part can be treated as the same (for example, copying a text, a text, or an article in WORD is the same operation)

 

[Html]
# Include <iostream>
# Include <string>
# Include <vector>
Using namespace std;
 
Class Component
{
Public:
String m_strName;
Component (string strName)
{
M_strName = strName;
}
Virtual void Add (Component * com) = 0;
Virtual void Display (int nDepth) = 0;
};
 
Class Leaf: public Component
{
Public:
Leaf (string strName): Component (strName ){}
 
Virtual void Add (Component * com)
{
Cout <"leaf can't add" <endl;
}
 
Virtual void Display (int nDepth)
{
String strtemp;
For (int I = 0; I <nDepth; I ++)
{
Strtemp + = "-";
}
Strtemp + = m_strName;
Cout <strtemp <endl;
}
};
 
Class Composite: public Component
{
Private:
Vector <Component *> m_component;
Public:
Composite (string strName): Component (strName ){}
 
Virtual void Add (Component * com)
{
M_component.push_back (com );
}
 
Virtual void Display (int nDepth)
{
String strtemp;
For (int I = 0; I <nDepth; I ++)
{
Strtemp + = "-";
}
Strtemp + = m_strName;
Cout <strtemp <endl;
 
Vector <Component *>: iterator p = m_component.begin ();
While (p! = M_component.end ())
{
(* P)-> Display (nDepth + 2 );
P ++;
}
}
 
};
 
// Client
Int main ()
{
Composite * p = new Composite ("John ");
P-> Add (new Leaf ("Xiao Li "));
P-> Add (new Leaf ("Xiao Zhao "));
 
Composite * p1 = new Composite ("Xiao Wu ");
P1-> Add (new Leaf ("Junior "));
 
P-> Add (p1 );
P-> Display (1 );
Return 0;
}


[Html]
# Include <iostream>
# Include <string>
# Include <vector>
Using namespace std;
 
Class Company
{
Protected:
String m_strName;
Public:
Company (string strName)
{
M_strName = strName;
}
 
Virtual void Add (Company * c) = 0;
Virtual void Display (int nDepth) = 0;
Virtual void LineOfDuty () = 0;
};
 
Class ConcreteCompany: public Company
{
Private:
Vector <Company *> m_company;
Public:
ConcreteCompany (string strName): Company (strName ){}
 
Virtual void Add (Company * c)
{
M_company.push_back (c );
}
Virtual void Display (int nDepth)
{
String strtemp;
For (int I = 0; I <nDepth; I ++)
{
Strtemp + = "-";
}
Strtemp + = m_strName;
Cout <strtemp <endl;
 
Vector <Company * >:: iterator p = m_company.begin ();
While (p! = M_company.end ())
{
(* P)-> Display (nDepth + 2 );
P ++;
}
}
Virtual void LineOfDuty ()
{
Vector <Company * >:: iterator p = m_company.begin ();
While (p! = M_company.end ())
{
(* P)-> LineOfDuty ();
P ++;
}
}
};
 
Class HrDepartment: public Company
{
Public:
 
HrDepartment (string strname): Company (strname ){}
 
Virtual void Display (int nDepth)
{
String strtemp;
For (int I = 0; I <nDepth; I ++)
{
Strtemp + = "-";
}
 
Strtemp + = m_strName;
Cout <strtemp <endl;
}
Virtual void Add (Company * c)
{
Cout <"error" <endl;
}
 
Virtual void LineOfDuty ()
{
Cout <m_strName <": Talent Recruitment" <endl;
}
};
 
// Client:
Int main ()
{Www.2cto.com
ConcreteCompany * p = new ConcreteCompany ("Tsinghua University ");
P-> Add (new HrDepartment ("talent department of Tsinghua University "));
 
ConcreteCompany * p1 = new ConcreteCompany ("Mathematics Department ");
P1-> Add (new HrDepartment ("Department of mathematics talents "));
 
ConcreteCompany * p2 = new ConcreteCompany ("Physics Department ");
P2-> Add (new HrDepartment ("Department of Physics talent "));
 
P-> Add (p1 );
P-> Add (p2 );
 
P-> Display (1 );
P-> LineOfDuty ();
Return 0;
}
 

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.