Updated abstract factor example.

This commit is contained in:
Ian Dinwoodie
2019-04-27 23:14:11 -04:00
parent 75383841df
commit 32f8135753

View File

@@ -312,13 +312,14 @@ classes.
#### Programmatic Example #### Programmatic Example
Translating the door example above. First of all we have our door base class and Translating the door example above. First of all we have our door base class
some derivation of it: and some derivation of it:
```cpp ```cpp
class Door class Door
{ {
public: public:
typedef std::shared_ptr<Door> ptr_t;
virtual void getDescription(void) = 0; virtual void getDescription(void) = 0;
}; };
@@ -347,6 +348,7 @@ Then we have some fitting experts for each door type:
class DoorFittingExpert class DoorFittingExpert
{ {
public: public:
typedef std::shared_ptr<DoorFittingExpert> ptr_t;
virtual void getDescription(void) = 0; virtual void getDescription(void) = 0;
}; };
@@ -379,35 +381,35 @@ fitting expert:
class DoorFactory class DoorFactory
{ {
public: public:
virtual Door makeDoor(void) = 0; virtual Door::ptr_t makeDoor(void) = 0;
virtual DoorFittingExpert makeFittingExpert(void) = 0; virtual DoorFittingExpert::ptr_t makeFittingExpert(void) = 0;
}; };
class WoodenDoorFactory : public DoorFactory class WoodenDoorFactory : public DoorFactory
{ {
public: public:
Door makeDoor(void) Door::ptr_t makeDoor(void)
{ {
return WoodenDoor(); return std::make_shared<WoodenDoor>();
} }
DoorFittingExpert makeFittingExpert(void) DoorFittingExpert::ptr_t makeFittingExpert(void)
{ {
return Carpenter; return std::make_shared<Carpenter>();
} }
}; };
class IronDoorFactory : public DoorFactory class IronDoorFactory : public DoorFactory
{ {
public: public:
Door makeDoor(void) Door::ptr_t makeDoor(void)
{ {
return IronDoor(); return std::make_shared<IronDoor>();
} }
DoorFittingExpert makeFittingExpert(void) DoorFittingExpert::ptr_t makeFittingExpert(void)
{ {
return Welder(); return std::make_shared<Welder>();
} }
}; };
``` ```
@@ -415,19 +417,19 @@ class IronDoorFactory : public DoorFactory
Here is how this can be used: Here is how this can be used:
```cpp ```cpp
DoorFactory woodenFactory = WoodenDoorFactory(); WoodenDoorFactory woodenFactory = WoodenDoorFactory();
Door door = woodenFactory.makeDoor(); Door::ptr_t door = woodenFactory.makeDoor();
DoorFittingExpert expert = woodenFactory.makeFittingExpert(); DoorFittingExpert::ptr_t expert = woodenFactory.makeFittingExpert();
door.getDescription(); \\ Output: I am a wooden door. door->getDescription(); // Output: I am a wooden door.
expert.getDescription(); \\ Output: I can only fit wooden doors. expert->getDescription(); // Output: I can only fit wooden doors.
DoorFactory ironFactory = IronDoorFactory(); IronDoorFactory ironFactory = IronDoorFactory();
Door door = ironFactory.makeDoor(); Door::ptr_t door2 = ironFactory.makeDoor();
DoorFittingExpert expert = woodenFactory.makeFittingExpert(); DoorFittingExpert::ptr_t expert2 = woodenFactory.makeFittingExpert();
door.getDescription(); \\ Output: I am an iron door. door2->getDescription(); // Output: I am an iron door.
expert.getDescription(); \\ Output: I can only fit iron doors. expert2->getDescription(); // Output: I can only fit iron doors.
``` ```
As you can see the wooden door factory has encapsulated the carpenter and the As you can see the wooden door factory has encapsulated the carpenter and the