IFE第四十四天到第四十六天:开一家餐厅吧(二)(单例模式)

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>IFE ECMAScript</title>
</head>

<body>

    <script>
        var id = 0;

        function generateID() {
            return ++id;
        }

        function Restaurant(attr) {
            this.cash = attr.cash;
            this.seats = attr.seats;
            this.staff = attr.staff;
        }

        Restaurant.prototype.hire = function (staff) {
            this.staff.push(staff);
        }

        Restaurant.prototype.fire = function (staff) {
            this.staff = this.staff.filter(function (el) {
                return el !== staff;
            });
        }

        function Staff(name, salary) {
            this.id = generateID();
            this.name = name;
            this.salary = salary;
        }

        Staff.prototype.work = function () {

        }

        var singletonWaiter = (function () {
            var waiter = null;

            function Waiter(name, salary) {
                Staff.call(this, name, salary);
            }
            Waiter.prototype = {
                construstor: Waiter,
                notifyCook: function (task) {
                    if (typeof task == "object") {
                        console.log("服务员:厨师,做一份" + task.name);
                        let _ck = singletonCook.getCook("李大厨", 10000);
                        _ck.cookDish(task.name);
                        _ck = null;
                    }
                },
                serveDish: function (dishes) {
                    console.log(`服务员:${dishes.dishname}来了!`);
                },
            }
            return {
                getWaiter: function (name, salary) {
                    if (waiter === null) {
                        waiter = new Waiter(name, salary);
                    }
                    return waiter;
                }
            }
        })();

        var singletonCook = (function () {
            var cook = null;

            function Cook(name, salary) {
                Staff.call(this, name, salary);
            }
            Cook.prototype = {
                construstor: Cook,
                cookDish: function (dishes) {
                    console.log(`厨师:${dishes}做好了!`);
                },
                notifyWaiter: function (dishes) {
                    console.log("可以上菜了!");
                    let _wt = singletonWaiter.getWaiter("王小二", 3500);
                    _wt.serveDish(dishes);
                    _wt = null;
                },
            }
            return {
                getCook: function (name, salary) {
                    if (cook === null) {
                        cook = new Cook(name, salary);
                    }
                    return cook;
                }
            }
        })();

        function Customer(name) {
            this.name = name;
            this.order = function (dishes) {
                console.log(name + "点的菜是" + dishes.dishname);
                singletonWaiter.getWaiter("王小二", 3500).serveDish(dishes);
            }
            this.eat = function () {
                console.log("顾客:开始吃菜,吃完离开!");
            }
        }

        function Dish(dishname, cost, price) {
            this.dishname = dishname;
            this.cost = cost;
            this.price = price;
        }

        function getMenu(dishes) {
            let menu = new Array();
            for (let i = 0; i < dishes.length; i++) {
                let obj = {};
                obj.dishname = dishes[i].dishname;
                obj.price = dishes[i].price;
                menu.push(obj);
            }
            return menu;
        }

        //test
        var ifeRestaurant = new Restaurant({
            cash: 1000000,
            seats: 1,
            staff: [],
        });

        var newCook = singletonCook.getCook("李大厨", 10000);
        var newWaiter = singletonWaiter.getWaiter("王小二", 3500);
        ifeRestaurant.hire(newCook);
        ifeRestaurant.hire(newWaiter);

        var customerQueue = ['赵先生', '钱女士', '孙大姐', '李大哥'];
        var ifedishes = [
            new Dish("麻辣小龙虾", 30, 88),
            new Dish("酸辣土豆丝", 8, 18),
            new Dish("肉末茄子", 12, 28),
            new Dish("水煮肉片", 30, 58),
        ];
        var menu = getMenu(ifedishes);
        console.log(menu);

        for (let i = 0, len = customerQueue.length; i < len; i++) {
            let cus = new Customer(customerQueue.shift());
            let j = Math.floor(Math.random() * menu.length);
            cus.order(menu[j]);
            cus.eat();
            console.log("------------------------------");
            if (!customerQueue.length) {
                console.log("服务员:没有顾客了,可以休息下了!");
            }
        }
    </script>
</body>

</html>

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值