Please help me to create the main class of this proxy design pattern?
//Filename:Payment.java
import java.math.*; import java.rmi.*;
public interface Payment extends Remote{
public void purchase(PaymentVO payInfo, BigDecimal price)
throws PaymentException, RemoteException; }
//Filename: PaymentException.java
`public class PaymentException extends Exception{
public PaymentException(String m){
super(m);
}
public PaymentException(String m, Throwable c){
super(m, c);
}
}`
//Filename:PaymentImpl.java
`import java.math.*;
import java.net.*;
import java.rmi.*;
import java.rmi.server.*;
public class PaymentImpl implements Payment{
private static final String PAYMENT_SERVICE_NAME = "paymentService";
public PaymentImpl() throws RemoteException, MalformedURLException{
UnicastRemoteObject.exportObject(this);
Naming.rebind(PAYMENT_SERVICE_NAME, this);
}
public void purchase(PaymentVO payInfo, BigDecimal price)
throws PaymentException{
}
}`
//Filename: PaymentService.java
import java.math.*;
public interface PaymentService{
public void purchase(PaymentVO payInfo, BigDecimal price)
throws PaymentException, ServiceUnavailableException;
}
//Filename: PaymentVO.java
public class PaymentVO{
}
//Filename: ServiceUnavailableException.java
public class ServiceUnavailableException extends Exception{
public ServiceUnavailableException(String m){
super(m);
}
public ServiceUnavailableException(String m, Throwable c){
super(m, c);
}
}
Refer below the class diagram for Proxy design pattern from : http://www.codeproject.com/Articles/186001/Proxy-Design-Pattern
In your question you have a Payment interface which is the "Subject". Then you have the PaymentImpl which implements Payment interface and this is the "Real Subject". However you are missing the "Proxy" here.
You need to write a PaymentProxy class which will also implement Payment interface. This class will have a reference to an object of type "Real Subject" (PaymentImpl) as a private field and will call the methods inherited from the "Subject" via this "Real Subject" object.
Example :
You may notice that the realPayment is created on demand when using the Proxy Design pattern. This is useful when object creation is expensive.
Following is the code for the main class :
}