0

以下のコードを使用してベースを知りたい: 1. ISubject と Operation() 2. realSubject: RealSubject と 3. Operation() Proxy デザイン パターンの UML ダイアグラムの 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{
}
}`
4

1 に答える 1

0

の代わりにPaymentProxy実装する必要があります。PaymentPaymentService

public class PaymentProxy implements Payment {

この場合:

  • PaymentですISubject
  • PaymentImplですRealSubject(フィールドもそうですimplementation);PaymentProxyrealSubject
  • オーバーライドされたpurchase()メソッドは in に対応しOperation()ますRealSubject

より堅牢な説明:

インターフェイスは、Payment両方のクラスによって実装されます。さらに、それぞれがpurchase()メソッドをオーバーライドします。ただし、オーバーライドされたメソッドで null チェックを集約して追加するため、これPaymentProxywrapperです。PaymentImpl

  public void purchase(PaymentVO pay, BigDecimal price) throws PaymentException,       ServiceUnavailableException, RemoteException{
      if (implementation != null){
         implementation.purchase(pay, price);
      }
   }
于 2014-12-13T09:22:21.683 に答える