策略模式:针对同一命令或行为,不同的策略做不同的动作。
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(); } } }