【发布时间】:2014-01-27 19:09:18
【问题描述】:
我有一个 ObjectFactory 和一个实现该工厂的特殊案例。我无法更改具有 0 参数的接口。 在其中一种实现中,我必须读取文件并加载一些数据。要传递文件名,我可以使用系统属性,因为我只需要共享一个字符串。 但在另一种实现中,我必须不是从文件开始,而是从内存结构开始。如何将对象(然后我认为是对象引用)传递给工厂?其他方法?我无法序列化文件上的对象,并且在我再次读取它之后,因为我想要避免的是正确的 I/O 占用空间。 谢谢
好的,更多信息:
这是我必须实现的接口和抽象工厂
public abstract interface A
{
public abstract Set<Foo> getFoo();
public abstract Set<Bar> getBar();
}
//this is otherpackage.AFactory
public abstract class AFactory
{
public static AccessFactory newInstance()
{
return a new built instance of the factory
}
public abstract A newA();
}
这是我的问题的实现:
public class AFactory extends otherpackage.AFactory
{
@Override
public Access newA()
{
return new AA();
}
}
public class AA implements A
{
protected AA()
{
this.objectReferenceIWantToSaveHere = I retrieve from the shared memory zone;
use the object
}
}
现在我想做这样的事情:
B b = something I built before
save b in a shared memory zone or something like that
otherpackage.AFactory f = mypackage.AccessFactory.newInstance();
A a = f.newA();
在 the f.newA() 调用中我想访问 b 对象
【问题讨论】:
-
请发布您的代码。我无法理解你的要求。您可以将引用传递给工厂构造函数。
-
好的,添加了一个例子。你应该明白我不能简单地传递引用,因为我必须实现一个接口。
-
您可以添加一个指定
setB(B)的接口,然后您可以在那里传递引用。或者,您可以使用Singleton。