• 设计模式学习1:简单工厂模式实现加减乘除等运算


    设计模式练习:简单工厂模式

      抽象基类(也可以用接口等实现):运算的基类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 简单工厂模式_四则运算
    {
        abstract class Operation
        {
            protected double a;
            protected double b;
            public void SetValue(double a,double b)
            {
                this.a = a;
                this.b = b;
            }
            public abstract double GetResult();
        }
    }

      派生类:加减乘除运算

    1.加法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 简单工厂模式_四则运算
    {
        class Jia:Operation
        {
            public override double GetResult()
            {
                return a + b;
            }
        }
    }

    2.减法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 简单工厂模式_四则运算
    {
        class Jian:Operation
        {
            public override double GetResult()
            {
                return a - b;
            }
        }
    }

    3.乘法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 简单工厂模式_四则运算
    {
        class Cheng:Operation
        {
            public override double GetResult()
            {
                return a * b;
            }
        }
    }

    4.除法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 简单工厂模式_四则运算
    {
        class Chu:Operation
        {
            public override double GetResult()
            {
                if(b!=0)
                {
                    return a / b;
                }
                else
                {
                    throw new Exception("除数为0");
                }
            }
        }
    }

    5.等等。。。。

      工厂类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 简单工厂模式_四则运算
    {
        class FactoryOperation
        {
            
            public static Operation GetOperation(double a,double b,string operation)
            {
                Operation value = null;
                if(operation=="+")
                {
                    value = new Jia();
                }
                else if(operation=="-")
                {
                    value = new Jian();
                }
    
                else if(operation=="*")
                {
                    value = new Cheng();
                }
    
                else if(operation=="/")
                {
                    value = new Chu();
                }
                value.SetValue(a, b);
                return value;
            }
        }
    }

      主程序调用:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 简单工厂模式_四则运算
    {
        class Program
        {
            static void Main(string[] args)
            {
                var x = FactoryOperation.GetOperation(3, 0, "/");
                double result = x.GetResult();
                Console.WriteLine(result);
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    Linux 升级内核开启 TCP BBR 有多大好处
    rbd-mirror配置指南-单向备份
    python调用dll方法
    Python调用Google翻译
    Python Sleep休眠函数
    用Python监听鼠标和键盘事件
    python编码(七)
    SceneControl+AE+鼠标滚轮缩放
    基于SceneControl单击查询功能的实现
    基于SceneControl的三维GIS开发
  • 原文地址:https://www.cnblogs.com/springword/p/6421372.html
Copyright © 2020-2023  润新知