java

位置:IT落伍者 >> java >> 浏览文章

Java与模式之单例模式


发布日期:2021年11月11日
 
Java与模式之单例模式

单例模式(Singleton) 属于最常见的设计模式之一大部分系统都会用到目的是为了维护系统中唯一的一个实例

可分为eager模式示例代码如下

Java代码

class EagerSingleton{

private static final EagerSingleton m_instance = new EagerSingleton()

private EagerSingleton(){}

public static EagerSingleton getInstance(){

return m_instance;

}

}

class EagerSingleton{

private static final EagerSingleton m_instance = new EagerSingleton()

private EagerSingleton(){}

public static EagerSingleton getInstance(){

return m_instance;

}

}

和 lazy模式示例代码如下

Java代码

class LazySingleton{

private static LazySingleton m_instance = null;

private LazySingleton(){}

public synchronized static getInstance(){

if(m_instance == null){

m_instance = new LazySingleton()

}

return m_instance;

}

}

class LazySingleton{

private static LazySingleton m_instance = null;

private LazySingleton(){}

public synchronized static getInstance(){

if(m_instance == null){

m_instance = new LazySingleton()

}

return m_instance;

}

}

java源码中RuntimegetRuntime()就是单例的一个例子

单例模式的精神就是整个系统中维护一个实例推广开来如果在一个系统中需要维护多个示例那么就产生了多例模式(multiton)

多例模式(Multiton) 通过聚集对象了保留自身的多个示例根据客户端的参数返回所需要的实例

示例代码如下

Java代码

class Multiton{

private final int INSTANCE_SIZE = ;

private static Map instances = new HashMap(INSTANCE_SIZE)

private String name;

private Multiton(){}

private Multiton(String name){

thisname = name;

}

public synchronized static getInstance(String name){

if(ntainsKey(name)){

return instancesget(name)

}

else{

ins = new Multiton(name)

instancesput(name ins)

return ins;

}

}

}

class Multiton{

private final int INSTANCE_SIZE = ;

private static Map instances = new HashMap(INSTANCE_SIZE)

private String name;

private Multiton(){}

private Multiton(String name){

thisname = name;

}

public synchronized static getInstance(String name){

if(ntainsKey(name)){

return instancesget(name)

}

else{

ins = new Multiton(name)

instancesput(name ins)

return ins;

}

}

}

[nextpage]

一个实用的例子就是KeyGenerator 示例代码如下

Java代码

class KeyGenerator{

private final int POOL_SIZE = ;

private static Map instances = new HashMap(

private KeyInfo keyinfo;

private KeyGenerator(){}

private KeyGenerator(String keyName){

thiskeyinfo = new KeyInfo(POOL_SIZE keyName)

}

public synchronized static getInstance(String keyName){

if(ntainsKey(keyName)){

return (KeyGenerator)instancesget(keyName)

}

else{

keyGen = new KeyGenerator(keyName)

instancesput(name keyGen)

return keyGen;

}

}

public synzhronized int getNextKey(){

return keyinfogetNextKey()

}

}

class KeyGenerator{

private final int POOL_SIZE = ;

private static Map instances = new HashMap(

private KeyInfo keyinfo;

private KeyGenerator(){}

private KeyGenerator(String keyName){

thiskeyinfo = new KeyInfo(POOL_SIZE keyName)

}

public synchronized static getInstance(String keyName){

if(ntainsKey(keyName)){

return (KeyGenerator)instancesget(keyName)

}

else{

keyGen = new KeyGenerator(keyName)

instancesput(name keyGen)

return keyGen;

}

}

public synzhronized int getNextKey(){

return keyinfogetNextKey()

}

}

其实用单例模式也照样可以实现KeyGenerator 无非是把聚集里的KeyGenerator实例换成KeyInfo示例而已

无论是用单例模式还是多例模式都可以做到同样的一件事情

               

上一篇:使用内部类让Java实现“多继承”

下一篇:java的10种设计模式介绍