选择建模项目,然后现在添加类图;添加一个包是为了让我们设计的类图后期自动生成代码时类就在统一的命名空间之下
自动生成代码的类
2 运算类
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class Operation { public virtual Double NumberA { get; set; } public virtual Double NumberB { get; set; } public virtual Double GetResult() { throw new System.NotImplementedException(); } } }
3.继承实现计算的具体4个类
加法类
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class OperationAdd : Operation { public override Double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } }
除法类
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class OperationDiv : Operation { public override Double GetResult() { double result = 0; if (NumberB == 0) throw new Exception("除数不能为0。"); result = NumberA / NumberB; return result; } } }
乘法类
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class OperationMul : Operation { public override Double GetResult() { double result = 0; result = NumberA * NumberB; return result; } } }
减法类
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class OperationSub : Operation { public override Double GetResult() { double result = 0; result = NumberA - NumberB; return result; } } }
定义计算工厂接口
契约:工厂必须具有返回计算的功能
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public interface IFactory { Operation Operation { get;set; } Operation CreateOperation(); } }
乘法工厂
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MulFactory : IFactory { public virtual Operation CreateOperation() { return new OperationMul(); } } }
除法工厂
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class DivFactory : IFactory { public virtual Operation CreateOperation() { return new OperationDiv(); } } }
加法工厂
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class OperationAdd : Operation { public override Double GetResult() { return new OperationAdd(); } } }
加法工厂
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool // Changes to this file will be lost if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace FactoryMethod { using System; using System.Collections.Generic; using System.Linq; using System.Text; public class AddFactory : IFactory { public virtual Operation CreateOperation() { return new OperationAdd(); } } }
5.调用试用一下
static void Main(string[] args) { IFactory operFactory = new AddFactory(); Operation oper = operFactory.CreateOperation(); oper.NumberA = 1; oper.NumberB = 2; double result=oper.GetResult(); Console.WriteLine(result); Console.Read(); }
意图:
定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。
适用:
1.当一个类不知道它所必须创建的对象的类的时候。
2.当一个类希望由它的子类来指定它所创建的对象的时候。
3.当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是 代理者这一信息局部化的时候。