1

我遇到了一个编程问题:

我的 Eclipse IDE 中有两个Java项目:ProjectA 和 ProjectB。项目 B 引用项目 A

我在 ProjectA 中声明了一个类:ClassA,在 ProjectB 中声明了一个类:ClassB,这样:

public class ClassA{
  public static Object foo(){
   //blah
  }
}

public class ClassB extends ClassA{
  public static Object foo(){
    //blah
  }
}

我在 ProjectA 中还有一个名为 ClientClass 的类。这个 ClientClass 之前用于创建 ClassA 的实例并使用它。但是现在,根据环境设置,应该为 ClientClass 提供使用 ClassA 或 ClassB 的选项。

这似乎是 AbstractFactory 模式的一个问题,或者我是这么认为的。我需要创建一个提供对 ClassA 或 ClassB 的访问的工厂。ClientClass 不应该知道它是 ClassA 还是 ClassB。这需要我为 ClassA 和 ClassB 创建一个接口。

我遇到的问题

  • ClientClass 不能直接引用 ClassB(没有导入语句/或新调用),因为 ClassB 在不同的项目中。这可能是 Eclipse IDE 的限制,但在将这两个项目作为 jar 文件查看时,这对我来说也很有意义。循环关系是可以避免的。
  • 我无法为 ClassA 和 ClassB 创建工厂接口和通用接口,然后通过 AbstractFactory 模式提供 ClassAFactory 或 ClassBFactory。这是因为要在 ClassA 和 ClassB 上调用的方法是静态方法。这些类的方法需要出现在接口上。但是,在 Java 中,不能有“抽象静态”修饰符

谁能为这个问题提出一个优雅的解决方案?

4

2 回答 2

2

Well, there are a number of problems here. For starters, and this is the biggest one, this isn't going to work because you can't override static methods in Java. The goal, I think, of what you're saying is to be able to substitute, at run time, ClassA for ClassB or B or A or whatever, depending on some parameters. In order to do that, you need to be able to take advantage of dynamic dispatch (or, simply put, virtual methods) that would allow the runtime system to select the memory address of the method to be executed at run time. This isn't possible with static methods. You need to make the methods nonstatic for this to work. You don't have to specifically design a Java Interface but when you extend ClassA with ClassB, you'll be able to treat your objects as if they are simply a ClassA object.

All of that said, if you remove the static modifiers from the methods and make them nonstatic, then you could use ClassB in project A without having any import statements what-so-ever in the client class you're talking about. However, somewhere in project A, somebody is going to need to be aware of ClassB in order to instantiate it. That is, of course, unless you want to do some runtime binding stuff and load the class dynamically using a string. Does that make sense?

于 2011-06-29T17:46:14.023 回答
0

Firs problem: Your Class B and Class A's foo method is static. Hence nothing is being overriden. You should not make them static if you intend to override foo in ClassB.

The second problem here is that upstream needs to be aware of the downstream. That is just wrong isn't it?

The question of whether this is an abstract factor pattern or not is besides the point. Design patterns just make code follow a known structure. It is not an end in itself.

For now, why is it that your ClientClass in Project A needs to know about ClientB?

As for the factory, your factory needs to be in Project B and it can be something like:

class Factory {

public static ClassA createTheRightOne(EnvironmentSettings settings) { //do the right thing }

}

once you fix the static modifier

  • Pavan
于 2011-06-29T17:57:22.227 回答