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?
An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface. Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types.
In Java, an interface specifies the behavior of a class by providing an abstract type. As one of Java's core concepts, abstraction, polymorphism, and multiple inheritance are supported through this technology. Interfaces are used in Java to achieve abstraction.
Differences between a Class and an Interface:A class can be instantiated i.e, objects of a class can be created. An Interface cannot be instantiated i.e, objects cannot be created. Classes does not support multiple inheritance. Interface supports multiple inheritance.
An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a class. A Java interface contains static constants and abstract methods. The interface in Java is a mechanism to achieve abstraction.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With