【问题标题】:What is the ISubject and Operation() of the code in proxy design pattern?代理设计模式中代码的 ISubject 和 Operation() 是什么?
【发布时间】:2023-03-26 07:58:01
【问题描述】:

我想使用下面的代码了解 base 是什么: 1. ISubject 和 Operation() 2. realSubject:RealSubject 和 3. 操作() realSubject.Operation() 关于代理设计模式的UML图

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

【问题讨论】:

  • 请修复代码。现在不可读。
  • 我是新使用堆栈。对不起
  • ISubject/operation() 在哪里?你到底需要什么?

标签: java proxy-pattern


【解决方案1】:

PaymentProxy 应该实现 Payment 而不是 PaymentService

public class PaymentProxy implements Payment {

在这种情况下:

  • PaymentISubject;
  • PaymentImplRealSubject(所以PaymentProxy 中的implementationrealSubject 字段);
  • 覆盖的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);
      }
   }

【讨论】:

  • 我没有添加 paymentService.java
  • 导入 java.math.*;公共接口 PaymentService{ 公共无效购买(PaymentVO payInfo,BigDecimal 价格)抛出 PaymentException,ServiceUnavailableException; }
  • 如果PaymentProxy 实现了另一个接口(在这种情况下为PaymentService),这不是代理模式,因为PaymentProxyPaymentImp 可能无法推广到同一类型。
猜你喜欢
  • 1970-01-01
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2019-05-18
  • 2016-11-17
  • 2011-11-14
相关资源
最近更新 更多