设计模式之禅--策略模式【STRATEGY  PATTERN 】

                                          策略模式【STRATEGY  PATTERN 】        

前言:

        对于设计模式,相信都不陌生,以前对于设计模式,只是在大学的时候,学过Head First上的内容,但也仅仅是学过而已。今天恰好看了《设计模式之禅》这本书,书上通过简单的例子,讲述了各种设计模式,通俗易懂,值得学习。废话不多说了,马上开始,Let's GO!

背景介绍:

        刘备要到江东娶老婆了,走之前诸葛亮给赵云(伴郎)三个锦囊妙计,说是按天机拆开解决棘手问题,嘿,还别说,真是解决了大问题,搞到最后是周瑜陪了夫人又折兵呀,那咱们先看看这个场景是什么样子的。 先说这个场景中的要素:三个妙计,一个锦囊,一个赵云,妙计是小亮同志给的,妙计是放置在锦囊里,俗称就是锦囊妙计嘛,那赵云就是一个干活的人,从锦囊中取出妙计,执行,然后获胜。

类图:





代码实现:

先定义一个接口类,作为一个策略接口。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    /// <summary>
    /// 首先定一个策略接口,这是诸葛亮老人家给赵云的三个锦囊妙计的接口
    /// </summary>
    public interface IStrategy
    {
        void Operate();
    }
}

然后是三个锦囊的类。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    /// <summary>
    /// 找乔国老帮忙,使孙权不能杀刘备
    /// </summary>
    public class Strategy_BackDoor:IStrategy
    {
        public void Operate()
        {
            Console.WriteLine("找乔国老帮忙,让吴国太给孙权施加压力");
        }
    }
}

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

namespace StrategyPattern
{
    /// <summary>
    /// 求吴国太开个绿灯
    /// </summary>
    public class Strategy_GreenLight:IStrategy
    {
        public void Operate()
        {
            Console.WriteLine("求吴国太开个绿灯,放行!");
        }
    }
}

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

namespace StrategyPattern
{
    /// <summary>
    /// 孙夫人断后,挡住追兵 
    /// </summary>
    public class Strategy_BlockEnemy:IStrategy
    {
        public void Operate()
        {
            Console.WriteLine("孙夫人断后,挡住追兵");
        }
    }
}

好了,大家看看,三个妙计是有了,那需要有个地方放这些妙计呀,放锦囊呀:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    /// <summary>
    /// 计谋有了,那还要有锦囊
    /// </summary>
    public class Context
    {
        IStrategy strategy;
        public Context(IStrategy strategy)
        {
            this.strategy = strategy;
        }
        public void Operate() 
        {
            strategy.Operate();
        }
    }
}

然后就是赵云雄赳赳的揣着三个锦囊,拉着已步入老年行列的、还想着娶纯情少女的、色迷迷的刘老爷子去入赘了,嗨,还别说,小亮的三个妙计还真是不错,瞅瞅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    /// <summary>
    /// 赵云出场了,他根据诸葛亮给他的交代,依次拆开妙计 
    /// </summary>
    public class MrZhaoyun
    {
        public static void Main(string[] args)
        {
            Context context;

            Console.WriteLine("========刚到吴国的时候拆开第一个=========");
            context = new Context(new Strategy_BackDoor());
            context.Operate();
            Console.WriteLine("\n");

            Console.WriteLine("========刘备乐不思蜀时候拆开第二个=========");
            context = new Context(new Strategy_GreenLight());
            context.Operate();
            Console.WriteLine("\n");

            Console.WriteLine("========孙权的小兵追来的时候拆开第三个=========");
            context = new Context(new Strategy_BlockEnemy());
            context.Operate();
            Console.WriteLine("\n");

            Console.ReadKey();
        }
    }
}

分析总结:

        问题来了:赵云实际不知道是那个策略呀,他只知道拆第一个锦囊, 而不知道是BackDoor 这个妙计,咋办?  似乎这个策略模式已经把计谋名称写出来了 。错!BackDoor 、GivenGreenLight 、BlockEnemy 只是一个代码,你写成first 、second、third ,没人会说你错!  
        策略模式的好处就是:体现了高内聚低耦合的特性呀,缺点嘛,这个那个,我回去再查查 
        就这三招,搞的周郎是“陪了夫人又折兵”呀!这就是策略模式,高内聚低耦合的特点也表现出来了,还有一个就是扩展性,也就是 OCP 原则,策略类可以继续增加下去,只要修改 Context.cs就可以了,这个不多说了,自己领会吧。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值