我以计算器为例写一个简单工厂模式,只完成加减乘除4个计算功能,考虑到其他功能方便日后扩展,遵循开放-封闭原则。
先看一下C#的简单工厂是如何实现的:
定义抽象类Operation,加减乘除的实现由子类派生,最后由OperationFactory决定实例化哪一个子类。
namespace OperationLibrary { public abstract class Operation { private int num_A = 0; public int Num_A { get { return num_A; } set { num_A = value; } } private int num_B = 0; public int Num_B { get { return num_B; } set { num_B = value; } } public abstract double getResult(); } public class OperationAdd : Operation { public override double getResult() { return Num_A + Num_B; } } public class OperationSub : Operation { public override double getResult() { return Num_A - Num_B; } } public class OperationMul : Operation { public override double getResult() { return Num_A * Num_B; } } public class OperationDiv : Operation { public override double getResult() { if (Num_B == 0) throw new Exception("除数不能为0"); return Num_A / Num_B; } } public class OperationFactory { public static Operation createOperate(string operate) { Operation oper = null; switch (operate) { case "+": oper = new OperationAdd(); break; case "-": oper = new OperationSub(); break; case "*": oper = new OperationMul(); break; case "/": oper = new OperationDiv(); break; } return oper; } } }
客户端调用:
Operation oper = OperationFactory.createOperate("/"); oper.Num_A = 6; oper.Num_B = 2; oper.getResult(); //3
js模拟C#的简单工厂:
var Operation = function(){ this.num_A = this.num_B = 0; }; var OperationAdd = function(){}; OperationAdd.prototype = new Operation(); OperationAdd.prototype.getResult = function(){ return this.num_A + this.num_B; }; var OperationSub = function(){}; OperationSub.prototype = new Operation(); OperationSub.prototype.getResult = function(){ return this.num_A - this.num_B; }; var OperationMul = function(){}; OperationMul.prototype = new Operation(); OperationMul.prototype.getResult = function(){ return this.num_A * this.num_B; }; var OperationDiv = function(){}; OperationDiv.prototype = new Operation(); OperationDiv.prototype.getResult = function(){ if(this.num_B == 0) throw new Error(‘除数不能为0‘); return this.num_A / this.num_B; }; var OperateFactory = function(){}; OperateFactory.createOperate = function(operate){ var oper; switch(operate){ case "+": oper = new OperationAdd(); break; case "-": oper = new OperationSub(); break; case "*": oper = new OperationMul(); break; case "/": oper = new OperationDiv(); break; } return oper; }; //调用: var oper = OperateFactory.createOperate(‘+‘); oper.num_A = 1; oper.num_B = 2; alert(oper.getResult()); //3
js完全照搬C#的模式显然太复杂,根据js语言的特性实现一个:
var Operation = function(){ this.num_A = this.num_B = 0; }; Operation.prototype.OperationAdd = function(){ return this.num_A + this.num_B; }; Operation.prototype.OperationSub = function(){ return this.num_A - this.num_B; }; Operation.prototype.OperationMul = function(){ return this.num_A * this.num_B; }; Operation.prototype.OperationDiv = function(){ if(this.num_B == 0) throw new Error(‘除数不能为0‘); return this.num_A / this.num_B; }; //策略 var operateStrategy = { ‘+‘:‘OperationAdd‘, ‘-‘:‘OperationSub‘, ‘*‘:‘OperationMul‘, ‘/‘:‘OperationDiv‘ }; //工厂 var OperateFactory = (function(){ var oper; return function(operate){ //只实例化一次 oper = oper || new Operation(); return { setNumber:function(a,b){ oper.num_A = a; oper.num_B = b; }, getResult:function(){ var fName = operateStrategy[operate], fun = oper[fName] || function(){}; return fun.call(oper); } }; }; })(); //调用: var oper = OperateFactory(‘+‘); oper.setNumber(1,3); alert(oper.getResult()); oper = OperateFactory(‘*‘); oper.setNumber(2,3); alert(oper.getResult());
总结:如果日后需要扩展计算器的功能,我们需要维护2个地方,一个是为Operation的原型添加所需要的新功能,另一个是维护策略对象:operateStrategy, 基本上不需要修改程序的内部方法,而是为它增加扩展,基本符合开放-封闭原则。
时间: 2024-10-15 13:42:36