JavaScript 设计模式之模板方法模式

模板方法模式

模板方法模式是一种只需使用继承就可以实现的非常简单的模式。

模板方法模式是由两部分结构组成:第一部分是抽象父类,第二部分是具体的实现子类。

例子一:

卡布奇诺和忘崽牛奶

// 饮料抽象类
let Beverage = function () {};
// 子类公共方法
Beverage.prototype.boilWater = function () {
  console.log("将水煮沸");
};
// 冲泡方法(需被子类重写)
Beverage.prototype.brew = function () {
  throw new Error("子类必须重写 brew 方法");
};
// 倒进杯子方法(需被子类重写)
Beverage.prototype.pourInCup = function () {
  throw new Error("子类必须重写 pourInCup 方法");
};
// 添加调味料方法(需被子类重写)
Beverage.prototype.addCondiments = function () {
  throw new Error("子类必须重写 addCondiments 方法");
};
// 初始化方法
Beverage.prototype.init = function () {
  this.boilWater();
  this.brew();
  this.pourInCup();
  this.addCondiments();
};

// 卡布奇诺类
let Cappuccino = function () {};
// 继承饮料抽象类
Cappuccino.prototype = new Beverage();
// 重写饮料方法
Cappuccino.prototype.brew = function () {
  console.log("用沸水冲泡卡布奇诺");
};
Cappuccino.prototype.pourInCup = function () {
  console.log("把卡布奇诺倒进杯子");
};
Cappuccino.prototype.addCondiments = function () {
  console.log("加糖");
};
let cappuccino = new Cappuccino();
cappuccino.init();

// 忘崽牛奶类
let ForgetSonMilk = function () {};
// 继承饮料类
ForgetSonMilk.prototype = new Beverage();
// 重写饮料方法
ForgetSonMilk.prototype.brew = function () {
  console.log("用沸水冲泡忘崽牛奶");
};
ForgetSonMilk.prototype.pourInCup = function () {
  console.log("把忘崽牛奶倒进杯子");
};
ForgetSonMilk.prototype.addCondiments = function () {
  console.log("加糖加牛奶");
};
let forgetSonMilk = new ForgetSonMilk();
forgetSonMilk.init();

例子二:

卡布奇诺和忘崽牛奶

// 饮料
let Beverage1 = function (param) {
  let boilWater = function () {
    console.log("把水煮沸");
  };
  let brew =
    param.brew ||
    function () {
      throw new Error("必须传递 brew 方法");
    };
  let pourInCup =
    param.pourInCup ||
    function () {
      throw new Error("必须传递 brew 方法");
    };
  let addCondiments =
    param.addCondiments ||
    function () {
      throw new Error("必须传递 brew 方法");
    };
  let F = function () {};
  F.prototype.init = function () {
    boilWater();
    brew();
    pourInCup();
    addCondiments();
  };
  return F;
};
// 卡布奇诺
let Cappuccino1 = Beverage1({
  brew: function () {
    console.log("用沸水冲泡卡布奇诺");
  },
  pourInCup: function () {
    console.log("把卡布奇诺倒进杯子");
  },
  addCondiments: function () {
    console.log("加糖");
  },
});
// 忘崽牛奶
let ForgetSonMilk1 = Beverage1({
  brew: function () {
    console.log("用沸水冲泡忘崽牛奶");
  },
  pourInCup: function () {
    console.log("把忘崽牛奶倒进杯子");
  },
  addCondiments: function () {
    console.log("加糖加牛奶");
  },
});
// 实现
let cappuccino1 = new Cappuccino1();
cappuccino1.init();

let forgetSonMilk1 = new ForgetSonMilk1();
forgetSonMilk1.init();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值