返回目录

策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。而对于客户端(UI)来说,可以通过IOC再配合工厂模块,实现动态策略的切换,策略模块通常于一个抽象策略对象(interface or abstract class),多个具体策略对象(implement class )和一个调用策略的入口组成。

何时能用到它?

对于UI来说,可能一个功能会有多种实现方式,而且实现方式可能还会有扩展,例如,一个ATM机,它目前支持工行,建行,家行,以后可能又出现了占占银行,而这时,ATM机的客户端程序需要扩展一个占占银行的实现策略,这时,使用策略模式就可以实现了,它不需要修改之前的代码,只要扩展一个占占银行实现类即可。

策略模式的结构图

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmwAAAELCAIAAADr7k0NAAAgAElEQVR4nO3dW3Ab150m8H6aylbtRtr3TMy8+SGJ5bysa2Ys01OV3ZrZ8VrOztqeycSmvLblWd/k+CLHUXSJHSkWVSG1KsWyLYu+lEa+xJa9lsUkdsRkYkmUlZgqiZIgESBAECBBkAD6gr4COL0PB2w2GrdGg0A3ur9fnUJRkCy3ff7nfDx90IeMDgAAAI4wbl8AAABAr0KIAgAAOIQQBQAAcAghCgAA4BBCFAAAwCGEKAAAgEMIUQAAAIcQogAAAA4hRAEAABxCiAIAADiEEAUAAHAIIQoAAOAQQhQAAMAhhCgAAIBDCFEAAACHEKIAAAAOIUQBAAAcQogCAAA4hBAFAABwCCEKAADgEEIUAADAIYQoAACAQwhRAAAAhxCiAAAADiFEAQAAHEKIAgAAOIQQBQAAcAghCgAA4BBCFAAAwCGEKAAAgEMIUQAAAIcQogAAAA4hRHvA5s2bJyYm8Nq7r25XEAB0CkLU63bv3r179+5cLjcxMYHXXnxdt27d2NiY23UEAB2BEPW6K1euzM/Pu30VAABQA0LU63bv3j06Olrzt3iOPXv21OFDBw7sH9z2kyefePx+tC63vXt2Htg/+N67b1248GW9Huzv7/f3ShR16HqzU4fQIQhRr6u3Er1y+eLePTuPHhm5cOHL6Wgow6UEJWu0vJKr1di8wuZVNq+yorVxNZuk8uWmVTeBNnml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AhhCgAAIBDCFEAAACHEKIAAAAOIUQBAAAcQogCAAA4hBAFAABwCCEKAADgEEIUAADAIYQoAACAQwhRAAAAhxCiAAAADiFEAQAAHEKIAgAAOIQQBQAAcAghCgAA4BBCFAAAwCGEKAAAgEMIUQAAAIcQogAAAA4hRAEAABxCiAIAADiEEAUAAHAIIQoAAOAQQhQAAMAhhCgAAIBDCFEAAACHEKIAAAAOIUQBAAAcQogCAAA4hBAFAABwCCEKAADgEEIUAADAIYQoAACAQwhRAAAAh/4/uK8nEyYoOGQAAAAASUVORK5CYII=" alt="" />

策略模式实现说明

IStrategy:抽象策略对象

ConcreteStrategyA:具体策略A

ConcreteStrategyB:具体策略B

ConcreteStrategyC:具体策略C

Context:客户端调用的入口

策略模式的C#实现

#region 策略模式

    // The classes that implement a concrete strategy should implement this
// The context class uses this to call the concrete strategy
interface IStrategy
{
void Execute();
} // Implements the algorithm using the strategy interface
class ConcreteStrategyA : IStrategy
{
public void Execute()
{
Console.WriteLine("Called ConcreteStrategyA.Execute()");
}
} class ConcreteStrategyB : IStrategy
{
public void Execute()
{
Console.WriteLine("Called ConcreteStrategyB.Execute()");
}
} class ConcreteStrategyC : IStrategy
{
public void Execute()
{
Console.WriteLine("Called ConcreteStrategyC.Execute()");
}
} // Configured with a ConcreteStrategy object and maintains a reference to a Strategy object
class Context
{
IStrategy strategy; // Constructor
public Context(IStrategy strategy)
{
this.strategy = strategy;
} public void Execute()
{
strategy.Execute();
}
} #endregion

返回目录

