设计模式 - 状态模式与策略模式的区别

类图:

状态模式和策略模式的类图几乎一模一样。

image

image

策略模式:

其思想是针对一组算法,将每一种算法都封装到具有共同接口的独立的类中,不同的行为使用不同的算法,并且它们可以相互替换。它是直接依赖注入到Context类的参数进行选择策略,所以客户端必须事先知道策略,由它去指定环境(Context)类使用哪种策略(算法)。

状态模式:

允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它的类。
每个状态子类需要包含环境类(Context),自己给自己的环境类切换状态来选择不同的行为,所以客户端不需要事先了解状态。

总结:

策略模式 需要由 客户端指定 Context选择哪种策略,所以客户端 需要 事先了解策略;
状态模式 是自己内部达到某种条件会自动切换 状态,所以客户端不需要了解状态,只需要调用状态方法。

例子:

状态模式:
//测试
public class StatePatternExample : MonoBehaviour
{
    void Start()
    {
        Context theContext = new Context();
        theContext.SetState(new ConcreteStateA(theContext));
        theContext.Request(5);
        theContext.Request(15);
        theContext.Request(25);
    }
}

//执行环境
public class Context
{
    State m_State = null;

    public void Request(int Value)
    {
        m_State.Handle(Value);
    }

    public void SetState(State theState)
    {
        Debug.Log("Context.SetState:" + theState);
        m_State = theState;
    }
}
// 状态的抽象基类
public abstract class State
{
    protected Context m_Context = null;

    public State(Context theContext)
    {
        m_Context = theContext;
    }
    public abstract void Handle(int Value);
}

// 状态A
public class ConcreteStateA : State
{
    public ConcreteStateA(Context theContext) : base(theContext)
    { }

    public override void Handle(int Value)
    {
        Debug.Log("ConcreteStateA.Handle");
        if (Value > 10)
            m_Context.SetState(new ConcreteStateB(m_Context));
    }

}

// 状态B
public class ConcreteStateB : State
{
    public ConcreteStateB(Context theContext) : base(theContext)
    { }

    public override void Handle(int Value)
    {
        Debug.Log("ConcreteStateB.Handle");
        if (Value > 20)
            m_Context.SetState(new ConcreteStateC(m_Context));
    }
}
策略模式:
//测试
public class StrategyPatternExample : MonoBehaviour
{
    void Start()
    {
        Context studentRecords = new Context();
        
        studentRecords.SetStrategy(new StrategyA());
        studentRecords.ExecuteAlgorithm();

        studentRecords.SetStrategy(new StrategyB());
        studentRecords.ExecuteAlgorithm();
    }
}


class Context
{
    private Strategy _strategy;

    public void SetStrategy(Strategy sortstrategy)
    {
        this._strategy = sortstrategy;
    }
    public void ExecuteAlgorithm()
    {
        _strategy.ExecuteAlgorithm();
    }
}

// 策略抽象类
public abstract class Strategy
{
    public abstract void ExecuteAlgorithm();
}

// 策略A
class StrategyA : Strategy
{
    public override void ExecuteAlgorithm()
    {
        Debug.Log("-------StrategyA------- ");
    }
}

// 策略B
class StrategyB : Strategy
{
    public override void ExecuteAlgorithm()
    {
        Debug.Log("-------StrategyB------ ");
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值