what is the code for main class in proxy design pattern?

566 views Asked by At

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);
}

}

1

There are 1 answers

3
maheeka On BEST ANSWER

Refer below the class diagram for Proxy design pattern from : http://www.codeproject.com/Articles/186001/Proxy-Design-Patternenter image description here

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 :

public class PaymentProxy implements Payment{

    private PaymentImpl realPayment;

    public PaymentProxy(){

    }

    public void purchase(PaymentVO payInfo, BigDecimal price) throws PaymentException{
       if(realPayment==null){
           realPayment = new PaymentImpl();
       }
       realPayment.purchase(payInfo, price);
    }
}

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 :

public class Client{
   public static void main(String[] args){

      PaymentProxy paymentProxy = new PaymentProxy(); //note that the real proxy object is not yet created
      //... code for resolving payInfo and price as per the requirement
      paymentProxy.purchase(payInfo, price); //this is where the real payment object of type PaymentImpl is created and invoked


   }

}