Static Methods in Factory Method

Asked

Viewed 310 times

5

I’m creating a class using Factory Method and I came up with the following question:.

The use of static methods in Factories?

In many examples I found we have to create an instance of the class Factory, but in my case I haven’t thought of 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(); 

Source

Note 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(); 

2 answers

5


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

  1. Factory Method - http://sourcemaking.com/design_patterns/factory_method
  2. Static Factory Method - http://www.informit.com/articles/article.aspx?p=1216151

So the answer to your question is, it depends on the desired Pattern design.

Factory Pattern is implemented as discussed so far, without static methods. However, the Static Factory Method, is a little different (the example is in Java but I believe 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, however, is not defined using static methods. Even, its implementation in wikipedia (http://en.wikipedia.org/wiki/Factory_method_pattern) does not use static methods.

I believe there is a BUT in all this. Design Patterns are good practice guides but can be adapted to our needs. If instantiating objects all the time that is called the Factory method is costly, I see no problem in using static methods. However, this is a modification of the Pattern design.

3

Not being static you win in the "configuration" and the "exchange" of your factory.

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.