关于W3Cschool定义的设计模式-常用的9种设计模式的介绍
一、设计模式
tip:每种设计模式,其实都是为了更高效的,更方便的解决在面对对象编程中所遇到的问题。
function Fn(){
this.name = "root";
}
var f = new Fn()
console.log(f.name)//root
单例模式
//单个实例,只有一个对象
//多次创建,返回同一个对象
function fn(){
if(!fn.obj) { //给函数添加一个属性。 因为函数是全局的,所以该属性一旦添加一直存在;
fn.obj = {
name : “土土皮皮"
};
}
return fn.obj;
}
var obj1 = new fn();
var obj2 = new fn();
console.log(obj1 == obj2);
//例如我们创建一个信息提示框,每一次执行toast方法,如果都创建一个新的元素,这样做太浪费了。
//因此,我们采取单例模式,确保无论调用多少次toast方法,都只创建一个DOM元素。
//我们只控制它的显示和隐藏,而不是每次创建每次销毁。
function Toast(){
var div = document.createElement("div");
div.className = "box";
document.body.appendChild(div);
setTimeout(function(){
div.remove();
},1000)
}
obtn.onclick = function(){
var a = new Toast();
var b = new Toast();
console.log(a == b)
}
function Toast(){
if(!Toast.div){
Toast.div = document.createElement("div");
Toast.div.className = "box";
document.body.appendChild(Toast.div);
clearTimeout(Toast.div.timer);
Toast.div.timer = setTimeout(function(){
Toast.div.style.display = "none";
},1000)
}else{
Toast.div.style.display = "block";
clearTimeout(Toast.div.timer);
Toast.div.timer = setTimeout(function(){
Toast.div.style.display = "none";
},1000)
}
return Toast;
}
obtn.onclick = function(){
var a = new Toast();
var b = new Toast();
console.log(a == b);
}
单例模式-弹出框案例
// 单例模式
function Toast(){
// 第一次执行函数时给构造函数添加属性为对象
if(!Toast.obj){
Toast.obj = {};
// 对对象进行加工
Toast.obj.dia = document.createElement("dialog");
Toast.obj.dia.innerHTML = "这是一个弹出框";
document.body.appendChild(Toast.obj.dia)
}
// 正常情况下,每次创建元素,都要立即显示
Toast.obj.dia.style.display = "block";
// 过一定的时间后,隐藏元素
clearTimeout(Toast.obj.t)
Toast.obj.t = setTimeout(() => {
Toast.obj.dia.style.display = "none"
}, 2000);
// 覆盖this的指向(覆盖new出来的对象)
return Toast.obj;
}
document.onclick = function(){
var t1 = new Toast()
var t2 = new Toast()
var t3 = new Toast()
var t4 = new Toast()
var t5 = new Toast()
//这些被new出来的对象,都指向一个对象
console.log(t1 == t2)//true
console.log(t3 == t2)//true
console.log(t5 == t2)//true
console.log(t4 == t2)//true
console.log(t1 == t3)//true
console.log(t3 == t5)//true
}
四、组合模式
1、组合模式是用来组合对象的,一般应用于页面,将对象按照一定的规律和关系组合,组成树状结构,类似于DOM元素中的树状结构,可以完成动态网页的生成、创建元素和修改样式。
2、将对象组合起来之后,可以实现:批量操作。
3、缺点:节省了操作,消耗了性能。
tip:组合模式最重要的是组合器。
// 最关键的是组合器:
function ImagesStore( id ){
this.children = [];
this.element = document.createElement("div");
this.element.id = id;
document.body.appendChild(this.element)
}
ImagesStore.prototype = {
constructor : ImagesStore,
add:function( child ){
this.children.push( child );
this.element.appendChild( child.getElement() );
},
remove:function( child ){
for( var node, i=0; node = this.getChild(i); i++ ){
if( node === child ){
this.children.splice( i, 1 );
break;
}
}
this.element.removeChild( child.getElement() );
},
getChild:function( i ){
return this.children[i];
},
show:function(){
this.element.style.border = 'solid 2px black';
for( var node, i=0; node = this.getChild(i); i++ ){
node.show();
}
},
hide:function(){
for( var node, i=0; node = this.getChild(i); i++ ){
node.hide();
}
this.element.style.border = 'none';
},
getElement:function(){
return this.element;
}
} function ImageItem( src ){
this.element = document.createElement("img");
this.element.src = src;
this.element.className = "img-item";
}
ImageItem.prototype = {
constructor:ImageItem,
add:function( child ){
console.log("这是子对象了,没有添加功能");
},
remove:function( child ){
console.log("这是子对象了,没有删除功能");
},
getChild:function( i ){
console.log("这是子对象了,没有获取子对象功能");
},
show:function(){
this.element.style.border = 'solid 2px black';
},
hide:function(){
this.element.style.border = 'none';
},
getElement:function(){
return this.element;
}
} var box = new ImagesStore("box");
var xbox = new ImagesStore("xbox"); var img1 = new ImageItem("https://www.baidu.com/img/bd_logo1.png")
var img2 = new ImageItem("https://www.baidu.com/img/bd_logo1.png") xbox.add(img1)
xbox.add(img2) box.add(xbox)
// box.remove(img1) // img1.show()
box.show() // img1.add()
五、观察者模式
1、观察者模式又叫发布订阅者模式:
(1)发布者:主题对象,一般只有一个。
(2)接收者:订阅者,多个,随时添加和删除。
(3)广播通信,一个对象发布信息,多个对象接收信息,并做出对应处理。
2、观察者模式的好处:
(1)支持简单的广播通信,自动通知所有已经订阅过的对象。
(2)页面载入后目标对象很容易与观察者存在一种动态关联,增加了灵活性。
(3)目标对象与观察者之间的抽象耦合关系能够单独扩展以及重用。
观察者模式-案例-看孩子还是打麻将
function child(n){
this.name = n;
this.type = function(){
return Math.random()>0.5? 0 : 1;
}
}
function mather(n,c){
this.name = n;
this.child = c;
this.listen = function(t){
if(t==0){
console.log(this.child.name + "哭了,"+this.name+"看孩子")
}else{
console.log(this.child.name + "睡了,"+this.name+"打麻将")
}
}
}
function father(n,c){
this.name = n;
this.child = c;
this.listen = function(t){
if(t==0){
console.log(this.child.name + "哭了,"+this.name+"看孩子")
}else{
console.log(this.child.name + "睡了,"+this.name+"打麻将")
}
}
}
var c = new child("大宝");
var t = c.type();
var m = new mather('大宝妈',c);
m.listen(t);
var f = new father('大宝爸',c);
f.listen(t);
六、代理模式
function girl(name){
this.name = name;
}
function boy(girl){
this.girl = girl;
this.sendGift = function(gift){
alert("你好,漂亮的"+this.girl.name+",这是我送你的:"+gift);
}
}
function porxyLitterBrother(girl){
this.girl = girl;
this.send = function(gift){
this.g = gift;
gift = "一个拥抱";
var b = new boy(girl);
b.sendGift(gift);
}
this.init = function(){
console.log(this.g)
}
}
var g = new girl("翠花");
var p = new porxyLitterBrother(g);
p.send("钻戒")
p.init()
七、适配器模式
适配器模式就是将原本不具有某些功能的对象,在使用这些功能时,不出问题,并让某些不具有特征的属性,变得特征
demo:
电子工厂:手机,平板
手机:打电话,玩游戏
平板:玩游戏
测试模块只有一个:想能测平板又能测手机,还正确测试,不出问题
// 应用场景
// 让某个不具有明显特征的功能,变得有特征
function phone(){
this.name = "phone";
this.call = function(){
console.log(this.name + "可以打电话");
};
this.game = function(){
console.log(this.name + "可以打游戏");
};
}
function pad(){
this.name = "pad";
this.game = function(){
console.log(this.name + "可以打游戏")
}
}
function text(obj){
if(obj.call){
obj.call();
}else{
console.log(obj.name+"没有打电话的功能");
};
if(obj.game){
obj.game();
}else{
consloe.log(obj.name+"没有打游戏的功能");
}
}
var p1 = new phone();
text(p1);
var p2 = new pad();
text(p2);
八、抽象工厂模式
在工厂模式中,将多个实例的相同属性或方法,再次抽象成一个公共对象,从公共对象上,再次创建出具体的实例。
demo:
造车厂:制造汽车
汽车需要车架子:轮子,引擎。
我们可以将相同的部分放一起,然后通过添加其他不同的零件,生产不行型号的车。
var f = (function (){
//抽象工厂模式主要就是这个公共对象,模具对象
function car(wheel,engine){ //内部配置函数,可以提供配置功能。
this.wheel = wheel;
this.engine = engine;
}
return function(wheel , engine){ // 构造器, 负责创建对象。
return new car(wheel,engine); // 这是对外提供的接口,负责和外部需求连接。
}
})();
var car1 = f("15","V8");
var car2 = f("13","V10");
//每次执行都会有这个公共对象,执行这个公共对象,获得一个新的对象
console.log(car1);//一个新的car对象
console.log(car2);//一个新的car对象
console.log(car1 == car2);//false
//这种模式,就是所谓的抽象工厂模式。
九、策略模式
策略:计划,规划,预制要做的事情,不同的情况定制不同的计划。
function fn(n){
if(n < 10 || n.length < 1){
return "0"+n
}else{
return n;
}
}
var num = fn(4);
console.log(num)//04
总结:这就是我们平时常用的九种设计模式,每种设计模式其实都是见名识义,很多种设计模式我们也只会在写一些大型的项目的时候我们才会使用,每一种设计模式我们都需要根据当前的实际需求,来判断我们该使用哪种设计模式,使我们的代码解构更强。
关于W3Cschool定义的设计模式-常用的9种设计模式的介绍的更多相关文章
- Java常用的几种设计模式
本来想写点spring相关的东西的,想来想去,先写点设计模式的东西吧 什么是设计模式?套用百度百科的话解释吧 设计模式(Design Pattern)是一套被反复使用.多数人知晓的.经过分类的.代码设 ...
- PHP常用的三种设计模式
本文为大家介绍常用的三种php设计模式:单例模式.工厂模式.观察者模式,有需要的朋友可以参考下. 一.首先来看,单例模式 所谓单例模式,就是确保某个类只有一个实例,而且自行实例化并向整个系统提供这个实 ...
- PHP常用的 五种设计模式及应用场景
设计模式六大原则 开放封闭原则:一个软件实体如类.模块和函数应该对扩展开放,对修改关闭. 里氏替换原则:所有引用基类的地方必须能透明地使用其子类的对象. 依赖倒置原则:高层模块不应该依赖低层模块,二者 ...
- JAVA设计模式总结之23种设计模式
上一篇总结了设计模式的六大原则<JAVA设计模式总结之六大设计原则>,这一篇,正式进入到介绍23种设计模式的归纳总结. 一.什么是设计模式 ...
- java:常用的两种设计模式(单例模式和工厂模式)
一.单例模式:即一个类由始至终只有一个实例.有两种实现方式(1)定义一个类,它的构造方法是私有的,有一个私有的静态的该类的变量在初始化的时候就实例化,通过一个公有的静态的方法获取该对象.Java代码 ...
- js对象定义的最常用的三种方法
定义对象:属性和方法的结合体(变量和函数的结合体) 1.(***)var obj = {} 2.var obj = new Object(); 3.使用function定义对象 具体例子分别为: // ...
- 常用的四种设计模式 PHP代码
// 工厂模式 interface Iuser { public function getUserName(); } class UserFactory { static public functio ...
- java中常用的几种缓存类型介绍
在平时的开发中会经常用到缓存,比如locache.redis等,但一直没有对缓存有过比较全面的总结.下面从什么是缓存.为什么使用缓存.缓存的分类以及对每种缓存的使用分别进行分析,从而对缓存有更深入的了 ...
- 23种设计模式全解析 (java版本)
转自:http://blog.csdn.net/longyulu/article/details/9159589 其中PHP常用的五种设计模式分别为:工厂模式,单例模式,观察者模式,策略模式,命令模式 ...
随机推荐
- File Compression and Archiving in linux (linux 中文件的归档)
1. Compressing Files at the Shell Prompt Red Hat Enterprise Linux provides the bzip2, gzip, and zip ...
- Linux应用开发自学之路
前言 在 「关于我 」那篇博文里,朋友们应该知道了我不是科班出身,是由机械强行转行到Linux应用开发方向.下面我就详细向大家介绍自己这一路上的转行历程,希望对大家有所启发. 我是学机械专业的,对于机 ...
- 跨库数据迁移利器 —— Sqoop
一.Sqoop 基本命令 1. 查看所有命令 # sqoop help 2. 查看某条命令的具体使用方法 # sqoop help 命令名 二.Sqoop 与 MySQL 1. 查询MySQL所有数据 ...
- python爬取豆瓣首页热门栏目详细流程
记录一下爬取豆瓣热门专栏的经过,通过这篇文章,你能学会requests,HTMLParser,json的基本使用,以及爬取网页内容的基本思路. 使用模块 1,获取豆瓣首页代码:首先我们需要访问豆瓣页面 ...
- c# timestamp转换datetime
一.Codes class Program { static void Main(string[] args) { ); } public static DateTime UnixTimeStampT ...
- React之 redux 的简单介绍及使用
1.为什么使用redux?在小型react项目的开发中 ,view(视图层)中的数据模型(即数据),可以存放在组件中的 state 对象,换句话说页面中的动态数据存放在 state 中. 但对于开发大 ...
- Redis的初识
简介 已经有了Membercache和各种数据库,Redis为什么会产生?Redis纯粹为应用而产生,它是一个高性能的key-value数据库.Redis的出现,很大程序补偿了Memcached这类k ...
- SDU暑期集训排位(3)
B. Mysterious LCM 做法 保留 \(a_i|x\) 的元素,其它元素解体. \(a_i\) 的某个质因子的指数,要和 \(x\) 的这个质因子一样多,才有贡献,否则这个质因子它在划水啊 ...
- 牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp
链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 White Cloud is exercising in the playground. Whi ...
- 微信小程序 select 下拉框组件
一.源码地址 https://github.com/imxiaoer/WeChatMiniSelect 二.效果图 录屏图片质量较差,所以大家会看到残影(捂脸) 三.组件源码 1. select.wx ...