vendredi 10 mai 2019

using an enum to "list" singleton sub class

Ok so I began a project and I want to represent adventurers ("Aventuriers" in french) each of the adventurers are unique and each have a special action.

I've came up with this :

public abstract class Aventurier {
    public abstract void specialAction();
}

public enum AvanturierE {
    NAVIGATEUR("Navigateur",Navigateur.getNavigateur()),
    PILOTE("Pilote",Pilote.getPilote()),
    INGENIEUR("Ingenieur",Ingenieur.getIngenieur());



    AvanturierE(String nom, Aventurier aventurier) {
        this.aventurier = aventurier;
        this.nom = nom;
    }

    Aventurier aventurier;
    String nom;

    public Aventurier getAventurier() {
        return aventurier;
    }
}

public class Navigateur extends Aventurier {
    private Navigateur(){

    };

    static Navigateur navigateur = new Navigateur();

    public static Navigateur getNavigateur(){
        return navigateur;
    }

    @Override
    public void specialAction() {

    }
}

public class Pilote extends Aventurier {



    private Pilote(){
    };

    static Pilote pilote = new Pilote();

    public static Pilote getPilote(){
        return pilote;
    }

    @Override
    public void specialAction() {

    }
}

public class Ingenieur extends Aventurier {

    private Ingenieur(){
    };

    static Ingenieur ingenieur = new Ingenieur();

    public static Ingenieur getIngenieur(){
        return ingenieur;
    }

    @Override
    public void specialAction() {

    }
}

It works as expected but as it's not a personal project I want to know if it's a valid design. And if not if there is a good alternative.

Aucun commentaire:

Enregistrer un commentaire