Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a class called Manhunt that inherits Minigame. To keep the code and post brief, I'm only including the pertinent methods

public abstract class Minigame {

    public abstract void startCountdown();

}
public class Manhunt extends Minigame {

    @Override
    public void startCountdown() {

    }
    
}

And Minigame is handled by MinigameManager

public class MinigameManager {

    public void startMinigame(String minigame, int gameKey) {
        switch (minigame) {
            case "manhunt":
                UUID runner = this.playersQueued.remove(this.random.nextInt(this.playersQueued.size()));
                this.minigame = new Manhunt(gameKey, runner, this.playersQueued);
                this.minigame.startCountdown();
                this.playersQueued.clear();
                break;
            case "tbr":
            case "boatracer":
                break;
            default:
        }
    }

}

The problem is, Manhunt and MinigameManager are in two separate plugins meaning this current setup requires IntelliJ to add dependencies for both programs that reference each other, leading to a circular dependency problem. I want to keep them separated in case I add extra Minigames (I just have to drag and drop extra Minigames as opposed to coding them all in the same program), but how do I resolve this circular dependency issue?

It seems like the Dependency Inversion Principle should solve this problem, but I'm not sure how to incorporate that to my program. Would I need to create an interface for MinigameManager or Minigame? I would appreciate it if someone could walk me through making the code more modular.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
321 views
Welcome To Ask or Share your Answers For Others

1 Answer

MinigameManager needs to be in a separate third plugin.

MinigameManager depends on Minigame, but Minigame does not depend on MinigameManager. Since there is no co-dependency, there is no reason to have the two in the same library.

MinigameManager will then depend on both Minigame and Manhunt, and Manhunt will still also depend on Minigame.

Alternatively, you could go with a "registry" type approach where MinigameManager exports an interface that Manhunt implements, and then some runtime code in MinigameManager performs some magic to enumerate and instantiate any type that implements its interface. This is a common approach in Java, but be clear: that is not cleanly separating dependencies. That is simply transforming an explicit dependency, which must follow cleanly-defined rules and results in a clear and easy-to-see structure, into an implicit dependency which enables you to hide important structure and sidestep DAG validation and protections.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...