没有连接的是尚未写的,计划要写的,欢迎阅读交流~
前端设计模式(0)面向对象&&设计原则
前端设计模式(1)--工厂模式
前端设计模式(2)--单例模式
前端设计模式(3)--适配器模式
前端设计模式(4)--装饰器模式
前端设计模式(5)--代理模式
前端设计模式(6)--外观模式&&观察者模式
前端设计模式(7)--状态和策略模式
前端设计模式(8)--原型模式
...css
单例就是保证一个类只有一个实例,实现方法通常是先判断实例存在与否,若是存在直接返回,若是不存在就建立了再返回,这就确保了一个类只有一个实例对象。在JavaScript里,单例做为一个命名空间提供者,从全局命名空间里提供一个惟一的访问点来访问该对象。html
function Window(name) {
this.name = name;
}
Window.prototype.getName = function () {
return this.name;
}
//这是类上的方法,只能够经过类来访问,而不能经过实例来访问
Window.getInstance = (function () {
let instance;
return function (name) {
if (!instance) {
instance = new Window(name);
}
return instance;
}
})();
let w1 = Window.getInstance();
let w2 = Window.getInstance();
console.log(w1 === w2) // --> true
复制代码
class Window {
constructor(name) {
this.name = name;
}
static getInstance() {
if (!this.instance) {
this.instance = new Window();
}
return this.instance;
}
}
let w1 = Window.getInstance();
let w2 = Window.getInstance();
console.log(w1 === w2)
复制代码
let w3 = new Window();
let w4 = new Window();
复制代码
//透明单例
let Window = (function () {
let window;
let Window = function (name) {
if (window) {
return window;
} else {
this.name = name;
return (window = this);
}
}
return Window;
})();
let w1 = new Window();
let w2 = new Window();
console.log(w1 === w2);
复制代码
可是有个问题:违反了单一职责原则,不够清晰,因此须要改进一下前端
//把类的实例的建立逻辑和单例逻辑分开
function Window(name) {
this.name = name;
}
Window.prototype.getName = function () {
console.log(this.name);
}
let CreateWindow = (function () {
let instance;
return function (name) {
if (!instance) {
instance = new Window(name);
}
return instance;
}
})();
let w1 = new CreateWindow('zfpx1');
let w2 = new CreateWindow('zfpx2');
console.log(w1 === w2);
复制代码
如今是清晰了,可是还有一个小问题,Window给写死了,做为一个优秀的程序员,确定要灵活,来来再改进一下。jquery
function Window(name) {
this.name = name;
}
Window.prototype.getName = function () {
console.log(this.name);
}
let CreateWindow = (function () {
let instance;
return function (name) {
if (!instance) {
instance = new Window(name);
}
return instance;
}
})();
let w1 = new CreateWindow('zfpx1');
let w2 = new CreateWindow('zfpx2');
console.log(w1 === w2);
复制代码
1.变量名冲突程序员
2.复杂层次对象的可读性要求es6
其实咱们以前用的jquery并无把变量都声明在 window上,而是都挂在$对象 jQuery,来,栗子两枚:ajax
$.get();
$.post();
$.ajax();
let $ = {
ajax(){},
get(){},
post(){}
}
let $ = {};
$.define = function (namespace, fn) {
let namespaces = namespace.split('.');
let fnName = namespaces.pop();
let current = $;
for (let i = 0; i < namespaces.length; i++) {
let namespace = namespaces[i];//dom
if (!current[namespace]) {
current[namespace] = {};//{dom:{}}
}
current = current[namespace];
}
current[fnName] = fn;
}
$.define('dom.class.addClass', function () {
console.log('dom.class.addClass');
});
$.define('dom.attr', function () {
console.log('dom.attr');
});
$.define('string.trim', function () {
console.log('string.trim');
});
console.log($) // define、dom、string都挂载上了了
$.dom.class.addClass('title'); // -> dom.class.addClass
$.dom.attr('src'); // -> dom.attr
$.string.trim(' abc '); // -> string.trim
复制代码
只会有一个jQuery实例redux
if(window.jQuery!=null){
return window.jQuery;
}else{
//init~~~~~~~
}
复制代码
通常网站都会有用户系统,例如咱们去淘宝买东西,第一时间须要你先登陆设计模式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button id="show-btn">显示登陆框</button>
<button id="hide-btn">隐藏登陆框</button>
<script>
class Login {
constructor() {
this.element = document.createElement('div');
this.element.innerHTML = (
`
用户名 <input name="username"/>
密码 <input name="password"/>
<input type="submit" value="登陆"/>
`
);
this.element.style.cssText = `width:100px;height:100px;position:absolute;left:50%;top:50%;margin-top:-50px;margin-left:-50px;display:none`;
document.body.appendChild(this.element);
}
show() {
this.element.style.display = 'block';
}
hide() {
this.element.style.display = 'none';
}
static getInstance() {
if (!this.instance) {
this.instance = new Login();
}
return this.instance;
}
}
document.getElementById('show-btn').addEventListener('click', function () {
Login.getInstance().show();
});
document.getElementById('hide-btn').addEventListener('click', function () {
Login.getInstance().hide();
});
</script>
</body>
</html>
复制代码
redux 整 个应用只有一个仓库,整 个仓库只有一个状态statebash
//redux 整 个应用只有一个仓库,整 个仓库只有一个状态state
function createStore(reducer) {
let state;
let listeners = [];
function subscribe(listener) {
listeners.push(listener);
}
function getState() {
return state;
}
function dispatch(action) {
state = reducer(state, action);
}
return {
getState,
dispatch,
subscribe
}
}
let reducer = function () {
}
let store = createStore(reducer);
复制代码
今天写到这里,喜欢的能够点赞,同时欢迎抛转,有空再写下总结吧~