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;
}