在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", "麻辣燙");

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章