2

I know this question has been asked a million times, but I can't find a decent answer.

I have a bunch of messenger methods like debug(String) and tell(CommandSender, String) that I want to use a variety of classes. It sounds simple and easy, but Java's lack of support for multiple inheritance has made the problem incredibly difficult for classes that already have to extend something else.

Interfaces won't work as far as I can tell because interfaces don't allow you to actually implement the methods that you put in them.

I have considered adding extra arguments to the method to make them work from a static utilities class, but this makes calls to the methods go from debug(String) to MessageUtil.debug(Plugin, String), which is far more bulky than I would like.

I have considered making a Messenger object that can handle these messages so that I can call messenger.debug(String), which is better, but I can't shake the feeling that there must be a better way to get debug(String) alone.

Any advice?

EDIT: These methods, unfortunately, cannot be made static without adding extra parameters; therefore, static imports will not work.

EDIT: Here is an example of one of the methods that I'm trying to use in multiple classes. As you can see by its use of non-static global variables like "plugin" and "debuggers", it cannot be made static.

protected myPlugin plugin;
private myList<String> debuggers = new myList<String>();

public void debug(String message) {
    if (debuggers.size() == 0)
        return;
    if (debuggers.contains("\\console")) {
        plugin.getServer().getConsoleSender().sendMessage(plugin.getColor() + message);
        if (debuggers.size() == 1)
            return;
    }
    for (Player player : plugin.getServer().getOnlinePlayers())
        if (debuggers.contains(player.getName()))
            player.sendMessage(plugin.getColor() + message);
}

Here's another example that boradcasts a message to every Player on the server and to the console using the same global variables as the one above:

public void broadcast(String message) {
    for (Player player : mCL.getServer().getOnlinePlayers())
        player.sendMessage(plugin.getColor() + message);

    mCL.getServer().getConsoleSender().sendMessage(message);
}

EDIT: The broadcast() method above actually isn't static; that was a copy-paste error. I have modified it to reflect that.

13
  • 5
    If you're finding the lack of multiple inheritance of classes a problem, you should really take a hard look at your class hierarchy. Multiple inheritance shouldn't be something that you rely on/need in Java, for the most part. Commented Jun 27, 2014 at 1:24
  • How would you suggest I modify it to avoid multiple inheritance, though? I don't want to rely on multiple inheritance (since I can't use it), but I don't know another good way. Commented Jun 27, 2014 at 1:25
  • 4
    Prefer composition over inheritance. Commented Jun 27, 2014 at 1:26
  • 1
    Are you opposed to static imports? Commented Jun 27, 2014 at 1:27
  • 1
    Well broadcast() could be import-static as is. Your debug() method appears to have quite a lot of state. Pass the MessageUtil instance to your callers, or use a Singleton. Commented Jun 27, 2014 at 1:47

2 Answers 2

6

Given

this makes calls to the methods go from debug(String) to MessageUtil.debug(Plugin, String), which is far more bulky than I would like.

Assuming your methods are static, or can be made static, you could use static import -

import static mypackage.MessageUtil.debug;

//
debug("Hello");
Sign up to request clarification or add additional context in comments.

4 Comments

It's a good idea, but unfortunately, it won't work here. I can't make the methods static without adding more and more parameters. I did use static imports for other things which could be made static, but that doesn't work here.
@REALDrummer Please edit your question, and what do you mean better way to get debug(String) alone? What does debug(String) do?
@ElliottFrisch OP commented in the question comments as to the function of these methods
@ElliottFrisch I edit the question to reflect that. debug(String) is meant to send the given message (String) to anyone currently in "debugging mode" and not to anyone else.
2

Interfaces won't work as far as I can tell because interfaces don't allow you to actually implement the methods that you put in them.

Have a look at one of Java 8's new feature: default methods in interface. Maybe you can do something with this.

4 Comments

I did see that, and that would be exactly what I need. However, this is a plugin written to work with a pre-existing Java program which I do not believe supports Java 8. I'll do more research to see if I can make it work with Java 8. If they do support Java 8, then this would be perfect.
@REALDrummer This is actually no different from the static imports solutions (unless you intend for them to be overridden). I don't see how this could help you, as you mention that you make use of non-static global variables which interfaces and default methods won't help you with. As mentioned in the comments, composition is the best way to go, or with a singleton.
@ggovan With default methods, I could make a class with all the global variables and static methods that take the extra parameters I don't want to have to use all the time. Then, I can make an interface with default methods which use those static methods, but pass the global variables from the class as the extra parameters needed. Then all the classes I want can implement that interface and have all the methods with no extra parameters and no composition. It's not the prettiest solution, but it works.
@ggovan Oh, and this wouldn't work with static imports because the methods I want to use can't be static. I'm working in a plugin interface with an overarching A.P.I. with lots of things that should be constant, but might not be. It's all very complicated and weird, but it means I can't make these methods static, unfortunately.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.