decoratedhelloworld.java

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

JAVA
105
字号
package Jt.examples.patterns;

import Jt.*;
import Jt.examples.HelloWorld;

/*
 * Demonstrates the use of JtDecorator
 */

public class DecoratedHelloWorld extends JtDecorator {

  public static final String JtCLASS_NAME = DecoratedHelloWorld.class.getName(); 
  public static final String JtHOLA = "JtHOLA";
  private static final long serialVersionUID = 1L;

  private String greetingMessage;

  public DecoratedHelloWorld() {
  }


  // Process object messages

  public Object processMessage (Object message) {

   String msgid = null;
   JtMessage msg = (JtMessage) message;
   Object obj;
   JtFactory factory = new JtFactory ();


     if (msg == null)
	  return null;

     msgid = (String) msg.getMsgId ();

     if (msgid == null)
	  return null;


     // Process JtHOLA (new functionality/messages)

     if (msgid.equals (DecoratedHelloWorld.JtHOLA)) {

        if (greetingMessage == null)
            greetingMessage = "Hola mundo ...";
        
        handleTrace ("HelloWorld returning a greeting message: " +  greetingMessage);
             
        return (greetingMessage);
     }
     
     // Original functionality 
     
     
     if (this.getComponent() == null) {
       obj = factory.createObject (HelloWorld.JtCLASS_NAME, "helloWorld");
       this.setComponent(obj);
     }
     
     return (((JtInterface) component).processMessage (msg));

  }


  // Demontrates the use of JtDecorator. This version of HelloWorld is able to handle an
  // additional language.

  public static void main(String[] args) {

    JtFactory factory = new JtFactory (); 
    String reply;
    JtDecorator decorator;

    // Create helloWorld (DecoratedHelloWorld class)

    decorator = (JtDecorator) factory.createObject (DecoratedHelloWorld.JtCLASS_NAME, 
            "newHelloWorld");

    // Send the Message

    reply = (String) factory.sendMessage (decorator, new JtMessage (HelloWorld.JtHELLO));

    // Print the reply message (Greeting)

    System.out.println (reply); 
  
    // Try the new functionality provided by JtHOLA

    reply = (String) factory.sendMessage (decorator, new JtMessage (DecoratedHelloWorld.JtHOLA));

    // Print the reply message
    System.out.println (reply);   

    // Remove helloWorld

    factory.removeObject (decorator);        

  }

}



⌨️ 快捷键说明

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