【设计模式】JAVA Design Patterns——State(状态模式)

🔍目的


允许对象在内部状态改变时改变它的行为。对象看起来好像修改了它的类。

🔍解释


真实世界例子

当在长毛象的自然栖息地观察长毛象时,似乎它会根据情况来改变自己的行为。它开始可能很平静但是随着时间推移当它检测到威胁时它会对周围的环境感到愤怒和危险

通俗描述

状态模式允许对象改变它的行为

维基百科

状态模式是一种允许对象在内部状态改变时改变它的行为的行为型设计模式。这种模式接近于有限状态机的概念。状态模式可以被理解为策略模式,它能够通过调用在模式接口中定义的方法来切换策略。

程序示例

创建模式接口和它具体的实现

public interface State {

  void onEnterState();

  void observe();
}

public class PeacefulState implements State {

  private static final Logger LOGGER = LoggerFactory.getLogger(PeacefulState.class);

  private final Mammoth mammoth;

  public PeacefulState(Mammoth mammoth) {
    this.mammoth = mammoth;
  }

  @Override
  public void observe() {
    LOGGER.info("{} is calm and peaceful.", mammoth);
  }

  @Override
  public void onEnterState() {
    LOGGER.info("{} calms down.", mammoth);
  }
}

public class AngryState implements State {

  private static final Logger LOGGER = LoggerFactory.getLogger(AngryState.class);

  private final Mammoth mammoth;

  public AngryState(Mammoth mammoth) {
    this.mammoth = mammoth;
  }

  @Override
  public void observe() {
    LOGGER.info("{} is furious!", mammoth);
  }

  @Override
  public void onEnterState() {
    LOGGER.info("{} gets angry!", mammoth);
  }
}

创建包含状态的长毛象

public class Mammoth {

  private State state;

  public Mammoth() {
    state = new PeacefulState(this);
  }

  public void timePasses() {
    if (state.getClass().equals(PeacefulState.class)) {
      changeStateTo(new AngryState(this));
    } else {
      changeStateTo(new PeacefulState(this));
    }
  }

  private void changeStateTo(State newState) {
    this.state = newState;
    this.state.onEnterState();
  }

  @Override
  public String toString() {
    return "The mammoth";
  }

  public void observe() {
    this.state.observe();
  }
}

 创建长毛象随着时间的推移后的整个行为示例

    var mammoth = new Mammoth();
    mammoth.observe();
    mammoth.timePasses();
    mammoth.observe();
    mammoth.timePasses();
    mammoth.observe();
    
    // The mammoth gets angry!
    // The mammoth is furious!
    // The mammoth calms down.
    // The mammoth is calm and peaceful.

🔍类图


🔍适用场景

在以下两种情况下,请使用State模式

  • 对象的行为取决于它的状态,并且它必须在运行时根据状态更改其行为。
  • 根据对象状态的不同,操作有大量的条件语句。此状态通常由一个或多个枚举常量表示。通常,几个操作将包含此相同的条件结构。状态模式把条件语句的分支分别放入单独的类中。这样一来,你就可以将对象的状态视为独立的对象,该对象可以独立于其他对象而变化。

🔍Ending


State(状态)模式是一种对象行为型设计模式,它允许对象在其内部状态发生改变时改变其行为。State模式的核心思想是将对象的行为封装在不同的状态对象中,并且使得对象在不同状态下可以切换。


希望本文能够帮助读者更深入地理解状态模式,在实际项目中发挥其优势。谢谢阅读!


希望这份博客草稿能够帮助到你。如果有其他需要修改或添加的地方,请随时告诉我

Learn how to implement design patterns in Java: each pattern in Java Design Patterns is a complete implementation and the output is generated using Eclipse, making the code accessible to all. The examples are chosen so you will be able to absorb the core concepts easily and quickly. This book presents the topic of design patterns in Java in such a way that anyone can grasp the idea. By giving easy to follow examples, you will understand the concepts with increasing depth. The examples presented are straightforward and the topic is presented in a concise manner. Key features of the book: Each of the 23 patterns is described with straightforward Java code. There is no need to know advanced concepts of Java to use this book. Each of the concepts is connected with a real world example and a computer world example. The book uses Eclipse IDE to generate the output because it is the most popular IDE in this field. This is a practitioner's book on design patterns in Java. Design patterns are a popular topic in software development. A design pattern is a common, well-described solution to a common software problem. There is a lot of written material available on design patterns, but scattered and not in one single reference source. Also, many of these examples are unnecessarily big and complex. Table of Contents Chapter 1: Introduction Chapter 2: Observer Patterns Chapter 3: Singleton Patterns Chapter 4: Proxy Patterns Chapter 5: Decorator Patterns Chapter 6: Template Method Patterns Chapter 7: Strategy Patterns (Or, Policy Patterns) Chapter 8: Adapter Patterns Chapter 9: Command Patterns Chapter 10: Iterator Patterns Chapter 11: Facade Patterns Chapter 12: Factory Method Patterns Chapter 13: Memento Patterns Chapter 14: State Patterns Chapter 15: Builder Patterns Chapter 16: Flyweight Patterns Chapter 17: Abstract Factory Patterns Chapter 18: Mediator Patterns Chapter 19: Prototype Patterns Chapter 20: Chain of Responsibility Patterns Chapter 21: Composite Patterns Chapter 22: Bridge Patterns (Or Handle/Body Patterns) Chapter 23: Visitor Patterns Chapter 24: Interpreter Patterns
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆呆企鹅仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值