设计模式尝试(组合模式、单例模式、模板方法模式)---js实现

  设计模式一直想要学习整理一下,但是一直没有时间。最近工作不算是特别忙,现在终于可以尝试一下了。感觉收获很多,在此记录一下。免得以后忘记

  

  组合模式:对于组合模式来说,给我个人的感觉就是拼接组合命令,之后通过执行根方法来进行所有的执行,感觉和面向对象的编程思想有点类似?只不过没有面向对象那么复杂。更多的是类似将所有想做的事情拼接成一棵树,我们只需要操控最根本的那个节点就可以触发整棵树的所有命令。

class combination {
    constructor() {
        this.list = []; // 所有子节点的存储数组
    }
    add(item) {
        this.list.push(item);    // 向其中添加新的子节点
    }
    emit() {
        for(let i=0; i < this.list.length; i++) {
            this.list[i].emit();   // 通过循环触发所有的子节点
        }
    }
}

const combination1 = new combination(); // 行为组合1
combination1.add({
    emit: () => console.log('分组1的任务1')
})
combination1.add({
    emit: () => console.log('分组1的任务2')
})

const combination2 = new combination();  // 行为组合2
combination2.add({
    emit: () => console.log('分组2的任务1')
})

const combination3 = new combination();  // 行为组合3
combination3.add({
    emit: () => console.log('分组3的任务1')
})
combination3.add({
    emit: () => console.log('分组3的任务2')
})
combination3.add({
    emit: () => console.log('分组3的任务3')
})

const combinationAll = new combination();  // 根节点
combinationAll.add(combination1);
combinationAll.add(combination2);
combinationAll.add(combination3);

combinationAll.emit();

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  

  单例模式: 相当经典的一个设计模式,生成一个对象。保持它本身的单一性。后续new的对象都和第一个对象保持一致,挺有意思的一个模式。

class singleCase {
    constructor(data) {
        this.a = data;
        this.singleItem = null; // 记录该对象是否被创建
    }

    static getSingleItem(data) { // 通过方法获取对象
        if(!this.singleItem) this.singleItem = new singleCase(data);
        return this.singleItem;
    }
}

const single1 = singleCase.getSingleItem('111');
const single2 = singleCase.getSingleItem('222');

console.log(single1);
console.log(single2);

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  模板方法模式:本质上就是继承,基于继承重写了父类的方法。和工厂模式联动比较多。其实在我眼里这俩耦合度有点高。想要区分可能有点难呀,或者没必要区分。一起记算了。

  

class temModel { // 基础模板
    constructor() {}

    init() {
        this.firstStep();
        this.secondStep();
        this.thirdStep();
        this.fourthStep();
    }

    firstStep() {
        console.log('模板方法第1111步');
    }
    secondStep() {}
    thirdStep() {
        console.log('模板方法第3333步');
    }
    fourthStep() {}
}
class test1Model extends temModel{ // test1模板类型
    constructor() {
        super();
    }
    secondStep() {
        console.log('test1 的第2222步');
    }
    fourthStep() {
        console.log('test1 的第4444步');
    }
}
class test2Model extends temModel{ // test2模板类型
    constructor() {
        super();
    }
    secondStep() {
        console.log('test2 的第2222步');
    }
    fourthStep() {
        console.log('test2 的第4444步');
    }
}

const test1 = new test1Model();
const test2 = new test2Model();
test1.init();
test2.init();

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  

原文地址:https://www.cnblogs.com/acefeng/p/12015241.html