枚举值管理JAVA项目字典的实战应用(适配器模式)

用枚举值管理项目字典的实战应用(适配器模式)

  1. 枚举值相比常量的优势
  2. 应用场景
  3. 模拟代码实现
    3.1 UML类图(适配器模式)
    3.2 项目Maven依赖
    3.3 示例代码实现
    4.4 测试

1.枚举值相比常量的优势

1.1 代码可读性高(如果有人问你 XXX系统的XX类型是那些,你能马上找到代码并回答出来)。
1.2 可以把一类字典归在一个类中,方便管理。比如(系统A字典 和 系统B字典 一个系统一个类,不用满世界的乱找)。
1.3 方便遍历。常量需要用到反射,而枚举的EnumSet提供了直接遍历的方法。
1.4 离散值面向对象,方便程序调用。

2.应用场景

在这里插入图片描述
现有系统A和系统B两套字典,每套字典值俊不相同,如程序在系统A和系统B来回交互时,就需要不停的转换。如果把转换的逻辑写在业务层,则会加重开发人员负担,而且后期转换的代码越来越多,会变得难以管理。故使用适配器模式,与enum类相结合,将字典转换的逻辑抽取出来,统一管理。

3.模拟代码实现

3.1UML类图(适配器模式)

在这里插入图片描述

3.2 项目Maven依赖

只需要junit

<dependency>  
   <groupId>junit</groupId>  
    <artifactId>junit</artifactId>  
    <version>${junit.version}</version>  
</dependency> 

<dependency>  
    <groupId>org.springframework</groupId>  
    <artifactId>spring-test</artifactId>  
    <version>${spring.version}</version>
    <scope>test</scope>  
</dependency>

3.3示例代码实现

BaseEnum枚举类接口用来抽象枚举类(枚举类不能继承,所以要用接口)

public interface BaseEnum {
	
	/** 得到code*/
	String getCode();
	
	/** 得到message*/
	String getMessage();
}

ConstantOfSystemA 系统A的所有字典,在此类管理,可以看到枚举的好处,归纳!!!

public class ConstantOfSystemA {

	/** 城市 */
	public enum City implements BaseEnum {
		/** 上海市 */
		SHANGEHAI("A-100", "上海"),
		/** 北京市 */
		BEIJIN("A-101", "北京"),
		/** 南京市 */
		NANJIN("A-100", "南京"),
		;
		public String code;
		public String message;
		private City(String code, String message) {this.code = code;this.message = message;}
		public String getCode() {return code;}
		public String getMessage() {return message;}
	}
	
	/** 性别 */
	public enum Gender implements BaseEnum {
		/** 男性 */
		MALE("A-MALE", "男"),
		/** 女性 */
		FEMALE("A-FEMALE", "女"),
		;
		public String code;
		public String message;
		private Gender(String code, String message) {this.code = code;this.message = message;}
		public String getCode() {return code;}
		public String getMessage() {return message;}
	}
	
}

ConstantOfSystemB 系统B的所有字典,在此类管理

public class ConstantOfSystemB {

	/** 城市 */
	public enum City implements BaseEnum {
		/** 上海市 */
		SHANGEHAI("B-100", "上海"),
		/** 北京市 */
		BEIJIN("B-101", "北京"),
		/** 南京市 */
		NANJIN("B-100", "南京"),
		;
		public String code;
		public String message;
		private City(String code, String message) {this.code = code;this.message = message;}
		public String getCode() {return code;}
		public String getMessage() {return message;}
	}
	
	/** 性别 */
	public enum Gender implements BaseEnum {
		/** 男性 */
		MALE("B-MALE", "男"),
		/** 女性 */
		FEMALE("B-FEMALE", "女"),
		;
		public String code;
		public String message;
		private Gender(String code, String message) {this.code = code;this.message = message;}
		public String getCode() {return code;}
		public String getMessage() {return message;}
	}
	
}

ConstantsService 字典服务类,用来提供统一的遍历方法。(否则每个字典都得写一套,麻烦)把统一的方法抽出来。

public class ConstantsService {

	/**
	 * 	通过code获得枚举值对象
	 * @param clazz  枚举.class
	 * @param code  code
	 * @return
	 */
	public <T extends Enum<T> & BaseEnum> T getByCode(Class<T> clazz,String code) {
		EnumSet<T> set = EnumSet.allOf(clazz);
		for(T t : set){
			if(t.getCode().equals(code)){
				return t;
			}
		}
		return null;
	}
	
}

ConstantAdapter 字典适配器,提供各个系统间字典转换的具体实现。这里是简单举例实现,具体实现还是要看具体业务需求。

public class ConstantAdapter {

	ConstantsService constantsService = new ConstantsService();
	
	/**
	 *    转城市
	 */
	public String adapterCity_A2B(String code) throws Exception {
		
		ConstantOfSystemA.City source = constantsService.getByCode(ConstantOfSystemA.City.class, code);
		try {
			ConstantOfSystemB.City target = ConstantOfSystemB.City.valueOf(source.name());
			return target.getCode();
		} catch (Exception e) {
			throw new Exception("转换失败。");
		}
	}
	
}

4测试

ConstantAdapterTest 具体开发者的业务程序调用者

public class ConstantAdapterTest {

	@Test
	public void adapterCity_A2B() throws Exception {
		
		//新建一个适配器
		ConstantAdapter constantAdapter = new ConstantAdapter();
		
		String code = ConstantOfSystemA.City.SHANGEHAI.getCode();
		System.out.println("系统A:上海,代码:"+code);
		
		code = constantAdapter.adapterCity_A2B(code);
		System.out.println("转换后变系统B:上海,代码:"+code);
		
	}
	
}

输出:

系统A:上海,代码:A-100
转换后变系统B:上海,代码:B-100
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值