zoukankan      html  css  js  c++  java
  • 大话设计模式-工厂方法模式(8)

    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;
            private double numberB = 0;
    
            public double NumberA
            {
                get { return numberA; }
                set { numberA = value; }
            }
    
            public double NumberB
            {
                get { return numberB; }
                set { numberB = value; }
            }
    
            public virtual double 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)
                {
                    throw new 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.工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,

         工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。你想要加功能,本来是改工厂类的,而现在是修改客户端.

  • 相关阅读:
    .Net加密保护工具分析介绍
    正则表达式
    easyui datagrid toolbar 添加搜索框
    为jQuery-easyui的tab组件添加右键菜单功能
    苹果系统无法启动的解决步骤
    解决MVC4发布在IIS7后,路径无法访问.apk文件的解决方法
    C#微信公众平台开发者模式开启代码
    ios svn无法连接xp或者win7系统svn的解决方法
    SQL SERVER 2008 R2 SP1更新时,遇上共享功能更新失败解决方案
    easyui textarea回车导致datagrid 数据无法展示的问题
  • 原文地址:https://www.cnblogs.com/rinack/p/5275978.html
Copyright © 2011-2022 走看看