首页 > 其他 > 详细

策略模式

时间:2014-03-01 04:25:08      阅读:426      评论:0      收藏:0      [点我收藏+]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SheJiMoShi
{
public interface IStrategy
{
void Option();
}

public class GivenGreenLight : IStrategy
{
public void Option()
{
Console.WriteLine("求吴国太开个绿灯,放行!");
}
}
/// <summary>
///
/// </summary>
public class ZhaoYun
{
static void Main(string[] args)
{
Console.WriteLine("//刚刚到吴国的时候拆第一个");
Context context;
context = new Context(new BackDoor());
context.operate();
Console.WriteLine("//刘备乐不思蜀了,拆第二个了");
context = new Context(new GivenGreenLight());
context.operate();
Console.WriteLine("//孙权的小兵追了,咋办?拆第三个");
context = new Context(new BlockEnemy ());
context.operate();
Console.Read();
}
}
/// <summary>
///
/// </summary>
public class BackDoor : IStrategy
{
public void Option()
{
Console.WriteLine("找乔国老帮忙,让吴国太给孙权施加压力");
}
}
/// <summary>
///
/// </summary>
public class BlockEnemy : IStrategy
{
public void Option()
{
Console.WriteLine("孙夫人断后,挡住追兵");
}
}

public class Context
{
private IStrategy straegy;
public Context(IStrategy strategy)
{
this.straegy = strategy;
}
//使用计谋了,看我出招了
public void operate()
{
this.straegy.Option();
}
}
/*
*问题来了:赵云实际不知道是那个策略呀,他只知道拆第一个锦囊,
*而不知道是BackDoor 这个妙计,咋办? 似乎这个策略模式已经把计谋名称写出来了
*
* 错!BackDoor 、GivenGreenLight 、BlockEnemy 只是一个代码,你写成first 、second、
third ,没人会说你错!
*
* 策略模式的好处就是:体现了高内聚低耦合的特性呀,缺点嘛,这个那个,我回去再查查
*/
}

策略模式,布布扣,bubuko.com

策略模式

原文:http://www.cnblogs.com/lifesteven/p/3574075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!