Answer the question
In order to leave comments, you need to log in
Private constructor or factory methods?
The question arose, is it necessary to give the opportunity to initialize the class constructor from anywhere?
public function __construct(
Uuid $uuid
) {
$this->uuid = $uuid;
}
public function createFromApi($newUserData)
{
UserFactory::fromApi($newUserData);
}
public function createFromAdminPanel($newUserData)
{
UserFactory::fromAminPanel($newUserData);
}
public function createFromImport($newUserData)
{
UserFactory::fromImport($newUserData);
}
Answer the question
In order to leave comments, you need to log in
whether it is necessary to give the chance to initialize the constructor of a class from everywhere?
public function createFromApi($newUserData)
public function createFromAdminPanel($newUserData)
public function createFromImport($newUserData)
This violates SOLID .
So, you have the wrong business logic and the wrong class structure.
A factory is just a helper that creates and sets some properties of an object. At the same time, no one bothers to change them later or do the same without a factory.
PS If the constructor is made private, then the factory (some third-party object) will not be able to create an object.
If you are going to put the factory inside the object itself, then this is shit code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question