(三)js前端开发中设计模式之工厂模式

工厂模式是一种创建型设计模式,它可以帮助我们根据需求创建对象。

简单工厂模式

js 复制代码
const BicycleShop = function () {};

BicycleShop.prototype = {
  sellBicycle(model) {
    let bicycle;
    switch (model) {
      case "BMW":
        bicycle = new BMW();
        break;
      case "BYD":
        bicycle = new BYD();
        break;
      default:
        bicycle = new YaDi();
    }
    return bicycle;
  },
};

function BMW() {
  this.name = "BMW";
}
BMW.prototype = {
  run() {
    console.log("BMW 正在运行");
  },
};

function BYD() {
  this.name = "BYD";
}

BYD.prototype = {
  run() {
    console.log("BYD 正在运行");
  },
};

function YaDi() {
  this.name = "YaDi";
}

YaDi.prototype = {
  run() {
    console.log("YaDi 正在运行");
  },
};

//出手宝马
const bicycleShop = new BicycleShop();
bicycleShop.sellBicycle("BMW").run();
  • 这里我们把成员对象的创建工作交给了外部对象,此时我们要添加新的车型,就不用改变 BicycleShop1,只需要在 BicycleFactory 中添加即可
js 复制代码
// 升级版
const BicycleShop1 = function () {};

const BicycleFactory = {
  createBicycle(model) {
    let bicycle;
    switch (model) {
      case "BMW":
        bicycle = new BMW();
        break;
      case "BYD":
        bicycle = new BYD();
        break;
      case "LuYuan":
        bicycle = new LuYuan();
        break;
      default:
        bicycle = new YaDi();
    }
    return bicycle;
  },
};

BicycleShop1.prototype = {
  sellBicycle(model) {
    let bicycle = BicycleFactory.createBicycle(model);
    return bicycle;
  },
};

//此时我们要添加新的车型,就不用改变BicycleShop1,只需要在BicycleFactory中添加即可

function LuYuan() {
  this.name = "LuYuan";
}

LuYuan.prototype = {
  run() {
    console.log("LuYuan 正在运行");
  },
};

//出手luyuan
const bicycleShop1 = new BicycleShop1();
bicycleShop1.sellBicycle("LuYuan").run();

真正工厂模式

  • 与简单工厂模式的区别在于,他不是使用一个类或是对象来创建实例,而是使用一个子类来创建实例
  • 按照正式的定义,工厂模式是一个将其成员对象的实例化延迟到子类中进行的模式。
js 复制代码
const Bicycle = function () {};
Bicycle.prototype = {
  sellBicycle(model) {
    const bicycle = this.createBycicle(model);
    return bicycle;
  },
  createBycicle(model) {
    throw new Error("子类必须实现createBycicle方法");
  },
};

const BWMBicycle = function () {};
extend(BWMBicycle, Bicycle);
BWMBicycle.prototype.createBycicle = function (model) {
  let bicycle;
  switch (model) {
    case "BWM":
      bicycle = new BMW();
      break;
    case "Yadi":
      bicycle = new YaDi();
      break;
    default:
      throw new Error("没有该型号的自行车");
  }
  return bicycle;
};

const YADIBicycle = function () {};
extend(YADIBicycle, Bicycle);
YADIBicycle.prototype.createBycicle = function (model) {
  let bicycle;
  switch (model) {
    case "BWM":
      bicycle = new BMW();
      break;
    case "Yadi":
      bicycle = new YaDi();
      break;
    default:
      throw new Error("没有该型号的自行车");
  }
  return bicycle;
};

function BMW() {
  this.name = "BMW";
}
BMW.prototype = {
  run() {
    console.log("BMW 正在运行");
  },
};

function BYD() {
  this.name = "BYD";
}

BYD.prototype = {
  run() {
    console.log("BYD 正在运行");
  },
};

function YaDi() {
  this.name = "YaDi";
}

YaDi.prototype = {
  run() {
    console.log("YaDi 正在运行");
  },
};

function extend(subClass, superClass) {
  var F = function () {};
  F.prototype = superClass.prototype;
  subClass.prototype = new F();
  subClass.prototype.constructor = subClass;

  //子类增加了一个属性,直接指向了父类的原型对象,prorotype
  subClass.superclass = superClass.prototype;
  //正常情况下,每个类的constructor属性都是指向自己,
  //保证父类的constructor属性指向父类
  if (superClass.prototype.constructor === Object.prototype.constructor) {
    superClass.prototype.constructor = superClass;
  }
}

const bwm = new BWMBicycle();
const yadi = new YADIBicycle();
const bwmBicycle = bwm.sellBicycle("BWM");
console.log("🚀 ~ bwmBicycle:", bwmBicycle);
const bwmBicycle2 = bwm.sellBicycle("Yadi");
console.log("🚀 ~ bwmBicycle2:", bwmBicycle2);
const yadiBicycle = yadi.sellBicycle("Yadi");
console.log("🚀 ~ yadiBicycle:", yadiBicycle);
const yadiBicycle2 = yadi.sellBicycle("BWM");
console.log("🚀 ~ yadiBicycle2:", yadiBicycle2);

/**
 * 🚀 ~ bwmBicycle: { name: 'BMW' }
  🚀 ~ bwmBicycle2: { name: 'YaDi' }
  🚀 ~ yadiBicycle: { name: 'YaDi' }
  🚀 ~ yadiBicycle2: { name: 'BMW' }
 */
相关推荐
小白不太白95020 分钟前
设计模式之 责任链模式
python·设计模式·责任链模式
何大春1 小时前
【弱监督语义分割】Self-supervised Image-specific Prototype Exploration for WSSS 论文阅读
论文阅读·人工智能·python·深度学习·论文笔记·原型模式
吾与谁归in1 小时前
【C#设计模式(13)——代理模式(Proxy Pattern)】
设计模式·c#·代理模式
吾与谁归in1 小时前
【C#设计模式(14)——责任链模式( Chain-of-responsibility Pattern)】
设计模式·c#·责任链模式
闲人一枚(学习中)1 小时前
设计模式-创建型-原型模式
设计模式
Iced_Sheep1 小时前
干掉 if else 之策略模式
后端·设计模式
哪 吒9 小时前
最简单的设计模式,抽象工厂模式,是否属于过度设计?
设计模式·抽象工厂模式
Theodore_10229 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
转世成为计算机大神12 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式
小乖兽技术13 小时前
23种设计模式速记法
设计模式