1.什么是状态模式
允许一个对象在其内部状态改变时改变它的行为
2.策略模式与状态模式区别
状态模式没有共同的抽象行为
3.如果选择状态模式还是策略模式
有共同行为选策略模式,没有共同行为选状态模式
4.状态模式实现
链接:https://pan.baidu.com/s/16qpud_njJwJFmShuRxPSHA
提取码:f1bn
1)pom依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.11.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
</dependencies>
2)接口
public interface OrderState<T> {
T orderService();
}
3)实现
import com.demo.service.OrderState;
import org.springframework.stereotype.Component;
@Component
public class AlreadySignedOrderState implements OrderState<String> {
@Override
public String orderService() {
return "切换已经签收状态";
}
}
import com.demo.service.OrderState;
import org.springframework.stereotype.Component;
@Component
public class InTransitOrderState implements OrderState<String> {
@Override
public String orderService() {
return "切换为正在运送状态";
}
}
import com.demo.service.OrderState;
import org.springframework.stereotype.Component;
@Component
public class ShippedAlreadyOrderState implements OrderState<String> {
public String orderService() {
return "已经发货..";
}
}
4)状态模式类
import com.demo.service.OrderState;
public class StateContext {
private OrderState orderState;
public StateContext(OrderState orderState) {
this.orderState = orderState;
}
public Object switchStateOrder() {
return orderState.orderService();
}
}
5)控制层
import com.demo.context.StateContext;
import com.demo.service.OrderState;
import com.demo.utils.SpringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@RequestMapping("/order")
public Object order(String stateBeanId) {
//1.使用Spring上下文获取bean中对象
OrderState orderState = SpringUtils.getBean(stateBeanId, OrderState.class);
// 2.使用上下文切换到不同的状态
StateContext stateContext = new StateContext(orderState);
return stateContext.switchStateOrder();
}
}
6)工具类
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
//获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
//通过name获取 Bean.
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
//通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
7)启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppOrderState {
public static void main(String[] args) {
SpringApplication.run(AppOrderState.class);
}
}
8)测试
http://localhost:8080/order?stateBeanId=shippedAlreadyOrderState