简体   繁体   English

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

[英]Java - Using annotations to call different functions?

I am currently refactoring a chat bot that I've created in Java that connects to multiple different websites, and must carry out actions differently depending on the platform. 我目前正在重构用Java创建的聊天机器人,该聊天机器人可以连接到多个不同的网站,并且必须根据平台采取不同的动作。 Each platform must function the same, but is simply executed differently, depending on that websites API. 每个平台必须具有相同的功能,但根据网站API的不同,它们的执行方式也不同。

For explanation, I will use platforms Alpha and Beta ( Platforms being IRC/Websocket platforms, not Operating Systems / Devices. ) 作为解释,我将使用AlphaBeta 平台平台是IRC / Websocket平台, 而不是操作系统/设备。

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

Sub Class 子类

/* 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.
   */
}

Could somebody explain a bit on how I could pull this off, or whether it's even possible or not? 有人可以解释一下我如何实现这一目标,或者是否有可能? Comment if I need to explain better. 如果我需要更好地解释,请发表评论。 I searched around a bit to see if I could find something similar, but may have missed something. 我四处搜寻以查看是否可以找到类似的东西,但是可能错过了一些东西。

There are multiple ways to do this, each can be considered a design pattern. 有多种方法可以执行此操作,每种方法都可以视为一种设计模式。

Abstract class: 抽象类:

For something like I would normally use abstract class over annotation. 对于类似的东西,我通常会在注释上使用抽象类。

In an abstract class you can decide which functions the parent class provides the implementation for and which the child classes. 在抽象类中,您可以确定父类提供哪些功能的实现以及子类提供哪些功能。 You can do something formal and create new classes called Alpha and Beta or you can do anonymous classes. 您可以执行正式的操作并创建称为AlphaBeta新类,也可以执行匿名类。 Below is example of anonymous class. 以下是匿名类的示例。

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
            */
        }
    };

}

Below is an example using a formal class for Beta vs. anonymous class 下面是使用Beta和匿名类的正式类的示例

    class Beta extends Parent {

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

}

Functional interface: 功能界面:

Similar to abstract class but with a few restrictions. 与抽象类相似,但有一些限制。 The shared function must use the default clause. 共享函数必须使用default子句。 And the interface has one method that must be implemented. 而且接口有一种必须实现的方法。

    static interface Parent {

    default public void sharedFunction() {
    }

    abstract void nitpick();

}

public static void main(String[] args) {

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

}

Pass the implementation as input: 将实现作为输入传递:

In this approach the implementation of the nitpick function is passed to the Parent class' constructor. 在这种方法中,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*/});

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM