工厂方法从建模到代码演练

1.用vs2013建模(计算器实现)
选择建模项目,然后现在添加类图;添加一个包是为了让我们设计的类图后期自动生成代码时类就在统一的命名空间之下
[img]http://dl2.iteye.com/upload/attachment/0110/5497/c9ef38e6-8d6f-3fe3-8029-6e71d2669c9f.jpg[/img]
自动生成代码的类

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.当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是 代理者这一信息局部化的时候。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值