基于Java的樣例:
比如現(xiàn)實(shí)中公司內(nèi)各部門的層級關(guān)系
抽象接口類:
package com.zyh.designpattern.composite;
public abstract class Company { private String name;
public Company(String name) {
super();
this.name = name;
}
public Company(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected abstract void add(Company company); protected abstract void romove(Company company);
protected abstract void display(int depth);
}
枝結(jié)點(diǎn)類:
package com.zyh.designpattern.composite;
import java.util.ArrayList;
import java.util.List;
public class ConcreteCompany extends Company {
private List cList;
public ConcreteCompany() {
cList = new ArrayList();
}
public ConcreteCompany(String name) {
super(name);
cList = new ArrayList();
}
@Override
protected void add(Company company) {
// TODO Auto-generated method stub
cList.add(company);
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
for (Company c : cList) {
c.display(depth + 2);
}
}
@Override
protected void romove(Company company) {
// TODO Auto-generated method stub
cList.remove(company);
}
}
兩個(gè)葉結(jié)點(diǎn)類:
------------------------- 1---------------------------.
package com.zyh.designpattern.composite;
public class HRDepartment extends Company {
public HRDepartment(String name) {
super(name);
}
@Override
protected void add(Company company) {
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
@Override
protected void romove(Company company) {
}
}
----------------2-------------------
package com.zyh.designpattern.composite;
public class FinanceDepartment extends Company {
public FinanceDepartment(String name) {
super(name);
}
@Override
protected void add(Company company) {
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
}
@Override
protected void romove(Company company) {
}
}
客戶端:
package com.zyh.designpattern.composite;
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Company root = new ConcreteCompany();
root.setName("北京總公司");
root.add(new HRDepartment("總公司人力資源部"));
root.add(new FinanceDepartment("總公司財(cái)務(wù)部"));
Company shandongCom = new ConcreteCompany("山東分公司");
shandongCom.add(new HRDepartment("山東分公司人力資源部"));
shandongCom.add(new FinanceDepartment("山東分公司賬務(wù)部"));
Company zaozhuangCom = new ConcreteCompany("棗莊辦事處");
zaozhuangCom.add(new FinanceDepartment("棗莊辦事處財(cái)務(wù)部"));
zaozhuangCom.add(new HRDepartment("棗莊辦事處人力資源部"));
Company jinanCom = new ConcreteCompany("濟(jì)南辦事處");
jinanCom.add(new FinanceDepartment("濟(jì)南辦事處財(cái)務(wù)部"));
jinanCom.add(new HRDepartment("濟(jì)南辦事處人力資源部"));
shandongCom.add(jinanCom);
shandongCom.add(zaozhuangCom);
Company huadongCom = new ConcreteCompany("上海華東分公司"); huadongCom.add(new HRDepartment("上海華東分公司人力資源部"));
huadongCom.add(new FinanceDepartment("上海華東分公司賬務(wù)部"));
Company hangzhouCom = new ConcreteCompany("杭州辦事處");
hangzhouCom.add(new FinanceDepartment("杭州辦事處財(cái)務(wù)部"));
hangzhouCom.add(new HRDepartment("杭州辦事處人力資源部"));
Company nanjingCom = new ConcreteCompany("南京辦事處");
nanjingCom.add(new FinanceDepartment("南京辦事處財(cái)務(wù)部"));
nanjingCom.add(new HRDepartment("南京辦事處人力資源部"));
huadongCom.add(hangzhouCom);
huadongCom.add(nanjingCom);
root.add(shandongCom);
root.add(huadongCom);
root.display(0);
}
}
基于C++的樣例:
//Menu.h
#include
class Menu
{
public:
virtual ~Menu();
virtual void Add(Menu*);
virtual void Remove(Menu*);
virtual Menu* GetChild(int);
virtual void Display() = 0;
protected:
Menu();
Menu(std::string);
std::string m_strName;
};
#include "Menu.h"
Menu::Menu()
{
}
Menu::Menu(std::string strName) : m_strName(strName)
{
}
Menu::~Menu()
{
}
void Menu::Add(Menu* pMenu)
{}
void Menu::Remove(Menu* pMenu)
{}
Menu* Menu::GetChild(int index)
{
return NULL;
}
//SubMenu.h
#include "Menu.h"
class SubMenu : public Menu
{
public:
SubMenu();
SubMenu(std::string);
virtual ~SubMenu();
void Display();
};
//SubMenu.cpp
#include "stdafx.h"
#include "SubMenu.h"
using namespace std;
SubMenu::SubMenu()
{
}
SubMenu::SubMenu(string strName) : Menu(strName)
{
}
SubMenu::~SubMenu()
{
}
void SubMenu::Display()
{
cout << m_strName << endl;
}
//CompositMenu.h
#include "Menu.h"
#include
class CompositMenu : public Menu
{
public:
CompositMenu();
CompositMenu(std::string);
virtual ~CompositMenu();
void Add(Menu*);
void Remove(Menu*);
Menu* GetChild(int);
void Display();
private:
std::vector
};
//CompositMenu.cpp
#include "stdafx.h"
#include "CompositMenu.h"
#include
CompositMenu::CompositMenu()
{
}
CompositMenu::CompositMenu(string strName) : Menu(strName)
{
}
CompositMenu::~CompositMenu()
{
}
void CompositMenu::Add(Menu* pMenu)
{
}
void CompositMenu::Remove(Menu* pMenu)
{
m_vMenu.erase(&pMenu);
}
Menu* CompositMenu::GetChild(int index)
{
return m_vMenu[index];
}
void CompositMenu::Display()
{
cout << "+" << m_strName << endl;
for (; it != m_vMenu.end(); ++it)
{
cout << "|-";
(*it)->Display();
}
}
#include "stdafx.h"
#include "Menu.h"
#include "SubMenu.h"
#include "CompositMenu.h"
int main(int argc, char* argv[])
{
Menu* pMenu = new CompositMenu("國內(nèi)新聞");
pMenu->Add(new SubMenu("時(shí)事新聞"));
pMenu->Add(new SubMenu("社會新聞"));
pMenu->Display();
pMenu = new CompositMenu("國際新聞");
pMenu->Add(new SubMenu("國際要聞"));
pMenu->Add(new SubMenu("環(huán)球視野"));
pMenu->Display();
return 0;
}