首页 > Windows开发 > 详细

C#设计模式:策略者模式(Stragety Pattern)

时间:2018-01-08 17:57:23      阅读:237      评论:0      收藏:0      [点我收藏+]

策略模式:针对同一命令或行为,不同的策略做不同的动作。

 

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

namespace StrategyDesign
{
    class Program
    {
        static void Main(string[] args)
        {
            StrategyContext context = new StrategyContext();
            //设置“随机策略“
            context.SetStrategy(new RandStrategy());
            context.Setup();
            //设置 ”直接发送“
            context.SetStrategy(new StraightStrategy());
            context.Setup();
        }
    }
    public abstract class AbstractStrategy
    {
        public abstract void Setup();
    }
    public class RandStrategy : AbstractStrategy
    {
        public override void Setup()
        {
            Console.WriteLine("千人千面模式下的邮件发送");
        }
    }
    public class StraightStrategy : AbstractStrategy
    {
        public override void Setup()
        {
            Console.WriteLine("普通商家发送的邮件");
        }
    }
    public class StrategyContext
    {
        AbstractStrategy strategy = null;
        public void SetStrategy(AbstractStrategy strategy)
        {
            this.strategy = strategy;
        }
        public void Setup()
        {
            this.strategy.Setup();
        }
    }
}

 

C#设计模式:策略者模式(Stragety Pattern)

原文:https://www.cnblogs.com/May-day/p/6553557.html

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