-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathG28_Bridge.cpp
141 lines (101 loc) · 3.22 KB
/
G28_Bridge.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**************************************************************************************************
*
* \file G28_Bridge.cpp
* \brief Guideline 28: Build Bridges to Remove Physical Dependencies
*
* 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/).
*
**************************************************************************************************/
//---- <Engine.h> ---------------------------------------------------------------------------------
class Engine
{
public:
virtual ~Engine() = default;
virtual void start() = 0;
virtual void stop() = 0;
// ... more engine-specific functions
private:
// ...
};
//---- <Car.h> ------------------------------------------------------------------------------------
//#include <Engine.h>
#include <memory>
#include <utility>
class Car
{
protected:
explicit Car( std::unique_ptr<Engine> engine )
: pimpl_{ std::move(engine) }
{}
public:
virtual ~Car() = default;
virtual void drive() = 0;
// ... more car-specific functions
protected:
Engine* getEngine() { return pimpl_.get(); }
Engine const* getEngine() const { return pimpl_.get(); }
private:
std::unique_ptr<Engine> pimpl_; // Pointer-to-implementation (pimpl)
// ... more car-specific data members (wheels, drivetrain, ...)
};
//---- <ElectricEngine.h> -------------------------------------------------------------------------
//#include <Engine.h>
class ElectricEngine : public Engine
{
public:
void start() override;
void stop() override;
private:
// ...
};
//---- <ElectricEngine.cpp> -----------------------------------------------------------------------
//#include <ElectricEngine.h>
#include <iostream>
void ElectricEngine::start()
{
std::cout << "Starting the 'ElectricEngine'...\n";
}
void ElectricEngine::stop()
{
std::cout << "Stopping the 'ElectricEngine'...\n";
}
//---- <ElectricCar.h> ----------------------------------------------------------------------------
//#include <Car.h>
class ElectricCar : public Car
{
public:
ElectricCar();
void drive();
// ...
private:
// ...
};
//---- <ElectricCar.cpp> --------------------------------------------------------------------------
//#include <ElectricCar.h>
//#include <ElectricEngine.h>
#include <iostream>
ElectricCar::ElectricCar()
: Car{ std::make_unique<ElectricEngine>( /*engine arguments*/ ) }
// ... Initialization of the other data members
{}
void ElectricCar::drive()
{
getEngine()->start();
std::cout << "Driving the 'ElectricCar'...\n";
getEngine()->stop();
}
// ... Other 'ElectricCar' member functions, primarily using the 'Engine'
// abstraction, but potentially also explicitly dealing with an
// 'ElectricEngine'.
//---- <Main.cpp> ---------------------------------------------------------------------------------
//#include <ElectricCar.h>
#include <cstdlib>
int main()
{
ElectricCar ecar{};
ecar.drive();
return EXIT_SUCCESS;
}