设计模式-工厂模式(统一获取枚举类)

目录

一、引入jar

二、公用接口

三、自定义枚举类

四、枚举工厂类

五、获取所有的枚举类参数


项目开发时,我们会写很多枚举类,有的是后端自己使用,有的给前端当做字典使用。

可以通过此工具包统一获取:

org.apache.commons:commons-lang3

一、引入jar

maven:

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

gradle:

// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation 'org.apache.commons:commons-lang3:3.12.0'

二、公用接口

public interface TypeEnum {
    /**
     * 自定义索引
     * @return
     */
    Integer index();

    /**
     * 字段
     * @return
     */
    String field();

    /**
     * 描述
     * @return
     */
    String desc();
}

三、自定义枚举类

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public enum xxxEnum implements TypeEnum {

    id(1, "ID", "id"),
    title(2, "标题", "title"),
    name(3, "用户名称", "name");

    private Integer index;
    private String desc;
    private String field;

    /**
     * 获取对应枚举类型
     *
     * @param index
     * @return
     */
    public static xxxEnum getEnum(Integer index) {
        for (xxxEnum xxx : xxxEnum.values()) {
            if (xxx.getIndex().intValue() == index.intValue()) {
                return xxx;
            }
        }
        return null;
    }

    public static xxxEnum getEnumByDesc(String desc) {
        for (xxxEnum enum : xxxEnum.values()) {
            if (xxx.getDesc().equals(desc)) {
                return xxx;
            }
        }
        return null;
    }

    @Override
    public Integer index() {
        return index;
    }

    @Override
    public String field() {
        return field;
    }

    @Override
    public String desc() {
        return desc;
    }

}

四、枚举工厂类

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Getter;
import lombok.ToString;

@Getter
@ToString
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum EnumFactory {

	xxxEnum("xxxEnum", "XXX枚举类", xxxEnum.class);

    private String method;
    private String methodName;
    private Class methodClass;

    EnumFactory(String method, String methodName, Class methodClass) {
        this.method = method;
        this.methodName = methodName;
        this.methodClass = methodClass;
    }

    /**
     * 获取对应枚举类型
     *
     * @param method
     * @return
     */
    public static EnumFactory getEnum(String method) {
        for (EnumFactory enums : EnumFactory.values()) {
            if (enums.getMethod().equals(method)) {
                return enums;
            }
        }
        return null;
    }

}

五、获取所有的枚举类参数

import cn.golaxy.bd.core.utils.EnumUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@AllArgsConstructor
public class EnumService {

    /**
     * @param type
     * @return java.lang.String
     * @Description 获取枚举字典
     */
    public List getEnumByType(String type) {
        List<EnumFactory> enumsList = EnumUtils.getEnumList(EnumFactory.class);
        List<Map> resultList = new ArrayList<>();
        enumsList.stream().forEach(enums -> {
            Map<String, Object> map = new HashMap<>();
            map.put("key", enums.getMethod());
            map.put("keyName", enums.getMethodName());
            List<Map> dictList = new ArrayList<>();
            Map<String, TypeEnum> baseEnumMap = EnumUtils.getEnumMap(enums.getMethodClass());
            baseEnumMap.forEach((k, v) -> {
                Map dictMap = new HashMap();
                dictMap.put("index", v.index());
                dictMap.put("field", v.field());
                dictMap.put("desc", v.desc());
                dictList.add(dictMap);
            });
            map.put("dictList", dictList);
            resultList.add(map);
        });
        return resultList;
    }

}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值