zoukankan      html  css  js  c++  java
  • 设计模式学习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();
            }
        }
    }
  • 相关阅读:
    [重写] 与 [重载]
    [抽象类] 与 [接口]
    (转载)虚函数表实现机制(即多态性实验机制)
    总结:细节问题(C++篇)
    串流类(istrstream)输入行为的探讨
    比较:I/O成员函数getline() 与 get()(第二种用法)的用法异同
    JS控制HTML元素的显示和隐藏
    cocos2dx ios iap接入
    关于cocos2dx 2.x lua 中cocos studio 界面,读入时,无法触摸的几点总结
    lua 元表,监控变量赋值及访问,并自动保存
  • 原文地址:https://www.cnblogs.com/springword/p/6421372.html
Copyright © 2011-2022 走看看