Java - 使用注释来调用不同的函数?

时间:2016-10-12 22:39:03

标签: java annotations

我目前正在重构我用Java创建的连接到多个不同网站的聊天机器人,并且必须根据平台执行不同的操作。每个平台必须具有相同的功能,但执行方式不同,具体取决于网站API。

为了便于说明,我将使用平台AlphaBeta平台是IRC / Websocket平台,操作系统/设备。

public class MyMainClass {
  public static void main(String[] args) {
    sharedClass Alpha = new sharedClass( "Alpha" );
    sharedClass Beta  = new sharedClass( "Beta" );
  }
}

子类

/* To do so, I have created a shared class */
public class sharedClass {
  public String platformName = null;

  public sharedClass( String name ) {
    this.platformName = name;
  }

  /* Both platforms use many shared
   * functions, so I want to keep them
   * in the same class file, rather than
   * separating them.
   */
  public String sharedFunctionA() { return null; }
  public String sharedFunctionB() { return null; }
  /* ... */

  /* However, some functions that touch
   * on the APIs required picking out
   * different information.
   *
   * My goal is to get rid of these functions
   * so I can leave them as-is and not have
   * to add a new else to it every time I
   * add a new platform.
   */
  public String nitpickFunction() {

    /* Many similar functions exist,
     * where I am required to use more
     * than just two platforms.
     */
    if ( this.platformName.equals( /* Platform-Alpha */ ) ) {
      /* Do Stuff For Platform Alpha */

    } else if ( this.platformName.equals( /* Platform-Beta */ ) ) {
      /* Do Stuff For Platform Beta */

    } /* else if ( Platform-Gamma ) {
    } else if ( Platform-Delta ) {
    } else if ( Platform-Epsilon ) {
    } else if ( Platform-Zeta ) {
    } ...
    */

  }

  public String properFunction() {
    /* My hope (New To Creating Annotations)
     * is to do something similar to the following,
     * so that I can call what would have been inside
     * an if/else block, from the platforms respective
     * class file.
     */
    @DoPlatformNitpick
    myNitpickFunction();
    /*
     * Or just something like this
     */
    MyPlatform mySelectedFunction = new MyPlatform();
    mySelectedFunction.nitpick();
    /* However I believe that would require my
     * Alpha and Beta platforms to use the same
     * class type.
     */
  }

  /* Please ignore the possible NullPointerException,
   * this is just an example.
   */
}

有人可以解释一下如何解决这个问题,或者它是否可行? 评论我是否需要更好地解释。我搜索了一下,看看我是否能找到类似的东西,但可能错过了什么。

1 个答案:

答案 0 :(得分:0)

有多种方法可以做到这一点,每种方法都可以被视为一种设计模式。

抽象类:

对于类似我通常使用抽象类而不是注释的东西。

在抽象类中,您可以决定父类为哪些函数提供实现以及哪些子类。您可以执行正式操作并创建名为AlphaBeta的新类,也可以执行匿名类。以下是匿名类的示例。

static abstract class Parent {

    public void sharedFunction() {

    }

    abstract void nitpick();

}

public static void main(String [] args) {

    Parent alpha = new Parent() {
        @Override
        void nitpick() {
            /*
            alpha specific code here
            */
        }
    };

}

以下是使用Beta与匿名类

的正式类的示例
    class Beta extends Parent {

    @Override
    void nitpick() {
        /*
        beta speicfic code goes here
        */
    }

}

功能界面:

与抽象类相似,但有一些限制。共享函数必须使用default子句。接口有一个必须实现的方法。

    static interface Parent {

    default public void sharedFunction() {
    }

    abstract void nitpick();

}

public static void main(String[] args) {

    Parent alpha = () -> {/* alpha specific code goes here*/
    };

}

将实现作为输入传递:

在这种方法中,nitpick函数的实现被传递给Parent类'构造函数。

    static class Parent {

    public void sharedFunction() {
    }

    final Runnable nitpick;

    public void nitPick() {
        nitpick.run();
    }

    public Parent(Runnable nitpick) {
        this.nitpick = nitpick;
    }

}

public static void main(String[] args) {

    Parent alpha = new Parent(() -> {/* alpha specific code goes here*/});

}