【问题标题】:what is the code for main class in proxy design pattern?代理设计模式中主类的代码是什么?
【发布时间】:2014-12-05 05:39:07
【问题描述】:

请帮我创建这个代理设计模式的主类?

//文件名:Payment.java

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

//文件名:PaymentException.java

    `public class PaymentException extends Exception{
    public PaymentException(String m){
        super(m);
    }
    public PaymentException(String m, Throwable c){
        super(m, c);
    }
}`

//文件名: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{
}

}`

//文件名:PaymentService.java

   import java.math.*;
public interface PaymentService{
    public void purchase(PaymentVO payInfo, BigDecimal price)
      throws PaymentException, ServiceUnavailableException;
}

//文件名:PaymentVO.java

    public class PaymentVO{
}

//文件名:ServiceUnavailableException.java

public class ServiceUnavailableException extends Exception{
public ServiceUnavailableException(String m){
    super(m);
}
public ServiceUnavailableException(String m, Throwable c){
    super(m, c);
}

}

【问题讨论】:

  • 您似乎在这里错过了一些课程。可以查吗?

标签: java design-patterns proxy-pattern


【解决方案1】:

请参阅下面的代理设计模式类图:http://www.codeproject.com/Articles/186001/Proxy-Design-Pattern

在您的问题中,您有一个支付界面,即“主题”。然后你就有了实现 Payment 接口的 PaymentImpl,这是“真正的主题”。但是,您在这里缺少“代理”。

您需要编写一个 PaymentProxy 类,该类也将实现 Payment 接口。此类将引用“Real Subject”(PaymentImpl)类型的对象作为私有字段,并将通过此“Real Subject”对象调用从“Subject”继承的方法。

例子:

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);
    }
}

您可能会注意到,在使用代理设计模式时,realPayment 是按需创建的。这在对象创建成本很高时很有用。

以下是主类的代码:

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


   }

}

【讨论】:

  • 您从哪里获得 BigDecimal?我能问一下主课是什么吗?
  • Big Decimal 是您在问题中的购买方法中所拥有的。在 main 方法中,您需要创建 PaymentProxy 的实例并使用。这将在一个单独的类中,由上图中的“客户端”引用。
  • 添加了主要方法,根据您问题中的新编辑。 :)
猜你喜欢
  • 2023-03-26
  • 2011-01-16
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多