dimanche 5 décembre 2021

Avoiding circular depenency between Arena and Portal

I'm trying to resolve this ciruclar dependency while preferebly keeping both objects immutable.

public class Arena {

    private final Portal portal;

    Arena(Portal portal) {
        this.portal = portal;
    }

    void close() {
        portal.close();
    }

    void start() {

    }
}

public class Portal {

    private final Arena arena;

    Portal(Arena arena) {
        this.arena = arena;
    }

    void close() {

    }

    void start() {
        arena.start();
    }

}

Basically, I need portal to be able to activate itself, and start the arena. Opposite, I need Arena to reset & close itself, and the portal. I found a solution by having two HashMap's <Arena, Portal> and <Portal, Arena>, however I want to figure out if it's a better way to solve this for learning and progression.

The classes are made more simple in here, as in reality they have more variables and identifiers.

Aucun commentaire:

Enregistrer un commentaire