2

、、などの各支払いモードのクラスがありCashます。関連するクラスをインスタンス化する必要があるオブジェクト値に基づいて、オブジェクトをパラメーターとして渡す必要があります。ChequeCard

どうすればこれを達成できますか?より良いデザインを提案してください

public interface CollectionInfo {
    //Code Goes here
}

public class Cash implements CollectionInfo {
    //Code goes here
}

public class CollectionFactory {
    public void newInstance(Enum<CollectionMode> collectionMode) {
    }
}

public interface Receipts {
    public Receipt createReceipt(String Amount, /*(Here i need to pass parameter of object either cash ,Cheque or card),*/Date date);
}
4

2 に答える 2

6

列挙型 (現金/小切手/カード) をファクトリに渡すことができますか?

例えば

Payment p = PaymentFactory.newInstance(PaymentMode.Cash);

そのメソッド内で次のようにします。

switch(mode) {
   case PaymentMode.Cash:
      return new CashPayment();

   // ...
}

CashPaymentなどはChequePaymentのサブクラスですPayment

于 2013-07-09T09:37:50.893 に答える
2

工場のパターンを見ることができます:

http://en.wikipedia.org/wiki/Factory_method_pattern

于 2013-07-09T09:37:56.797 に答える