mercredi 16 décembre 2020

determining whether the composite pattern is used properly or not

I am trying to use the composite design pattern in Java. However, I am not sure if I have used it validly/correctly. Is the code below a valid/correct use of the composite design pattern? I have a class leafGoal, which class compositeGoal extends. However, there is no interface they are implementing, which is a common pattern I saw online with the composite pattern. Does this matter? That I have no common interface for the two classes? Is it still a valid composite pattern?

public class leafGoal {

    String goalName;
    //private GoalStrategy strategy;
    GoalStrategy strategy;
    private Dungeon dungeon;
    private Inventory inventory;
public class compositeGoal extends leafGoal {

    ArrayList<leafGoal> goals = new ArrayList<leafGoal>();
    
    public compositeGoal(String goalName, GoalStrategy strategy, Dungeon dungeon, Inventory inventory) {
        super(goalName, strategy, dungeon, inventory);
    }

    public void addGoal(leafGoal g) {

        goals.add(g);
    }

Aucun commentaire:

Enregistrer un commentaire