策略模式是一种对象的行为型模式,定义一系列算法,并将每一个算法封装起来,并让它们可以相互替换。策略模式比算法独立于使用它的客户而变化,其目的是将行为和环境分隔,当出现新的行为时,只需要实现新的策略类。

优点:

1)另一种子类化方法。

2)在类自身中定义了每一个行为,这样就减少了条件语句。

3)更容易扩展模型。在不对应用程序进行代码修改的情况下,使该模式具有新的行为。

使用场景:

1)许多相关类只是在行为方面有所区别。

2)许多算法的不同变体。

3)算法使用客户端未知的数据。

Strategy 模式

public class Hand
{
public const int HANDVALUE_GUU = ;//石头
public const int HANDVALUE_CHO = ;//剪刀
public const int HANDVALUE_PAA = ;//布 private int handvalue;
private Hand(int handvalue)
{
this.handvalue = handvalue;
} public static Hand[] hands =
{
new Hand(HANDVALUE_GUU),
new Hand(HANDVALUE_CHO),
new Hand(HANDVALUE_PAA)
}; /// <summary>
/// 从值获得对象实例
/// </summary>
/// <param name="handvalue"></param>
/// <returns></returns>
public static Hand GetHand(int handvalue)
{
return hands[handvalue];
}
}
public interface Strategy
{
Hand NextHand();
}
public class WinningStrategy : Strategy
{
private bool won = false;
private Hand prevHand;
private Random random = new Random(); public Hand NextHand()
{
if (!won)
{
prevHand = Hand.GetHand(random.Next());
}
Console.WriteLine("调用了WinningStrategy策略");
return prevHand;
}
}
public class ProbStrategy : Strategy
{
public Hand NextHand()
{
int handvalue = ;
//省略具体实现 Console.WriteLine("调用了ProbStrategy策略");
return Hand.GetHand(handvalue);
}
}
public class Player
{
private string name;
private Strategy strategy;
public Player(string name, Strategy strategy)
{
this.name = name;
this.strategy = strategy;
} /// <summary>
/// 向战略请示手势
/// </summary>
/// <returns></returns>
public Hand NextHand()
{
return strategy.NextHand();
}
}
class Program
{
static void Main(string[] args)
{
//策略模式
while (true)
{
Console.WriteLine("/r/n请选择:1、WinningStrategy策略游戏;2、ProbStrategy策略游戏...");
string line = Console.ReadLine();
if (line.Equals(""))
{
Strategy.Strategy strategy = new WinningStrategy();
Player player = new Player("abc", strategy);
player.NextHand();
}
else if (line.Equals(""))
{
Strategy.Strategy strategy = new ProbStrategy();
Player player = new Player("abc", strategy);
player.NextHand();
}
}
}
}

23种设计模式之策略模式(Strategy)的更多相关文章

  1. 二十四种设计模式:策略模式(Strategy Pattern)

    策略模式(Strategy Pattern) 介绍定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换.本模式使得算法的变化可独立于使用它的客户. 示例有一个Message实体类,对它的操作有 ...

  2. 【Unity3D与23种设计模式】策略模式(Strategy)

    GoF中定义: "定义一组算法,并封装每个算法,让它们之间可以彼此交换使用. 策略模式让这些算法在客户端使用它们时能更加独立." 游戏开发过程中 不同的角色会有不同的属性计算方法 ...

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

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

  4. java的23种设计模式之建造者模式

    场景和本质 场景 本质 案例 原理 应用场景 场景和本质 场景 我们要建造一个复杂的产品.比如:神州飞船,Iphone.这个复杂的产品的创建.有这样一个问题需要处理:装配这些子组件是不是有个步骤问题? ...

  5. 实践GoF的23种设计模式:建造者模式

    摘要:针对这种对象成员较多,创建对象逻辑较为繁琐的场景,非常适合使用建造者模式来进行优化. 本文分享自华为云社区<[Go实现]实践GoF的23种设计模式:建造者模式>,作者: 元闰子. 简 ...

  6. 反馈法学习设计模式(一)——策略模式Strategy Pattern

    简介(Introduction) 之前学习Java8实战时,遇到一个很好的策略模式示例.便想着借着这个示例结合反馈式的方法来,学习策略设计模式,也以便后面反复琢磨学习. 首先我们通过练习,逐步写出符合 ...

  7. JAVA设计模式之策略模式 - Strategy

    在策略模式(Strategy Pattern)中,一个类的行为或其算法可以在运行时更改.这种类型的设计模式属于行为型模式. 在策略模式中,我们创建表示各种策略的对象和一个行为随着策略对象改变而改变的 ...

  8. 8.6 GOF设计模式四: 策略模式… Strategy Pattern

    策略模式… Strategy Pattern  在POS系统中,有时需要实行价格优惠, 该如何处理?  对普通客户或新客户报全价  对老客户统一折扣5%  对大客户统一折扣10%  注:课件 ...

  9. JAVA开发的23种设计模式之 --- 桥接模式

    桥接模式 概述:将抽象部分与他的实现部分分离,这样抽象化与实现化解耦,使他们可以独立的变化.如何实现解耦的呢,就是通过提供抽象化和实现化之间的桥接结构.    应用场景        实现系统可能有多 ...

随机推荐

  1. VS2010 正则批量替换头文件路径

        最近在项目实践中,需要统一对工程头文件进行重构,具体要求是,将之前 #include "../../abc/def.h" 类似的头文件引用路径 替换为#include &q ...

  2. [转]调试AngularJS应用

    原文链接:Debugging AngularJS Apps from the Console 当我们开发AngularJS应用的时候,我们想在Chrome/FF/IE控制台调试隐藏在应用中的数据和服务 ...

  3. UNIX环境编程学习笔记(27)——多线程编程(二):控制线程属性

    lienhua342014-11-09 1 线程属性概括 POSIX 线程的主要属性包括 scope 属性.detach 属性.堆栈地址.堆栈大小.优先级.在头文件 pthread.h 中定义了结构体 ...

  4. 在Eclipse中查看Javadoc文档

    当我们需要查看JDK中类的API介绍时,通常采用的方式是直接查看离线文档或者某些网站提供的在线文档.如下图: 而本文档最终达到的效果是,不需要切换出eclipse,直接在eclipse中查看JDK的J ...

  5. scala中获取Map中key和value的方法

    val scores=Map("Alice"->10,"Bob"->3,"Cindy"->8) // 获取所有的key v ...

  6. Springboot @webfilter @order filter过滤器

    我们使用@WebFilter注解的时候发现注解里面没有提供可以控制执行顺序的参数 @WebFilter 的属性 属性名 类型 描述 filterName String 指定过滤器的 name 属性,等 ...

  7. TIMEOUT HANDLING WITH HTTPCLIENT

    https://www.thomaslevesque.com/2018/02/25/better-timeout-handling-with-httpclient/ The problem If yo ...

  8. RF实现多次失败重跑结果合并的基础方法和优化方法

    实现思路:通过分次执行失败案例重跑,然后通过结果文件合并命令实现多次失败重跑结果文件的合并,并输出合并后的log和report文件: 说明:具体失败案例重跑命令和结果文件合并命令请参考本博客其他相关章 ...

  9. RabbitMq 之简单队列

    简单队列类似于我们的生产者,消费者, 一个生产者,对应一个消费者. 直接上代码: package com.j1.rabbitmq.simple; import com.j1.rabbitmq.util ...

  10. PHP 图片 平均分割

    $filename = 'D://WWW/1.jpg'; $p = 5; // Get new sizes list($width, $height) = getimagesize($filename ...