mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 12:10:10 +02:00
a REAL abstract factory not a helper-like full of static
This commit is contained in:
@@ -1,42 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns;
|
||||
namespace DesignPatterns\AbstractFactory;
|
||||
|
||||
/**
|
||||
* Abstract Factory pattern
|
||||
*
|
||||
* Purpose:
|
||||
* to create series of related or dependant objects without specifying their concrete classes,
|
||||
* usually the created classes all implement the same interface
|
||||
*
|
||||
* Examples:
|
||||
* - A Factory to create media in a CMS: classes would be text, audio, video, picture
|
||||
* - SQL Factory (types are all strings with SQL, but they vary in detail (tables, fields, etc.))
|
||||
* - Zend Framework: Zend_Form::createElement() creates form elements, but you could also call new T
|
||||
* TextElement() instead
|
||||
* - an abstract factory to create various exceptions (e.g. Doctrine2 uses this method)
|
||||
*
|
||||
* usually the created classes all implement the same interface. The client of the abstract
|
||||
* factory does not care about how these objects are created, he just knows they goes together.
|
||||
*
|
||||
* Sometimes also known as "Kit" in a GUI libraries.
|
||||
*
|
||||
* This design pattern implements the Dependency Inversion Principle since
|
||||
* it is the concrete subclass which creates concrete components.
|
||||
*
|
||||
* In this case, the abstract factory is a contract for creating some components
|
||||
* for the web. There are two components : Text and Picture. There is two ways
|
||||
* of rendering : HTML or JSON.
|
||||
*
|
||||
* Therefore 4 concretes classes, but the client just need to know this contract
|
||||
* to build a correct http response (for a html page or for an ajax request)
|
||||
*/
|
||||
abstract class AbstractFactory
|
||||
{
|
||||
/**
|
||||
* @static
|
||||
* Creates a text component
|
||||
*
|
||||
* @param string $content
|
||||
* @return AbstractFactory\Text
|
||||
* @return Text
|
||||
*/
|
||||
public static function createText($content)
|
||||
{
|
||||
return new AbstractFactory\Text($content);
|
||||
}
|
||||
abstract public function createText($content);
|
||||
|
||||
/**
|
||||
* @static
|
||||
* Createss a picture component
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $name
|
||||
* @return AbstractFactory\Picture
|
||||
* @return Picture
|
||||
*/
|
||||
public static function createPicture($path, $name = '')
|
||||
{
|
||||
return new AbstractFactory\Picture($path, $name);
|
||||
}
|
||||
abstract public function createPicture($path, $name = '');
|
||||
}
|
||||
|
Reference in New Issue
Block a user