zoukankan      html  css  js  c++  java
  • 20.策略者模式(Stragety Pattern)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication3
    {
        /// <summary>
        /// 策略模式是针对一组算法,将每个算法封装到具有公共接口的独立的类中,
        /// 从而使它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。
        /// </summary>
        class Program
        {
            static void Main(string[] args)
            {
                // 个人所得税方式
                InterestOperation operation = new InterestOperation(new PersonalTaxStrategy());
                Console.WriteLine("个人支付的税为:{0}", operation.GetTax(5000.00));
    
                // 企业所得税
                operation = new InterestOperation(new EnterpriseTaxStrategy());
                Console.WriteLine("企业支付的税为:{0}", operation.GetTax(50000.00));
                Console.Read();
            }
        }
    
        // 所得税计算策略
        public interface ITaxStragety
        {
            double CalculateTax(double income);
        }
    
    
        // 个人所得税
        public class PersonalTaxStrategy : ITaxStragety
        {
            public double CalculateTax(double income)
            {
                return income * 0.12;
            }
        }
    
        // 企业所得税
        public class EnterpriseTaxStrategy : ITaxStragety
        {
            public double CalculateTax(double income)
            {
                return (income - 3500) > 0 ? (income - 3500) * 0.045 : 0.0;
            }
        }
    
        /// <summary>
        /// 选择方法
        /// </summary>
        public class InterestOperation
        {
            private ITaxStragety m_strategy;
            public InterestOperation(ITaxStragety strategy)
            {
                this.m_strategy = strategy;
            }
    
            public double GetTax(double income)
            {
                return m_strategy.CalculateTax(income);
            }
        }
    }
  • 相关阅读:
    关于位运算(转)
    计蒜客第三场
    数组与指针
    计蒜客第二场
    指针概念
    爬楼梯(动态规划)
    线性表基本操作的实现(合并)
    4123=喵帕斯之天才少女
    3889=神奇的函数
    1586=计算组合数
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4729263.html
Copyright © 2011-2022 走看看