本文原作者cnblogs的瓜籽博客.在些表示感谢~
希望大家转载时加上该版权信息以示对原作者尊重~
今天刚看了“组合模式”,我也现学现卖了。“组合模式”顾名思意就是将多种实现组合在一起而达到牵一处而动全身的效果。怎么说呢?!就像是动物(组合对象)一样,当它生下后代(叶对象)时,它的后代就有了某种功能(比如:挖洞,听力好等等);也像是一棵树,它有一个根(组合对象)然后是从这个棵树向外冒出的其他枝杆(组合对象)以及从这些枝杆又向外长的叶子(叶对象)。换句话说,就是当祖先已经有了,那么只要从这个祖先衍生出来的其他孩子(包括这个祖先下的其他组合对象)已经就具备了某种功能,看上去貌似又有些像是继承。“组合模式”在组合对象的层次体系中有两种类型的对象:叶对象和组合对象。组合模式擅长于对大批对象进行操作。
“组合模式”就是在做一个项目的时候,我们要把在这个项目中即将出现的方法都在组合对象中定义(包括叶对象中的方法),而它们的叶对象将会继承组合对象。当组合对象被实例化后其叶对象的方法也相应地被实例化了。可能我说的有些乱,下面就用一个例子来说明吧。
“组合模式”是一种专为创建Web上的动态用户界面而量身定制的模式。使用这种模式,可以用一条命令在多个对象上激发复杂的或递归的行为。
使用“组合模式”可以为我们带来两大好处:
1、可以用同样的方法处理对象的集合与其中的特定子对象。
2、可以用来把一批子对象组织成树形结构,并且使整棵树都可以被遍历。
只有同时具备以下两个条件进才适合使用组合模式:
1、存在一批组织成某处层次体系的对象(具体结构可能在开发期间无法知道)。
2、希望对这批对象或其中的一部分对象实话一个操作。
下面看一下示例:
具体要求是做一个图片库且可以有选择地隐藏或显示图片库的特定部分。这可能是单独图片,也可能是图片库。现在就需要两个类来完成这个功能:用作图片库的组合对象类和用于图片本身的叶对象类,代码如下:
01 |
var Composite = new Interface('Composite', ['add', 'remove', 'getChild']); // 检查组合对象Composite应该具备的方法 |
02 |
var GalleryItem = new Interface('GalleryItem', ['hide', 'show']); // 检查组合对象GalleryItem应该具备的方法 |
03 |
|
04 |
// DynamicGallery Class |
05 |
|
06 |
var DynamicGallery = function(id){ // 实现Composite,GalleryItem组合对象类 |
07 |
this.children = []; |
08 |
|
09 |
this.element = document.createElement('div'); |
10 |
this.element.id = id; |
11 |
this.element.className = 'dynamic-gallery'; |
12 |
} |
13 |
|
14 |
DynamicGallery.prototype = { |
15 |
|
16 |
// 实现Composite组合对象接口 |
17 |
|
18 |
add : function(child){ |
19 |
Interface.ensureImplements(child, Composite, DynamicGallery); |
20 |
this.children.push(child); |
21 |
this.element.appendChild(child.getElement()); |
22 |
}, |
23 |
|
24 |
remove : function(child){ |
25 |
for(var node, i = 0; node = this.getChild(i); i++){ |
26 |
if(node == child){ |
27 |
this.children.splice(i, 1); |
28 |
break; |
29 |
} |
30 |
} |
31 |
this.element.removeChild(child.getElement()); |
32 |
}, |
33 |
|
34 |
getChild : function(i){ |
35 |
return this.children[i]; |
36 |
}, |
37 |
|
38 |
// 实现DynamicGallery组合对象接口 |
39 |
|
40 |
hide : function(){ |
41 |
for(var node, i = 0; node = this.getChild(i); i++){ |
42 |
node.hide(); |
43 |
} |
44 |
this.element.style.display = 'none'; |
45 |
}, |
46 |
|
47 |
show : functioln(){ |
48 |
this.element.style.display = 'block'; |
49 |
for(var node, i = 0; node = getChild(i); i++){ |
50 |
node.show(); |
51 |
} |
52 |
}, |
53 |
|
54 |
// 帮助方法 |
55 |
|
56 |
getElement : function(){ |
57 |
return this.element; |
58 |
} |
59 |
} |
在上面的代码中,首先定义的是组合对象类和叶对象类应该实现的接口。除了常规的组合远景一分钱上,这些类要怕硬欺软 操作只包括hide和show。接下来我们定义叶对象。叶对象实现hide和show,代码如下:
01 |
// GalleryImage class |
02 |
|
03 |
var GalleryImage = function(src){ // 实现Composite和GalleryItem组合对象中所定义的方法 |
04 |
this.element = document.createElement('img'); |
05 |
this.element.className = 'gallery-image'; |
06 |
this.element.src = src; |
07 |
} |
08 |
|
09 |
GalleryImage.prototype = { |
10 |
|
11 |
// 实现Composite接口 |
12 |
// 这些是叶结点,所以我们不用实现这些方法,我们只需要定义即可 |
13 |
|
14 |
add : function(){}, |
15 |
remove : function(){}, |
16 |
getChild : function(){}, |
17 |
|
18 |
// 实现GalleryItem接口 |
19 |
|
20 |
hide : function(){ |
21 |
this.element.style.display = 'none'; |
22 |
}, |
23 |
|
24 |
show : function(){ |
25 |
this.element.style.display = ''; |
26 |
}, |
27 |
|
28 |
// 帮助方法 |
29 |
|
30 |
getElement : function(){ |
31 |
return this.element; |
32 |
} |
33 |
} |
这是一个演示组合模式的工作方式的例子。每个类都很简单,但由于有了这样一种层次体系,我们就可以执行一些复杂操作。GalleryImage类的构造函数会创建一个image元素。这个类定义中的其余部分由空的组合对象方法(因为这是叶结点)和GalleryItem要求的操作组成。现在我们可以使用这两个类来管理图片:
01 |
var topGallery = new DynamicGallery('top-gallery'); |
02 |
|
03 |
topGallery.add(new GalleryImage('/img/image-1.jpg')); |
04 |
topGallery.add(new GalleryImage('/img/image-2.jpg')); |
05 |
topGallery.add(new GalleryImage('/img/image-3.jpg')); |
06 |
|
07 |
var vacationPhotos = new DyamicGallery('vacation-photos'); |
08 |
|
09 |
for(var i = 0, i < 30; i++){ |
10 |
vacationPhotos.add(new GalleryImage('/img/vac/image-' + i + '.jpg')); |
11 |
} |
12 |
|
13 |
topGallery.add(vacationPhotos); |
14 |
topGallery.show(); |
15 |
vacationPhotos.hide(); |
组合模式之利,使用组合模式,简单的操作也能产生复杂的结果。不必编写大师手工遍历数组或其他数据结构的粘合代码,只需对最顶层的对象执行操作,主每一个子对象自己传递这个操作即可。这对于那些再三执行的操作尤其有用。在组合模式中,各个对象之间的耦合非常松散。每当对顶层组合对象执行一个操作时,实际上是在对整个结构进行尝试优先的搜索以查找节点。
组合模式之弊,由于对组合模式调用的任何操作都会被颇佳北至 它的所有子对象,如果这个层次体系很大的话,系统的性能将会受到影响。

