gpt4 book ai didi

javascript - 为 Cordova 插件重新设计非单例 Java 类?

转载 作者:行者123 更新时间:2023-11-30 15:19:15 26 4
gpt4 key购买 nike

当您为 Cordova 插件创建 Java 类并希望将其提供给 JavaScript 端时,您必须将 Java 类设计为单例。至少我是这么理解的。
由于几个明显的原因,这是一个非常有问题的问题。现在我的问题是,是否有关于如何设计该 Java 类的最佳实践或类似的东西,以便我可以在多个实例上工作,或者至少得到我在 JavaScript 方面的错觉。
注意:我不需要继承来处理 JavaScript 类。

例如,如果这是我的插件代码:

public class Player extends CordovaPlugin {
private static int idCounter = 0;
private int id;
private int health;
private int gold;
private String area = "Home";

public Player(health,gold) {
this.health = health;
this.gold = gold;
this.id = idCounter++;
}

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("move")) {
String area = args.getString(0);
this.move(area, callbackContext);
return true;
}
return false;
}

private void move(String area, CallbackContext callbackContext) {
if (area != null && area.length() > 0) {
this.area = area
callbackContext.success();
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}
}

我如何实现它才能真正与 Cordova 兼容?

编辑:我应该提一下,我正在开发的应用程序仅适用于 Android。

最佳答案

我会将逻辑分为两个类:一个单例插件类和一个可实例化的播放器类,如下所示:

为多个播放器实例更新

public class PlayerPlugin extends CordovaPlugin {

private HashMap<int, Player> players = new HashMap<int, Player>();

@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("create")) {
this.create(args, callbackContext);
return true;
}else if (action.equals("move")) {
this.move(args, callbackContext);
return true;
}else if (action.equals("get")) {
this.get(args, callbackContext);
return true;
}
return false;
}

private void create(JSONArray args, CallbackContext callbackContext) {
int health = args.getInt(0);
int gold = args.getInt(1);
Player player = new Player(health, gold);
players.put(player.id, player);
callbackContext.success(player.id);
}

private void move(JSONArray args, CallbackContext callbackContext) {
int playerId = args.getInt(0);
String area = args.getString(1);
if (area != null && area.length() > 0) {
Player player = players.get(playerId);
player.area = area;
callbackContext.success();
} else {
callbackContext.error("Expected one non-empty string argument.");
}
}

private void get(JSONArray args, CallbackContext callbackContext) {
int playerId = args.getInt(0);
Player player = players.get(playerId);

JSONObject jsPlayer = new JSONObject();
jsPlayer.put("health", player.health);
jsPlayer.put("gold", player.gold);
jsPlayer.put("area", player.area);
callbackContext.success(jsPlayer);
}
}

public class Player {
private static int idCounter = 0;
private int id;
private int health;
private int gold;
private String area = "Home";

public Player(health,gold) {
this.health = health;
this.gold = gold;
this.id = idCounter++;
}
}

关于javascript - 为 Cordova 插件重新设计非单例 Java 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43998581/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com