java - 我们可以通过java中的另一个引用创建一个引用吗?
问题描述
我有 4 个班级test1Adapter
, PStorageAdapter
, Storage
& ClientsFactory
。
test1适配器类
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);
}}
存储适配器
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)
- 另一种方法是在 3 级实现继承。
test1Adapter---> PStorageAdapter --> Storage(base class)
. ,类已经从该类继承test1Adapter
,那么 JAVA 不允许这样做,因为它需要。此外,我需要将该类实现为我无法执行的接口,因为该类已在代码库中定义。PStorageAdapter
ClientsFactory
Multiple Inheritance
Storage
此外,目标是:
- 使用,假设
Storage
类**(Total func = 8假设)**的5个功能进入PStorageAdapter
类,但也暴露其余8-5 = 3个,稍后这3个功能将以某种方式被类使用。test1Adapter
Storage
上面定义的目标应该只在会话中创建的对象的单个副本中使用/调整。PStorageAdapter
类和类之间不应该有任何依赖关系test1Adapter
。例如,我绑定了test1Adapter
类withing的对象创建execute() method of storage adapter class
,这意味着在我执行上述方法之前,test1Adapter
永远不会被调用。!。这不是我想要的。- 此外,
test1Adapter
&PStorageAdapter
必须扩展ClientsFactory
类的功能。
保持上述限制,社区中的任何人都可以提供解决此问题的方法。提前致谢。
解决方案
这听起来像是使用依赖注入和适配器模式的组合。依赖注入确保不同的对象在同一个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
包装 aStorage
只公开方法 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
包装 aStorage
以仅公开方法 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()
}
}
并且 a 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;
}
}
推荐阅读
- google-apps-script - 有没有办法通过 Google Docs GUI 定义命名范围?
- laravel - 如何在 laravel 5.8.23 版本中创建创建视图的命令?
- python - 如何根据配置文件中指定的内容启用脚本中的功能?
- angular-material - 如何始终在数字类型的 matInput 中显示“增量箭头”?
- c# - 异步方法不将控制权返回给调用方方法
- qt - Logs via qDebug() are invisible when run via docker-compose
- python - 网络x中最短路径长度的标准偏差
- javascript - 屏幕不滚动 Expo web
- react-native - 如何在屏幕中间位置在 React-Native 上制作标签
- sql-server - 遇到异常。这可能是由扩展引起的?如何在我的 SQL Server Management Studio 中解决此问题