组合模式笔记

组合模式又称部分-整体模式,将对象组合成树形结构以表示'部分整体'的层次结构

组合模式使得用户对单个对象和组合对象的使用具有一致性

demo实例 :表单模块

要调用到前面学习到的寄生组合继承方法

             //原型式继承
function inheritobject(o){
//声明一个过渡函数对象
function F(){
}
//过渡原型对象继承父对象
F.prototype=o;
//返回过渡对象的一个实列,该实例的原型继承了父对象
return new F();
}
/*
*寄生式继承 继承原型
* 传递参数subclass 子类
* 传递参数superclass 父类
* */
function inheritPrototype(subclass,superclass){
//复制一份父类的原型副本保存在变量中
var p=inheritobject(superclass.prototype);
//修正因为重写子类原型导致子类的constructor属性被修改
p.constructor=subclass;
//设置子类原型
subclass.prototype=p;
}

表单 demo

1.表单虚拟父类 Base

            //表单虚拟父类 Base
var Base = function(){
this.children = [];
this.element = null;
};
Base.prototype = {
init : function(){
throw new Error("请重写你的方法");
},
add : function(){
throw new Error("请重写你的方法");
},
getElement : function(){
throw new Error("请重写你的方法");
}
};

2.FormItem容器类

           //FormItem
var FormItem = function(id,parent){
Base.call(this);
this.id = id;
this.parent = parent;
this.init();
};
inheritPrototype(FormItem,Base);
FormItem.prototype = {
init : function(){
this.element =document.createElement('form');
this.element.id=this.id;
this.element.className='new-form';
},
add : function(child){
this.children.push(child);
this.element.appendChild(child.getElement());
return this;
},
getElement : function(){
return this.element;
},
show : function(){
this.parent.appendChild(this.element);
}
};

3.FieldsetItem类

             //FieldsetItem
var FieldsetItem = function(classname,legend){
Base.call(this);
this.classname=classname || '';
this.legend=legend;
this.init();
};
inheritPrototype(FieldsetItem,Base);
FieldsetItem.prototype = {
init : function(){
this.element = document.createElement('fieldset');
var legendname = document.createElement('legend');
legendname.innerHTML = this.legend;
this.element.appendChild(legendname);
this.element.className='new-fieldset';
},
add : function(child){
this.children.push(child);
this.element.appendChild(child.getElement());
return this;
},
getElement : function(){
return this.element;
},
};

4.Group类

            var Group = function(){
Base.call(this);
this.init();
};
inheritPrototype(Group,Base);
Group.prototype = {
init : function(){
this.element = document.createElement('div');
this.element.className='group';
},
add : function(child){
this.children.push(child);
this.element.appendChild(child.getElement());
return this;
},
getElement : function(){
return this.element;
}
};

5.InputItem

             //InputItem
var InputItem = function(name){
Base.call(this);
this.classname = name;
this.id = name;
this.init();
};
inheritPrototype(InputItem,Base);
InputItem.prototype = {
init : function(){
this.element = document.createElement('input');
this.element.className=this.classname;
this.element.id=this.id;
},
add : function(){ },
getElement : function(){
return this.element;
}
};

6.LabelItem类

             //LabelItem
var LabelItem = function(name,nameText){
Base.call(this);
this.text = nameText;
this.name = name;
this.init();
};
inheritPrototype(LabelItem,Base);
LabelItem.prototype = {
init : function(){
this.element = document.createElement('label');
this.element.name=this.name;
this.element.innerHTML=this.text;
},
add : function(){ },
getElement : function(){
return this.element;
}
};

7.SpanItem类

             //SpanItem
var SpanItem = function(warntext){
Base.call(this);
this.text = warntext;
this.init();
};
inheritPrototype(SpanItem,Base);
SpanItem.prototype = {
init : function(){
this.element = document.createElement('span');
this.element.innerHTML=this.text;
},
add : function(){ },
getElement : function(){
return this.element;
}
};

测试代码

             var form =new FormItem('FormItem',document.body);
form.add(
new FieldsetItem('account','账号').add(
new Group().add(
new LabelItem('uname','用户名:')
).add(
new InputItem('uname')
).add(
new SpanItem('4到6位数字或字母')
)
).add(
new Group().add(
new LabelItem('pwd','密&nbsp码:')
).add(
new InputItem('pwd')
).add(
new SpanItem('6到12位数字或字母')
)
)
).add(
new FieldsetItem('info','信息').add(
new Group().add(
new LabelItem('name','昵称:')
).add(new InputItem('name'))
).add(
new Group().add(
new LabelItem('status','状态:')
).add(
new InputItem('status')
)
)
).show();

浏览器显示

生成的html代码

