Static Methods in Factory Method

4

I'm creating a simple class using Factory Method and the following question came up for me.

Is it good practice to use static methods in Factories?

In many examples I found there, we have an instance of the Factory class, and I have not identified any reason why it needs to be instantiated.

<?php
/* Factory and car interfaces */
interface CarFactory {
    public function makeCar();
}

interface Car {
    public function getType();
}

/* Implementações da Factory e Car */
class SedanFactory implements CarFactory {
    public function makeCar() {
        return new Sedan();
    }
}

class Sedan implements Car {
    public function getType() {
        return 'Sedan';
    }
}

/* Client */
$factory = new SedanFactory();
$car = $factory->makeCar();
print $car->getType(); 

Font

Notice the difference in implementation with static methods:

<?php
/* Factory and car interfaces */
interface CarFactory {
    public static function makeCar();
}

interface Car {
    public function getType();
}

/* Implementações da Factory e Carro */
class SedanFactory implements CarFactory {
    public static function makeCar() {
        return new Sedan();
    }
}

class Sedan implements Car {
    public function getType() {
        return 'Sedan';
    }
}

/* Client */
$car = SedanFactory::makeCar();
print $car->getType(); 
    
asked by anonymous 08.07.2014 / 14:53

2 answers

4

Note that there are 2 design patterns related to Factory Method:

  • Factory Method - link
  • Static Factory Method - link
  • So the answer to your question is, it depends on the design pattern you want.

    The Factory Pattern is implemented as discussed so far, without static methods. However, the Static Factory Method is a bit different (the example is in Java but I think the idea is the same):

    public class Boolean {
      ...
      public static Boolean valueOf(boolean b) {
         return b ? Boolean.TRUE : Boolean.FALSE;
      }
      ...
    }
    

    The Static Factory Method is simpler and already returns the type of the class itself, as in the Boolean example.

    The Factory Method is not defined using static methods. In fact, its implementation on wikipedia ( link ) does not use static methods.

    I believe that there is a PLEASE in all of this. Design Patterns are guides to good practice but can be adapted to our needs. If I instantiate objects at any time called the factory method is expensive, I see no problem using static methods. However, this is a modification of the design pattern.

        
    12.07.2014 / 01:30
    1

    Not being static you gain in the "setup" and "swap" of your factory.

    With configuration, I mean you can change the value or call any method of the SedanFactory class before calling makeCar() , changing how the car will be built (as Renan said).

    With factory switching, I mean that you can have a method that takes as parameter an object of any class that implements CarFactory , knowing that you can call makeCar() to build a car without needing to know if it is SedanFactory or SUVFactory .

        
    12.07.2014 / 01:47