Singleton Factory方法

时间:2008-12-10 13:18:02

标签: java

是否可以在工厂方法中使用单例?我有很多使用工厂方法的域名。我该如何解决这个问题。请帮我举个例子。

5 个答案:

答案 0 :(得分:6)

在这个例子中,我相信你会想要同步你的getInstance()方法,以确保两个线程不同时输入它。否则,两个线程可以在实例化单例的块内部结束,这是非常有问题的。此解决方案的唯一问题是,每次调用getInstance()时,都要为方法的同步支付额外费用。 例如:

public static synchronized Singleton getInstance()
{
   // since whole method is synchronized only 1 thread can 
   // enter the following block ensuring only one instance 
   // is ever created. however we pay a synchronization
   // penalty every time this method is called.
   if(mInstance==null) { 
      mInstance=new Singleton();
   }
   return mInstance;
}

或者你也可以切换到使用单例实例的急切初始化而不是延迟初始化,如果初始化是便宜的,这保证了并发性以及不支持调用getInstance()方法的同步惩罚。 例如:

// no synchronization penalty, but penalty for eager init
private static Singleton mInstance = new Singleton();                          

public static Singleton getInstance()
{
    return mInstance;
}

最优化的方法是使用双重检查锁定,由于1.4或更早版本的JVM中volatile关键字的不同实现,您需要Java 1.5或更高版本才能可靠地使用(请参阅“Head First Design Patterns”第5章由O'Reilly Media,Inc。出版的第182页 - 这是我第一次阅读此内容。) 例如:

private volatile static Singleton mInstance;

private Singleton(){}

public static Singleton getInstance()
{
   if(mInstance==null) {
      synchronized (Singleton.class) { 
          // only pay synchronization penalty once
          if(mInstance==null){
              mInstance=new Singleton();
          }
      }
   }
   return mInstance;
}

答案 1 :(得分:4)

“...为创建Singleton类实例的对象创建一个接口。这实际上是GoF书中Abstract Factory,Factory Method和Functor模式的组合。”

/**
 * An interface defining objects that can create Singleton
 * instances.
 */
public interface SingletonFactoryFunctor {
   /**
    * @return An instance of the Singleton.
    */
   public Singleton makeInstance();
}

答案 2 :(得分:3)

您应该从工厂方法调用Singleton getInstance()方法。 getInstance()逻辑应该处理返回Singleton的一个实例的细节。

答案 3 :(得分:2)

Singleton你可以实现如下:

public class Singleton {

private static Singleton mInstance;

private Singleton(){}

public static Singleton getInstance()
{
   if(mInstance==null){
      mInstance=new Singleton();
   }
return mInstance;
}

}

这个类你可以在你想要的每个工厂方法中返回,就像之前描述的mepcotterell一样。

答案 4 :(得分:0)

此示例不是正式的工厂模式(GoF),但如果您像Static Factory Method

那样使用它仍然有用

abstract class Product {

}

class ConcreteProduct extends Product{

}

class ProductSupportFactory {

    private static ProductSupportFactory instance = null;
    private ConcreteProduct product = null;

    static {
        instance = new ProductSupportFactory();
    }
    private ProductSupportFactory() {

        product = new ConcreteProduct(); //object initialization
    }

    public static ProductSupportFactory getInstance(){
        return instance;
    }

    public ConcreteProduct getProduct() {
        return product;
    }

    public void setProduct(ConcreteProduct product) {
        this.product = product;
    }

}

public class ProductConsumer {

    public static void main(String args[]){ //client

        ConcreteProduct uniqueInstance = ProductSupportFactory.getInstance().getProduct();
        ConcreteProduct sharedInstance = ProductSupportFactory.getInstance().getProduct(); //same object hash

    }
}

abstract class Product { } class ConcreteProduct extends Product{ } class ProductSupportFactory { private static ProductSupportFactory instance = null; private ConcreteProduct product = null; static { instance = new ProductSupportFactory(); } private ProductSupportFactory() { product = new ConcreteProduct(); //object initialization } public static ProductSupportFactory getInstance(){ return instance; } public ConcreteProduct getProduct() { return product; } public void setProduct(ConcreteProduct product) { this.product = product; } } public class ProductConsumer { public static void main(String args[]){ //client ConcreteProduct uniqueInstance = ProductSupportFactory.getInstance().getProduct(); ConcreteProduct sharedInstance = ProductSupportFactory.getInstance().getProduct(); //same object hash } }

寻找你感兴趣的贴纸↓↓↓
豫ICP备18024241号-1