javascript-组合模式的更多相关文章

  1. 轻松掌握:JavaScript组合模式

    组合模式 组合模式:将一组对象组合成树形结构,并统一对待组合对象和叶对象,忽略它们之间的不同(因为叶对象也可以也可以包含叶对象而成为组合对象),组合模式中的对象只能是一对多的关系,不能出现多对一. 基 ...

  2. JavaScript组合模式---引入

    首先:使用一个例子来引入组合模式,需求为(1)有一个学校有2个班(一班,二班)(2)每个班级分2个小组(一班一组,一班二组,二班一组,二班二组)(3)学校计算机教室有限,每一个小组分着来上课 然后:根 ...

  3. [设计模式] javascript 之 组合模式

    组合模式说明 组合模式用于简单化,一致化对单组件和复合组件的使用:其实它就是一棵树: 这棵树有且只有一个根,访问入口,如果它不是一棵空树,那么由一个或几个树枝节点以及子叶节点组成,每个树枝节点还包含自 ...

  4. javascript设计模式学习之十——组合模式

    一.组合模式定义及使用场景 组合模式将对象组合成树形结构,用以表示“部分—整体”的层次结构,除了用来表示树形结构之外,组合模式还可以利用对象的多态性表现,使得用户对单个对象和组合对象的使用具有一致性. ...

  5. javascript设计模式-组合模式

    组合模式所要解决的问题: 可以使用简单的对象组合成复杂的对象,而这个复杂对象有可以组合成更大的对象.可以把简单这些对象定义成类,然后定义一些容器类来存储这些简单对象. 客户端代码必须区别对象简单对象和 ...

  6. JavaScript高级---组合模式设计

    一.设计模式 javascript里面给我们提供了很多种设计模式: 工厂.桥.组合.门面.适配器.装饰者.享元.代理.观察者.命令.责任链 在前面我们实现了工厂模式和桥模式 工厂模式 : 核心:为了生 ...

  7. 读书笔记之 - javascript 设计模式 - 组合模式

    组合模式是一种专为创建Web上的动态用户界面而量身定制的模式,使用这种模式,可以用一条命令在对各对象上激发复杂的或递归的行为. 在组合对象的层次体系中有俩种类型对象:叶对象和组合对象.这是一个递归定义 ...

  8. 再起航,我的学习笔记之JavaScript设计模式15(组合模式)

    组合模式 组合模式(Composite): 又称部分-整体模式,将对象组合成树形结构以表示"部分整体"的层次结构.组合模式使得用户对单个对象和组合对象的使用具有一致性. 如果有一个 ...

  9. javascript设计模式——组合模式

    前面的话 在程序设计中,有一些和“事物是由相似的子事物构成”类似的思想.组合模式就是用小的子对象来构建更大的对象,而这些小的子对象本身也许是由更小的“孙对象”构成的.本文将详细介绍组合模式 宏命令 宏 ...

  10. JavaScript设计模式(5)-组合模式

    组合模式 1. 适合使用组合模式的条件: 存在一批组织成某种层次体系的对象,如树形结构(具体的结构在开发期间可能无法得知) 希望对这批对象或其中的一部分对象实施一个相同的操作 2. 注意点: 组合对象 ...

随机推荐

  1. 周六搞事情,微信小程序开发文档已放出!

    程序员们,你们有事干了! 个人感觉不管是什么形式的UI技术,都无法决定一个产品的生死,核心还是服务和模式的创新. 某些方面和ApiCloud好像,但发展前景远远胜过ApiCloud. 微信小程序可以为 ...

  2. Go语言实战 - revel框架教程之缓存和Job

    所有的网站应该都会有一个非常简单的需求,首页一秒之内打开. 满足的方式主要有两种: 页面静态化,效果最好,对服务器基本没负担,只要带宽足够就好了.我知道一个PV过亿的站点就是全站静态(以前新浪也是), ...

  3. 【翻译】用AIML实现的Python人工智能聊天机器人

    前言 用python的AIML包很容易就能写一个人工智能聊天机器人. AIML是Artificial Intelligence Markup Language的简写, 但它只是一个简单的XML. 下面 ...

  4. 【原】关于Python中setuptools安装的问题

    在生成package的时候,需要在setup.py中引入setuptools包,可是却报告如下错误: ImportError: No module named setuptools 解决办法就是下载s ...

  5. C语言图形库简单对比及EGE库的安装小手册

    近期在琢磨C语言的图形库,发现主要有如下几种选择: Turbo C 的graphics库 SDL EasyX EGE 1. 普遍认为Graphics库太老了,而且TurboC本身使用比较麻烦,网上一边 ...

  6. 浅谈angular2+ionic2

    浅谈angular2+ionic2   前言: 不要用angular的语法去写angular2,有人说二者就像Java和JavaScript的区别.   1. 项目所用:angular2+ionic2 ...

  7. RabbitMQ简单测试

  8. .net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)

    正则表达式的本质是使用一系列特殊字符模式,来表示某一类字符串.正则表达式无疑是处理文本最有力的工具,而.NET的System.dll类库提供的System.Text.RegularExpression ...

  9. WCF学习之旅—第三个示例之一(二十七)

    一.前言 通过前面二十几个章节的学习,我们知道了什么是WCF:WCF中的A.B.C:WCF的传输模式:WCF的寄宿方式:WCF的异常处理.本文综合应用以上知识点,一步一步写一个小的WCF应用程序——书 ...

  10. 在 CSS 预编译器之后:PostCSS

    提到css预编译器(css preprocessor),你可能想到Sass.Less以及Stylus.而本文要介绍的PostCSS,正是一个这样的工具:css预编译器可以做到的事,它同样可以做到. “ ...