首页 / 设计模式 / 大话设计模式-工厂方法模式(8)
大话设计模式-工厂方法模式(8)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了大话设计模式-工厂方法模式(8),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3452字,纯文字阅读大概需要5分钟。
内容图文
![大话设计模式-工厂方法模式(8)](/upload/InfoBanner/zyjiaocheng/1303/f75ab3e2d38348838266f3961218aa96.jpg)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { interface IFactory { Operation CreateOperation(); } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { class AddFactory:IFactory { public Operation CreateOperation() { return new OperationAdd(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { class SubFactory:IFactory { public Operation CreateOperation() { return new OperationSub(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { class DivFactory:IFactory { public Operation CreateOperation() { return new OperationDiv(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { class MulFactory:IFactory { public Operation CreateOperation() { return new OperationMul(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { /// <summary> /// 抽象父类 /// </summary> public class Operation { private double numberA = 0; privatedouble numberB = 0; publicdouble NumberA { get { return numberA; } set { numberA = value; } } publicdouble NumberB { get { return numberB; } set { numberB = value; } } publicvirtualdouble GetResult() { double result = 0; return result; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { public class OperationAdd : Operation { public override double GetResult() { double result = 0; result = NumberA + NumberB; return result; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { public class OperationDiv:Operation { public override double GetResult() { double result = 0; if (NumberB == 0) { thrownew Exception("除数不能为0"); } result = NumberA / NumberB; return result; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { public class OperationMul:Operation { public override double GetResult() { double result = 0; result = NumberA * NumberB; return result; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { public class OperationSub:Operation { public override double GetResult() { double result = 0; result = NumberA - NumberB; return result; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MethodsFactory { class Program { static void Main(string[] args) { Console.Write("请输入数字A:"); string numberA = Console.ReadLine(); Console.Write("请输入数字B:"); string numberB = Console.ReadLine(); IFactory operFactory = new AddFactory(); Operation oper = operFactory.CreateOperation(); oper.NumberA = Convert.ToDouble(numberA); oper.NumberB = Convert.ToDouble(numberB); double result = oper.GetResult(); Console.Write("计算结果是:" + result.ToString()); Console.ReadLine(); } } }
工厂方法模式
工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个工厂。工厂方法使一个类的实例化延迟到其子类.
简单工厂与工厂方法
1.简单工厂模式的最大优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。
2.工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,
工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。你想要加功能,本来是改工厂类的,而现在是修改客户端.
原文:http://www.cnblogs.com/rinack/p/5275978.html
内容总结
以上是互联网集市为您收集整理的大话设计模式-工厂方法模式(8)全部内容,希望文章能够帮你解决大话设计模式-工厂方法模式(8)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。