zoukankan      html  css  js  c++  java
  • 设计模式-抽象工厂模式(二)

    计算器实例

    1.运算基类

     public class OperateBase
        {
            private float _number = 0.0f;
    
            private float _secondnumber = 0.0f;
            public float Number
            {
                get
                {
                    return _number;
                }
    
                set
                {
                    _number = value;
                }
            }
    
            public float Secondnumber
            {
                get
                {
                    return _secondnumber;
                }
    
                set
                {
                    _secondnumber = value;
                }
            }
    
            public virtual float ComputeResult() { return 0; }
        }

    2.运算结果类

    public class MulOperate:OperateBase
        {
            public override float ComputeResult()
            {
                return Number / Secondnumber;
            }
        }

    3.加法运算结果

      public  class AddOperate:OperateBase
        {
            public override float ComputeResult()
            {
                return Number+Secondnumber;
            }
        }

    4.减法运算结果

     public class SubOperate:OperateBase
        {
            public override float ComputeResult()
            {
                return Number - Secondnumber;
            }
        }

    5.乘法运算结果

     public class DivOperate:OperateBase
        {
            public override float ComputeResult()
            {
                return Number*Secondnumber;
            }
        }

    6.除法运算结果

    public class MulOperate:OperateBase
        {
            public override float ComputeResult()
            {
                return Number / Secondnumber;
            }
        }

    7.抽象工厂

     public interface IFactory
        {
            OperateBase CreateOperate();
        }

    8.加法实现

    public class AddFactory : IFactory
        {
            public OperateBase CreateOperate()
            {
                return new  AddOperate();
            }
        }

    9.减法实现

     public class SubFactory : IFactory
        {
            public OperateBase CreateOperate()
            {
                return new SubOperate();
            }
        }

    10.乘法实现

     public class DivFactory : IFactory
        {
            public OperateBase CreateOperate()
            {
                return new DivOperate();
            }
        }

    11.除法实现

      public class MulFactory : IFactory
        {
            public OperateBase CreateOperate()
            {
                return new MulOperate();
            }
        }

    12.调用

                IFactory facotry = new DivFactory();
                OperateBase add = facotry.CreateOperate();
                add.Number = 1;
                add.Secondnumber = 1;
                Console.WriteLine(add.ComputeResult());
        
    萌橙 你瞅啥?
  • 相关阅读:
    Windows Server 2012 R2的安装(GUI桌面版本)
    CentOS安装-(CentOS7)最小化安装
    【转】Makefile步步为营
    【转载】人工智能必备数学知识
    【萌新向】cartographer_ros最新安装指南 2019-12
    Ubuntu 16.04 允许进行vnc远程控制【转】
    UVW平台运动控制算法以及matlab仿真
    Java语言基础13—IO
    Java数组元素去重(不使用集合)
    java基础12—集合类
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/12082408.html
Copyright © 2011-2022 走看看