• 设计模式学习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();
            }
        }
    }
  • 相关阅读:
    php安装扩展的几种方法
    navicat连接linux系统中mysql-错误:10038
    linux下报错bash: service: command not found
    linux配置防火墙和重启防火墙
    linux 环境安装
    匿名函数
    workman的学习总结
    xampp/apache启动失败解决方法
    Linux 查看IP
    慢查询日志
  • 原文地址:https://www.cnblogs.com/springword/p/6421372.html
Copyright © 2020-2023  润新知