mirror of
https://github.com/iandinwoodie/cpp-design-patterns-for-humans.git
synced 2025-12-17 04:24:40 +03:00
Updated output comment location for improved readability.
Reading the output before reading the line that would result in such output seemed to interrupt the flow of reading.
This commit is contained in:
41
README.md
41
README.md
@@ -155,18 +155,18 @@ Here is how this can be used:
|
|||||||
// Make a door with dimensions 100x200.
|
// Make a door with dimensions 100x200.
|
||||||
std::shared_ptr<Door> door = DoorFactory::makeDoor(100, 200);
|
std::shared_ptr<Door> door = DoorFactory::makeDoor(100, 200);
|
||||||
|
|
||||||
// Output: width = 100
|
|
||||||
std::cout << "width = " << door->getWidth() << std::endl;
|
std::cout << "width = " << door->getWidth() << std::endl;
|
||||||
// Output: height = 200
|
// Output: width = 100
|
||||||
std::cout << "height = " << door->getHeight() << std::endl;
|
std::cout << "height = " << door->getHeight() << std::endl;
|
||||||
|
// Output: height = 200
|
||||||
|
|
||||||
// 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.
|
||||||
std::shared_ptr<Door> door2 = DoorFactory::makeDoor(50, 100);
|
std::shared_ptr<Door> door2 = DoorFactory::makeDoor(50, 100);
|
||||||
|
|
||||||
// Output: width = 50
|
|
||||||
std::cout << "width = " << door2->getWidth() << std::endl;
|
std::cout << "width = " << door2->getWidth() << std::endl;
|
||||||
// Output: height = 100
|
// Output: width = 50
|
||||||
std::cout << "height = " << door2->getHeight() << std::endl;
|
std::cout << "height = " << door2->getHeight() << std::endl;
|
||||||
|
// Output: height = 100
|
||||||
```
|
```
|
||||||
|
|
||||||
#### When To Use
|
#### When To Use
|
||||||
@@ -583,8 +583,8 @@ std::shared_ptr<Burger> burger = BurgerBuilder(2).
|
|||||||
addLettuce().
|
addLettuce().
|
||||||
addTomato().
|
addTomato().
|
||||||
build();
|
build();
|
||||||
// Output: 2 patties, pepperoni, lettuce, tomato
|
|
||||||
burger->getDescription();
|
burger->getDescription();
|
||||||
|
// Output: 2 patties, pepperoni, lettuce, tomato
|
||||||
|
|
||||||
// One triple patty buger with everything.
|
// One triple patty buger with everything.
|
||||||
std::shared_ptr<Burger> burger2 = BurgerBuilder(3).
|
std::shared_ptr<Burger> burger2 = BurgerBuilder(3).
|
||||||
@@ -593,8 +593,8 @@ std::shared_ptr<Burger> burger2 = BurgerBuilder(3).
|
|||||||
addLettuce().
|
addLettuce().
|
||||||
addTomato().
|
addTomato().
|
||||||
build();
|
build();
|
||||||
// Output: 3 patties, cheese, pepperoni, lettuce, tomato
|
|
||||||
burger2->getDescription();
|
burger2->getDescription();
|
||||||
|
// Output: 3 patties, cheese, pepperoni, lettuce, tomato
|
||||||
```
|
```
|
||||||
|
|
||||||
#### When To Use
|
#### When To Use
|
||||||
@@ -1029,10 +1029,10 @@ std::shared_ptr<Theme> darkTheme = std::make_shared<DarkTheme>();
|
|||||||
About about(darkTheme);
|
About about(darkTheme);
|
||||||
Careers careers(darkTheme);
|
Careers careers(darkTheme);
|
||||||
|
|
||||||
// Output: About page in dark palette
|
|
||||||
std::cout << about.getContent() << std::endl;
|
std::cout << about.getContent() << std::endl;
|
||||||
// Output: Careers page in dark palette
|
// Output: About page in dark palette
|
||||||
std::cout << careers.getContent() << std::endl;
|
std::cout << careers.getContent() << std::endl;
|
||||||
|
// Output: Careers page in dark palette
|
||||||
```
|
```
|
||||||
|
|
||||||
#### When To Use
|
#### When To Use
|
||||||
@@ -1320,28 +1320,28 @@ Here is how this can be used:
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
std::shared_ptr<Coffee> simple = std::make_shared<SimpleCoffee>();
|
std::shared_ptr<Coffee> simple = std::make_shared<SimpleCoffee>();
|
||||||
// Output: 3
|
|
||||||
std::cout << simple->getPrice() << std::endl;
|
std::cout << simple->getPrice() << std::endl;
|
||||||
// Output: Simple coffee
|
// Output: 3
|
||||||
std::cout << simple->getDescription() << std::endl;
|
std::cout << simple->getDescription() << std::endl;
|
||||||
|
// Output: Simple coffee
|
||||||
|
|
||||||
std::shared_ptr<Coffee> 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;
|
std::cout << milk->getPrice() << std::endl;
|
||||||
// Output: Simple coffee, milk
|
// Output: 3.5
|
||||||
std::cout << milk->getDescription() << std::endl;
|
std::cout << milk->getDescription() << std::endl;
|
||||||
|
// Output: Simple coffee, milk
|
||||||
|
|
||||||
std::shared_ptr<Coffee> 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;
|
std::cout << whip->getPrice() << std::endl;
|
||||||
// Output: Simple coffee, milk, whip
|
// Output: 5.5
|
||||||
std::cout << whip->getDescription() << std::endl;
|
std::cout << whip->getDescription() << std::endl;
|
||||||
|
// Output: Simple coffee, milk, whip
|
||||||
|
|
||||||
std::shared_ptr<Coffee> 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;
|
std::cout << vanilla->getPrice() << std::endl;
|
||||||
// Output: Simple coffee, milk, whip, vanilla
|
// Output: 6.5
|
||||||
std::cout << vanilla->getDescription() << std::endl;
|
std::cout << vanilla->getDescription() << std::endl;
|
||||||
|
// Output: Simple coffee, milk, whip, vanilla
|
||||||
```
|
```
|
||||||
|
|
||||||
#### When To Use
|
#### When To Use
|
||||||
@@ -1437,16 +1437,16 @@ Here is how this can be used:
|
|||||||
std::shared_ptr<Computer> computer = std::make_shared<Computer>();
|
std::shared_ptr<Computer> computer = std::make_shared<Computer>();
|
||||||
ComputerFacade facade(computer);
|
ComputerFacade facade(computer);
|
||||||
|
|
||||||
|
facade.turnOn();
|
||||||
// Output:
|
// Output:
|
||||||
// Beep!
|
// Beep!
|
||||||
// Loading...
|
// Loading...
|
||||||
// Ready to use!
|
// Ready to use!
|
||||||
facade.turnOn();
|
|
||||||
|
|
||||||
|
facade.turnOff();
|
||||||
// Output:
|
// Output:
|
||||||
// Closing all programs!
|
// Closing all programs!
|
||||||
// Zzz
|
// Zzz
|
||||||
facade.turnOff();
|
|
||||||
```
|
```
|
||||||
|
|
||||||
#### When To Use
|
#### When To Use
|
||||||
@@ -1568,6 +1568,11 @@ std::cout << shop.getPreferenceCount() << std::endl; // Output: 3
|
|||||||
|
|
||||||
// Serve the customers.
|
// Serve the customers.
|
||||||
shop.serve();
|
shop.serve();
|
||||||
|
// Output: (Note: Since the map is unordered, the serving order may vary.)
|
||||||
|
// Serving tea to table 4
|
||||||
|
// Serving tea to table 5
|
||||||
|
// Serving tea to table 1
|
||||||
|
// Serving tea to table 2
|
||||||
```
|
```
|
||||||
|
|
||||||
#### When To Use
|
#### When To Use
|
||||||
|
|||||||
@@ -95,8 +95,8 @@ int main()
|
|||||||
addLettuce().
|
addLettuce().
|
||||||
addTomato().
|
addTomato().
|
||||||
build();
|
build();
|
||||||
// Output: 2 patties, pepperoni, lettuce, tomato
|
|
||||||
burger->getDescription();
|
burger->getDescription();
|
||||||
|
// Output: 2 patties, pepperoni, lettuce, tomato
|
||||||
|
|
||||||
// One triple patty buger with everything.
|
// One triple patty buger with everything.
|
||||||
std::shared_ptr<Burger> burger2 = BurgerBuilder(3).
|
std::shared_ptr<Burger> burger2 = BurgerBuilder(3).
|
||||||
@@ -105,8 +105,8 @@ int main()
|
|||||||
addLettuce().
|
addLettuce().
|
||||||
addTomato().
|
addTomato().
|
||||||
build();
|
build();
|
||||||
// Output: 3 patties, cheese, pepperoni, lettuce, tomato
|
|
||||||
burger2->getDescription();
|
burger2->getDescription();
|
||||||
|
// Output: 3 patties, cheese, pepperoni, lettuce, tomato
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,18 +46,18 @@ int main()
|
|||||||
// Make a door with dimensions 100x200.
|
// Make a door with dimensions 100x200.
|
||||||
std::shared_ptr<Door> door = DoorFactory::makeDoor(100, 200);
|
std::shared_ptr<Door> door = DoorFactory::makeDoor(100, 200);
|
||||||
|
|
||||||
// Output: width = 100
|
|
||||||
std::cout << "width = " << door->getWidth() << std::endl;
|
std::cout << "width = " << door->getWidth() << std::endl;
|
||||||
// Output: height = 200
|
// Output: width = 100
|
||||||
std::cout << "height = " << door->getHeight() << std::endl;
|
std::cout << "height = " << door->getHeight() << std::endl;
|
||||||
|
// Output: height = 200
|
||||||
|
|
||||||
// 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.
|
||||||
std::shared_ptr<Door> door2 = DoorFactory::makeDoor(50, 100);
|
std::shared_ptr<Door> door2 = DoorFactory::makeDoor(50, 100);
|
||||||
|
|
||||||
// Output: width = 50
|
|
||||||
std::cout << "width = " << door2->getWidth() << std::endl;
|
std::cout << "width = " << door2->getWidth() << std::endl;
|
||||||
// Output: height = 100
|
// Output: width = 50
|
||||||
std::cout << "height = " << door2->getHeight() << std::endl;
|
std::cout << "height = " << door2->getHeight() << std::endl;
|
||||||
|
// Output: height = 100
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,10 +99,10 @@ int main()
|
|||||||
About about(darkTheme);
|
About about(darkTheme);
|
||||||
Careers careers(darkTheme);
|
Careers careers(darkTheme);
|
||||||
|
|
||||||
// Output: About page in dark palette
|
|
||||||
std::cout << about.getContent() << std::endl;
|
std::cout << about.getContent() << std::endl;
|
||||||
// Output: Careers page in dark palette
|
// Output: About page in dark palette
|
||||||
std::cout << careers.getContent() << std::endl;
|
std::cout << careers.getContent() << std::endl;
|
||||||
|
// Output: Careers page in dark palette
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -92,28 +92,28 @@ class VanillaCoffee : public Coffee
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::shared_ptr<Coffee> simple = std::make_shared<SimpleCoffee>();
|
std::shared_ptr<Coffee> simple = std::make_shared<SimpleCoffee>();
|
||||||
// Output: 3
|
|
||||||
std::cout << simple->getPrice() << std::endl;
|
std::cout << simple->getPrice() << std::endl;
|
||||||
// Output: Simple coffee
|
// Output: 3
|
||||||
std::cout << simple->getDescription() << std::endl;
|
std::cout << simple->getDescription() << std::endl;
|
||||||
|
// Output: Simple coffee
|
||||||
|
|
||||||
std::shared_ptr<Coffee> 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;
|
std::cout << milk->getPrice() << std::endl;
|
||||||
// Output: Simple coffee, milk
|
// Output: 3.5
|
||||||
std::cout << milk->getDescription() << std::endl;
|
std::cout << milk->getDescription() << std::endl;
|
||||||
|
// Output: Simple coffee, milk
|
||||||
|
|
||||||
std::shared_ptr<Coffee> 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;
|
std::cout << whip->getPrice() << std::endl;
|
||||||
// Output: Simple coffee, milk, whip
|
// Output: 5.5
|
||||||
std::cout << whip->getDescription() << std::endl;
|
std::cout << whip->getDescription() << std::endl;
|
||||||
|
// Output: Simple coffee, milk, whip
|
||||||
|
|
||||||
std::shared_ptr<Coffee> 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;
|
std::cout << vanilla->getPrice() << std::endl;
|
||||||
// Output: Simple coffee, milk, whip, vanilla
|
// Output: 6.5
|
||||||
std::cout << vanilla->getDescription() << std::endl;
|
std::cout << vanilla->getDescription() << std::endl;
|
||||||
|
// Output: Simple coffee, milk, whip, vanilla
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,16 +59,16 @@ int main()
|
|||||||
std::shared_ptr<Computer> computer = std::make_shared<Computer>();
|
std::shared_ptr<Computer> computer = std::make_shared<Computer>();
|
||||||
ComputerFacade facade(computer);
|
ComputerFacade facade(computer);
|
||||||
|
|
||||||
|
facade.turnOn();
|
||||||
// Output:
|
// Output:
|
||||||
// Beep!
|
// Beep!
|
||||||
// Loading...
|
// Loading...
|
||||||
// Ready to use!
|
// Ready to use!
|
||||||
facade.turnOn();
|
|
||||||
|
|
||||||
|
facade.turnOff();
|
||||||
// Output:
|
// Output:
|
||||||
// Closing all programs!
|
// Closing all programs!
|
||||||
// Zzz
|
// Zzz
|
||||||
facade.turnOff();
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -83,6 +83,11 @@ int main()
|
|||||||
|
|
||||||
// Serve the customers.
|
// Serve the customers.
|
||||||
shop.serve();
|
shop.serve();
|
||||||
|
// Output: (Note: Since the map is unordered, the serving order may vary.)
|
||||||
|
// Serving tea to table 4
|
||||||
|
// Serving tea to table 5
|
||||||
|
// Serving tea to table 1
|
||||||
|
// Serving tea to table 2
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user