设计模式系列(十四)组合模式(Composite Pattern)
组合模式就是允许将对象组成树形结构来表现“整体/部分”的层次结构。组合能让客户以一致的方式处理个别对象和对象组合。简单来说,可以把组合模式看成树形结构的一种表示,例如平常经常见到的多级菜单,每一级菜单下面都可能还存在子菜单,以此类推,直到最后没有子菜单的选项,最后的选项就是该菜单的叶节点,从而构成一个复杂的菜单系统,也可以看作是一个树形结构的具体化。
组合结构内的任意对象都统称为组件,组件可以是组合或者叶节点,那么组合模式中的主要角色是:
(1)抽象构件角色(Component):我们可以称之为“组件”,它可以是接口或抽象类,为叶子构件和容器构件对象声明接口,在该角色中可以包含所有子类共有行为的声明和实现。在抽象构件中定义了访问及管理它的子构件的方法,如增加子构件、删除子构件、获取子构件等。
(2)叶子构件角色(Leaf):我们可以称之为“叶节点”,它在组合结构中表示叶子节点对象,叶子节点没有子节点,它实现了在抽象构件中定义的行为。对于那些访问及管理子构件的方法,可以通过异常等方式进行处理。
(3)容器构件角色(Composite):我们可以称之为“组合”,它在组合结构中表示容器节点对象,容器节点包含子节点,其子节点可以是叶子节点,也可以是容器节点,它提供一个集合用于存储子节点,实现了在抽象构件中定义的行为,包括那些访问及管理子构件的方法,在其业务方法中可以递归调用其子节点的业务方法。
组合模式的关键是定义了一个抽象构件类,它既可以代表叶子,又可以代表容器,而客户端针对该抽象构件类进行编程,无须知道它到底表示的是叶子还是容器,可以对其进行统一处理。同时容器对象与抽象构件类之间还建立一个聚合关联关系,在容器对象中既可以包含叶子,也可以包含容器,以此实现递归组合,形成一个树形结构。如果不使用组合模式,客户端代码将过多地依赖于容器对象复杂的内部实现结构,容器对象内部实现结构的变化将引起客户代码的频繁变化,带来了代码维护复杂、可扩展性差等弊端。组合模式的引入将在一定程度上解决这些问题。
上面的角色关系在C++中简单来说就是:
(1)Component是一个基类,它可以是抽象类,也可以是实现了默认方法实现的具体类,但是构造函数用protected属性即可,保证该类不能被外部直接创建对象,这个类中声明或者定义了所有可能用到的函数,由继承它的子类来决定具体有意义的实现,这里也埋下了一个问题,即如果子类不需要其中的一些函数该怎么办,后面我们会说到。
(2)Leaf类继承了Component类,然后实现了其中的一部分函数,这个类是叶节点,所以不会再包含其他的容器组合。
(3)Composite类也继承了Component类,然后实现了其中的大部分函数,其中一般都会包含子菜单的一个组合。
从上面的介绍可以看出,两个子类都继承自一个基类,这个基类就是组件,然后分别衍生出了叶节点和组合,但是对于用户来说,用户不需要区分到底是叶节点还是组合,用户只需要使用透明的基类即可,其他实现由子类决定。这里就涉及到刚才说的问题,即一些不需要的函数怎么办?一般分为两种:
(1)透明组合:所谓透明,就是对于用户来说是完全透明的,用户不需要关心和区分具体的使用,只需要统一使用基类中的方法即可,此时有可能有一些函数子类根本用不到,此时可以采用异常处理的方法,即在基类中把所有函数都实现为抛出异常,子类只实现自己需要的函数,然后使用时对异常进行处理即可。透明组合强调的是透明性、统一性和一致性。
(2)安全组合:所谓安全,就是不存在上述的异常处理情况,基类中只声明了叶节点和容器组合共同的函数,它们不同的函数由子类自己写出,这样就避免了出现使用不到的函数的情况,但是虽然安全了,不过对于用户却不再透明,因为用户需要关心具体的使用方法,需要区分叶节点和容器组合,它们有不同的方法,所以会相对麻烦一些。
至于到底使用哪种组合方式,根据项目的具体需求进行选择。
组合模式的主要优点是:
(1)组合模式可以清楚地定义分层次的复杂对象,表示对象的全部或部分层次,它让客户端忽略了层次的差异,方便对整个层次结构进行控制。
(2)客户端可以一致地使用一个组合结构或其中单个对象,不必关心处理的是单个对象还是整个组合结构,简化了客户端代码。
(3)在组合模式中增加新的容器构件和叶子构件都很方便,无须对现有类库进行任何修改,符合“开闭原则”。
(4)组合模式为树形结构的面向对象实现提供了一种灵活的解决方案,通过叶子对象和容器对象的递归组合,可以形成复杂的树形结构,但对树形结构的控制却非常简单。
组合模式的主要缺点是:
在增加新构件时很难对容器中的构件类型进行限制。有时候我们希望一个容器中只能有某些特定类型的对象,例如在某个文件夹中只能包含文本文件,使用组合模式时,不能依赖类型系统来施加这些约束,因为它们都来自于相同的抽象层,在这种情况下,必须通过在运行时进行类型检查来实现,这个实现过程较为复杂。
在以下情况下可以考虑使用组合模式:
(1)在具有整体和部分的层次结构中,希望通过一种方式忽略整体与部分的差异,客户端可以一致地对待它们。
(2)在一个使用面向对象语言开发的系统中需要处理一个树形结构。
(3)在一个系统中能够分离出叶子对象和容器对象,而且它们的类型不固定,需要增加一些新的类型。
下面我们来看一个例子,该例由三个文件组成,依次是:
CompositePattern.h、
CompositePattern.cpp、
CompositePatternTest.cpp
。
// 组合模式
// CompositePattern.h文件
#ifndef COMPOSITE
#define COMPOSITE
#include
#include
#include
#include
#include
using namespace std;
// 组合模式中的抽象构件角色
class MenuComponent
{
public:
virtual ~MenuComponent(){}
virtual void add(MenuComponent* menuComponent);
virtual void remove(MenuComponent* menuComponent);
virtual MenuComponent* getChild(int i);
virtual string getName();
virtual string getDescription();
virtual double getPrice();
virtual bool isVegetarian();
virtual void print();
protected:
MenuComponent(){}
};
// 组合模式中的叶子构件
class MenuItem : public MenuComponent
{
public:
MenuItem(string name, string description, bool vegetarian, double price)
{
this->name = name;
this->description = description;
this->vegetarian = vegetarian;
this->price = price;
}
string getName();
string getDescription();
double getPrice();
bool isVegetarian();
void print();
private:
string name;
string description;
bool vegetarian;
double price;
};
// 组合模式中的容器构件
class Menu : public MenuComponent
{
public:
Menu(string name, string description)
{
this->name = name;
this->description = description;
}
void add(MenuComponent* menuComponent);
void remove(MenuComponent* menuComponent);
MenuComponent* getChild(int i);
string getName();
string getDescription();
void print();
private:
vector vecMenu;
string name;
string description;
};
// 组合模式中的客户
class Waitress
{
public:
Waitress(MenuComponent* allMenus)
{
this->allMenus = allMenus;
}
void printMenu();
private:
MenuComponent* allMenus;
};
#endif
// CompositePattern.cpp文件
#include "CompositePattern.h"
// 组合模式中的抽象构件角色
void MenuComponent::add(MenuComponent* menuComponent)
{
runtime_error r("UnsupportedOperationException");
throw r;
}
void MenuComponent::remove(MenuComponent* menuComponent)
{
runtime_error r("UnsupportedOperationException");
throw r;
}
MenuComponent* MenuComponent::getChild(int i)
{
runtime_error r("UnsupportedOperationException");
throw r;
}
string MenuComponent::getName()
{
runtime_error r("UnsupportedOperationException");
throw r;
}
string MenuComponent::getDescription()
{
runtime_error r("UnsupportedOperationException");
throw r;
}
double MenuComponent::getPrice()
{
runtime_error r("UnsupportedOperationException");
throw r;
}
bool MenuComponent::isVegetarian()
{
runtime_error r("UnsupportedOperationException");
throw r;
}
void MenuComponent::print()
{
runtime_error r("UnsupportedOperationException");
throw r;
}
// 组合模式中的叶子构件
string MenuItem::getName()
{
return name;
}
string MenuItem::getDescription()
{
return description;
}
double MenuItem::getPrice()
{
return price;
}
bool MenuItem::isVegetarian()
{
return vegetarian;
}
void MenuItem::print()
{
cout << " " << getName();
if (isVegetarian())
{
cout << "(v)";
}
cout << ", " << getPrice() << endl;
cout << " -- " + getDescription() << endl;
}
// 组合模式中的容器构件
void Menu::add(MenuComponent* menuComponent)
{
vecMenu.push_back(menuComponent);
}
void Menu::remove(MenuComponent* menuComponent)
{
for (vector::iterator itr = vecMenu.begin(); itr != vecMenu.end();)
{
if ((*itr)->getName() == menuComponent->getName()
&& (*itr)->getDescription() == menuComponent->getDescription())
{
itr = vecMenu.erase(itr);
break;
}
else
{
++itr;
}
}
}
MenuComponent* Menu::getChild(int i)
{
return vecMenu[i];
}
string Menu::getName()
{
return name;
}
string Menu::getDescription()
{
return description;
}
void Menu::print()
{
cout << "\n" << getName();
cout << ", " + getDescription() << endl;
cout << "---------------------" << endl;
for (vector::iterator itr = vecMenu.begin(); itr != vecMenu.end(); ++itr)
{
(*itr)->print();
}
}
// 组合模式中的客户
void Waitress::printMenu()
{
allMenus->print();
}
// CompositePatternTest.cpp文件
#include "CompositePattern.h"
void main()
{
// 树根
MenuComponent* allMenus = new Menu("ALL MENUS", "All menus combined");
// 树根下的一级菜单三个
MenuComponent* pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");
MenuComponent* dinerMenu = new Menu("DINER MENU", "Lunch");
MenuComponent* cafeMenu = new Menu("CAFE MENU", "Dinner");
// 二级菜单两个
MenuComponent* dessertMenu = new Menu("DESSERT MENU", "Dessert of course!");
MenuComponent* coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee");
// 添加一级菜单及其叶节点
allMenus->add(pancakeHouseMenu);
allMenus->add(dinerMenu);
allMenus->add(cafeMenu);
MenuItem mu1(
"K&B's Pancake Breakfast",
"Pancakes with scrambled eggs, and toast",
true,
2.99);
pancakeHouseMenu->add(&mu1);
MenuItem mu2(
"Regular Pancake Breakfast",
"Pancakes with fried eggs, sausage",
false,
2.99);
pancakeHouseMenu->add(&mu2);
MenuItem mu3(
"Blueberry Pancakes",
"Pancakes made with fresh blueberries, and blueberry syrup",
true,
3.49);
pancakeHouseMenu->add(&mu3);
MenuItem mu4(
"Waffles",
"Waffles, with your choice of blueberries or strawberries",
true,
3.59);
pancakeHouseMenu->add(&mu4);
MenuItem mu5(
"Vegetarian BLT",
"(Fakin') Bacon with lettuce & tomato on whole wheat",
true,
2.99);
dinerMenu->add(&mu5);
MenuItem mu6(
"BLT",
"Bacon with lettuce & tomato on whole wheat",
false,
2.99);
dinerMenu->add(&mu6);
MenuItem mu7(
"Soup of the day",
"A bowl of the soup of the day, with a side of potato salad",
false,
3.29);
dinerMenu->add(&mu7);
MenuItem mu8(
"Hotdog",
"A hot dog, with saurkraut, relish, onions, topped with cheese",
false,
3.05);
dinerMenu->add(&mu8);
MenuItem mu9(
"Steamed Veggies and Brown Rice",
"Steamed vegetables over brown rice",
true,
3.99);
dinerMenu->add(&mu9);
MenuItem mu10(
"Pasta",
"Spaghetti with Marinara Sauce, and a slice of sourdough bread",
true,
3.89);
dinerMenu->add(&mu10);
dinerMenu->add(dessertMenu);
MenuItem mu11(
"Apple Pie",
"Apple pie with a flakey crust, topped with vanilla icecream",
true,
1.59);
dessertMenu->add(&mu11);
MenuItem mu12(
"Cheesecake",
"Creamy New York cheesecake, with a chocolate graham crust",
true,
1.99);
dessertMenu->add(&mu12);
MenuItem mu13(
"Sorbet",
"A scoop of raspberry and a scoop of lime",
true,
1.89);
dessertMenu->add(&mu13);
MenuItem mu14(
"Veggie Burger and Air Fries",
"Veggie burger on a whole wheat bun, lettuce, tomato, and fries",
true,
3.99);
cafeMenu->add(&mu14);
MenuItem mu15(
"Soup of the day",
"A cup of the soup of the day, with a side salad",
false,
3.69);
cafeMenu->add(&mu15);
MenuItem mu16(
"Burrito",
"A large burrito, with whole pinto beans, salsa, guacamole",
true,
4.29);
cafeMenu->add(&mu16);
cafeMenu->add(coffeeMenu);
MenuItem mu17(
"Coffee Cake",
"Crumbly cake topped with cinnamon and walnuts",
true,
1.59);
coffeeMenu->add(&mu17);
MenuItem mu18(
"Bagel",
"Flavors include sesame, poppyseed, cinnamon raisin, pumpkin",
false,
0.69);
coffeeMenu->add(&mu18);
MenuItem mu19(
"Biscotti",
"Three almond or hazelnut biscotti cookies",
true,
0.89);
coffeeMenu->add(&mu19);
// 创建客户对象,并打印菜单
Waitress waitress(allMenus);
waitress.printMenu();
delete(pancakeHouseMenu);
delete(dinerMenu);
delete(cafeMenu);
delete(dessertMenu);
delete(coffeeMenu);
delete(allMenus);
pancakeHouseMenu = NULL;
dinerMenu = NULL;
cafeMenu = NULL;
dessertMenu = NULL;
coffeeMenu = NULL;
allMenus = NULL;
}
该例的运行结果如图1和图2所示,由于运行结果较长,所以只能采用分开截图表示,UML类图如图3所示。
图1 运行结果1
图2 运行结果2
图3 UML类图
从该例中可以看出,该例使用了透明组合方式来实现了组合模式,因为组合和叶节点具有相同的函数接口,基类中进行了异常抛出默认实现,如果想采用安全组合模式,可以将基类中对于子类不同的方法放到具体的子类中去实现即可。
该例的角色对应关系是:
(1)MenuComponent对应于Component角色,即抽象构件角色,又称之为组件。
(2)MenuItem对应于Leaf角色,即叶节点角色,又称之为叶节点。
(3)Menu对应于Composite角色,即容器角色,又称之为组合。
(4)Waitress则是组合模式中的一个客户,来使用这个多级菜单,该例就是一个餐厅的多级食品菜单。
参考资料:
http://blog.csdn.net/lovelion/article/details/7956908
http://blog.csdn.net/lovelion/article/details/7956937
http://blog.csdn.net/lovelion/article/details/7956962