Interface Class
c#, interface, oop
Solution
One approach is to make `OrderInfo` a base class and make your provider-specific payment types subclasses of it, like so:
public class OrderInfo
{
/* For Common - Protected members are accessible to subclasses! */
protected string OrderNo {get; set;}
protected string CustomerNo { get; set;}
protected decimal Amount {get; set;}
}
public class CreditCardPaymentInfo : OrderInfo
{
/* For Credit Card */
string CCNum {get; set;}
string ExpDate { get; set;}
}
public class GooglePaymentInfo : OrderInfo
{
/* For Google */
string GoogleOrderID {get; set;}
...
}
public class PaypalPaymentInfo : OrderInfo
{
/* For Paypal */
...
}
You can then implement your Payment classes like so, and fulfil the requirements of the IPayment interface:
public class PaypalPayment : IPayment
{
public void MakePayment(PaypalPaymentInfo orderInfo)
{
...
}
public void MakeRefund (PaypalPaymentInfo orderInfo)
{
...
}
}
Since `PaypalPaymentInfo` can be used wherever `OrderInfo` is specified, this is a valid implementation of an `IPayment`. You can follow the same pattern for your credit-card and Google payment implementations.
Problem
I don't understand about using interface class clearly. I read a lot of article and tutorial about interface class related OOP so I know what is the interface but I do understand about using in real project. For example, I made IPayment interface class. and I defined 2 method that use common of all payment class. ``` public interface IPayment { void MakePayment(OrderInfo orderInfo); void MakeRefund (OrderInfo orderInfo); } ``` I made 3 payment classes that are CreditCardPayment, PaypalPayment and GooglePayment. and I defined 2 methods in each classes. I am confused in this part, I need to make OrderInfo class that contain order information that need to be used for processing payment or refund. And each class need different information. CreditCartPayment class need Credit Card No, Expiration Date .... But the other payment class are not. And GooglePayment class need Google Order number but the other class are not. So, finally the OrderInfo class has to have many extra field. And it look so dirty... Ex) ``` Public class OrderInfo { /* For Common */ string orderNo {get; set;} string customerNo { get; set;} decimal amount {get; set;} /* For Credit Card */ string CCNum {get; set;} string expDate { get; set;} /* For Google */ string googleOrderID {get; set;} ... /* For Paypal */ ... } ``` My question is, In this case, is it right to use IPayment? or I need to define each class with right parameters without Interface class? I guess the advantage of using Interface class is easy to figure out payment class in later. because the Interface class will show what methods are defined in each payment class. is there the other pros? and do you have any advice for the understanding Interface class in real world? [EDIT] Thank you for all the advice. I write the example codes again. could you please review this code? ``` public interface IPayment { void MakePayment(OrderInfo orderInfo); // !! void MakeRefund (OrderInfo orderInfo); // !! } public class OrderInfo { protected string OrderNo {get; set;} protected string CustomerNo { get; set;} protected decimal Amount {get; set;} } public class CreditCardPaymentInfo : OrderInfo { string CCNum {get; set;} string ExpDate { get; set;} } public class GooglePaymentInfo : OrderInfo { string GoogleOrderID {get; set;} } public class PaypalPaymentInfo : OrderInfo { string PaypalID {get; set;} } public void MakePayment() { IPayment paymentModule; // Get Order Info if(orderType == "Paypal"){ paymentModule = new PaypalPayment(); PaypalPaymentInfo orderInfo = new PaypalPaymentInfo(); orderInfo.PaypalID = "TEST"; }else if(orderType == "Google"){ paymentModule = new GooglePayment(); GooglePaymentInfo orderInfo = new GooglePaymentInfo(); orderInfo.GoogleOrderID = "TEST"; }else{ paymentModule = new CreditCardPayment(); CreditCardPaymentInfo orderInfo = new CreditCardPaymentInfo(); orderInfo.CCNum = "1111111111111111"; orderInfo.ExpDate = "11/11"; } orderInfo.OrderNo = "123"; orderInfo.CustomerNo = "ABC"; orderInfo.Amount = 12.20m; paymentModule.MakePayment(); } ``` It occur an error : Error 1 'com.WebUI.Models.CreditCardPaymentInfo' does not implement interface member 'com.WebUI.Models.IPaymentProcess.makeRefund(WebUI.Models.RefundModel)' I think I need to fix the Interface class. Anybody know how should I fix it please?