-
Notifications
You must be signed in to change notification settings - Fork 80
/
G30_Prototype.cpp
76 lines (55 loc) · 2.05 KB
/
G30_Prototype.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/**************************************************************************************************
*
* \file G30_Prototype.cpp
* \brief Guideline 30: Apply Prototype for Abstract Copy Operations
*
* Copyright (C) 2022 Klaus Iglberger - All Rights Reserved
*
* This file is part of the supplemental material for the O'Reilly book "C++ Software Design"
* (https://www.oreilly.com/library/view/c-software-design/9781098113155/).
*
**************************************************************************************************/
//---- <Animal.h> ---------------------------------------------------------------------------------
#include <memory>
class Animal
{
public:
virtual ~Animal() = default;
virtual void makeSound() const = 0;
virtual std::unique_ptr<Animal> clone() const = 0; // Prototype design pattern
};
//---- <Sheep.h> ----------------------------------------------------------------------------------
//#include <Animal.h>
#include <string>
class Sheep : public Animal
{
public:
explicit Sheep( std::string name ) : name_{ std::move(name) } {}
void makeSound() const override;
std::unique_ptr<Animal> clone() const override; // Prototype design pattern
private:
std::string name_;
};
//---- <Sheep.cpp> --------------------------------------------------------------------------------
//#include <Sheep.h>
#include <iostream>
void Sheep::makeSound() const
{
std::cout << "baa\n";
}
std::unique_ptr<Animal> Sheep::clone() const
{
return std::make_unique<Sheep>(*this); // Copy-construct a sheep
}
//---- <Main.cpp> ---------------------------------------------------------------------------------
//#include <Sheep.h>
#include <cstdlib>
#include <memory>
int main()
{
std::unique_ptr<Animal> dolly = std::make_unique<Sheep>( "Dolly" );
std::unique_ptr<Animal> dollyClone = dolly->clone();
dolly->makeSound(); // Triggers the first Dolly's beastly sound
dollyClone->makeSound(); // The clone sounds just like Dolly
return EXIT_SUCCESS;
}