jtstrategy.java

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

JAVA
122
字号


package Jt;


/**
 * Jt implementation of the Strategy pattern.
 */

public class JtStrategy extends JtObject {


  public static final String JtCLASS_NAME = JtStrategy.class.getName(); 
  private static final long serialVersionUID = 1L;
  protected Object concreteStrategy = null; // Concrete Strategy object
  public static final String JtEXECUTE_STRATEGY = "JtEXECUTE_STRATEGY"; 


  public JtStrategy () {
  }



/**
  * Specifies the reference to the concrete strategy.
  */


  public void setConcreteStrategy (Object concreteStrategy) {
     this.concreteStrategy = concreteStrategy;
  }


/**
  * Returns the reference to the concrete strategy.
  */

  public Object getConcreteStrategy () {
     return (concreteStrategy);
  }






  /**
   * Process object messages.
   * <ul>
   * <li>JtREMOVE - Performs any housekeeping that may be needed before the object
   * is removed.
   * </ul>
   */

  public Object processMessage (Object event) {

      String msgid = null;
      JtMessage e = (JtMessage) event;
      //Object content;
      //Object data;


      if (e == null)
          return null;

      msgid = (String) e.getMsgId ();

      if (msgid == null)
          return null;

      //content = e.getMsgContent();
      //data = e.getMsgData ();


      //if (msgid.equals (JtObject.JtREMOVE)) {
      //    return (null);     
      //}

      // Let the concrete strategy object handle the message

      if (concreteStrategy == null) {
          handleError ("processMessage: concreteStrategy attribute must be set");
          return (null);
      }

      return (((JtInterface) concreteStrategy).processMessage (event));


  }

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

  public static void main(String[] args) {

    JtFactory factory = new JtFactory ();
    JtStrategy strategy;
    JtObject concreteStrategy;

    // Create an instance of JtStrategy

    strategy = (JtStrategy) factory.createObject (JtStrategy.JtCLASS_NAME, "strategy");

    // Specify the concrete strategy to be executed

    concreteStrategy = (JtObject) factory.createObject (JtEcho.JtCLASS_NAME, "concreteStrategy");
    factory.setValue (strategy, "concreteStrategy", concreteStrategy);

    
    factory.sendMessage (strategy, new JtMessage (JtStrategy.JtEXECUTE_STRATEGY));

    factory.removeObject ("strategy");


  }

}


⌨️ 快捷键说明

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