I do not understand how I can use the class interface. I have read many articles and tutorials about OOP related to an interface class, so I know what an interface is, but I understand how to use it in a real project.
For instance,
I made an IPayment interface class. and I have defined 2 methods that use common to all payment classes.
public interface IPayment { void MakePayment(OrderInfo orderInfo); void MakeRefund (OrderInfo orderInfo); }
I made 3 classes of payments, which are CreditCardPayment, PaypalPayment and GooglePayment.
and I defined 2 methods in each class.
I got confused in this part, I need to create an OrderInfo class that contains order information that should be used to process the payment or refund. And each class needs different information.
CreditCartPayment class requires credit card No, expiry date .... But there is no other payment class.
And the GooglePayment class needs a Google order number, but there is no other class.
So finally, the OrderInfo class should have a lot of extra field. And it looks so dirty ...
Ex)
Public class OrderInfo { string orderNo {get; set;} string customerNo { get; set;} decimal amount {get; set;} string CCNum {get; set;} string expDate { get; set;} string googleOrderID {get; set;} ... ... }
My question is:
In this case, is it right to use IPayment? or do I need to define each class with the correct parameters without an interface class?
I assume that the advantage of using an interface class is to easily define a payment class later. because the Interface class will show which methods are defined in each payment class. are there any other pros?
and do you have any tips for understanding the interface of an interface in the real world?
[EDIT]
Thanks for all the tips.
I am writing code examples again. could you view 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(); }
An error occurs:
Error 1 'com.WebUI.Models.CreditCardPaymentInfo' does not implement the member of the interface 'com.WebUI.Models.IPaymentProcess.makeRefund (WebUI.Models.RefundModel)'
I think I need to fix the interface class. Does anyone know how to fix it?