简体   繁体   中英

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 : 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. eg by using super("100", "200");

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM