做你PayPay
和Google
類代表了必需的數據描述付款?通常,付款類應代表付款。如果類的工作是過程款項,應該可能有一個名稱,如PaymentProcessor
,其界面類似IPaymentProcessor
(或實際上IPaymentService
,或類似的東西)。
如果支付類代表實際支付,那麼該類應該不需要任何參數,其MakePayment()
方法;相反,它將依靠實例數據來描述正在進行的付款。
或者,你可以有這樣的事情(仍在使用Payment
來形容支付本身):
interface IPaymentProcessor<T> where T : IPayment
{
void ProcessPayment(T payment);
}
class PayPayPaymentProcessor : IPaymentProcessor<PayPay>
{
void ProcessPayment(PayPay payment) { /* some implementation here */ }
}
class PayPayPaymentProcessor : IPaymentProcessor<Google>
{
void ProcessPayment(Google payment) { /* some implementation here */ }
}
我可能會命名類PayPayPayment
和GooglePayment
所以名稱更清楚地代表類型:
class PayPayPaymentProcessor : IPaymentProcessor<PayPayPayment>
{
void ProcessPayment(PayPayPayment payment) { /* some implementation here */ }
}
class PayPayPaymentProcessor : IPaymentProcessor<GooglePayment>
{
void ProcessPayment(GooglePayment payment) { /* some implementation here */ }
}
請注意,這與其他建議使用類的方法非常類似,但它更貼近單責任原則。在Brian Cauthon的回答中,PaymentParameters
課程必須爲任何類型的付款保留所有可能參數的聯合;在這裏,參數類型可以(也應該)特定於它們所代表的支付需求。
的界面和接口的所有實現需要具有相同的簽名(意味着相同的參數列表)。這真的沒有辦法。 – Servy 2012-04-18 14:18:21
潛在誘惑; http://stackoverflow.com/questions/6114519/how-to-create-method-interface-with-variable-parameters-different-method-signa – dash 2012-04-18 14:18:44
除了「接口類」是矛盾的; 「接口類型」或只是「接口」會更好。 – phoog 2012-04-18 14:23:42