• 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);
            }
        }
    }
  • 相关阅读:
    bShare一个强大的网页分享插件
    免费软件,到底是谁在获益?
    波西的小球——优化日志
    CSDN无故删除东西,强烈抗议 枯木
    网站排障分析常用的命令 枯木
    KVM在线迁移(动态迁移) 枯木
    RHEL6 KVM安装备忘 枯木
    MySQL备份和恢复具体实施(上) 枯木
    Nginx支持php相关配置 枯木
    关于RHEL6中ulimit的nproc限制 枯木
  • 原文地址:https://www.cnblogs.com/lgxlsm/p/4729263.html
Copyright © 2020-2023  润新知