【C#】简单工厂模式
来源:互联网 发布:js div禁止点击 编辑:程序博客网 时间:2024/04/23 19:11
- using System;
- // 可维护,可复用,可扩展,灵活性好
- namespace 简单工厂模式
- {
- // 运算类
- public class Operation
- {
- private double numberA = 0;
- public double NumberA {
- get {
- return numberA;
- }
- set {
- numberA = value;
- }
- }
- private double numberB = 0;
- public double NumberB {
- get {
- return numberB;
- }
- set {
- numberB = value;
- }
- }
- public virtual double GetResult()
- {
- double testResult = 0;
- return testResult;
- }
- }
- // 加法
- class OperationAdd:Operation
- {
- public override double GetResult ()
- {
- double addResult = 0;
- addResult = NumberA + NumberB;
- return addResult;
- }
- }
- // 减法
- class OperationSub :Operation
- {
- public override double GetResult ()
- {
- double subResult = 0;
- subResult = NumberA - NumberB;
- return subResult;
- }
- }
- // 乘法
- class OperationMul :Operation
- {
- public override double GetResult ()
- {
- double mulResult = 0;
- mulResult = NumberA * NumberB;
- return mulResult;
- }
- }
- // 除法
- class OperationDiv :Operation
- {
- public override double GetResult ()
- {
- double divResult = 0;
- divResult = NumberA / NumberB;
- return divResult;
- }
- }
- 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;
- }
- }
- class MainClass
- {
- public static void Main (string[] args)
- {
- Operation oper;
- oper = OperationFactory.createOperate("/");
- oper.NumberA = 12;
- oper.NumberB = 2;
- Console.WriteLine (oper.GetResult());
- }
- }
- }
0 0
- C#-简单工厂模式
- 【C#】简单工厂模式
- 【C#】简单工厂模式
- 简单工厂模式(SimpleFactory-C#)
- 简单工厂模式(c++)
- 【C++】Chapter1:简单工厂模式
- Objective C--简单工厂模式
- Objective C 简单工厂模式
- 简单工厂模式(C#)
- Objective C--简单工厂模式
- C#--简单工厂设计模式
- 设计模式 - 简单工厂模式(C++)
- C#--设计模式之简单工厂模式
- 设计模式之简单工厂,工厂方法模式(c++)
- 工厂模式-简单工厂
- 工厂模式-简单工厂
- 简单工厂模式C语言实现
- 简单工厂模式(C语言实现)
- 【maven】 在 MyEcplise上使用maven搭建Web项目
- 商品进行 “支付宝 ” 结算(接口调用) controller层
- FastDFS安装、配置、部署(二)-Tracker配置详解
- Android Studio新功能解析,你真的了解Instant Run吗?(转)
- Jenkins进阶系列之——05FTP publisher plugin插件
- 【C#】简单工厂模式
- 【翻译整理】Web API 2 返回值
- [从头读历史] 第255节 孔子和他的《春秋》 [BC657至BC598]
- FastDFS安装、配置、部署(三)-Storage配置详解
- 一深入理解Nginx的 模块化 ,全局观
- Jenkins进阶系列之——07更改Jenkins的主目录
- 【C#】Action委托类型
- Android开发请求网络方式详解
- synchronized用法