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

2021-05-15 12:49

阅读:588

标签:contex   c#   rri   span   设计模式   模式   pos   his   ide   

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

 

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)

标签:contex   c#   rri   span   设计模式   模式   pos   his   ide   

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


评论


亲,登录后才可以留言!