mirror of
https://github.com/iandinwoodie/cpp-design-patterns-for-humans.git
synced 2025-12-17 04:24:40 +03:00
Rolling back use of typedef with smart pointers.
This is to reduce the obfuscation of the code.
This commit is contained in:
@@ -4,7 +4,6 @@
|
|||||||
class Door
|
class Door
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<Door> ptr_t;
|
|
||||||
virtual void getDescription(void) = 0;
|
virtual void getDescription(void) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -29,7 +28,6 @@ class IronDoor : public Door
|
|||||||
class DoorFittingExpert
|
class DoorFittingExpert
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<DoorFittingExpert> ptr_t;
|
|
||||||
virtual void getDescription(void) = 0;
|
virtual void getDescription(void) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -48,26 +46,25 @@ class Carpenter : public DoorFittingExpert
|
|||||||
void getDescription(void)
|
void getDescription(void)
|
||||||
{
|
{
|
||||||
std::cout << "I can only fit wooden doors." << std::endl;
|
std::cout << "I can only fit wooden doors." << std::endl;
|
||||||
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class DoorFactory
|
class DoorFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual Door::ptr_t makeDoor(void) = 0;
|
virtual std::shared_ptr<Door> makeDoor(void) = 0;
|
||||||
virtual DoorFittingExpert::ptr_t makeFittingExpert(void) = 0;
|
virtual std::shared_ptr<DoorFittingExpert> makeFittingExpert(void) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class WoodenDoorFactory : public DoorFactory
|
class WoodenDoorFactory : public DoorFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Door::ptr_t makeDoor(void)
|
std::shared_ptr<Door> makeDoor(void)
|
||||||
{
|
{
|
||||||
return std::make_shared<WoodenDoor>();
|
return std::make_shared<WoodenDoor>();
|
||||||
}
|
}
|
||||||
|
|
||||||
DoorFittingExpert::ptr_t makeFittingExpert(void)
|
std::shared_ptr<DoorFittingExpert> makeFittingExpert(void)
|
||||||
{
|
{
|
||||||
return std::make_shared<Carpenter>();
|
return std::make_shared<Carpenter>();
|
||||||
}
|
}
|
||||||
@@ -76,12 +73,12 @@ class WoodenDoorFactory : public DoorFactory
|
|||||||
class IronDoorFactory : public DoorFactory
|
class IronDoorFactory : public DoorFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Door::ptr_t makeDoor(void)
|
std::shared_ptr<Door> makeDoor(void)
|
||||||
{
|
{
|
||||||
return std::make_shared<IronDoor>();
|
return std::make_shared<IronDoor>();
|
||||||
}
|
}
|
||||||
|
|
||||||
DoorFittingExpert::ptr_t makeFittingExpert(void)
|
std::shared_ptr<DoorFittingExpert> makeFittingExpert(void)
|
||||||
{
|
{
|
||||||
return std::make_shared<Welder>();
|
return std::make_shared<Welder>();
|
||||||
}
|
}
|
||||||
@@ -90,15 +87,15 @@ class IronDoorFactory : public DoorFactory
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
WoodenDoorFactory woodenFactory = WoodenDoorFactory();
|
WoodenDoorFactory woodenFactory = WoodenDoorFactory();
|
||||||
Door::ptr_t door = woodenFactory.makeDoor();
|
std::shared_ptr<Door> door = woodenFactory.makeDoor();
|
||||||
DoorFittingExpert::ptr_t expert = woodenFactory.makeFittingExpert();
|
std::shared_ptr<DoorFittingExpert> 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.
|
||||||
|
|
||||||
IronDoorFactory ironFactory = IronDoorFactory();
|
IronDoorFactory ironFactory = IronDoorFactory();
|
||||||
Door::ptr_t door2 = ironFactory.makeDoor();
|
std::shared_ptr<Door> door2 = ironFactory.makeDoor();
|
||||||
DoorFittingExpert::ptr_t expert2 = woodenFactory.makeFittingExpert();
|
std::shared_ptr<DoorFittingExpert> expert2 = woodenFactory.makeFittingExpert();
|
||||||
|
|
||||||
door2->getDescription(); // Output: I am an iron door.
|
door2->getDescription(); // Output: I am an iron door.
|
||||||
expert2->getDescription(); // Output: I can only fit iron doors.
|
expert2->getDescription(); // Output: I can only fit iron doors.
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ class BurgerBuilder;
|
|||||||
class Burger
|
class Burger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<Burger> ptr_t;
|
|
||||||
Burger(BurgerBuilder* builder);
|
Burger(BurgerBuilder* builder);
|
||||||
void getDescription(void);
|
void getDescription(void);
|
||||||
|
|
||||||
@@ -51,10 +50,9 @@ class BurgerBuilder
|
|||||||
return (*this);
|
return (*this);
|
||||||
}
|
}
|
||||||
|
|
||||||
Burger::ptr_t build(void)
|
std::shared_ptr<Burger> build(void)
|
||||||
{
|
{
|
||||||
Burger::ptr_t burger(new Burger(this));
|
return std::make_shared<Burger>(this);
|
||||||
return burger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int patties;
|
int patties;
|
||||||
@@ -92,7 +90,7 @@ void Burger::getDescription(void)
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// One double patty burger with no dairy.
|
// One double patty burger with no dairy.
|
||||||
Burger::ptr_t burger = BurgerBuilder(2).
|
std::shared_ptr<Burger> burger = BurgerBuilder(2).
|
||||||
addPepperoni().
|
addPepperoni().
|
||||||
addLettuce().
|
addLettuce().
|
||||||
addTomato().
|
addTomato().
|
||||||
@@ -101,7 +99,7 @@ int main()
|
|||||||
burger->getDescription();
|
burger->getDescription();
|
||||||
|
|
||||||
// One triple patty buger with everything.
|
// One triple patty buger with everything.
|
||||||
Burger::ptr_t burger2 = BurgerBuilder(3).
|
std::shared_ptr<Burger> burger2 = BurgerBuilder(3).
|
||||||
addPepperoni().
|
addPepperoni().
|
||||||
addCheese().
|
addCheese().
|
||||||
addLettuce().
|
addLettuce().
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
class Interviewer
|
class Interviewer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
typedef std::shared_ptr<Interviewer> ptr_t;
|
|
||||||
virtual void askQuestions(void) = 0;
|
virtual void askQuestions(void) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -31,18 +30,18 @@ class HiringManager
|
|||||||
public:
|
public:
|
||||||
void takeInterview(void)
|
void takeInterview(void)
|
||||||
{
|
{
|
||||||
Interviewer::ptr_t interviewer = makeInterviewer();
|
std::shared_ptr<Interviewer> interviewer = makeInterviewer();
|
||||||
interviewer->askQuestions();
|
interviewer->askQuestions();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual Interviewer::ptr_t makeInterviewer(void) = 0;
|
virtual std::shared_ptr<Interviewer> makeInterviewer(void) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class DevelopmentManager : public HiringManager
|
class DevelopmentManager : public HiringManager
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
Interviewer::ptr_t makeInterviewer(void)
|
std::shared_ptr<Interviewer> makeInterviewer(void)
|
||||||
{
|
{
|
||||||
return std::make_shared<Developer>();
|
return std::make_shared<Developer>();
|
||||||
}
|
}
|
||||||
@@ -51,7 +50,7 @@ class DevelopmentManager : public HiringManager
|
|||||||
class MarketingManager : public HiringManager
|
class MarketingManager : public HiringManager
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
Interviewer::ptr_t makeInterviewer(void)
|
std::shared_ptr<Interviewer> makeInterviewer(void)
|
||||||
{
|
{
|
||||||
return std::make_shared<CommunityExecutive>();
|
return std::make_shared<CommunityExecutive>();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ class WoodenDoor : public Door
|
|||||||
class DoorFactory
|
class DoorFactory
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static Door::ptr_t makeDoor(float width, float height)
|
static std::shared_ptr<Door> makeDoor(float width, float height)
|
||||||
{
|
{
|
||||||
return std::make_shared<WoodenDoor>(width, height);
|
return std::make_shared<WoodenDoor>(width, height);
|
||||||
}
|
}
|
||||||
@@ -44,7 +44,7 @@ class DoorFactory
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
// Make a door with dimensions 100x200.
|
// Make a door with dimensions 100x200.
|
||||||
Door::ptr_t door = DoorFactory::makeDoor(100, 200);
|
std::shared_ptr<Door> door = DoorFactory::makeDoor(100, 200);
|
||||||
|
|
||||||
// Output: width = 100
|
// Output: width = 100
|
||||||
std::cout << "width = " << door->getWidth() << std::endl;
|
std::cout << "width = " << door->getWidth() << std::endl;
|
||||||
@@ -52,7 +52,7 @@ int main()
|
|||||||
std::cout << "height = " << door->getHeight() << std::endl;
|
std::cout << "height = " << door->getHeight() << std::endl;
|
||||||
|
|
||||||
// We can use the factory again to make a door with dimensions 50x100.
|
// We can use the factory again to make a door with dimensions 50x100.
|
||||||
Door::ptr_t door2 = DoorFactory::makeDoor(50, 100);
|
std::shared_ptr<Door> door2 = DoorFactory::makeDoor(50, 100);
|
||||||
|
|
||||||
// Output: width = 50
|
// Output: width = 50
|
||||||
std::cout << "width = " << door2->getWidth() << std::endl;
|
std::cout << "width = " << door2->getWidth() << std::endl;
|
||||||
|
|||||||
Reference in New Issue
Block a user