dimanche 7 juin 2015

Valid Factory Pattern implementation?

Is this a valid minimal Factory Pattern Implementation? Aroud internet I saw always a massive use of if..else condition in the factory method. Isn't better use a duck type to implments it?

The interface Product:

 <?php
 abstract class Product
  {  
     public $id;
     public function statusId();
  }
 ?>

The concerete products:

class ConcreteProductA extends Product{

public function statusId($statusId)
  {
    super.$id = $statusId;
   }
}

class ConcreteProductB extends Product{

public function statusId($statusId)
     {
    super.$id = $statusId;
    }
}

The interface Factory:

interface ProductFactoryInterface
{
   function getProduct();
}

class ProductFactory implements ProductFactoryInterface
{   private $product;

public function __construct(Product $product) {
    $this->product = $product;
}

And a Cocrete Factory:

public function getProduct($id)
   {  
    $this->product->$id = $id;
    return $this->product;
   }
}

Aucun commentaire:

Enregistrer un commentaire