Answer the question
In order to leave comments, you need to log in
Is this code a factory method or a static factory?
There is a class that has a static method that receives an argument as input (in this example, transportId), there is a conditional operator that returns the desired transport based on the ID. It is important to note that the returned object in any case is the heir of one abstract class (in this case, AbstractTransport).
class TransportFactory
{
public static function createTransport($transportId)
{
switch ($transportId) {
case Transport::CARS:
return new Car();
break;
case Transport::PLANE:
return new Plane();
break;
case Transport::TRAIN:
return new Train();
break;
default:
throw new Exception("No active transport");
}
}
}
Answer the question
In order to leave comments, you need to log in
This is a static factory.
https://refactoring.guru/en/design-patterns/factor...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question