1

I have two classes (D and E) that have some common methods, the only difference is that D instantiates a singleton (A) and E instantiates another singleton(B).

E inherits from D and since the methods exec the same code, I' d like to check in D if I should create A or B singleton for not rewriting in E the same code of every common method...

A and B have inheritance from C abstract class...

Any advices?

Thanks in advance...

//Check subtype
if ....
  A field = A.getInstance().getMethod();
else...
  B field = B.getInstance().getMethod();

field.set...
4

3 に答える 3

0

私ができる最善のアドバイスは、OO 設計に関する優れた本を読むことです。

さらに言えば、継承は、父と息子の間の真の関係が「is a」関係である場合にのみ使用する必要があります。コードの再利用は、他の方法で実現できます (たとえば、両方で使用されるユーティリティ クラス)。

シングルトンのみを作成して使用するメソッドを D で作成できます。D で 1 つの方法で実装し、E で別の実装でオーバーライドします。

于 2013-10-28T09:19:13.723 に答える
0

シングルトン パラメトリックを作成するメソッドを作成し、単一のクラス (D と E の代わりに) を使用してジョブを実行できないでしょうか?

于 2013-10-28T09:24:26.670 に答える