KeyboardPianoV1.4.1 配置管理


详细步骤

例行说明

  • 就如之前 V1.3 - V1.3.1 的关系一样,V1.3 做前期准备,V1.3.1 做调用处理
    这里 V1.4 - V1.4.1 也是同个道理,但这个版本只做配置管理,为下个版本做调用前准备

具体步骤

  • 新建一个 PropertiesManage 配置管理类
    这里实际上有一入另一种设计模式 Singleton Singledog 单例单身狗模式 ╮( ̄﹏ ̄)╭

  • 通过 Properties 怼到配置文件 *.properties,以 key 为桥梁,get 到对应的 value (Chinglish lol)

代码分析

  • PropertiesManage 详解
    一眼望去好多 static,这其实是 Singleton 的特点
    1. static Properties 成员变量有俩个,因为有两个配置文件要处理,所以分配两条不同的引用
    2. Properties.load() 通过 class 文件所在处 加载 配置文件(V1.4 已准备好了)
    3. private PropertiesManage() { } 是什么鬼?
      单例下,单纯只想对外暴露静态方法,显式定义私有的空的构造函数,其实是为了避免用户 new 出该类的对象,破坏这个单身派对(单身嘛,还哪来的对象?!还不如做一只无人可 new 的单身汪
      注意:这是单例的特点,也是核心
    4. 最后俩个方法就是通过配置引用提起文件中配置信息
      提取方式是通过 get(key) => return value, 类似 Map<K, V>
import java.io.IOException;
import java.util.Properties;

public class PropertiesManage {

	static Properties propPic = new Properties(); 
	static Properties propWav = new Properties();
	
	static {
		try {
			propPic.load(PropertiesManage.class.getClassLoader().getResourceAsStream("config\\pic.properties"));
			propWav.load(PropertiesManage.class.getClassLoader().getResourceAsStream("config\\wav.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private PropertiesManage() { }
	
	public static String getPicProperty(String key) {
		return propPic.getProperty(key);
	}
	
	public static String getWavProperty(String key) {
		return propWav.getProperty(key);
	}
	
}

可以在主类 KeyboardPiano 对配置管理类 PropertiesManage 再包(封装)一层
这样调用的时候 kp.getPicPath(key) 即可,而无需写 PropertiesManage.getPicProperty(key); 辣么长
实际上这也是 Manage 设计模式的思维,KeyboardPiano 作为 main class,理应起到 管理的作用 详见 Option1

单例模式详解 请见 相关链接


Options

Option1

  • KeyboardPiano 加入以下两个配置引用方法
/*
 * get path of picture from the key
 */
public static String getPicPath(String key) {
	return PropertiesManage.getPicProperty(key);
}

/*
 * get path of music(wav) from the key
 */
public static String getWavPath(String key) {
	return PropertiesManage.getWavProperty(key);
}

相关链接

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值