组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象的使用具有一致性。
它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。它可以帮助开发者对多个具有相似功能对象进行分类, 提高规范化设计
有许多关于分级数据结构的例子,使得组合模式非常有用武之地。关于分级数据结构的一个普遍性的例子是你每次使用电脑时所遇到的:文件系统。文件系统由目录和文件组成。每个目录都可以装内容。目录的内容可以是文件,也可以是目录。按照这种方式,计算机的文件系统就是以递归结构来组织的。如果你想要描述这样的数据结构,那么你可以使用组合模式composite。
涉及角色
特点在组合模式的层次体系中有两种类型的对象: 叶对象和组合对象, 这是一种递归定义, 但者也是其有用的原因所在, 一个组合对象可以由其他组合对象和叶子对象组成, 但叶子对象不再包含子对象, 组合对象用于叶子节点的分类
设计这里借用 javascript设计模式 的图来说明组合模式的设计
interface 是组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理component子部件。
field 在组合中表示叶子结点对象,叶子结点没有子结点, 可以设计成抽象类, 通过继承设计出不同类别的叶子对象.
composite 定义有子节点行为,用来存储子部件,在component接口中实现与子部件有关操作,如增加(add)和删除(remove)等。
接口
/* interfaces. */
var composite = new interface('composite', ['add', 'remove', 'getchild']);
var formitem = new interface('formitem', ['save']);
组合对象类
/* compositeform class. */
var compositeform = function(id, method, action) { // implements composite, formitem
this.formcomponents = [];
this.element = document.createelement('form');
this.element.id = id;
this.element.method = method || 'post';
this.element.action = action || '#';
};
compositeform.prototype.add = function(child) {
interface.ensureimplements(child, composite, formitem);
this.formcomponents.push(child);
this.element.appendchild(child.getelement());
};
compositeform.prototype.remove = function(child) {
for(var i = 0, len = this.formcomponents.length; i < len; i++) {
if(this.formcomponents[i] === child) {
this.formcomponents.splice(i, 1); // remove one element from the array at
// position i.
break;
}
}
};
compositeform.prototype.getchild = function(i) {
return this.formcomponents[i];
};
compositeform.prototype.save = function() {
for(var i = 0, len = this.formcomponents.length; i < len; i++) {
this.formcomponents[i].save();
}
};
compositeform.prototype.getelement = function() {
return this.element;
};
叶子对象类
叶子对象可以是简单的一个类, 也可以设计成抽象类构建不同类别的叶子, 在此采用抽象类设计不同类别的叶子
/* field class, abstract. */
var field = function(id) { // implements composite, formitem
this.id = id;
this.element;
};
field.prototype.add = function() {};
field.prototype.remove = function() {};
field.prototype.getchild = function() {};
field.prototype.save = function() {
setcookie(this.id, this.getvalue);
};
field.prototype.getelement = function() {
return this.element;
};
field.prototype.getvalue = function() {
throw new error('unsupported operation on the class field.');
};
inputfield 类
/* inputfield class. */
var inputfield = function(id, label) { // implements composite, formitem
field.call(this, id);
this.input = document.createelement('input');
this.input.id = id;
this.label = document.createelement('label');
var labeltextnode = document.createtextnode(label);
this.label.appendchild(labeltextnode);
this.element = document.createelement('p');
this.element.classname = 'input-field';
this.element.appendchild(this.label);
this.element.appendchild(this.input);
};
extend(inputfield, field); // inherit from field.
inputfield.prototype.getvalue = function() {
return this.input.value;
};
textareafield 类
/* textareafield class. */
var textareafield = function(id, label) { // implements composite, formitem
field.call(this, id);
this.textarea = document.createelement('textarea');
this.textarea.id = id;
this.label = document.createelement('label');
var labeltextnode = document.createtextnode(label);
this.label.appendchild(labeltextnode);
this.element = document.createelement('p');
this.element.classname = 'input-field';
this.element.appendchild(this.label);
this.element.appendchild(this.textarea);
};
extend(textareafield, field); // inherit from field.
textareafield.prototype.getvalue = function() {
return this.textarea.value;
};
selectfield 类
/* selectfield class. */
var selectfield = function(id, label) { // implements composite, formitem
field.call(this, id);
this.select = document.createelement('select');
this.select.id = id;
this.label = document.createelement('label');
var labeltextnode = document.createtextnode(label);
this.label.appendchild(labeltextnode);
this.element = document.createelement('p');
this.element.classname = 'input-field';
this.element.appendchild(this.label);
this.element.appendchild(this.select);
};
extend(selectfield, field); // inherit from field.
selectfield.prototype.getvalue = function() {
return this.select.options[this.select.selectedindex].value;
};
使用/* usage. */
var contactform = new compositeform('contact-form', 'post', 'contact.php');
contactform.add(new inputfield('first-name', 'first name'));
contactform.add(new inputfield('last-name', 'last name'));
contactform.add(new inputfield('address', 'address'));
contactform.add(new inputfield('city', 'city'));
contactform.add(new selectfield('state', 'state', statearray));
// var statearray =[{'al', 'alabama'}, ...]
contactform.add(new inputfield('zip', 'zip'));
contactform.add(new textareafield('comments', 'comments'));
addevent(window, 'unload', contactform.save);
组合模式适合对大批对象进行操作, 且操作对象具有层次关系, 通过对对象的分类操作籍此弱化对象之间的耦合, 这种模式使得代码模块化程度更高, 层次更鲜明, 维护性较好相关推荐:
js桥接设计模式详解
node.js中通用基础设计模式实例分析
js代理设计模式详解
以上就是js组合设计模式详解的详细内容。