策略模式实战

        今天在做公司业务的时候,遇到一个需求,业务场景是要求增删改使用同一个接口,通过接口文档提供的Status操作状态的值0(新增),1(编辑),2(删除)实现业务类型的判断,我就想到了策略模式,我的想法是三种业务分别实现抽象策略方法,通过传过来的Status值调用具体实现类的方法,接下来就是我的实现:

public class ExampleServiceImpl implements ExampleService {

    @Resource
    private ExampleMapper exampleMapper;

    @Resource
    protected DepartmentClient departmentClient;
    
    // 抽象策略模式接口
    private interface AbstractStrategyMode {
        ResponseResult<ExampleVO> exec(ExampleDTO dto);
    }

    // 具体业务类型实现类
    private class AddStrategy implements AbstractStrategyMode {
        @Override
        public ResponseResult<ExampleVO> exec(ExampleDTO dto) {
            int result = ExampleMapper.addConfig(dto);
            if (result == 1) {
                String oppositeId = ExampleMapper.findIdBySourceId(dto.getSourceId());
                return ResponseResult.ok(new ExampleVO(dto.getSourceId(), oppositeId));
            }
            return ResponseResult.err("添加失败,请联系管理员");
        }
    }

    private class EditStrategy implements AbstractStrategyMode {
        @Override
        public ResponseResult<ExampleVO> exec(ExampleDTO dto) {
            int result = ExampleMapper.editConfig(contractUniqueId, dto);
            if (result == 1) {
                String oppositeId = ExampleMapper.findIdBySourceId(dto.getSourceId());
                return ResponseResult.ok(new ExampleVO(dto.getSourceId(), contractUniqueId));
            }
            return ResponseResult.err("编辑失败,请联系管理员");
        }
    }

    private class DeleteStrategy implements AbstractStrategyMode {
        @Override
        public ResponseResult<ExampleVO> exec(ExampleDTO dto) {
            int result = ExampleMapper.deleteConfig(dto);
            if (result == 1) {
                String oppositeId = ExampleMapper.findIdBySourceId(dto.getSourceId());
                return ResponseResult.ok(new ExampleVO(dto.getSourceId(), oppositeId));
            }
            return ResponseResult.err("删除失败,请联系管理员");
        }
    }

    // 使用业务对象判断具体需要哪个实现类
    private class RealityBusiness {
        private AbstractStrategyMode business = null;

        public RealityBusiness(int status) {
            if (status == 0) {
                business = new AddStrategy();
            }
            if (status == 1) {
                business = new EditStrategy();
            }
            if (status == 2) {
                business = new DeleteStrategy();
            }
        }

        public ResponseResult<ExampleVO> performOperation(int status, ExampleDTO dto) {
            return business.exec(dto);
        }
    }

    // 执行三层架构的业务逻辑方法
    @Override
    public ResponseResult<ExampleVO> optionConfig(ExampleDTO dto) {
        // ... 公司的其他业务开始

        // ... 公司的其他业务完成
        int status = dto.getStatus();
        RealityBusiness realityBusiness = new RealityBusiness(status);
        return realityBusiness.performOperation(status, dto);
    }

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值