jtproxy.java

来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 105 行

JAVA
105
字号
package Jt;

import Jt.examples.HelloWorld;



/**
 * Jt Implementation of the Proxy pattern.
 */


public class JtProxy extends JtObject {

  public static final String JtCLASS_NAME = JtProxy.class.getName(); 
  private static final long serialVersionUID = 1L;
  private Object subject;

  public JtProxy() {
  }

/**
  * Specifies the proxy's subject.
  *
  * @param subject subject
  */

  public void setSubject (Object subject) {
     this.subject = subject; 

  }

/**
  * Returns the proxy's subject.
  */

  public Object getSubject () {
     return (subject);
  }




  /**
    * Process object messages by forwarding them to the proxy's subject. 

    * @param event Jt Message    
    */

  public Object processMessage (Object event) {

   //String msgid = null;
   //JtMessage e = (JtMessage) event;
   //Object content;
   //JtFactory factory = new JtFactory ();

     // Let the subject process the request

     if (subject == null) {
       handleError ("JtProxy.process: the subject attribute needs to be set");
       return (null);
     }


     //return (factory.sendMessage (subject, event));
     return (((JtInterface) subject).processMessage(event));


  }

  /**
    * Demonstrates the messages processed by JtProxy.   
    */

  public static void main(String[] args) {

    JtFactory main = new JtFactory ();
    JtMessage msg;
    HelloWorld helloWorld;
    JtProxy proxy;


    // Create an instance of JtProxy

    proxy = (JtProxy)
      main.createObject (JtProxy.JtCLASS_NAME, 
      "proxy");
    helloWorld = (HelloWorld) main.createObject (HelloWorld.JtCLASS_NAME, 
      "helloWorld");

    //main.setValue (proxy, "subject", helloWorld);  
    proxy.setSubject(helloWorld); // this is the correct way of setting the attribute
                                  // subject
    

    msg = new JtMessage(HelloWorld.JtHELLO);
    
    // Send a message to the Proxy
    
    System.out.println ("Reply:" + main.sendMessage (proxy, msg));
         

  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?