Java 枚举 Enum 三大实战场景:状态定义、策略模式、接口统一返回码

一、场景 1:状态定义(订单 / 业务状态,替代魔法数字)

点击查看代码
// 订单状态枚举
public enum OrderStatus {
    WAIT_PAY(0, "待支付"),
    PAID(1, "已付款"),
    DELIVERED(2, "已发货"),
    FINISH(3, "已完成"),
    CANCEL(4, "已取消");

    private final Integer code;
    private final String desc;

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

    public Integer getCode() {
        return code;
    }
    public String getDesc() {
        return desc;
    }

    // 根据code反向匹配枚举
    public static OrderStatus getByCode(Integer code) {
        for (OrderStatus status : OrderStatus.values()) {
            if (status.getCode().equals(code)) {
                return status;
            }
        }
        return null;
    }
}

场景二:统一接口返回码(前后端响应规范)

点击查看代码
interface ResultInterface {
    int getCode();
    String getMsg();
}

public enum ResultCode implements ResultInterface {
    SUCCESS(200, "请求成功"),
    SERVER_ERROR(500, "服务器异常"),
    PARAM_ERR(400, "参数错误"),
    NO_AUTH(401, "未授权");

    private final int code;
    private final String msg;

    ResultCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    @Override
    public int getCode() {
        return code;
    }
    @Override
    public String getMsg() {
        return msg;
    }
}

// 统一返回实体
class R<T> {
    private int code;
    private String msg;
    private T data;

    public static <T> R<T> ok(T data) {
        R<T> r = new R<>();
        r.code = ResultCode.SUCCESS.getCode();
        r.msg = ResultCode.SUCCESS.getMsg();
        r.data = data;
        return r;
    }
    public static <T> R<T> fail(ResultCode code) {
        R<T> r = new R<>();
        r.code = code.getCode();
        r.msg = code.getMsg();
        return r;
    }

    public int getCode() {return code;}
    public String getMsg() {return msg;}
    public T getData() {return data;}
}
// 测试
class TestResp {
    public static void main(String[] args) {
        R<String> res = R.fail(ResultCode.NO_AUTH);
        System.out.println(res.getCode() + ":" + res.getMsg());
    }
}

场景三:枚举实现策略模式(消除大量 if-else)

点击查看代码
// 策略接口
interface DiscountStrategy {
    double calculate(double price);
}

// 枚举承载所有策略
public enum DiscountEnum implements DiscountStrategy {
    NORMAL{
        @Override
        public double calculate(double price) {
            return price;
        }
    },
    MEMBER{
        @Override
        public double calculate(double price) {
            return price * 0.8;
        }
    },
    VIP{
        @Override
        public double calculate(double price) {
            return price * 0.6;
        }
    };
}
// 测试
class TestStrategy {
    public static void main(String[] args) {
        double price = 1000;
        System.out.println(DiscountEnum.VIP.calculate(price));
    }
}

三大场景简要总结
业务状态定义:用code+描述封装业务状态,替换硬编码数字,提供根据编码反向查找枚举的方法,统一项目状态规范,可读性高。
接口统一返回码:实现公共接口约束返回码结构,集中管理所有业务响应编码,前后端交互标准统一,便于全局异常处理。
策略模式:枚举实例直接实现策略接口,每一个枚举对应一套独立业务逻辑,新增分支只需新增枚举项,彻底消除多层 if/else,代码易于扩展维护。
枚举天然单例、线程安全,相比普通常量类结构更严谨,是项目开发中标准常量管理方案。

posted @ 2026-06-18 15:33  pigeon1237  阅读(1)  评论(0)    收藏  举报