Posted on 2007-02-22 21:45
acooly 阅读(677)
评论(1) 编辑 收藏 所属分类:
Java开发 、
设计模式
多例模式Multiton基本特性同单例Singleton,由自己创建和管理自身实例,并向系统提供自身实例。不同之处是多例类在同一容器(类加载器)或JVM中可以存在多个实例。
应用实例:序列键值生成器本例采用hqldb作为演示数据库。表结构如下:
CREATE TABLE KEYTABLE(
KEYNAME VARCHAR(32) NOT NULL PRIMARY KEY,
KEYVALUE int NOT NULL
)package pattern.multiton;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
/**//*
* Key service<br>
* table strunction:<br/>
* CREATE TABLE KEYTABLE(<br/>
* KEYNAME VARCHAR(32) NOT NULL PRIMARY KEY,<br/>
* KEYVALUE int NOT NULL<br/>
* )<br>
@author ZhangPu
Feb 22, 2007 4:30:47 PM
*/
public class KeyService {
private int cache;
private String keyName;
private int maxValue;
private int nextValue;
public KeyService(int cache,String keyName) {
this.cache = cache;
this.keyName = keyName;
retrieveKey();
}
/** *//**
* get next key form keyinfo
*
* @return
*/
public int getNextKey() {
if(nextValue >= maxValue){
retrieveKey();
}
return nextValue++;
}
/** *//**
* retrieve current value form db.
* @return
*/
private void retrieveKey(){
System.out.println("retrieve database!");
String sqlUpdate = "update KEYTABLE set KEYVALUE = KEYVALUE + " + cache + " where KEYNAME = '"+keyName+"'";
String sqlSelete = "select KEYVALUE from KEYTABLE where KEYNAME = '"+keyName+"'";
Connection conn = null;
Statement stmt = null;
int keyFormDb = 0;
try{
Driver driver = (Driver)Class.forName("org.hsqldb.jdbcDriver").newInstance();
DriverManager.registerDriver(driver);
String url = "jdbc:hsqldb:hsql://localhost/pattern";
String user = "sa";
String passwd = "";
conn = DriverManager.getConnection(url,user,passwd);
stmt = conn.createStatement();
stmt.executeUpdate(sqlUpdate);
ResultSet rs = stmt.executeQuery(sqlSelete);
if(rs != null && rs.next()){
keyFormDb = rs.getInt(1);
}
}catch(Exception e){
try {
stmt.close();
conn.close();
} catch (Exception ex) {
ex.printStackTrace();
}
e.printStackTrace();
}
maxValue = keyFormDb;
nextValue = keyFormDb - cache;
}
}
package pattern.multiton;
import java.util.HashMap;
import java.util.Map;
/*
* KeyGenerator with multiton pattern
@author ZhangPu
Feb 22, 2007 5:55:24 PM
*/
public class KeyGenerator {
/**
* save keyServices
*/
private static Map<String,KeyGenerator> KeyGenerators = new HashMap<String,KeyGenerator>();
/**
* the current instance.
*/
private KeyService keyService;
/**
* cache pool size
*/
private static final int KEY_CACHE = 20;
private KeyGenerator(){}
private KeyGenerator(String keyName){
keyService = new KeyService(KEY_CACHE,keyName);
}
/**
* Multiton instance
* @param keyName
* @return
*/
public synchronized static KeyGenerator getInstance(String keyName){
KeyGenerator keyGenerator;
if(KeyGenerators.containsKey(keyName)){
keyGenerator = KeyGenerators.get(keyName);
}else{
keyGenerator = new KeyGenerator(keyName);
KeyGenerators.put(keyName, keyGenerator);
}
return keyGenerator;
}
/**
* business method.
* @return
*/
public int getNextKey(){
return keyService.getNextKey();
}
}
package pattern.multiton;
/**//*
@author ZhangPu
Feb 22, 2007 6:27:22 PM
*/
public class Client {
public static void main(String[] args) {
String keyName = "SYS_RESOURCE";
KeyGenerator keyGenerator = KeyGenerator.getInstance(keyName);
for(int i=0;i<20;i++){
System.out.println("time"+(i+1)+":"+keyGenerator.getNextKey());
}
}
}