我有4个班级test1Adapter
,PStorageAdapter
,Storage
和ClientsFactory
。
test1Adapter类
public class test1Adapter extends ClientsFactory {
public Storage TestRefToSharedStorage;
public test1Adapter(Storage SharedStorage) {
this.TestRefToSharedStorage=SharedStorage;
}
public void execute(int clientID, String text) {
tempBuffer = new byte[TestRefToSharedStorage.pageSize];
long firstPageNumber = TestRefToSharedStorage.AllocatePage();
TestRefToSharedStorage.WritePage( firstPageNumber , tempBuffer );
TestRefToSharedStorage.ReadPage(firstPageNumber, tempBuffer );
TestRefToSharedStorage.DeAllocatePage(firstPageNumber);
}}
PStorageAdapter
public class PStorageAdapter extends ClientsFactory {
public int pageSize;
// create storage.
Storage MyStorage = new Storage();
public void execute(int clientID, String text) {
MyStorage.LoadStorage(filename);
MyStorage.UnloadStorage();
}
}
}
存储
public class Storage{
private String fileName;
private long fileSize;
public RandomAccessFile file;
public int pageSize;
private int bitMapSize;
public void CreateStorage(String fileName,int pageSize, int fileSize) throws Exception{
...
}
public void LoadStorage(String fileName) throws Exception{
...
}
public void ReadPage(long n, byte [] buffer) throws Exception{
...
}
public void WritePage(long n, byte[] buffer) throws Exception{
...
}
public long AllocatePage() throws Exception{
...
}
public void DeAllocatePage(long n) throws Exception{
...
}
public void printStats(){
...}}
test1Adapter
和PStorageAdapter
必须扩展ClientsFactory
类的功能。 PStorageAdapter
类使用storage
类中的某些函数。因此,我在Storage
类中创建了PStorageAdapter
类型的对象,并对其进行了访问。现在,
我想做什么?
Storage
类访问test1Adapter
类中PStorageAdapter
类的方法。其背后的原因是,应该在类型为Storage
的会话中创建单个对象,该会话应由其他PStorageAdapter
和test1Adapter
类进行调整/使用,否则它将创建两个单独的对象对我来说毫无意义。我正在尝试做的似乎不可行的方法。
我试图在test1Adapter
类中创建一个PStorageAdapter
类型的对象,并在其构造函数中传递了Storage
对象。稍后,我在Storage
类中创建了类型为test1Adapter
的引用变量,该变量指向在Storage
的构造函数中传递的上述test1Adapter
对象。如下所示
import cyclients.PStorage.Storage;
import cyclients.Test1.test1Adapter;
public class PStorageAdapter extends ClientsFactory {
// create storage.
Storage MyStorage = new Storage();
public void execute(int clientID, String text) {
MyStorage.CreateStorage(filename, pageSize, fileSize);
if (MyStorage != null) {
test1Adapter testing = new test1Adapter(MyStorage);
}
}}
这会导致运行时错误( EDIT )
java.lang.InstantiationException: cyclients.Test1.test1Adapter
at java.lang.Class.newInstance(Unknown Source)
test1Adapter---> PStorageAdapter --> Storage(base class)
。 test1Adapter
,PStorageAdapter
类已经从类ClientsFactory
继承,然后JAVA不允许这样做,因为它需要Multiple Inheritance
。此外,我需要将Storage
类实现为无法执行的接口,因为该类已在代码库中定义。 此外,目标是:
Storage
类**(总func = 8假设)**中的5个功能进入PStorageAdapter
类,但将其余部分暴露出来 8还有-5 = 3个,稍后,{strong> test1Adapter
类将以某种方式使用这3个功能。Storage
对象的单个副本中使用/调整。 PStorageAdapter
类和test1Adapter
类之间不应有任何依赖关系。例如,我将test1Adapter
类的对象创建与execute() method of storage adapter class
绑定在一起,这意味着在执行上述方法之前,将永远不会调用test1Adapter
。这不是我想要的。test1Adapter
和PStorageAdapter
必须扩展ClientsFactory
类的功能。将以上矛盾置于图片中,社区中的任何人都可以提供解决此问题的方法。预先感谢。
答案 0 :(得分:1)
这听起来像是将依赖项注入与适配器模式结合使用。依赖项注入确保不同的对象在同一Storage
实例上工作,并且适配器限制Storage
方法的公开范围。您在帖子中未描述的部分将类似于SessionManager
类,以协调工作。
例如,Storage
定义了8个公共方法:
public class Storage {
public void method1(){}
public void method2(){}
public void method3(){}
public void method4(){}
public void method5(){}
public void method6(){}
public void method7(){}
public void method8(){}
}
但是PStorage
包装了一个Storage
,只公开了方法1到5:
public class PStorage {
private final Storage storage;
public PStorage(Storage storage) {
this.storage = storage;
}
public void method1(){storage.method1();}
public void method2(){storage.method2();}
public void method3(){storage.method3();}
public void method4(){storage.method4();}
public void method5(){storage.method5();}
}
然后TestStorage
包装一个Storage
,只公开方法6到8:
public class TestStorage {
private final Storage storage;
public PStorage(Storage storage) {
this.storage = storage;
}
public void method6(){storage.method6();}
public void method7(){storage.method7();}
public void method8(){storage.method8();}
}
那么PStorageAdapter
实例上就可以使用PStorage
(这是一个好名字吗?):
public class PStorageAdapter extends ClientsFactory {
private final PStorage storage;
public PStorageAdapter(PStorage storage) {
this.storage = storage;
}
public execute(...) {
// can only call storage.method1() through storage.method5()
}
}
一个TestStorageAdapter
实例上有一个TestStorage
(这是一个好名字吗?):
public class TestStorageAdapter extends ClientsFactory {
private final TestStorage storage;
public TestStorageAdapter(TestStorage storage) {
this.storage = storage;
}
public execute(...) {
// can only call storage.method6() through storage.method8()
}
}
最后,有一些班级来协调/管理活动(可能是单身人士?)
public class SessionManager {
Map<String, Storage> sessions = new ...;
public getPStorageAdapterFor(String sessionId) {
return new PStorageAdapter(new PStorage(getStorage(sessionId)));
}
public getTestStorageAdapterFor(String sessionId) {
return new TestStorageAdapter(new TestStorage(getStorage(sessionId)));
}
private Storage getStorageFor(String sessionId) {
Storage storage = sessions.get(sessionId);
if (storage == null) {
storage = new Storage();
sessions.put(sessionId, storage);
}
return storage;
}
}