工厂模式
<!-- 工厂模式:根据不同的输入返回不同类的实例,一般用来创建同一类对象。 -->
function restaurant(menu) {
switch (menu) {
case '鱼香肉丝':
return new YuXiangRouSi();
case '宫保鸡丁':
return new GongBaoJiDin();
default:
throw new Error('这个菜本店没有');
}
};
function YuXiangRouSi() { this.type = '鱼香肉丝' };
YuXiangRouSi.prototype.eat = function () {
console.log(this.type + ' 真香');
};
function GongBaoJiDin() { this.type = '宫保鸡丁' };
GongBaoJiDin.prototype.eat = function () {
console.log(this.type + ' 让我想起了外婆做的菜~');
};
const dish1 = restaurant('鱼香肉丝');
dish1.eat();
const dish2 = restaurant('红烧排骨');
建造者模式
class CarBuilder {
constructor({ color = 'white', weight = 0 }) {
this.color = color;
this.weight = weight;
}
buildTyre(type) {
const tyre = {}
switch (type) {
case 'small':
tyre.tyreType = '小号轮胎'
tyre.tyreIntro = '正在使用小号轮胎'
break
case 'normal':
tyre.tyreType = '中号轮胎'
tyre.tyreIntro = '正在使用中号轮胎'
break
case 'big':
tyre.tyreType = '大号轮胎'
tyre.tyreIntro = '正在使用大号轮胎'
break
}
this.tyre = tyre;
}
buildEngine(type) {
const engine = {}
switch (type) {
case 'small':
engine.engineType = '小马力发动机'
engine.engineIntro = '正在使用小马力发动机'
break
case 'normal':
engine.engineType = '中马力发动机'
engine.engineIntro = '正在使用中马力发动机'
break
case 'big':
engine.engineType = '大马力发动机'
engine.engineIntro = '正在使用大马力发动机'
break
}
this.engine = engine
}
};
class BenChiDirector {
constructor(tyre, engine, param) {
const car = new CarBuilder(param);
car.buildTyre(tyre);
car.buildEngine(engine);
return car;
}
};
const benchi = new BenChiDirector('small', 'big', { color: 'red', weight: '1600kg' });
发布订阅模式
class PubSub{
#list = []
subscribe(sub){
this.#list.push(sub)
}
publish(){
this.#list.forEach(item=>{
item.update()
})
}
unsubscribe(sub){
this.#list = this.#list.filter(item=>item!==sub)
}
}
class Subscriber{
constructor(name){
this.name = name
}
update(){
console.log(this.name)
}
}
const pubsub = new PubSub()
let subscriber1 = new Subscriber('xiaoming')
let subscriber2 = new Subscriber('tiechui')
pubsub.subscribe(subscriber1)
pubsub.subscribe(subscriber2)
pubsub.publish()
单例模式
class Single{
constructor(name, age){
if(!Single.instance){
this.name = name;
this.age = age;
Single.instance = this;
}
return Single.instance;
}
}
let a = new Single("tom", 18);
let b = new Single("tiechui", 28);
console.log(a)
console.log(b)
适配器模式
class TencentMap{
show(){
console.log('开始渲染腾讯地图');
}
}
class BaiduMap{
display(){
console.log('开始渲染百度地图');
}
}
class BaiduAdapator extends BaiduMap{
constructor(){
super();
}
show(){
this.display()
}
}
function renderMap(map){
map.show();
}
renderMap(new TencentMap());
renderMap(new BaiduAdapator());
装饰器模式
Function.prototype.before = function (fn) {
var _this = this;
return function () {
fn.apply(this, arguments);
return _this.apply(_this, arguments);
}
}
Function.prototype.after = function (fn) {
var _this = this;
return function () {
var result = _this.apply(_this, arguments);
fn.apply(this, arguments);
return result;
}
}
function test(params) {
console.log(params);
}
var test1 = test.before(function (params) {
console.log('前置函数');
params.token = 'ajsbasczxc';
});
var test2 = test.after(function (params) {
console.log('后置函数');
params.date = new Date()
});
test1({
name: 'zhe'
})
test2({
name: 'zhe'
})