Basic difference between Abstract Factory and Factory?

Asked

Viewed 1,032 times

7

I was taking a look at Manual for PHP and saw an example of implementing the standard Factory.

Example:

class Exemplo
{
    // Método Factory parametrizado
    public static function factory($type)
    {
        if (include_once 'Drivers/' . $type . '.php') {
            $classname = 'Driver_' . $type;
            return new $classname;
        } else {
            throw new Exception ('Driver não encontrado');
        }
    }
}

Exit:

// Carregar um driver MySQL
$mysql = Exemplo::factory('MySQL');

// Carregar um driver SQLite 
$sqlite = Exemplo::factory('SQLite');

In the PHP the Right Way, I saw that, through the Factory, a class simply creates the object you’d like to use.

I have no example of Abstract Factory, but I’ve seen it in a course I’ve done. And it’s got me confused.

I would like to know, as simply as possible, what the differences are between these two Patterns (standards).

  • It would be nice if you took a look here too. The concept of interfaces is new in PHP.

  • 1

    In the first sentence of this manual article there is a false statement. Object Interfaces allows code creation that specifies which methods and variables a class must implement ... PHP Does not accept declaring variables in an interface :\

  • Are you sure about this? See example 4.

  • 1

    @Romaniomorrisonmendez, that is a constant, not a variable. Constants can be put in an interface, variables cannot.

  • Maybe if referred to properties. Anyway, it’s an unfortunate statement. Pity I didn’t find it in the documentation.

  • 1

    In the English version they do not speak in haha variables, an interface defines which behaviors will be implemented by a class or an interface has no concrete method (it does not implement any behavior) so it makes no sense to have been. If you need to maintain a state of something, an abstract class seems to be more suitable.

  • 1

    @Wallacemaxters If you’d like to see more details: http://answall.com/q/157466/101

Show 2 more comments

1 answer

8

The name of Pattern until it is quite significant.

Basically, Abstract Factory is the technique of using a Factory abstracting the knowledge of the concrete implementation of that Factory.

The same way as a Factory has the ability to abstract the concreted implementation of the type it delivers by returning for example an interface instead of returning a class, the default Abstract Factory determines an interface of Factory abstracting the concrete implementation of the Factory which is returned to the consumer of Factory.

Example in pseudo-code

Factory

Factory factory = new Factory();
IObjeto objeto = factory.create();

Abstract Factory

IFactory factory = abstractFactory.create();
IObjeto objeto = factory.create();

In these examples, Iobject and Ifactory are declarations of interfaces and not of types. The type of object that is actually obtained in each of the examples is "unknown" or irrelevant to the consumer.

The concrete implementation of Factory which will be obtained by using the Pattern Abstract Factory will then depend on decisions external to the consumer code of the Factory, and will be determined for example by configuration file, observance of business rules, dependency injection, etc.

Remembering that I mentioned interface in the sense of a contract. Although interface declaration is a fairly common basic resource in object-oriented languages, you do not necessarily need to use this type of statement if it is not required by the language or if there is a nicer way.

  • 1

    One question: in Abstract Factory, you first manufacture the factory, which then is used to manufacture the object? It would be a factory of factories then?

  • 1

    @Piovezan Precisely. It is clear that the explanation could be more detailed, with deeper examples of implementation... But the question asked for the simplified explanation, so this is it.

Browser other questions tagged

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