在JS中总结一下设计模式(单例-懒汉,单例-饿汉,工厂,代理,观察者)

1.代理模式


// 代理模式   
// 应用场景:一个男孩想要跟女神表白,没有足够的勇气,只好找了同学(代理)将礼物送出去,表达心意

// 一个男孩心中的女神
var girl = function (name) {
    this.name = name;
}
// 一个羞涩的男孩
var boy = function (name, girl) {
    this.name = name;
    this.girl = girl;  // 心中装着的女神

    // 想给女神送个礼物表名心意,思考良久,还是没有勇气送出去
    this.sendGift = function (gift) {
        console.log(this.name + " 送给 " + this.girl.name + " 的 " + gift);
    }
}

// 同学中的红娘(代理),替男孩送礼物
var proxy = function (boy) {
    this.boy = boy;    // 代替的男孩

    this.sendGift = function (gift) {      // 红娘的代理行为
        this.boy.sendGift(gift);
    }
}

var girl1 = new girl("小苏");

var boy1 = new boy("小任", girl1);

var biaobai = new proxy(boy1);
biaobai.sendGift("乌江榨菜");


2.单例模式:

2.1懒汉式

// 单例模式  懒汉式
var singleton = (function () {
    var instance;
    return function getInstance() {
        if (instance == null) {
            instance = {             // 相当于私有化构造函数
                product: "单例-懒汉"
            }
        }
        return instance;
    }
})()

var product = singleton();
console.log(product);

2.2饿汉式

// 单例模式  饿汉式
var singletonHungry = (function () {
    var instance = { product: "单例-饿汉" };
    return function () {
        return instance;
    }
})()

var product = singletonHungry();
console.log(product);


3.工厂模式:


// 工厂模式
var Factory = (function () {
    var phone = function (name, version) {
        this.name = name;
        this.version = version;
    }

    phone.prototype.play = function () {
        console.log(this.name + " " + this.version);
    }

    return function getInstance(name, version) {       // 工厂向客户开放大门
        return new phone(name, version);       // 返给客户成型产品  区别于单例,每次调用都会new一个新实例
    }
})()

var myPhone = Factory("HUAWEI", "Mate30");
myPhone.play();

4.观察者模式(ES5、ES6两种写法):


// 观察者模式(1)es6写法    
// 模拟场景:有一个饭馆前台接受客户订单,当食物做好了,喊一声,客户确认是自己的食物,就来控制台取餐
class Platform {
    constructor() {
        this.orders = [];
    }

    submit(order) {
        this.orders.push(order);
    }

    notify(good) {
        this.orders.forEach(item => {
            item.callMe(good);
        })
    }
}


class customer {
    constructor(name, good) {
        this.name = name;
        this.good = good;
    }

    callMe(good) {
        if (good == this.good) {
            console.log(this.name + " 的 " + this.good + " 做好了!!!");
        }
    }
}

var meituan = new Platform();
meituan.submit(new customer("蘇姐", "奶茶"));
meituan.submit(new customer("張三", "麻辣拌"));

meituan.notify("麻辣拌");

// 观察者模式(2)  闭包写法(es5)   
// 模拟场景:一个百货工厂,接受客户各类需求(食物、衣服制作),客户提交自己的需求类型(食物、衣服)和具体的商品名称给百货工厂,
// 百货工厂做好了,喊一声,客户确认是否是自己的需求类型 具体商品,如果是去前台领取
var DepartmentStore = (() => {
    var goods = {};

    function addOrder(order, declare) {
        if (!goods[order]) {
            goods[order] = [];
        }
        goods[order].push(declare);
    }

    function notify(order, ...args) {
        for (let fn of goods[order]) {
            fn(...args);
        }
    }

    return {
        addOrder,
        notify
    }
})()

var Person = function (name, requirement) {
    var name = name;
    var requirement = requirement;
    return function callMe(good) {
        if (good == requirement) {
            console.log(name + " 的 " + requirement + " 做好了!!!")
        }
    }
}

DepartmentStore.addOrder("food", Person("小苏", "奶茶"));
DepartmentStore.addOrder("food", Person("李四", "麻辣烫"));
DepartmentStore.addOrder("clothes", Person("小兰", "连衣裙"));

DepartmentStore.notify("food", "麻辣烫");

你可能感兴趣的:(JavaScript基础)