java version 1.7.0_65
我有一个单例设计模式类。这将始终返回最初创建的相同实例。
但是,我遇到的问题是这个类需要从另一个类创建许多其他对象。我为此使用了组合(POI
类的实例ArlabFacade
)。从这个单例实例中,客户端应该能够创建许多 POI 对象。而且我不想暴露 POI 类的内部工作原理,一切都必须通过单例实例。
private static ArlabFacade mArlabFacade = null;
private POI mPoi; /* Should be able to create many object of this class */
private ArlabFacade(Context context) {
/* initialize properties */
mContext = context;
mPoi = null;
}
public static ArlabFacade getInstance(Context context) {
/* Create a synchronised singleton instance */
ReentrantLock lock = new ReentrantLock();
lock.lock();
if(mArlabFacade == null) {
mArlabFacade = new ArlabFacade(context);
}
lock.unlock();
return mArlabFacade;
}
我试过做这样的事情,但它有两个问题。
1) I don't want to return the class instance of POI
2) because I only have a single instance the mPoi will be overwritten by the next client call to this function.
这个函数只会覆盖:
public POI createNewPOI() {
return mPoi = new POI();
}
有没有解决这个问题的设计模式?