java - Is it possible to call diffrent instances from static util
问题描述
Update: I rewrite my question with more details.
Class Disk is a third party with long init time so I want to create it only once as a static member. Since I can't change it I want to wrap it with my own logic as a utility class.
Lets say I have created utilities:
class DiskUtilC {
static Disk disk = new Disk("c:\\");
List static searchFile(String name) {
return soSomething(disk.search(name));
}
...many more static methods ...
}
class DiskUtilD {
static Disk disk = new Disk("d:\\");
List static searchFile(String name) {
return soSomething(disk.search(name));
}
...many more static methods ...
}
Since both classes use the same static method I want move them all to a common location but keep the same usage i.e.
DiskUtilC.searchFile("xxx");
DiskUtilD.searchFile("xxx");
It's not possible to use polymorphism with static I'm not sure if it is possible.
I know that I can always do something like this DiskUtil.getInstance("c").searchFile("xxx") but it's too cumbersome. Is there a way to do it like I demonstrated above?
解决方案
There isn't any benefit to defining two classes DiskUtilC and DiskUtilD with identical code and different static fields - one may edited out of step with the other.
If the handling logic for C+D drives is identical (that is they don't need different soSomething
implementations) you could declare one DiskUtil class with 2 static fields for C and D drives and instance methods such as getName
/ searchFile
. Example:
class DiskUtil {
static final DiskUtil C = new DiskUtil("c:\\");
static final DiskUtil D = new DiskUtil("d:\\");
private final Disk disk;
private DiskUtil(String root) {
disk = new Disk(root)
}
void getName() {
return disk.name();
}
List searchFile(String name) {
return soSomething(disk.search(name));
}
...many more... (not-static methods)
}
Then the usage is via DiskUtil:
DiskUtil.C.searchFile("xxx");
DiskUtil.D.searchFile("xxx");
But it's probably better to use DiskUtil.getInstance("c")
or similar so you can make getInstance
initialise any drives as necessary on demand on first use rather than hardwire C/D only. An example on demand load can also setup say C()
to simplify typing:
private static final ConcurrentHashMap<String,DiskUtil> DISKS = new ConcurrentHashMap<>();
private static DiskUtil getInstance(String drive) {
return DISKS.computeIfAbsent(drive.toLowerCase(), letter -> new DiskUtil(letter+":\\"));
}
// Handy access to C:
static DiskUtil C() { return getInstance("C"); }
推荐阅读
- c# - 为什么显式实现的接口成员是私有的并且与接口定义中的“公共”不一致
- javascript - 如何重新创建这种预加载效果?
- c# - Unity 教程中的循环引用
- c++ - 构造函数列表以特定顺序与指针
- android - 使用 ProGuard 缩小代码后,我在 APK 分析器中的哪里可以找到我的 BuildConfig?
- c# - 使用 ISerializable 接口处理可序列化类时如何修复内部 NullReferenceException?
- mongoose-schema - 如何根据 feathersjs 中的模型模式验证 context.data?
- python - 如何连接多个具有长索引的数据帧而不会出错?
- android - Android - 如何在主页按钮最小化后几秒钟重新打开应用程序?
- java - 为什么我的 GPA 方法在传递正确的参数后不能正确计算?