jeudi 30 mars 2023

How would I use the Factory method if I only have 1 class?

Let me explain: I have a concrete GUI class for my game:

public class GameGUI
{
    public void addElement(GameGuiElement element) { ... }
}

and classes that implement the GameGuiElement interface, such as Button and TextBox.

Until today, I always designed my GUIs like this:

public class SimpleGUI extends GameGUI
{
    public SimpleGUI()
    {
        super("Simple GUI Title");

        addElement(new Button("Click Here"));
    }
}


//somewhere in another class
new SimpleGUI().display(player);

But recently I read that it's bad practice to use inheritance only for configuration, so I did some research and found the Factory pattern; But how would I apply it if I only have the GameGUI class? I thought of creating this interface:

public interface GameGUIFactory
{
    GameGUI createFirstGUI();
    GameGUI createSecondGUI();
}

but my instinct immediately rejects that because configuring even 1 GUI takes a lot of code.. and also doesn't it blatantly violate SRP by being responsible for creating more than 1 GUI?

Aucun commentaire:

Enregistrer un commentaire