如何创建一个管理器类来保存特定变量,而不滥用静态变量?

huangapple go评论68阅读模式
英文:

How do I create a manager class to hold certain variables without abusing static?

问题

以下是翻译好的部分:

public class BwMgr {

    Boolean enabled = false;

    final HashSet<Player> red = new HashSet<>();
    final HashSet<Player> green = new HashSet<>();
    final HashSet<Player> blue = new HashSet<>();
    final HashSet<Player> orange = new HashSet<>();
    final HashSet<Player> purple = new HashSet<>();
    final HashSet<Player> yellow = new HashSet<>();
    final HashSet<Player> black = new HashSet<>();
    final HashSet<Player> white = new HashSet<>();

    public void assignTeams() {

    }

}

每个 HashSet 应该包含玩家的团队。其他类将访问布尔值来确定插件是否启用:

public class BwListener implements Listener {

    private final BwMgr bwMgr = new BwMgr();

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent e) {
        if (bwMgr.enabled) {
            // 做一些操作
        }
    }
}

enabled 应该在这里更新:

public class BwCmd implements CommandExecutor {

    private BwMgr bwMgr = new BwMgr();

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("bw")) {
            if (args[0].equalsIgnoreCase("enable")) {
                bwMgr.enabled = true;             
            } else if (args[0].equalsIgnoreCase("disable")) {
                bwMgr.enabled = false;
            }
        }
        return false;
    }
}

当然,由于每个类都在使用不同的 BwMgr 实例,它们使用的是不同版本的布尔值 enabled。我可以在 BwMgr 的所有内容前面加上 static,但我觉得那可能会被滥用。非常感谢您的任何建议。

英文:

Java n00b here. I'm making a plugin for a Minecraft server, and I would like to use one class to hold all of the variables for the plugin:

public class BwMgr {

    Boolean enabled = false;

    final HashSet&lt;Player&gt; red = new HashSet&lt;&gt;();
    final HashSet&lt;Player&gt; green = new HashSet&lt;&gt;();
    final HashSet&lt;Player&gt; blue = new HashSet&lt;&gt;();
    final HashSet&lt;Player&gt; orange = new HashSet&lt;&gt;();
    final HashSet&lt;Player&gt; purple = new HashSet&lt;&gt;();
    final HashSet&lt;Player&gt; yellow = new HashSet&lt;&gt;();
    final HashSet&lt;Player&gt; black = new HashSet&lt;&gt;();
    final HashSet&lt;Player&gt; white = new HashSet&lt;&gt;();

    public void assignTeams() {

    }

}

Each HashSet is supposed to contain teams of players. The boolean will be accessed by other classes to determine whether the plugin is enabled or not:

public class BwListener implements Listener {

    private final BwMgr bwMgr = new BwMgr();

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent e) {
        if (bwMgr.enabled) {
            // do stuff
        }
    }
}

enabled should be updated here:

public class BwCmd implements CommandExecutor {

    private BwMgr bwMgr = new BwMgr();

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase(&quot;bw&quot;)) {
            if (args[0].equalsIgnoreCase(&quot;enable&quot;)) {
                bwMgr.enabled = true;             
            } else if (args[0].equalsIgnoreCase(&quot;disable&quot;)) {
                bwMgr.enabled = false;
            }
        }
        return false;
    }
}

Of course, since each class is using a different instance of BwMgr, they are using different versions of the Boolean enabled. I could just put static in front of everything in BwMgr but I feel like that would be abuse. Any ideas would be much appreciated.

答案1

得分: 2

你需要通过BwCmd和BwListener的构造函数传递相同的BwMgr实例。

所以在BwCmd中应该如下所示:

    public class BwCmd implements CommandExecutor {

    private BwMgr bwMgr;

    // 通过构造函数传递BwMgr实例。
    public BwCmd(BwMgr bwMgr) {
        this.bwMgr = bwMgr;
    }
  
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("bw")) {
            if (args[0].equalsIgnoreCase("enable")) {
                bwMgr.enabled = true;             
            } else if (args[0].equalsIgnoreCase("disable")) {
                bwMgr.enabled = false;
            }
        }
        return false;
    }
}

BwListener类:

    public class BwListener implements Listener {

    private final BwMgr bwMgr;
    
    // 通过构造函数传递BwMgr实例。
    public BwListener(BwMgr bwMgr) {
        this.bwMgr = bwMgr;
    }

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent e) {
        if (bwMgr.enabled) {
            // 做一些操作
        }
    }
}

在你的主类中:

// 创建一个bwMgr实例。
BwMgr bwMgr = new BwMgr();

// 使用相同的BwMgr初始化BwListener和BwCmd。
BwListener bwListener = new BwListener(bwMgr);
BwCmd bwCmd = new BwCmd(bwMgr);
英文:

You need to pass the same instance of BwMgr through the constructor of BwCmd and BwListener.

so in BwCmd should look like:

    public class BwCmd implements CommandExecutor {

    private BwMgr bwMgr;

    // Pass through the constructor an instance of BwMgr.
    public BwCmd(BwMgr bwMgr) {
        this.bwMgr = bwMgr;
    }
  
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase(&quot;bw&quot;)) {
            if (args[0].equalsIgnoreCase(&quot;enable&quot;)) {
                bwMgr.enabled = true;             
            } else if (args[0].equalsIgnoreCase(&quot;disable&quot;)) {
                bwMgr.enabled = false;
            }
        }
        return false;
    }
}

BwListener class:

    public class BwListener implements Listener {

    private final BwMgr bwMgr;
    
    // Pass through the constructor an instance of BwMgr.
    public BwListener(BwMgr bwMgr) {
        this.bwMgr = bwMgr;
    }

    @EventHandler
    public void onPlayerJoinEvent(PlayerJoinEvent e) {
        if (bwMgr.enabled) {
            // do stuff
        }
    }
}

And in your main class:

// Create one instance of bwMgr.
BwMgr bwMgr = new BwMgr();

// Initialize BwListener and BwCmd with the same BwMgr.
BwListener bwListener = new BwListener(bwMgr);
BwCmd bwCmd = new BwCmd(bwMgr);

huangapple
  • 本文由 发表于 2020年9月15日 00:15:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63888137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定