码迷,mamicode.com
首页 > 其他好文 > 详细

简单工厂实现

时间:2015-01-30 01:20:44      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 简单工厂
{
    class Program
    {
        static void Main(string[] args)
        {
           Calculation Fu =  Realization.CreateInstance("+");
           Fu.Num1 = 1;
           Fu.Num2 = 2;
           Console.WriteLine(Fu.CalculationMethod());
        }
    }

    /// <summary>
    /// 计算器抽象类
    /// </summary>
    public abstract class Calculation
    {
        public int Num1 { get; set; }
        public int Num2 { get; set; }
        //抽象计算方法
        public abstract int CalculationMethod();
    }


    /// <summary>
    /// 加法类abstractCalculation
    /// </summary>
    public class JiaFa : Calculation {

        public override int CalculationMethod()
        {
            return Num1 +Num2 ;
        }
    }

    /// <summary>
    /// 减法类abstractCalculation
    /// </summary>
    public class JianFa : Calculation
    {

        public override int CalculationMethod()
        {
            return Num1 - Num2;
        }
    }

    /// <summary>
    /// 实现类(实现加减乘除)
    /// </summary>
    public class Realization
    {
        public static Calculation CreateInstance(string operate)
        {
            switch (operate)
            {
                case "+":
                    return new JiaFa();
                case "-":
                    return new JianFa();
                default:
                    throw new Exception("无效操作");
            }
        }
    }
}

 

简单工厂实现

标签:

原文地址:http://www.cnblogs.com/zhangruiBlog/p/4261351.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!