阅读 73

设计模式

简单介绍

原则

  1. 单一职责原则

复制代码

  1. 最少知识原则

复制代码

  1. 开放 - 封闭原则

复制代码

  1. 接口和面向接口编程

复制代码

  1. 代码重构

复制代码

设计模式

单例模式

定义: 保证一个类仅有一个实例, 并提供一个访问它的全局访问点

应用: 线程池、全局缓存、浏览器中的 window、弹框...

实现: 用一个变量标识是否已经创建过对象, 如果是, 则在下一次获取该实例时, 直接返回之前创建的对象

优点: 节约资源, 保持访问一致性, 减小垃圾回收机制的压力

var Singleton = function(name) {     this.name = name;     this.instance = null; } Singleton.prototype.getName = function() {     return this.name; } Singleton.getInstance = function(name) {     if(!this.instance) {         this.instance = new Singleton(name);     }     return this.instance; } 复制代码

class PlayStation {   static instance = undefined;   constructor() {     this.state = "off";   }   play() {     if (this.state === "on") {       console.log("已经开启了");       return;     }     this.state = "on";     console.log("开启ing...");   }   shutdown() {     if (this.state === "off") {       console.log("已经关机了");       return;     }     this.state = "off";     console.log("关机ing...");   } } /* 把赋能的操作写在外面, 跟功能分开 */ PlayStation.getInstance = function () {   if (!this.instance) {     this.instance = new PlayStation();   }   return this.instance; }; 复制代码

/* 功能类 */ class PlayStation {   constructor() { ... }   play() { ... }   shutdown() { ... } } /* 赋能类 */ const SinglePlayStation = (function() {     let instance = null;          const Single = function() {         if(!instance) {             instance = new PlayStation();         }         return instance;     }          Single.getInstance = function() {         if(!instance) {             instance = new PlayStation();         }         return instance;     }          return Single; })(); 复制代码

策略模式

定义: 定义一系列的算法, 把它们一个个封装起来, 并且使他们可以相互替换

应用: 表单校验, 实现功能有多个方案可选择时...

实现: 把请求委托给策略类中的某一个策略类

优点: 避免多重条件选择语句, 易于切换, 易于扩展

// 策略类 const strategies = {     isNonEmpty({value}) {         if(value === '') return '值不能为空';     },     minLength({value, length}) {         if(value.length < length) return `值长度不能小于${length}位`;     } } // 校验类 export class Validator {   // 保存校验规则   checkCache = [];   // 添加校验类   add(rule) {     if (typeof rule === 'function') {       this.checkCache.push(rule);     }     if (Array.isArray(rule)) {       rule.forEach((fn) => {         this.add(fn);       });     }   }      // 开始校验   start(params) {     for (const validatorFunc of this.checkCache) {       const errMsg = validatorFunc(params);       // 如果有返回值,说明校验没有通过       if (errMsg) {         return errMsg;       }     }   } } // 调用方 const validator = new Validator(); validator.add([     strategies.isNonEmpty,     strategies.minLength ]); const params = {     value: '值' } const errorMsg = validator.start(params); console.log('有错误信息', errorMsg); 复制代码

代理模式

复制代码

适配器模式

复制代码

发布 - 订阅模式

复制代码

职责链模式

复制代码

装饰者模式

复制代码

中介者模式

复制代码

迭代器模式

复制代码

命令模式

复制代码

组合模式

复制代码

模板方法模式

复制代码

享元模式

复制代码

状态模式


作者:气泡啊啊啊
链接:https://juejin.cn/post/7020430674741952525


文章分类
后端
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