【设计模式】策略模式与状态模式区别

转载 https://www.cnblogs.com/m7777/p/7723879.html

策略模式

public interface IStrategy
    {
       double CountMoney(double money);
    }


   public class discountA:IStrategy
    {
        public double CountMoney(double money)
        {
            return money * 0.8;
           // throw new NotImplementedException();
        }
    }

  /// 
    /// 打折活动B
    /// 
   public class discountB:IStrategy
    {

       /// 
       /// 满100-20
       /// 
       /// 
       /// 
        public double CountMoney(double money)
        {
            return money - ((int)(money / 100) * 20);
            //throw new NotImplementedException();
        }
    }

public class Strategy
    {
       IStrategy iStrategy;
       public Strategy(IStrategy strategy)
       {
           iStrategy = strategy;
       }
       public double CountMoney(double money)
       {
           return iStrategy.CountMoney(money);
       }
    }

 public class MainStrategys
    {
       public MainStrategys()
       {
           Strategy strategt;
           //执行A打折策略
           discountA da = new discountA();
           strategt = new Strategy(da);
           Console.WriteLine("A打折策略--"+strategt.CountMoney(111));

           //执行B打折策略
           discountB db = new discountB();
           strategt = new Strategy(db);
           Console.WriteLine("B打折策略--" + strategt.CountMoney(111));

           Console.ReadKey();
       }
    }

状态模式:

 

public  interface IState
    {
       void Submit(FileSub file);
    }

 public class BeginState :IState
    {

        public void Submit(FileSub file)
        {
            Console.WriteLine("begin-------");
            file.SetState(new WorkingState());
        }
    }

 public  class WorkingState:IState
    {
        public void Submit(FileSub file)
        {
            Console.WriteLine("working-------------");
            file.SetState(new EndState());
            //throw new NotImplementedException();
        }
    }


 public class EndState:IState
    {
        public void Submit(FileSub file)
        {
            Console.WriteLine("end------------");
            file.SetState(new BeginState());
            //throw new NotImplementedException();
        }
    }

 public class FileSub
    {
        private IState istate;
        public FileSub()
        {
            istate = new BeginState();
        }
        public void SetState(IState state)
        {
            istate = state;
        }
        public void Submit()
        {
            istate.Submit(this);
        }
    }

    public class Main
    {
        public Main()
        {
            FileSub file = new FileSub();
            file.SetState(new BeginState());
            file.Submit();
            file.Submit();
            file.Submit();
            Console.ReadKey();
        }
    }

 

 

 策略模式与状态模式在实现上有共同之处,都是把不同的情形抽象为统一的接口来实现,就放在一起进行记录。2个模式的UML建模图基本相似,区别在于状态模式需要在子类实现与context相关的一个状态行为。

  状态模式的的思想是,状态之间的切换,在状态A执行完毕后自己控制状态指向状态B。状态模式是不停的切换状态执行。

  策略模式的思想上是,考虑多种不同的业务规则将不同的算法封装起来,便于调用者选择调用。策略模式只是条件选择执行一次。

https://images2017.cnblogs.com/blog/359226/201710/359226-20171024150436223-1528637255.gif

https://images2017.cnblogs.com/blog/359226/201710/359226-20171024150443769-2093839329.png

你可能感兴趣的:(设计模式)