duanhongyi2964 2017-10-30 13:53 采纳率: 100%
浏览 70

接口和类与简单工厂设计模式

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");

  • 写回答

2条回答 默认 最新

  • dtjxhf595733 2017-10-30 14:26
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题