mirror of
https://github.com/iandinwoodie/cpp-design-patterns-for-humans.git
synced 2025-12-16 20:17:08 +03:00
Rolling back use of typedef with smart pointers.
This is to reduce the obfuscation of the code.
This commit is contained in:
@@ -37,7 +37,6 @@ class Hunter
|
||||
class WildDog
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<WildDog> ptr_t;
|
||||
void bark(void)
|
||||
{
|
||||
std::cout << "*wild dog bark*" << std::endl;
|
||||
@@ -47,7 +46,7 @@ class WildDog
|
||||
class WildDogAdapter : public Lion
|
||||
{
|
||||
public:
|
||||
WildDogAdapter(WildDog::ptr_t dog)
|
||||
WildDogAdapter(std::shared_ptr<WildDog> dog)
|
||||
: dog_(dog)
|
||||
{
|
||||
}
|
||||
@@ -58,12 +57,12 @@ class WildDogAdapter : public Lion
|
||||
}
|
||||
|
||||
private:
|
||||
WildDog::ptr_t dog_;
|
||||
std::shared_ptr<WildDog> dog_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
WildDog::ptr_t wildDog = std::make_shared<WildDog>();
|
||||
std::shared_ptr<WildDog> wildDog = std::make_shared<WildDog>();
|
||||
WildDogAdapter wildDogAdapter(wildDog);
|
||||
|
||||
Hunter hunter;
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
class Theme
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<Theme> ptr_t;
|
||||
virtual std::string getColor(void) = 0;
|
||||
};
|
||||
|
||||
@@ -18,7 +17,7 @@ class WebPage
|
||||
class About : public WebPage
|
||||
{
|
||||
public:
|
||||
About(Theme::ptr_t theme)
|
||||
About(std::shared_ptr<Theme> theme)
|
||||
: theme_(theme)
|
||||
{
|
||||
}
|
||||
@@ -29,13 +28,13 @@ class About : public WebPage
|
||||
}
|
||||
|
||||
private:
|
||||
Theme::ptr_t theme_;
|
||||
std::shared_ptr<Theme> theme_;
|
||||
};
|
||||
|
||||
class Projects : public WebPage
|
||||
{
|
||||
public:
|
||||
Projects(Theme::ptr_t theme)
|
||||
Projects(std::shared_ptr<Theme> theme)
|
||||
: theme_(theme)
|
||||
{
|
||||
}
|
||||
@@ -46,14 +45,14 @@ class Projects : public WebPage
|
||||
}
|
||||
|
||||
private:
|
||||
Theme::ptr_t theme_;
|
||||
std::shared_ptr<Theme> theme_;
|
||||
};
|
||||
|
||||
|
||||
class Careers : public WebPage
|
||||
{
|
||||
public:
|
||||
Careers(Theme::ptr_t theme)
|
||||
Careers(std::shared_ptr<Theme> theme)
|
||||
: theme_(theme)
|
||||
{
|
||||
}
|
||||
@@ -64,7 +63,7 @@ class Careers : public WebPage
|
||||
}
|
||||
|
||||
private:
|
||||
Theme::ptr_t theme_;
|
||||
std::shared_ptr<Theme> theme_;
|
||||
};
|
||||
|
||||
class DarkTheme : public Theme
|
||||
@@ -96,7 +95,7 @@ class AquaTheme : public Theme
|
||||
|
||||
int main()
|
||||
{
|
||||
Theme::ptr_t darkTheme = std::make_shared<DarkTheme>();
|
||||
std::shared_ptr<Theme> darkTheme = std::make_shared<DarkTheme>();
|
||||
About about(darkTheme);
|
||||
Careers careers(darkTheme);
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
class Employee
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<Employee> ptr_t;
|
||||
virtual std::string getName(void) = 0;
|
||||
virtual void setSalary(float salary) = 0;
|
||||
virtual float getSalary(void) = 0;
|
||||
@@ -84,7 +83,7 @@ class Designer : public Employee
|
||||
class Organization
|
||||
{
|
||||
public:
|
||||
void addEmployee(Employee::ptr_t employee)
|
||||
void addEmployee(std::shared_ptr<Employee> employee)
|
||||
{
|
||||
employees_.push_back(employee);
|
||||
}
|
||||
@@ -100,14 +99,14 @@ class Organization
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<Employee::ptr_t> employees_;
|
||||
std::vector<std::shared_ptr<Employee>> employees_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
// Prepare the employees.
|
||||
Employee::ptr_t jane = std::make_shared<Developer>("Jane Doe", 50000);
|
||||
Employee::ptr_t john = std::make_shared<Designer>("John Doe", 45000);
|
||||
std::shared_ptr<Employee> jane = std::make_shared<Developer>("Jane", 50000);
|
||||
std::shared_ptr<Employee> john = std::make_shared<Designer>("John", 45000);
|
||||
|
||||
// Add them to the organization.
|
||||
Organization org;
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
class Coffee
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<Coffee> ptr_t;
|
||||
virtual float getPrice(void) = 0;
|
||||
virtual std::string getDescription(void) = 0;
|
||||
};
|
||||
@@ -27,7 +26,7 @@ class SimpleCoffee : public Coffee
|
||||
class MilkCoffee : public Coffee
|
||||
{
|
||||
public:
|
||||
MilkCoffee(Coffee::ptr_t coffee)
|
||||
MilkCoffee(std::shared_ptr<Coffee> coffee)
|
||||
: coffee_(coffee)
|
||||
{
|
||||
}
|
||||
@@ -43,13 +42,13 @@ class MilkCoffee : public Coffee
|
||||
}
|
||||
|
||||
private:
|
||||
Coffee::ptr_t coffee_;
|
||||
std::shared_ptr<Coffee> coffee_;
|
||||
};
|
||||
|
||||
class WhipCoffee : public Coffee
|
||||
{
|
||||
public:
|
||||
WhipCoffee(Coffee::ptr_t coffee)
|
||||
WhipCoffee(std::shared_ptr<Coffee> coffee)
|
||||
: coffee_(coffee)
|
||||
{
|
||||
}
|
||||
@@ -65,13 +64,13 @@ class WhipCoffee : public Coffee
|
||||
}
|
||||
|
||||
private:
|
||||
Coffee::ptr_t coffee_;
|
||||
std::shared_ptr<Coffee> coffee_;
|
||||
};
|
||||
|
||||
class VanillaCoffee : public Coffee
|
||||
{
|
||||
public:
|
||||
VanillaCoffee(Coffee::ptr_t coffee)
|
||||
VanillaCoffee(std::shared_ptr<Coffee> coffee)
|
||||
: coffee_(coffee)
|
||||
{
|
||||
}
|
||||
@@ -87,30 +86,30 @@ class VanillaCoffee : public Coffee
|
||||
}
|
||||
|
||||
private:
|
||||
Coffee::ptr_t coffee_;
|
||||
std::shared_ptr<Coffee> coffee_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Coffee::ptr_t simple = std::make_shared<SimpleCoffee>();
|
||||
std::shared_ptr<Coffee> simple = std::make_shared<SimpleCoffee>();
|
||||
// Output: 3
|
||||
std::cout << simple->getPrice() << std::endl;
|
||||
// Output: Simple coffee
|
||||
std::cout << simple->getDescription() << std::endl;
|
||||
|
||||
Coffee::ptr_t milk = std::make_shared<MilkCoffee>(simple);
|
||||
std::shared_ptr<Coffee> milk = std::make_shared<MilkCoffee>(simple);
|
||||
// Output: 3.5
|
||||
std::cout << milk->getPrice() << std::endl;
|
||||
// Output: Simple coffee, milk
|
||||
std::cout << milk->getDescription() << std::endl;
|
||||
|
||||
Coffee::ptr_t whip = std::make_shared<WhipCoffee>(milk);
|
||||
std::shared_ptr<Coffee> whip = std::make_shared<WhipCoffee>(milk);
|
||||
// Output: 5.5
|
||||
std::cout << whip->getPrice() << std::endl;
|
||||
// Output: Simple coffee, milk, whip
|
||||
std::cout << whip->getDescription() << std::endl;
|
||||
|
||||
Coffee::ptr_t vanilla = std::make_shared<VanillaCoffee>(whip);
|
||||
std::shared_ptr<Coffee> vanilla = std::make_shared<VanillaCoffee>(whip);
|
||||
// Output: 6.5
|
||||
std::cout << vanilla->getPrice() << std::endl;
|
||||
// Output: Simple coffee, milk, whip, vanilla
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
class Computer
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<Computer> ptr_t;
|
||||
|
||||
void makeBootSound(void)
|
||||
{
|
||||
std::cout << "Beep!" << std::endl;
|
||||
@@ -34,7 +32,7 @@ class Computer
|
||||
class ComputerFacade
|
||||
{
|
||||
public:
|
||||
ComputerFacade(Computer::ptr_t computer)
|
||||
ComputerFacade(std::shared_ptr<Computer> computer)
|
||||
: computer_(computer)
|
||||
{
|
||||
}
|
||||
@@ -53,12 +51,12 @@ class ComputerFacade
|
||||
}
|
||||
|
||||
private:
|
||||
Computer::ptr_t computer_;
|
||||
std::shared_ptr<Computer> computer_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Computer::ptr_t computer = std::make_shared<Computer>();
|
||||
std::shared_ptr<Computer> computer = std::make_shared<Computer>();
|
||||
ComputerFacade facade(computer);
|
||||
|
||||
// Output:
|
||||
|
||||
@@ -2,18 +2,14 @@
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
class Tea
|
||||
struct Tea
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<Tea> ptr_t;
|
||||
};
|
||||
|
||||
class TeaMaker
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<TeaMaker> ptr_t;
|
||||
|
||||
Tea::ptr_t make(const std::string& preference)
|
||||
std::shared_ptr<Tea> make(const std::string& preference)
|
||||
{
|
||||
auto match = availableTea_.find(preference);
|
||||
if (match == availableTea_.end()) {
|
||||
@@ -29,13 +25,13 @@ class TeaMaker
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, Tea::ptr_t> availableTea_;
|
||||
std::unordered_map<std::string, std::shared_ptr<Tea>> availableTea_;
|
||||
};
|
||||
|
||||
class TeaShop
|
||||
{
|
||||
public:
|
||||
TeaShop(TeaMaker::ptr_t maker)
|
||||
TeaShop(std::shared_ptr<TeaMaker> maker)
|
||||
: maker_(maker)
|
||||
{
|
||||
}
|
||||
@@ -58,13 +54,13 @@ class TeaShop
|
||||
}
|
||||
|
||||
private:
|
||||
TeaMaker::ptr_t maker_;
|
||||
std::unordered_map<int, Tea::ptr_t> orders_;
|
||||
std::shared_ptr<TeaMaker> maker_;
|
||||
std::unordered_map<int, std::shared_ptr<Tea>> orders_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
TeaMaker::ptr_t maker = std::make_shared<TeaMaker>();
|
||||
std::shared_ptr<TeaMaker> maker = std::make_shared<TeaMaker>();
|
||||
TeaShop shop(maker);
|
||||
|
||||
// No orders have been taken, so there are no available teas.
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
class Door
|
||||
{
|
||||
public:
|
||||
typedef std::shared_ptr<Door> ptr_t;
|
||||
virtual void open(void) = 0;
|
||||
virtual void close(void) = 0;
|
||||
};
|
||||
@@ -27,7 +26,7 @@ class LabDoor : public Door
|
||||
class SecuredDoor
|
||||
{
|
||||
public:
|
||||
SecuredDoor(Door::ptr_t door)
|
||||
SecuredDoor(std::shared_ptr<Door> door)
|
||||
: door_(door)
|
||||
{
|
||||
}
|
||||
@@ -52,12 +51,12 @@ class SecuredDoor
|
||||
return password == "Bond007";
|
||||
}
|
||||
|
||||
Door::ptr_t door_;
|
||||
std::shared_ptr<Door> door_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
Door::ptr_t labDoor = std::make_shared<LabDoor>();
|
||||
std::shared_ptr<Door> labDoor = std::make_shared<LabDoor>();
|
||||
SecuredDoor securedDoor(labDoor);
|
||||
|
||||
securedDoor.open("invalid"); // Output: No way, Jose!
|
||||
|
||||
Reference in New Issue
Block a user