简体   繁体   中英

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

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 :

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


   }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM