赞
踩
状态模式允许一个对象在其内部状态改变的时候改变其行为。这个对象看上去就像是改变了它的类一样。
public String orderState(String state) {
if (state.equals("0")) {
return "已经发货";
}
if (state.equals("1")) {
return "正在运送中...调用第三方快递接口 展示 运送信息";
}
if (state.equals("2")) {
return "正在派送中... 返回派送人员信息";
}
if (state.equals("3")) {
return "已经签收,提示给用户快递员评价";
}
if (state.equals("4")) {
return "拒绝签收, 重新开始申请退单";
}
if (state.equals("5")) {
return "订单交易失败,调用短信接口提示 ";
}
return "未找到对应的状态";
}
}
1、状态模式重点在各状态之间的切换从而做不同的事情,而策略模式更侧重于根据具体情况选择策略,并不涉及切换。
2、状态模式不同状态下做的事情不同,而策略模式做的都是同一件事,例如聚合支付平台,有支付宝、微信支付、银联支付,虽然策略不同,但最终做的事情都是支付,也就是说他们之间是可替换的。反观状态模式,各个状态的同一方法做的是不同的事,不能互相替换。
状态模式封装了对象的状态,而策略模式封装算法或策略。因为状态是跟对象密切相关的,它不能被重用;而通过从Context中分离出策略或算法,我们可以重用它们。
在状态模式中,每个状态通过持有Context的引用,来实现状态转移;但是每个策略都不持有Context的引用,它们只是被Context使用。
排序算法 快速排序 选择排序 冒泡排序 策略
只要是和状态相关 订单、交易、支付状态。
maven:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- sprinboot web -->
<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>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies>
启动类:
package com.demo.order;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppOrderService {
public static void main(String[] args) {
SpringApplication.run(AppOrderService.class);
}
}
utils:
package com.demo.order.utils;
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);
}
}
package com.demo.order.service;
public interface OrderState {
/**
* 返return
*/
Object orderService();
}
package com.demo.order.service.impl;
import com.demo.order.service.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class AlreadySignedOrderState implements OrderState {
@Override
public Object orderService() {
log.info(">> 切换已经签收状态..");
return "切换已经签收状态";
}
}
package com.demo.order.service.impl;
import com.demo.order.service.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class InTransitOrderState implements OrderState {
@Override
public Object orderService() {
log.info(">> 切换为正在运送状态..");
return "切换为正在运送状态";
}
}
package com.demo.order.service.impl;
import com.demo.order.service.OrderState;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class ShippedAlreadyOrderState implements OrderState {
@Override
public Object orderService() {
log.info(">> 切换为已经发货状态..");
return "切换为已经发货状态";
}
}
package com.demo.order.context;
import com.demo.order.service.OrderState;
public class StateContext {
private OrderState orderState;
public StateContext(OrderState orderState) {
this.orderState = orderState;
}
public void switchStateOrder() {
orderState.orderService();
}
}
package com.demo.order;
import com.demo.order.context.StateContext;
import com.demo.order.service.impl.ShippedAlreadyOrderState;
public class Test {
public static void main(String[] args) {
StateContext stateContext = new StateContext(new ShippedAlreadyOrderState());
stateContext.switchStateOrder();
}
}
package com.demo.order.controller;
import com.demo.order.context.StateContext;
import com.demo.order.service.OrderState;
import com.demo.order.utils.SpringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class OrderController {
@RequestMapping("/order")
public String order(String stateBeanId) {
// 1.使用Spring上下文获取bean对象
OrderState orderState = SpringUtils.getBean(stateBeanId, OrderState.class);
StateContext stateContext = new StateContext(orderState);
stateContext.switchStateOrder();
return "success";
}
}
1.运行项目
2.分别访问:
http://127.0.0.1:8080/order?stateBeanId=alreadySignedOrderState
http://127.0.0.1:8080/order?stateBeanId=inTransitOrderState
http://127.0.0.1:8080/order?stateBeanId=shippedAlreadyOrderState
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。