js设计模式之建造者模式

197 阅读1分钟

 下面一起来看下建造者模式

//建造者--施工团队
    let builder = function () {
        //成员01--决定厅室
        function Rooms(member) {
            if (member <= 0) {
                throw new Error("入住人数错误");
            }
            this.rooms = member > 3 ? 3 : 2;
        }
        //成员02--决定面积
        function floorSpace(budget) {
            if ((typeof budget !== "number") || Number.isNaN(budget) || (budget < 60)) {
                throw new Error("预算过低或错误");
            }
            this.budget = budget / 2;
        }
        //成员03--整体风格
        function Style(style) {
            this.style = style||"普通风格";
        }
 
        return class {
            constructor(member, budget, style) {
                Rooms.call(this, member);
                floorSpace.call(this, budget);
                Style.call(this, style)
            }
        };
    }();
 
let obj4=new builder(4,70,"欧美风格")
console.log(obj4);