General Problem
I have some objects that have some variables that are not known at creation time.
Right now I create these objects and gradually fill them until they are fully instantiated. But I’m wondering, “Is it a good design to gradually fill an object until it is fully instantiated?”.
My Specific Problem
I’m developing a Java program that has a hierarchy of Task objects. These tasks have some variables that are known when I create them, and some variables that only become known when the task is being scheduled. Right now I create these tasks with the variables that are known, and when the task is ready for scheduling I call scheduleTask(Task t) and this method will set the variables that become known when the task is being scheduled.
But is it a good solution to create objects that are not fully instantiated, and set the remaining variables when they become known?
I was thinking about deferring the task creation until all variables are known (when it is being scheduled). But some taks can only be scheduled after getting an approval from external sources.
Does somebody have some design ideas on how to solve this problem?
EDIT: I forgot to mention that I have different types of Tasks that all inherit from “Task”.
EDIT 2: How about “ProposedTask” objects that have the variables that are known before scheduling? References to these can be hold until it gets approval. Then a new “Task” can be created while scheduling based upon the “ProposedTask” object (‘scheduleTask(ProposedTask p)’ now takes a ProposedTask object).
Use the Builder pattern. E.g:
Many frameworks use it to create partially created objects and then execute some action. E.g.: Http requests builders -> HttpPool executors