Skip to main content

策略模式 + 服务发现

策略模式 + 服务发现

  • 策略模式 + 服务发现

定义服务接口

public interface ITAfterSaleCommonService{

boolean agree(Order order);

boolean supports(Class<?> orderType);

}

定义实体类接口

public interface AfterSaleEntity {

BusinessTypeEnum getBusinessType();
Long getBusinessId();
Long getId();
}

实体类实现接口规范

public class TAfterSale implements AfterSaleEntity {}

枚举类

@Getter
public enum BusinessTypeEnum implements IEnum<String> {

CARE_ORDER("4", "护理订单");

private final String code;
private final String desc;

BusinessTypeEnum(String code, String desc) {
this.code = code;
this.desc = desc;
}

/**
* 根据字符串 code 获取枚举(严格匹配)
*/
public static BusinessTypeEnum fromCode(String code) {
if (code == null) {
throw new IllegalArgumentException("业务状态不能为 null");
}
for (BusinessTypeEnum status : values()) {
if (status.code.equals(code)) {
return status;
}
}
throw new IllegalArgumentException("无效的业务状态码: " + code);
}

/**
* 安全版本:code 不存在时返回 null
*/
public static BusinessTypeEnum fromCodeOrNull(String code) {
if (code == null) {
return null;
}
for (BusinessTypeEnum status : values()) {
if (status.code.equals(code)) {
return status;
}
}
return null;
}

@Override
public String getValue() {
return this.code;
}

@JsonValue
public String getValueForJson() {
return this.code;
}
}

实现案例1

@Service
public class ExampleOrderServiceImpl implements ITAfterSaleCommonService {

@Override
@Transactional(rollbackFor = Exception.class)
public boolean agree(Long id, Long userId) {
return true;
}

// @Override
// public boolean supports(Class<?> orderType) {
// return ExampleOrder.class.isAssignableFrom(orderType);
// }
@Override
public boolean supports(BusinessTypeEnum businessType) {
return BusinessTypeEnum.CARE_ORDER.equals(businessType);
}

}

接口实现2

@Service
public class OrderServiceImpl implements ITAfterSaleCommonService {

@Override
@Transactional(rollbackFor = Exception.class)
public boolean agree(Long id, Long userId) {
return true;
}

// @Override
// public boolean supports(Class<?> orderType) {
// return Order.class.isAssignableFrom(orderType);
// }
@Override
public boolean supports(BusinessTypeEnum businessType) {
return BusinessTypeEnum.CARE_ORDER.equals(businessType);
}

}

策略模式 + 服务发现

@Component
public class AfterSaleHandler {
List<ITAfterSaleCommonService> handlers;
public AfterSaleHandler(List<ITAfterSaleCommonService> handlers) {
this.handlers = handlers;
}
// 动态路由入口
public boolean agree(AfterSaleEntity entity, Long userId) {
if (entity.getId() == null) throw new IllegalArgumentException("订单不能为空");

// 遍历所有处理器,找到支持该订单类型的
for (ITAfterSaleCommonService handler : handlers) {
if (handler.supports(entity.getBusinessType())) {
return handler.agree(entity.getId(), userId);
}
}

throw new UnsupportedOperationException("不支持的订单类型: " + entity.getBusinessType());
}
public boolean agreeBatch(List<? extends AfterSaleEntity> list, Long userId, Object order){}
}

使用

@RestController
public class AfterSaleController {

@Autowired
private AfterSaleHandler router;

public void agreeAfterSale(Long id, Long userId) {
// 无论传入什么订单,自动路由到正确实现!使用new演示
boolean success = router.agree(new TAfterSale(), userId);
if (!success) {
throw new RuntimeException("售后处理失败");
}
}
}

如果知道具体类型

public interface ITAfterSaleCommonService<T> {

boolean agree(T order);
}
@Service
public class ExampleOrderServiceImpl implements ITAfterSaleCommonService<ExampleOrder> {

@Override
@Transactional(rollbackFor = Exception.class)
public boolean agree(ExampleOrder order) {
return true;
}
}

使用

@RestController
public class AfterSaleController {

@Autowired
private ITAfterSaleCommonService router;

public void agreeAfterSale(ExampleOrder order) {
// 自动路由到正确实现!
boolean success = router.agree(order);
if (!success) {
throw new RuntimeException("售后处理失败");
}
}
}