js常见设计模式

// 单例模式
// ES5
function Person()  {
    this.money = 10
    if(Person.instance){
        return Person.instance
    }
    else{
        Person.instance = this
    }
}
// ES6
class Demo{
    constructor(){
        if(Demo.instance){
            return Demo.instance
        }
        else return Demo.instance=this
    }
    hh = 10
}


// 代理模式
// ES5
function girl(name){
    this.name = name 
}

function boy(girl){
    this.boyname = '小华' 
    this.girlname = girl.name
    this.altername = function(name){
        this.boyname = name
    }
    this.send = function(gift){
        console.log(this.boyname+'送了'+this.girlname+gift);
    }
}

function kdy(girl){
    this.skd = function(gift){
        new boy(girl).send(gift)
    }
}
var xz = new kdy(new girl('小芳'))
xz.skd('蓝牙音箱')

// ES6
class es6girl{
    constructor(name){
        this.name = name
    }
}

class es6boy{
    constructor(girl){
        this.girlname = girl.name
    }
    send(gift){
        console.log('您好:'+this.girlname+'您的'+gift)
    }
}

class es6Proxy{
    constructor(girl){
        this.jjr = girl
    }
    ps(girl){
        new boy(this.jjr).send(gift)
    }
}

// 装饰器模式
// ES5
function decoratorfn(fn,beforfn){
    return function(){
        var ret = beforfn.apply(this,arguments)
        if(ret !== false){
            fn.apply(this,arguments)
        }
    }
}

function skill() {
    console.log('数学');
}

function skillMusic() {
    console.log('音乐');
}

function skillRun() {
    console.log('跑步');
}

var skilldecorator = decoratorfn(skill,skillMusic)
skilldecorator();

//适配器模式
// ES6
const API = {
    qq: () => ({
        n: "菊花台",
        a: "周杰伦",
        f: 1
    }),
    netease: () => ({
        name: "菊花台",
        author: "周杰伦",
        f: false
    })
}

const adapter = (info = {}) => ({
    name: info.name || info.n,
    author: info.author || info.a,
    free: !!info.f
})
console.log(adapter(API.qq()))
console.log(adapter(API.netease()))

//命令模式  业务分离,低耦合
// ES5
// 点餐人员 关注的对象:菜单
// 厨房老大 关注的对象:分配
// 厨师     关注的对象:菜单

//厨师
var cook1 = {
    name: '王小二',
    make: function(foodType){
        switch(foodType){
            case 'tudou': 
                console.log(this.name,'做土豆');
                break;
            case 'jidan': 
                console.log(this.name,'做鸡蛋');
                break;
            case 'fanqie': 
                console.log(this.name,'做番茄');
                break;
            default: 
                console.log('no cant')
                break;
        }
    }
}

var cook2 = {
    name: '王大二',
    make: function(foodType){
        switch(foodType){
            case 'tudou': 
                console.log(this.name,'做土豆加辣椒');
                break;
            case 'jidan': 
                console.log(this.name,'做鸡蛋加白糖');
                break;
            case 'fanqie': 
                console.log(this.name,'做番茄加酱油');
                break;
            default: 
                console.log('no cant')
                break;
        }
    }
}

//服务员
var foodList = ['tudou','jidan','fanqie'];

//点餐系统/厨师长

function makeFoodCommand(cook,foodType){
    this.cook = cook
    this.foodType = foodType
}
makeFoodCommand.prototype.execute = function(){
    this.cook.make(this.foodType)
}

//做菜命令
var commands = []
for(let i=0; i

你可能感兴趣的:(js常见设计模式)