Is there something special(i.e. a design pattern being related) to a method named create()?
Background
The case is, I often make some instances of objects (of the same class/prototype). These objects do not always already objects reflect a state in which they are complete yet. Potentially some initialization needs yet to be done to make them more completly initialized (which cannot be done in the constructor function/method) since for some reasons the information to do so might existing already. An alternative making of the objects once this information is available is made difficult since the objects already made are made available via references to other objects (even though some functionality is not yet possible because of the some missing initalization).
In order to have a way to complete the initialization of the object I would like to call a method .create([...])
on the objects which should be a way for a second step completion of some of the objects initialization.
My interest is if related to this background is if this is a legitimate/reasonable course of action (i.e. to have some objects that may have different states of readyness; which are made into a “complete” state via a method – I thought to name that .create([...])
)?
Specifically I would of course like to know if there are for instance already patterns with standard method names for the described task (two-step initialization of objects) and also if the method name .create[...]
chosen in worst case conflicts with some existing pattern and would confuse readers of my code.
3
From what I have both read and experienced, it is not a good practice to have more than one single step in initialization of an object. This is not good because
- You need to babysit all such objects, so that the required initialization steps are completed before the objects are used
- You would have to expect anyone using your code to also properly babysit their objects.
You can easily see that step 1 is quite error-prone, as one might forget some of the steps, or have a hard time to track whether a certain step completed. And, of course, you should not allow the users of your code to break it easily, meaning you should spare them step 2.
However, there are known exceptions to the above preachings which have made it to the development world and became widespread and proven practices. The most important thing is to not allow programmatically for your objects to be used before being properly initialized. This would imply that you have certain encapsulation mechanisms over the initialization lifecycle. Here are possible approaches I can think of, and the respective use cases in real life:
-
Factory classes
A factory class, (or simply a factory), is a separate class that exposes a single method (
create
if you wish), that is used to instantiate your working object. It must return a fully initialized and ready-to-use instance of your class, therefore all the initialization steps happen inside of that method. For instance this code (used C# as language):public class FooFactory { public Foo Create() { Foo foo = new Foo(); foo.configure(...); foo.init(...); foo.complete(...); return foo; } }
This is considered a design pattern, see wikipedia article. In most cases the creation method is called
Create
orCreateFoo
but you can choose whatever suits you. -
Dependency Injection (DI for short)
This requires you to get familiar with a dependency injection framework and employ it in your project. The DI framework will take care for passing completely initialized instances of your objects and some implementations support post-constructor initialization steps that are execute before the object is ready for use. This acts in a similar way to the above factory pattern, but the advantages of dependency injection. An example is the popular Spring framework (primarily addressing Java development, has also a .NET port), which allows you to specify both
init
anddestroy
methods on your class. Theinit
method can be any method of the class you select, so you can easily fit your intention with thecreate
method to work with that.<beans ...> <bean class="com.mycompany.Foo" init-method="create"> ... </bean> </beans>
See this short article for some reference.
An alternative to the above approach is to rather specify a static class and method that would initialize your
Foo
class. For the below example, thecreateFoo
is a static method of theFooFactory
class and returns an instance ofFoo
.<beans ...> <bean class="com.mycompany.FooFactory" factory-method="com.mycompany.FooFactory.createFoo" /> </beans>
The above list is not exhausting, there are other solutions that might seem appropriate in certain scenarios.
2