1

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 : https://github.com/kamranahmedse/design-patterns-for-humans

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?


EDITED

Argument that tells that I can easy manage changes in many occurences of given element repeal by this solution :

Creating a given class (as an given factory type in factory solution) like :

class LongWoodenDoor which extends WoodenDoor class and use WoodenDoor constructor with given parameters. e.g by using super("100", "200");

  • 1
    https://softwareengineering.stackexchange.com/questions/253254/why-should-i-use-a-factory-class-instead-of-direct-object-construction – Jap Mul Oct 30 '17 at 13:57
  • 1
    https://softwareengineering.stackexchange.com/questions/200647/why-is-the-factory-method-design-pattern-more-useful-than-having-classes-and-cal – iainn Oct 30 '17 at 13:57
  • 1
    Constructor vs factory pattern: https://stackoverflow.com/questions/628950/constructors-vs-factory-methods – BenRoob Oct 30 '17 at 13:57
  • @BenRoob I believe you are confusing the *Factory pattern* with *static factory methods*... – Chetan Kinger Oct 30 '17 at 17:38

2 Answers2

0

You could definitely use

Door door = new WoodenDoor(100, 200);

to create doors, but I would quote from the tutorial that you are following:

Simple factory simply generates an instance for client without exposing any instantiation logic to the client

The purpose of a factory is that the client doesn't need to know anything about the constructor of the door class in order to create a door.

Your factory could take no parameters at all when creating a door:

makeDoor function:

Door* DoorFactory::makeDoor()
{
    Door *newDoor = new Door(200, 100);
    return newDoor;
}

Where your create the door:

Door door = DoorFactory::makeDoor();

In this case the factory is encapsulating information about the Door so that you don't need to know about the dimension of the door. Obviously you would want to have the flexibility to define the door with your own dimension, but in some cases, it is beneficial to hide parameters to avoid changes.

Also if you would like to modify the constructor of class Door in the future, say adding another parameter called thickness:

Door::Door(double width, double height, double thickness)

You don't have to change every single door instantiation in the project, your can just modify your factory code at one place:

Door* DoorFactory::makeDoor(double width, double height)
{
    Door *newDoor = new Door(width, height, 5);
    return newDoor;
}

That way it is easier to do refactoring if your project's requirements change.

  • Okey. but I can create class which will be for example LongWoodenDoor which will Implement WoodenDoor. And in LongWoodenDoor contructor it will simply call super (WoodenDoor constructor) with constant parameters. And I will call it like Door door = new LongWoodeDoor(); and I also don't have information about dimensions here. – Patryk Janik Oct 30 '17 at 14:37
  • You could probably do something like this: `DoorFactory::makeDoor("normal wood door");` or `DoorFactory::makeDoor("long wood door");` Then modify your logic in the factory. Sorry I don't have a whole lot of experience of using Factory. – Xiangyu Gavin Luo Oct 30 '17 at 14:51
0

Design Patterns is really good advice!
And factory method pattern is also good advice. The aim of factory method - is to define an interface for creating an object, but which object will be created you can determine in runtime! Hence if you don't know will it be wooden or metal or plastic door - you have to use factory method.

But if you definitely know that it will be wooden dor - you can stick with KISS (Keep it simple, stupid) and YAGNI (You aren't gonna need it) principles.

cn007b
  • 16,596
  • 7
  • 59
  • 74
  • But also I can tdetermine it in a runtime when I will do LongWoodenDoor class which will extend WoodenDoor with simply running constructor of WoodenDoor with given parameter then I also can manage this in run time. Which extended class will I use. – Patryk Janik Oct 31 '17 at 07:39
  • Yes, you can do it - it is option #1, but option #2 - factory. What will you pick - you will decide. It's just alternative... – cn007b Oct 31 '17 at 08:35
  • So finally I don't need to implement Simple Factory. Just I need to know how to solve problem ? – Patryk Janik Oct 31 '17 at 10:23
  • In general - yes! Development - is creative work... You can solve problems in different ways... More important - reach the goal. How you did it - it's another conversation... – cn007b Oct 31 '17 at 10:35