0

my query is about architecture and relation of classes, I would like to know better solution than I have implemented.

I have 4 classes , explaining one line for each

  1. core.php class which will have all common functions
  2. A.php class file which contains specific function for task A like writing on csv file A.
  3. B.php class file which contains specific function for task B like writing another csv file which is totally different from csv A.
  4. X.php class which is another task specific class.

Now come to general script which will use all 4 classes. so I am not understanding which class will extend which another class

No relation between A.php and B.php

But x.php must use all 3 classes core.php, A.php and B.php.

currently what I have done , I design one interface for core class, and x.php extend core class and implement interface. I design A.php and B.php as static classes.

Please guide

4

2 に答える 2

2

互いに関係がない場合は、クラスを作成する必要 はありません。これらの 2 つのクラスが継承関係やインターフェイスに関する関係を持たずに、あるクラスを別のクラス内でextend単純に使用できます。

次のように構成できます。

  • class Commonすべてのクラスが継承する基本クラスであり、すべてのクラスが必要とする共通のコード (ロギングやデータベース アクセスのコードなど) が含まれています。
  • class A extends Common一部の CSV 関連のタスクに特化しています。このクラスは、それ自体で実際に何かを行う場合としない場合があります。
  • class B extends ACSV を含む特定のタスクにさらに特化しています。
  • class X extends Commonそしてまた別の仕事です。

一般に、クラスがほとんど同じことを行うが、わずかに特殊化されているか異なる場合はextend、共通の基本クラスです。クラスに別のクラスとの共通点がまったくない場合は、それらをスタンドアロン クラスにします。interfacesポリモーフィズムに使用します。それはあなたが今必要としているものではないようです。

于 2012-07-03T10:46:10.137 に答える
0

これは、基本的な依存関係管理のように思えます。なぜ明示的に何かを拡張したいのかわかりませんか?

おそらく代わりにすべきことは、必要な依存関係を注入することです。インスタンスCore化し、依存しているオブジェクトでインスタンス化する前AB個別に。X

于 2012-07-03T10:48:09.177 に答える