Skip to content

Better documentation of factory pattern with Symfony #6029

Closed
@ghost

Description

I think the documentation about the factory pattern should be better.

This is the current documentation:
http://symfony.com/doc/master/components/dependency_injection/factories.html

Example A:
I have a Product entity. It has a field type. The type could be table, chair or something else.

Example B:
I have a File entity with types xml, php or yaml.

I think the current documentation does not easily explain that a factory pattern allows something like this:

class FileFactory
{
    public static function get($type)
    {
        switch ($type)
        {
            case 'xml':
                return new XmlFile($type);
                break;
            case 'php':
                return new PhpFile($type);
                break;
            case 'yml':
                return new YamlFile($type);
                break;
        }

        throw new \InvalidArgumentException();
    }
}

Instead currently it just contains this code:

class NewsletterManagerFactory
{
    public static function createNewsletterManager()
    {
        $newsletterManager = new NewsletterManager();

        // ...

        return $newsletterManager;
    }
}

Where are the different types? I understand that it is easily possible but I think the documentation should be updated for a better explaination about the factory pattern. Everybody should easily understand why they should use a factory pattern.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions