单例模式写法:
package kg;
import java.util.HashMap;
import java.util.Map;
public class KeyGene {
private static KeyGene instance;
private Map<String, Key> cache;
/** 缓存大小 */
public static int SIZE = 10;
private KeyGene() {
cache = new HashMap<String, Key>();
}
public static KeyGene getInstance() {
if (instance == null) {
instance = new KeyGene();
}
return instance;
}
public int getNextKey(String keyName) {
Key key = cache.get(keyName);
if (key == null) {
key = new Key(SIZE, keyName);
cache.put(keyName, key);
}
return key.getNext();
}
public static void main(String[] args) {
KeyGene kg = KeyGene.getInstance();
int key = kg.getNextKey("goodjob");
}
}
class Key {
private int max;
private int size;
private int next;
private String name;
public Key(int size, String name) {
this.size = size;
this.name = name;
dbOperate();
}
public synchronized int getNext() {
if (next > max) {
dbOperate();
}
return next++;
}
private void dbOperate() {
// 执行数据库操作
// if next >1 then
// max = next+size;
// update key_store set value=max where code = this.name
// else
// next = 1;
// max = next+size;
// insert into key_store(code,value)values(this.name,this.max);
}
}
多例模式写法:
package kg;
import java.util.Map;
public class KeyGene2 {
private static Map<String, KeyGene2> cache;
public static int SIZE = 10;
private Key key;
private KeyGene2(String keyName) {
key = new Key(SIZE, keyName);
}
public static KeyGene2 getInstance(String keyName) {
KeyGene2 kg = cache.get(keyName);
if (kg == null) {
kg = new KeyGene2(keyName);
cache.put(keyName, kg);
}
return kg;
}
public int getNextKey() {
return key.getNext();
}
public static void main(String[] args) {
KeyGene2 kg = KeyGene2.getInstance("goodjob");
int key = kg.getNextKey();
}
}
就我个人来看,多例模式写法更常用些。
分享到:
相关推荐
找KEY网-卡巴斯基KEY生成助手、找KEY网-卡巴斯基KEY生成助手
卡巴斯基KEY生成助手 卡巴斯基KEY生成助手
卡巴斯基Key生成器,方便的key生成工具
卡巴斯基 key 生卡巴斯基 key 生卡卡巴斯基 key 生成 斯基 key 生卡巴斯基 key 生卡巴斯基 key 生成
Twincad2.4 Beckhoff_KEY生成器
卡巴斯基Key生成助手4.3 Build0908绿色版 软件版本: 4.3 Build0908绿色版 软件类别: 国产软件 软件语言: 简体中文 授权文式: 免费软件 应用平台: Win2003,WinXP,Win2000,Win9X,Vista 卡巴斯基Key生成助手...
万能KEY生成助手v2.1,你的使用小助手。
CS正CDKEY生成器 详情请打开后有解
Java生成PublicKey方法
该软件更新较快,无需再反复找激活KEY了。只要是最新版本就可以激活,百试不爽啊!
kav kis 789的激活文件下载器,good, 折腾这么多年卡巴,表示还是淘宝一个序列号1.99用一年。找激活文件实在是累人。
CKFinder v2.4.1 License Key生成器
plsqldev903 +key生成器
打开软件运行能够生成卡巴斯基kav kis的key文件,方便贫民。强烈建议使用卡巴斯基正版软件,中国软件就是因为盗版猖獗一路低迷的。
卡巴key自动生成工具,key文件过期后可以方便查找新的key
卡巴斯基Key生成助手卡巴斯基Key生成助手卡巴斯基Key生成助手卡巴斯基Key生成助手