dimanche 19 février 2023

Best Practice Open Close Principle

Please take a look at my class. In it WoW avatars are created. In the constructor method instances of different classes are created. If the program is extended by a new class (e.g. Tauren), the constructor method must be changed stupidly. One would have to add another object instantiation into the method. This contradicts the Open Close principle, according to which a software should be open for extensions but closed for changes. How can I solve this problem.

public class UpdateAvatarFactory{

    public UpdateAvatarFactory(Client client){
        ICreateMethod createHuman = new CreateHuman();
        createHuman.name="Human";
        ICreateMethod createElf = new CreateElf();
        createElf.name="Elf";
        ICreateMethod createGnome = new CreateGnome();
        createGnome.name="Gnome";
        ICreateMethod createOrc = new CreateOrc();
        createOrc.name="Orc";

        client.avatarFactory.addCreateMethod(createHuman);
        client.avatarFactory.addCreateMethod(createElf);
        client.avatarFactory.addCreateMethod(createGnome);
        client.avatarFactory.addCreateMethod(createOrc); 
    }

}

Aucun commentaire:

Enregistrer un commentaire