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 - ) > ? (income - ) * 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);
}
}
}

20.策略者模式(Stragety Pattern)的更多相关文章

  1. C#设计模式(20)——策略者模式(Stragety Pattern)

    一.引言 前面主题介绍的状态模式是对某个对象状态的抽象,而本文要介绍的策略模式也就是对策略进行抽象,策略的意思就是方法,所以也就是对方法的抽象,下面具体分享下我对策略模式的理解. 二.策略者模式介绍 ...

  2. c#设计模式之策略者模式(Strategy Pattern)

    场景出发 假设存在如下游戏场景: 1:角色可以装备木剑,铁剑,魔剑3种装备,分别对怪物造成20HP,50HP,100HP伤害(未佩戴装备则无法攻击); 2角色可以向怪物攻击,一次攻击后损失角色所佩戴装 ...

  3. C#设计模式之二十策略模式(Stragety Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第七个模式,该模式是[策略模式],英文名称是:Stragety Pattern.在现实生活中,策略模式的例子也非常常见,例如,在一个 ...

  4. C#设计模式之十九策略模式(Stragety Pattern)【行为型】

    一.引言 今天我们开始讲“行为型”设计模式的第七个模式,该模式是[策略模式],英文名称是:Stragety Pattern.在现实生活中,策略模式的例子也非常常见,例如,在一个公司中,会有各种工作人员 ...

  5. 【20】策略者模式(Strategy Pattern)

    一.引言 本文要介绍的策略模式也就是对策略进行抽象,策略的意思就是方法,所以也就是对方法的抽象,下面具体分享下我对策略模式的理解. 二.策略者模式介绍 2.1 策略模式的定义 在现实生活中,策略模式的 ...

  6. 设计模式(一):“穿越火线”中的“策略模式”(Strategy Pattern)

    在前段时间呢陆陆续续的更新了一系列关于重构的文章.在重构我们既有的代码时,往往会用到设计模式.在之前重构系列的博客中,我们在重构时用到了“工厂模式”.“策略模式”.“状态模式”等.当然在重构时,有的地 ...

  7. 设计模式 - 策略模式(Strategy Pattern) 具体解释

    策略模式(Strategy Pattern) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879 本文版权全 ...

  8. HeadFirst设计模式读书笔记(1)-策略模式(Strategy Pattern)

    策略模式(Strategy Pattern): 定义了了算法簇,分别封装起来,让它们之间可以相互替换,此模式让算法的变化独立于使用算法的客户端. 第一个设计原则:找出应用中可能需要变化之处,把他们独立 ...

  9. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

随机推荐

  1. div 加滚动条

    div 加滚动条的方法: <div style="position:absolute; height:400px; overflow:auto"></div> ...

  2. GFF3格式

    GFF3是GFF注释文件的新标准.文件中每一行为基因组的一个属性,分为9列,以TAB分开. 依次是: 1. reference sequence:参照序列 指出注释的对象.如一个染色体,克隆或片段.可 ...

  3. Python自动化装饰器问题解疑

    问题一 到底是怎么执行的? import time def timer(timeout=0): def decorator(func): def wrapper(*args, **kwargs): # ...

  4. 9. JEB 1.5插件编写一

    2.0之后好像新增加了很多API,有所区别 本文采用Java+Eclipse作为开发环境 插件支持的语言  文档: jeb-1.5/doc/index.html 一.Hello World工程 1. ...

  5. Droid4x设置代理抓包

    Droid4x也是基于virtualbox+x86架构的   代理设置 设置->WIFI->鼠标按住WiredSSID选项不放->修改网络->显示高级选项->代理-> ...

  6. Java中流的概念

    http://wapwenku.baidu.com/view/04714847b307e87101f69656.html?ssid=0&from=1086k&uid=0&pu= ...

  7. iOS开源App整理

    http://duxinfeng.com/2015/07/14/iOS%E5%BC%80%E6%BA%90App%E6%95%B4%E7%90%86/ http://blog.csdn.net/dux ...

  8. Java Statement和PreparedStatement性能测试(转)

    本文转载自http://blog.csdn.net/liubo5005/article/details/7239935 先上代码: import java.sql.Connection; import ...

  9. spring mvc 重定向加传参

    常用: ModelAndViewi: return new ModelAndView("redirect:/toList");  或者 ii:return "redire ...

  10. How to take partial screenshot with Selenium WebDriver in python

    from selenium import webdriver from PIL import Image fox = webdriver.Firefox() fox.get('http://stack ...