20.策略者模式(Stragety Pattern)
2015-08-14 10:35
99 查看
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); } } }
相关文章推荐
- Dll 导出 别人总结
- Wlan_station例程测试
- Xamarin.Forms之Resx
- android 检测常用的手势
- Java基础——网络编程(二)
- Ubuntu学习4_Ubuntu默认防火墙安装、启用、配置、端口、查看状态相关信息
- C++序列操作之std::partition
- poj 3468__A Simple Problem with Integers(区间求和,成段更新)
- Git教程
- printf交替使用
- ASP.NET验证控件应用实例与详解。
- HDU 2222 Keywords Search(AC自动机)
- HDU 1255 覆盖的面积 (线段树 + 离散化 + 扫描线)
- Nginx+Resin+JSP+Memcached+MySQL安装配置整合
- POJ 1751 Highways(最小生成树prim)
- String StringBuffer和StringBuilder的区别
- LightOJ 1258 Making Huge Palindromes (回文&KMP)
- 在flash builder里全局搜索里如何“全字匹配”搜索
- js正则控制文本框只能输入数字以及小数
- 管式超滤系统:管式超滤膜技术在饮水净化中作用解析