zoukankan      html  css  js  c++  java
  • 工厂方法模式

    简单工厂模式违背了开闭原则,扩张性不好

    工厂方法模式 :
    有需求就去创建工厂
    新工厂继承工厂类
    这样避免修改其他工厂

    把操作对象改变成操作各自的工厂

    class Animal  {
        constructor(name)  {
            this.name=name;
        }
        eat() {
             console.log('吃什么呀')
        }
    }
    class  Dog  extends  Animal {
      constructor(name)  {
           super(name);
           this.call='汪汪'
      }
    }
    
    class  Cat  extends  Animal {
      constructor(name)  {
           super(name);
           this.call='喵喵'
      }
    }
    
    class DogFactory{
        create() {
            return new Dog('xiao');
        }
    }
    
    class CatFactory{
        create() {
            return new Cat('wei');
        }
    }
    
     
     const settings={
        'dog': DogFactory,
        'cat': CatFactory
    }
    let dog=new settings['dog']().create();
    console.log(dog);
    let cat=new settings['cat']().create();
    console.log(cat);
  • 相关阅读:
    微服务之SpringCloud基础
    java 多线程学习
    tomcat性能优化
    js模块化规范
    jsonp及cors
    es6之各种数据类型的扩展
    es6之Promise
    es6之Set和Map
    es6之Generator
    es6之Iterator
  • 原文地址:https://www.cnblogs.com/guangzhou11/p/10747730.html
Copyright © 2011-2022 走看看