首页 > 解决方案 > 我们可以通过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必须扩展类的功能ClientsFactoryPStorageAdapter类使用类中的一些函数storage。所以,我StoragePStorageAdapter类中创建了一个类型的对象并访问了它们。现在,

我想做的事?

我正在尝试做的似乎不是可行的方法。

此外,目标是

保持上述限制,社区中的任何人都可以提供解决此问题的方法。提前致谢。

标签: javaperformance

解决方案


这听起来像是使用依赖注入和适配器模式的组合。依赖注入确保不同的对象在同一个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;
   }

}


推荐阅读