簡體   English   中英

代理設計模式中的代碼的ISubject和Operation()是什么?

[英]What is the ISubject and Operation() of the code in proxy design pattern?

我想知道使用以下代碼的含義是什么:1. ISubject和Operation()2. realSubject:RealSubject和3.代理設計模式的UML圖上的Operation()realSubject.Operation()

鏈接

//Payment.java
import java.math.*; import java.rmi.*;
public interface Payment extends Remote{
public void purchase(PaymentVO payInfo, BigDecimal price)
    throws PaymentException, RemoteException; }`

//PaymentProxy.java
import java.net.*;
import java.math.*;
import java.rmi.*;
      public class PaymentProxy implements PaymentService{
      private Payment implementation;
      private String serviceMachine = "localhost";
      private String serviceName = "paymentService";
      public PaymentProxy() throws ServiceUnavailableException{
          lookupRemoteService();
          }
private void lookupRemoteService() throws ServiceUnavailableException{
    try{
        String url = "//" + serviceMachine + "/" + serviceName;
        Object lookup = Naming.lookup(url);
            if (lookup instanceof Payment){
            implementation = (Payment)lookup;
    }
            else{
            throw new ServiceUnavailableException("Cannot locate remote service");
    }
    }
   catch (RemoteException exc){
   throw new ServiceUnavailableException("Error during remote service lookup", exc);
    }
    catch (NotBoundException exc){
    throw new ServiceUnavailableException("Remote service is not registered with naming server",     exc);
    }
    catch (MalformedURLException exc){
    throw new ServiceUnavailableException("Malformed URL for naming lookup", exc);
    }
    }
      public void setServiceMachine(String machineName){
       serviceMachine = machineName;
    }
      public void setServiceName(String svcName){
       serviceName = svcName;
      }
       public void purchase(PaymentVO pay, BigDecimal price) throws PaymentException,     ServiceUnavailableException{
     try{
    if (implementation != null){
      implementation.purchase(pay, price);
     }
   }
   catch (RemoteException exc){
   try{
      lookupRemoteService();
       implementation.purchase(pay, price);
   }
    catch (RemoteException exc2){
     throw new PaymentException("Cannot process payment: remote communication problems with payment service", exc2);
     }
   }
  }
  }`

 //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{
}
}`

PaymentProxy應該實現Payment ,而不是PaymentService

public class PaymentProxy implements Payment {

在這種情況下:

  • PaymentISubject ;
  • PaymentImplRealSubject (以便implementationPaymentProxyrealSubject字段);
  • 覆蓋的RealSubject purchase()方法對應於RealSubject Operation()

更可靠的解釋:

這兩個類都實現了Payment接口。 此外,它們每個都覆蓋了purchase()方法。 PaymentProxy包裝 ,因為它聚合PaymentImpl ,並在覆蓋方法增加了空檢查:

  public void purchase(PaymentVO pay, BigDecimal price) throws PaymentException,       ServiceUnavailableException, RemoteException{
      if (implementation != null){
         implementation.purchase(pay, price);
      }
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM