I have an OO design question.
I’m using a Factory pattern to create objects of a class appropriate to the parameters I pass the Factory. However, there are some attributes that I can only get after object creation which I want to then use to further subclass or “specialize” the type of object.
I need subclass for polimorphism to use custom logic for object with additional input parameters.
I think it’s some design pattern “Redetermination object class at runtime”. How can i implement it? Oh may be it is bad practice?
You can’t change object type at runtime. If you have instance of base class, then it will stay of base class type. But you can create new objects at runtime. Use composition instead of inheritance.
I.e. instead of inheriting from your base class, create another objects hierarchy – extract to new classes (not inherited from your base class) code which changes after your base object creation. It could be some strategy. Then provide different implementations of strategy to your base object at runtime (i.e. “specialize” base class instance).