说说设计模式~策略模式(Strategy)的更多相关文章

  1. [.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型)

    [.net 面向对象程序设计深入](26)实战设计模式——策略模式 Strategy (行为型) 1,策略模式定义 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模 ...

  2. 设计模式--策略模式(strategy)

    1.策略模式(strategy ['strætədʒi]) 我的理解是:方案候选模式 (反正关键就是有很多的候选,哈哈) 看了很多例子,都是在说鸭子的,那个例子很好,在这里可以看 他们生产鸭子,我们就 ...

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

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

  4. java设计模式 策略模式Strategy

    本章讲述java设计模式中,策略模式相关的知识点. 1.策略模式定义 策略模式,又叫算法簇模式,就是定义了不同的算法族,并且之间可以互相替换,此模式让算法的变化独立于使用算法的客户.策略模式属于对象的 ...

  5. 设计模式——策略模式(Strategy Pattern)

    写在前面: 直接将书中的例子用来作为记录自己学习的成果,不知道这样好不好,如果给原作者带来什么不利的影响不妨告知一声,我及时删掉. UML图: 抽象策略:Strategy package com.cn ...

  6. C#设计模式——策略模式(Strategy Pattern)

    一.概述我们来实现一个企业的工资系统,该企业中不同级别的员工工资算法都不相同,针对该问题,最容易想到的莫过于在代码中堆积一大堆if…else…语句或者是switch…case…语句.如果该企业中不同级 ...

  7. 设计模式-策略模式Strategy以及消灭if else

    概述 如果在开发过程中,出现大量的if else或者switch case 语句,如果这些语句块中的代码并不是包含业务逻辑,只是单纯的分流方法,那么,每一个语句块中都是一个算法或者叫策略. 背景 比如 ...

  8. 设计模式---策略模式Strategy(对象行为型)

    1. 概述 策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换.策略模式让算法独立于使用它的客户而独立变化. 策略模式是对算法的封装,它把算法的责任和算法本身分割开,委派给不 ...

  9. 大话设计模式--策略模式 strategy -- C++实现实例

    1. 策略模式: 它定义了算法家族, 分别封装起来,使他们之间可以相互替换,此模式让算法变化, 不会影响到使用算法的客户. 用相同的方法调用不同的算法,减少各种算法类与使用算法类之间的耦合. 实例中策 ...

随机推荐

  1. Number()数据类型转换

    Number() 如果是 Boolean 值, 和.如果是数字值,只是简单的传入和返回.如果是 .如果是 undefined,返回NaN. 如果是字符串,遵循下列规则:    如果字符串中只包含数字( ...

  2. Android系统启动顺序

    Android是一个基于Linux的开源操作系统.x86(x86是一系列的基于intel 8086 CPU的计算机微处理器指令集架构)是linux内核部署最常见的系统.然而,所有的Android设备都 ...

  3. linux下 tar解压 gz解压 bz2等各种解压文件使用方法

    http://alex09.iteye.com/blog/647128 大致总结了一下linux下各种格式的压缩包的压缩.解压方法. .tar 解包:tar xvf FileName.tar 打包:t ...

  4. 【dubbo】消费者Consumer搭建

    一.consumer搭建(可以web/jar) 1.新建Maven项目,groupId:com.dubbo.consumer.demo artifactId:demo projectName:dubb ...

  5. 原生javaScript中使用Ajax实现异步通信

    AJAX本质就是在HTTP协议的基础上以异步的方式与服务器进行通信,所谓异步,就是指某段程序执行时不会阻塞其它程序执行,其表现形式为程序的执行顺序不依赖程序本身的书写顺序,相反则为同步. 以下开始简单 ...

  6. hdu 3065 AC自动机

    // hdu 3065 AC自动机 // // 题目大意: // // 给你n个短串,然后给你一个长串,问:各个短串在长串中,出现了多少次 // // 解题思路: // // AC自动机,插入,构建, ...

  7. LintCode 463 Sort Integer

    这个是O(n2)的排序的总结 /* bubble sort */public static void sortIntegers(int[] A) { // Write your code here i ...

  8. JavaScript 基础第二天

    一.前言 感觉昨天的内容确实是有点细碎.复杂.感觉是没有书上写的那么的细致而且有导入性,但是我还是喜欢这样只说干货.今天的内容将继续接着昨天最后的内容JS中的语言结构继续讲解并且重点讲解一下其中的内容 ...

  9. ps(process status)

    1.PS ps -a(all):显示现行终端机下的所有进程,包括其他用户的进程: ps -ax: 同时加上x参数会显示没有控制终端的进程. ps  -j:显示与作业有关的信息:会话ID.进程组ID等 ...

  10. Arcengine 中,创建色带

    1,利用combobox创建色带,首先draw private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)        { ...