Constructors are fine when they contain short, simple code.
When initialization becomes more than assigning a few variables to the fields, a factory makes sense. Here are some of the benefits:
Long, complicated code makes more sense in a dedicated class (a factory). If the same code is put in a constructor which calls a bunch of static methods, this will pollute the main class.
In some languages and some cases, throwing exceptions in constructors is a really bad idea throwing exceptions in constructors is a really bad idea, since it can introduce bugs.
When you invoke a constructor, you, the caller, needs to know the exact type of the instance you want to create. This is not always the case (as a
Feeder
, I just need to construct theAnimal
in order to feed it; I don't care if it's aDog
or aCat
).
Constructors are fine when they contain short, simple code.
When initialization becomes more than assigning a few variables to the fields, a factory makes sense. Here are some of the benefits:
Long, complicated code makes more sense in a dedicated class (a factory). If the same code is put in a constructor which calls a bunch of static methods, this will pollute the main class.
In some languages and some cases, throwing exceptions in constructors is a really bad idea, since it can introduce bugs.
When you invoke a constructor, you, the caller, needs to know the exact type of the instance you want to create. This is not always the case (as a
Feeder
, I just need to construct theAnimal
in order to feed it; I don't care if it's aDog
or aCat
).
Constructors are fine when they contain short, simple code.
When initialization becomes more than assigning a few variables to the fields, a factory makes sense. Here are some of the benefits:
Long, complicated code makes more sense in a dedicated class (a factory). If the same code is put in a constructor which calls a bunch of static methods, this will pollute the main class.
In some languages and some cases, throwing exceptions in constructors is a really bad idea, since it can introduce bugs.
When you invoke a constructor, you, the caller, needs to know the exact type of the instance you want to create. This is not always the case (as a
Feeder
, I just need to construct theAnimal
in order to feed it; I don't care if it's aDog
or aCat
).
Constructors are fine when they contain short, simple code.
When initialization becomes more than assigning a few variables to the fields, a factory makes sense. Here are some of the benefits:
Long, complicated code makes more sense in a dedicated class (a factory). If the same code is put in a constructor which calls a bunch of static methods, this will pollute the main class.
In some languages and some cases, throwing exceptions in constructors is a really bad idea, since it can introduce bugs.
When you invoke a constructor, you, the caller, needs to know the exact type of the instance you want to create. This is not always the case (as a
Feeder
, I just need to construct theAnimal
in order to feed it; I don't care if it's aDog
or aCat
).