lundi 30 octobre 2017

Interfaces and classes vs Simple Factory design pattern

One of my friend told me if I want to be a good programmer then I need to learn Design Patterns. And I started on that site : http://ift.tt/2kOwAUY

I started from Simple Factory. And as you can see on that page you need to implement :

  • interface Door
  • class WoodenDoor
  • class DoorFactory

And you can use it like this (PHP) :

$door = DoorFactory::makeDoor(100, 200);
echo 'Width: ' . $door->getWidth();
echo 'Height: ' . $door->getHeight();

But I was wondering why I need the layer of class DoorFactory which gives me new instance of WoodenDoor with given parameter when I can simply do :

Door door = new WoodenDoor(100, 200);

What is the big deal in making that factory when I can simple create instance by passing given constructor parameter by using new ClassName statement?

Aucun commentaire:

Enregistrer un commentaire