El patrón de fábrica es uno de los patrones de diseño más utilizados en Java. Este tipo de patrón de diseño se incluye en el patrón de creación, ya que este patrón proporciona una de las mejores formas de crear un objeto.
En el patrón de fábrica, creamos un objeto sin exponer la lógica de creación al cliente y nos referimos al objeto recién creado usando una interfaz común.
Vamos a crear una interfaz Shape y clases concretas implementando la interfaz Shape. Una clase de fábrica ShapeFactory se define como el siguiente paso.
FactoryPatternDemo, nuestra clase de demostración utilizará ShapeFactory para obtener un objeto Shape. Pasará información (CÍRCULO / RECTÁNGULO / CUADRADO) a ShapeFactory para obtener el tipo de objeto que necesita.
Crea una interfaz.
Shape.php
<?php
declare(strict_types=1);
interface Shape
{
public function draw(): void;
}Crea clases concretas implementando la misma interfaz.
Rectangle.php
<?php
declare(strict_types=1);
require_once __DIR__ . '/Shape.php';
class Rectangle implements Shape
{
public function draw(): void
{
print sprintf("Inside Rectangle::draw() method." . PHP_EOL);
}
}Square.php
<?php
declare(strict_types=1);
require_once __DIR__ . '/Shape.php';
class Square implements Shape
{
public function draw(): void
{
print sprintf("Inside Square::draw() method." . PHP_EOL);
}
}Circle.php
<?php
declare(strict_types=1);
require_once __DIR__ . '/Shape.php';
class Circle implements Shape
{
public function draw(): void
{
print sprintf("Inside Circle::draw() method." . PHP_EOL);
}
}Cree una fábrica para generar un objeto de clase concreta basado en información dada.
ShapeFactory.php
<?php
declare(strict_types=1);
include_once __DIR__ . '/Circle.php';
include_once __DIR__ . '/Rectangle.php';
include_once __DIR__ . '/Square.php';
class ShapeFactory
{
//use getShape method to get object of type shape
public function getShape(string $shapeType): Shape
{
if ($shapeType == null) {
return null;
}
if (strcasecmp($shapeType, "CIRCLE") == 0) {
return new Circle();
} else if (strcasecmp($shapeType, "RECTANGLE") == 0) {
return new Rectangle();
} else if (strcasecmp($shapeType, "SQUARE") == 0) {
return new Square();
}
return null;
}
}Utilice Factory para obtener un objeto de una clase concreta pasando una información como el tipo.
FactoryPatternDemo.php
<?php
include_once __DIR__ . '/ShapeFactory.php';
$shapeFactory = new ShapeFactory();
//get an object of Circle and call its draw method.
$shape1 = $shapeFactory->getShape("CIRCLE");
//call draw method of Circle
$shape1->draw();
//get an object of Rectangle and call its draw method.
$shape2 = $shapeFactory->getShape("RECTANGLE");
//call draw method of Rectangle
$shape2->draw();
//get an object of Square and call its draw method.
$shape3 = $shapeFactory->getShape("SQUARE");
//call draw method of square
$shape3->draw();Verifique la salida.
Inside Circle::draw() method.
Inside Rectangle::draw() method.
Inside Square::draw() method.
