2014-12-13 49 views
0

我想用下面是什麼代碼就知道基地: 1. ISubject和操作() 2. realSubject:RealSubject和 3.操作() realSubject.Operation() 代理設計的UML圖上模式什麼是代理設計模式中的代碼的ISubject和Operation()?

link

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

請修改代碼。現在它是不可讀的。 – 2014-12-13 09:01:52

+0

我是新的使用堆棧。對不起 – 2014-12-13 09:08:04

+0

ISubject/operation()在哪裏?你究竟需要什麼? – SMA 2014-12-13 09:16:09

回答

0

PaymentProxy應該實現Payment而不是PaymentService

public class PaymentProxy implements Payment { 

在這種情況下:

  • PaymentISubject;
  • PaymentImplRealSubject(所以implementationPaymentProxyrealSubject字段);
  • 重寫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); 
     } 
    } 
+0

我沒有添加paymentService.java – 2014-12-13 10:36:49

+0

import java.math。*; public interface PaymentService {0} {0} {0} public void purchase(PaymentVO payInfo,BigDecimal price) throws PaymentException,ServiceUnavailableException; } – 2014-12-13 10:38:52

+0

如果'PaymentProxy'實現了另一個iterface(在這種情況下爲'PaymentService'),那麼** NOT **是一個代理模式,因爲PaymentProxy和Pa​​ymentImp不能被推廣到相同的類型。 – 2014-12-13 10:45:06