├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE.md ├── PULL_REQUEST_TEMPLATE.md └── workflows │ └── main.yml ├── .gitignore ├── CONTRIBUTING.md ├── Ch01-Adapter ├── 01_02 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── incompatible-interface.cpp ├── 01_03 │ ├── begin │ │ ├── .vscode │ │ │ └── tasks.json │ │ └── object-adapter.cpp │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── object-adapter.cpp ├── 01_04 │ ├── begin │ │ ├── .vscode │ │ │ └── tasks.json │ │ └── class-adapter.cpp │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── class-adapter.cpp ├── 01_05 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── cloud-storage.cpp └── 01_06 │ ├── begin │ ├── .vscode │ │ └── tasks.json │ └── cloud-storage.cpp │ └── end │ ├── .vscode │ └── tasks.json │ └── cloud-storage.cpp ├── Ch02-Bridge ├── 02_02 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── inheritance-explosion.cpp ├── 02_03 │ ├── begin │ │ ├── .vscode │ │ │ └── tasks.json │ │ └── inheritance-explosion.cpp │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── bridge-refactor.cpp └── 02_05 │ └── end │ ├── .vscode │ └── tasks.json │ └── vehicles-engines.cpp ├── Ch03-Composite ├── 03_02 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── boxes-products.cpp ├── 03_03 │ ├── begin │ │ ├── .vscode │ │ │ └── tasks.json │ │ └── boxes-products.cpp │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── boxes-products.cpp ├── 03_04 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── drawing-shapes.cpp └── 03_05 │ ├── begin │ ├── .vscode │ │ └── tasks.json │ └── drawing-shapes.cpp │ └── end │ ├── .vscode │ └── tasks.json │ └── drawing-shapes.cpp ├── Ch04-Decorator ├── 04_02 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── computershop.cpp ├── 04_03 │ ├── begin │ │ ├── .vscode │ │ │ └── tasks.json │ │ └── computershop.cpp │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── computershop.cpp ├── 04_04 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── pizza.cpp └── 04_05 │ ├── begin │ ├── .vscode │ │ └── tasks.json │ └── pizza.cpp │ └── end │ ├── .vscode │ └── tasks.json │ └── pizza.cpp ├── Ch05-The Facade ├── 05_02 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── hotel-reservation.cpp ├── 05_03 │ ├── begin │ │ ├── .vscode │ │ │ └── tasks.json │ │ └── hotel-reservation.cpp │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── hotel-reservation.cpp ├── 05_04 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── weatherapi.cpp └── 05_05 │ ├── begin │ ├── .vscode │ │ └── tasks.json │ └── weatherapi.cpp │ └── end │ ├── .vscode │ └── tasks.json │ └── weatherapi.cpp ├── Ch06-The Flyweight ├── 06_02 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── sprites.cpp ├── 06_03 │ ├── begin │ │ ├── .vscode │ │ │ └── tasks.json │ │ └── sprites.cpp │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── sprites.cpp ├── 06_04 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── text-editor.cpp └── 06_05 │ ├── begin │ ├── .vscode │ │ └── tasks.json │ └── text-editor.cpp │ └── end │ ├── .vscode │ └── tasks.json │ └── text-editor.cpp ├── Ch07-The Proxy ├── 07_02 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ ├── config.txt │ │ └── read-config.cpp ├── 07_03 │ ├── begin │ │ ├── .vscode │ │ │ └── tasks.json │ │ ├── config.txt │ │ └── read-config.cpp │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ ├── config.txt │ │ └── read-config.cpp ├── 07_04 │ └── end │ │ ├── .vscode │ │ └── tasks.json │ │ └── confidential.cpp └── 07_05 │ ├── begin │ ├── .vscode │ │ └── tasks.json │ └── confidential.cpp │ └── end │ ├── .vscode │ └── tasks.json │ └── confidential.cpp ├── LICENSE ├── NOTICE └── README.md /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Codeowners for these exercise files: 2 | # * (asterisk) denotes "all files and folders" 3 | # Example: * @producer @instructor 4 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 7 | 8 | ## Issue Overview 9 | 10 | 11 | ## Describe your environment 12 | 13 | 14 | ## Steps to Reproduce 15 | 16 | 1. 17 | 2. 18 | 3. 19 | 4. 20 | 21 | ## Expected Behavior 22 | 23 | 24 | ## Current Behavior 25 | 26 | 27 | ## Possible Solution 28 | 29 | 30 | ## Screenshots / Video 31 | 32 | 33 | ## Related Issues 34 | 35 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: Copy To Branches 2 | on: 3 | workflow_dispatch: 4 | jobs: 5 | copy-to-branches: 6 | runs-on: ubuntu-latest 7 | steps: 8 | - uses: actions/checkout@v2 9 | with: 10 | fetch-depth: 0 11 | - name: Copy To Branches Action 12 | uses: planetoftheweb/copy-to-branches@v1.2 13 | env: 14 | key: main 15 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .tmp 4 | npm-debug.log 5 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | 2 | Contribution Agreement 3 | ====================== 4 | 5 | This repository does not accept pull requests (PRs). All pull requests will be closed. 6 | 7 | However, if any contributions (through pull requests, issues, feedback or otherwise) are provided, as a contributor, you represent that the code you submit is your original work or that of your employer (in which case you represent you have the right to bind your employer). By submitting code (or otherwise providing feedback), you (and, if applicable, your employer) are licensing the submitted code (and/or feedback) to LinkedIn and the open source community subject to the BSD 2-Clause license. 8 | -------------------------------------------------------------------------------- /Ch01-Adapter/01_02/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch01-Adapter/01_02/end/incompatible-interface.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Component 7 | { 8 | public: 9 | virtual void run() = 0; 10 | virtual ~Component() = default; 11 | }; 12 | 13 | class ConcreteComponentA: public Component 14 | { 15 | public: 16 | virtual void run() override 17 | { 18 | cout << "Executing ConcreteComponentA::run()" << endl; 19 | } 20 | }; 21 | 22 | class ConcreteComponentB: public Component 23 | { 24 | public: 25 | virtual void run() override 26 | { 27 | cout << "Executing ConcreteComponentB::run()" << endl; 28 | } 29 | }; 30 | 31 | // Incompatible class 32 | class LegacyComponent 33 | { 34 | public: 35 | void go() 36 | { 37 | cout << "Executing LegacyComponent::go()" << endl; 38 | } 39 | }; 40 | 41 | int main() 42 | { 43 | const unique_ptr components[] 44 | { 45 | make_unique(), 46 | make_unique(), 47 | // The next line will trigger a compiler error (no viable conversion from 'unique_ptr' to 'const unique_ptr') 48 | make_unique() 49 | }; 50 | 51 | for (const auto& component : components) 52 | { 53 | component->run(); 54 | } 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /Ch01-Adapter/01_03/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch01-Adapter/01_03/begin/object-adapter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Component 6 | { 7 | public: 8 | virtual void run() = 0; 9 | virtual ~Component() = default; 10 | }; 11 | 12 | class ConcreteComponentA: public Component 13 | { 14 | public: 15 | virtual void run() override 16 | { 17 | cout << "Executing ConcreteComponentA::run()" << endl; 18 | } 19 | }; 20 | 21 | class ConcreteComponentB: public Component 22 | { 23 | public: 24 | virtual void run() override 25 | { 26 | cout << "Executing ConcreteComponentB::run()" << endl; 27 | } 28 | }; 29 | 30 | // Incompatible class 31 | class LegacyComponent 32 | { 33 | public: 34 | void go() 35 | { 36 | cout << "Executing LegacyComponent::go()" << endl; 37 | } 38 | }; 39 | 40 | class LegacyClassAdapter: public Component, private LegacyComponent 41 | { 42 | public: 43 | LegacyAdapter() : m_adaptee(make_unique()) {} 44 | 45 | virtual void run() override 46 | { 47 | cout << "LegacyAdapter::run() -> calling LegacyComponent::go()" << endl; 48 | m_adaptee->go(); 49 | } 50 | 51 | private: 52 | unique_ptr m_adaptee; 53 | }; 54 | 55 | 56 | int main() 57 | { 58 | const unique_ptr components[] 59 | { 60 | make_unique(), 61 | make_unique(), 62 | make_unique() 63 | }; 64 | 65 | for (const auto& component : components) 66 | { 67 | component->run(); 68 | } 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /Ch01-Adapter/01_03/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch01-Adapter/01_03/end/object-adapter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Component 6 | { 7 | public: 8 | virtual void run() = 0; 9 | virtual ~Component() = default; 10 | }; 11 | 12 | class ConcreteComponentA : public Component 13 | { 14 | public: 15 | virtual void run() override 16 | { 17 | cout << "Executing ConcreteComponentA::run()" << endl; 18 | } 19 | }; 20 | 21 | class ConcreteComponentB : public Component 22 | { 23 | public: 24 | virtual void run() override 25 | { 26 | cout << "Executing ConcreteComponentB::run()" << endl; 27 | } 28 | }; 29 | 30 | // Incompatible class 31 | class LegacyComponent 32 | { 33 | public: 34 | void go() 35 | { 36 | cout << "Executing LegacyComponent::go()" << endl; 37 | } 38 | }; 39 | 40 | class LegacyAdapter : public Component 41 | { 42 | public: 43 | LegacyAdapter() : m_adaptee(make_unique()) {} 44 | 45 | virtual void run() override 46 | { 47 | cout << "LegacyAdapter::run() -> Calling LegacyComponent::go()" << endl; 48 | m_adaptee->go(); 49 | } 50 | private: 51 | unique_ptr m_adaptee; 52 | }; 53 | 54 | int main() 55 | { 56 | const unique_ptr components[] 57 | { 58 | make_unique(), 59 | make_unique(), 60 | make_unique() 61 | }; 62 | 63 | for (const auto &component : components) 64 | { 65 | component->run(); 66 | } 67 | return 0; 68 | } 69 | -------------------------------------------------------------------------------- /Ch01-Adapter/01_04/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch01-Adapter/01_04/begin/class-adapter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Component 7 | { 8 | public: 9 | virtual void run() = 0; 10 | virtual ~Component() = default; 11 | }; 12 | 13 | class ConcreteComponentA: public Component 14 | { 15 | public: 16 | virtual void run() override 17 | { 18 | cout << "Executing ConcreteComponentA::run()" << endl; 19 | } 20 | }; 21 | 22 | class ConcreteComponentB: public Component 23 | { 24 | public: 25 | virtual void run() override 26 | { 27 | cout << "Executing ConcreteComponentB::run()" << endl; 28 | } 29 | }; 30 | 31 | // Incompatible class 32 | class LegacyComponent 33 | { 34 | public: 35 | void go() 36 | { 37 | cout << "Executing LegacyComponent::go()" << endl; 38 | } 39 | }; 40 | 41 | 42 | class LegacyClassAdapter: public Component, private LegacyComponent 43 | { 44 | public: 45 | virtual void run() override 46 | { 47 | cout << "LegacyClassAdapter::run() -> calling LegacyComponent::go()" << endl; 48 | go(); 49 | } 50 | }; 51 | 52 | 53 | int main() 54 | { 55 | const unique_ptr components[] 56 | { 57 | make_unique(), 58 | make_unique(), 59 | make_unique() 60 | }; 61 | 62 | for (const auto& component : components) 63 | { 64 | component->run(); 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Ch01-Adapter/01_04/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch01-Adapter/01_04/end/class-adapter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Component 6 | { 7 | public: 8 | virtual void run() = 0; 9 | virtual ~Component() = default; 10 | }; 11 | 12 | class ConcreteComponentA: public Component 13 | { 14 | public: 15 | virtual void run() override 16 | { 17 | cout << "Executing ConcreteComponentA::run()" << endl; 18 | } 19 | }; 20 | 21 | class ConcreteComponentB: public Component 22 | { 23 | public: 24 | virtual void run() override 25 | { 26 | cout << "Executing ConcreteComponentB::run()" << endl; 27 | } 28 | }; 29 | 30 | // Incompatible class 31 | class LegacyComponent 32 | { 33 | public: 34 | void go() 35 | { 36 | cout << "Executing LegacyComponent::go()" << endl; 37 | } 38 | }; 39 | 40 | // Class Adapter 41 | class LegacyClassAdapter : public Component, private LegacyComponent 42 | { 43 | public: 44 | void run() override 45 | { 46 | // Delegate the request to the Adaptee 47 | cout << "LegacyClassAdapter::run() -> Calling LegacyComponent::go()" << endl; 48 | go(); 49 | } 50 | }; 51 | 52 | int main() 53 | { 54 | const unique_ptr components[] 55 | { 56 | make_unique(), 57 | make_unique(), 58 | make_unique() 59 | }; 60 | 61 | for (const auto& component : components) 62 | { 63 | component->run(); 64 | } 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /Ch01-Adapter/01_05/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch01-Adapter/01_05/end/cloud-storage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CloudStorage 9 | { 10 | public: 11 | virtual bool uploadContents(const string& content) = 0; 12 | virtual int getFreeSpace() = 0; 13 | virtual ~CloudStorage() = default; 14 | }; 15 | 16 | class CloudDrive : public CloudStorage 17 | { 18 | public: 19 | bool uploadContents(const string& content) override 20 | { 21 | cout << "Uploading " << content.length() << " bytes to CloudDrive: " << endl; 22 | 23 | return true; 24 | } 25 | 26 | int getFreeSpace() override 27 | { 28 | // Implement the logic for getting the free space on CloudDrive here. 29 | const int size = arc4random_uniform(20); 30 | cout << "Available CloudDrive storage: " << size << "GB" << endl; 31 | return size; 32 | } 33 | }; 34 | 35 | class FastShare : public CloudStorage 36 | { 37 | public: 38 | bool uploadContents(const string& content) override 39 | { 40 | cout << "Uploading " << content.length() << " bytes to FastShare: " << endl; 41 | return true; 42 | } 43 | 44 | int getFreeSpace() override 45 | { 46 | const int size = arc4random_uniform(10); 47 | cout << "Available FastShare storage: " << size << "GB" << endl; 48 | return size; 49 | } 50 | }; 51 | 52 | // 3rd party service 53 | class VirtualDrive 54 | { 55 | public: 56 | bool uploadData(const string& data, const int uniqueID) 57 | { 58 | cout << "Uploading to VirtualDrive: \"" << data << "\" ID: " << uniqueID << endl; 59 | return true; 60 | } 61 | int usedSpace() 62 | { 63 | return arc4random_uniform(10); 64 | } 65 | const int totalSpace = 15; 66 | }; 67 | 68 | int main() 69 | { 70 | // Create an array of pointers to CloudStorage objects. 71 | const std::unique_ptr cloudServices[] 72 | { 73 | std::make_unique(), 74 | std::make_unique(), 75 | }; 76 | 77 | // Iterate through the array and invoke the uploadContents and getFreeSpace 78 | // methods on each object. 79 | const string content = "Beam me up, Scotty!"; 80 | for (const auto &service : cloudServices) 81 | { 82 | service->uploadContents(content); 83 | service->getFreeSpace(); 84 | cout << endl; 85 | } 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Ch01-Adapter/01_06/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch01-Adapter/01_06/begin/cloud-storage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CloudStorage 9 | { 10 | public: 11 | virtual bool uploadContents(const string &content) = 0; 12 | virtual int getFreeSpace() = 0; 13 | virtual ~CloudStorage() = default; 14 | }; 15 | 16 | class CloudDrive : public CloudStorage 17 | { 18 | public: 19 | bool uploadContents(const string& content) override 20 | { 21 | cout << "Uploading " << content.length() << " bytes to CloudDrive: " << endl; 22 | 23 | return true; 24 | } 25 | 26 | int getFreeSpace() override 27 | { 28 | // Implement the logic for getting the free space on CloudDrive here. 29 | const int size = arc4random_uniform(20); 30 | cout << "Available CloudDrive storage: " << size << "GB" << endl; 31 | return size; 32 | } 33 | }; 34 | 35 | class FastShare : public CloudStorage 36 | { 37 | public: 38 | bool uploadContents(const string& content) override 39 | { 40 | cout << "Uploading " << content.length() << " bytes to FastShare: " << endl; 41 | return true; 42 | } 43 | 44 | int getFreeSpace() override 45 | { 46 | const int size = arc4random_uniform(10); 47 | cout << "Available FastShare storage: " << size << "GB" << endl; 48 | return size; 49 | } 50 | }; 51 | 52 | // 3rd party service 53 | class VirtualDrive 54 | { 55 | public: 56 | bool uploadData(const string& data, const int uniqueID) 57 | { 58 | cout << "Uploading to VirtualDrive: \"" << data << "\" ID: " << uniqueID << endl; 59 | return true; 60 | } 61 | int usedSpace() 62 | { 63 | return arc4random_uniform(10); 64 | } 65 | const int totalSpace = 15; 66 | }; 67 | 68 | int main() 69 | { 70 | // Create an array of pointers to CloudStorage objects. 71 | const std::unique_ptr cloudServices[] 72 | { 73 | std::make_unique(), 74 | std::make_unique(), 75 | }; 76 | 77 | // Iterate through the array and invoke the uploadContents and getFreeSpace 78 | // methods on each object. 79 | const string content = "Beam me up, Scotty!"; 80 | for (const auto &service : cloudServices) 81 | { 82 | service->uploadContents(content); 83 | service->getFreeSpace(); 84 | cout << endl; 85 | } 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Ch01-Adapter/01_06/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | }, 28 | { 29 | "type": "cppbuild", 30 | "label": "C/C++: clang build active file", 31 | "command": "/usr/bin/clang", 32 | "args": [ 33 | "-fcolor-diagnostics", 34 | "-fansi-escape-codes", 35 | "-g", 36 | "${file}", 37 | "-o", 38 | "${fileDirname}/${fileBasenameNoExtension}" 39 | ], 40 | "options": { 41 | "cwd": "${fileDirname}" 42 | }, 43 | "problemMatcher": [ 44 | "$gcc" 45 | ], 46 | "group": "build", 47 | "detail": "Task generated by Debugger." 48 | } 49 | ], 50 | "version": "2.0.0" 51 | } -------------------------------------------------------------------------------- /Ch01-Adapter/01_06/end/cloud-storage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class CloudStorage 9 | { 10 | public: 11 | virtual bool uploadContents(const string &content) = 0; 12 | virtual int getFreeSpace() = 0; 13 | virtual ~CloudStorage() = default; 14 | }; 15 | 16 | class CloudDrive : public CloudStorage 17 | { 18 | public: 19 | virtual bool uploadContents(const string& content) override 20 | { 21 | cout << "Uploading " << content.length() << " bytes to CloudDrive: " << endl; 22 | 23 | return true; 24 | } 25 | 26 | virtual int getFreeSpace() override 27 | { 28 | // Implement the logic for getting the free space on CloudDrive here. 29 | const int size = arc4random_uniform(20); 30 | cout << "Available CloudDrive storage: " << size << "GB" << endl; 31 | return size; 32 | } 33 | }; 34 | 35 | class FastShare : public CloudStorage 36 | { 37 | public: 38 | virtual bool uploadContents(const string& content) override 39 | { 40 | cout << "Uploading " << content.length() << " bytes to FastShare: " << endl; 41 | return true; 42 | } 43 | 44 | virtual int getFreeSpace() override 45 | { 46 | const int size = arc4random_uniform(10); 47 | cout << "Available FastShare storage: " << size << "GB" << endl; 48 | return size; 49 | } 50 | }; 51 | 52 | // 3rd party service 53 | class VirtualDrive 54 | { 55 | public: 56 | bool uploadData(const string& data, const int uniqueID) 57 | { 58 | cout << "Uploading to VirtualDrive: \"" << data << "\" ID: " << uniqueID << endl; 59 | return true; 60 | } 61 | int usedSpace() 62 | { 63 | return arc4random_uniform(10); 64 | } 65 | static const int totalSpace = 15; 66 | }; 67 | 68 | class VirtualDriveAdapter : public CloudStorage, private VirtualDrive 69 | { 70 | public: 71 | virtual bool uploadContents(const string& content) override 72 | { 73 | // Generate a unique ID for this content 74 | int uniqueID = generateUID(); 75 | // Delegate the request to the Adaptee 76 | cout << "VirtualDriveAdapter::uploadContents() -> Calling VirtualDrive::uploadData()" << endl; 77 | return uploadData(content, uniqueID); 78 | } 79 | 80 | virtual int getFreeSpace() override 81 | { 82 | // Delegate the request to the Adaptee 83 | cout << "VirtualDriveAdapter::getFreeSpace() -> Calling VirtualDrive::getAvailableStorage()" << endl; 84 | const int available = totalSpace - usedSpace(); 85 | cout << "Available VirtualDrive storage: " << available << " GB" << endl; 86 | return available; 87 | } 88 | 89 | private: 90 | // generates an ID from seconds passed since Epoch 91 | int generateUID() 92 | { 93 | // seconds since the Epoch 94 | const time_t result = time(nullptr); 95 | return result; 96 | } 97 | }; 98 | 99 | int main() 100 | { 101 | // Create an array of pointers to CloudStorage objects. 102 | const std::unique_ptr cloudServices[] 103 | { 104 | make_unique(), 105 | make_unique(), 106 | make_unique() 107 | }; 108 | 109 | // Iterate through the array and invoke the uploadContents and getFreeSpace 110 | // methods on each object. 111 | const string content = "Beam me up, Scotty!"; 112 | for (const auto& service : cloudServices) 113 | { 114 | service->uploadContents(content); 115 | service->getFreeSpace(); 116 | cout << endl; 117 | } 118 | 119 | return 0; 120 | } 121 | -------------------------------------------------------------------------------- /Ch02-Bridge/02_02/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch02-Bridge/02_02/end/inheritance-explosion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ITextShare { 5 | public: 6 | 7 | virtual bool shareText(const string& text) = 0; 8 | virtual ~ITextShare() = default; 9 | }; 10 | 11 | class EmailShare : public ITextShare { 12 | public: 13 | bool shareText(const string& text) override { 14 | cout << "EmailShare::shareText() sharing text: " << text << endl; 15 | return true; 16 | } 17 | }; 18 | 19 | class SMSShare : public ITextShare { 20 | public: 21 | bool shareText(const string& text) override { 22 | cout << "SMSShare::shareText() sharing text: " << text << endl; 23 | return true; 24 | } 25 | }; 26 | 27 | class EmailShareEncrypted : public EmailShare 28 | { 29 | public: 30 | bool shareText(const string &text) override 31 | { 32 | cout << "EmailShareEncrypted::shareText() encrypting text..." << endl; 33 | string encrypted = xorEncrypt(text); 34 | return EmailShare::shareText(encrypted); 35 | } 36 | 37 | private: 38 | string xorEncrypt(const string &input) 39 | { 40 | char key = 64; 41 | string output = input; 42 | 43 | for (int i = 0; i < input.size(); ++i) 44 | output[i] = input[i] ^ key; 45 | 46 | return output; 47 | } 48 | }; 49 | 50 | class SMSShareEncrypted : public SMSShare 51 | { 52 | public: 53 | bool shareText(const string &text) override 54 | { 55 | cout << "SMSShareEncrypted::shareText() encrypting text..." << endl; 56 | string encrypted = xorEncrypt(text); 57 | return SMSShare::shareText(encrypted); 58 | } 59 | 60 | private: 61 | string xorEncrypt(const string input) 62 | { 63 | char key = 64; 64 | string output = input; 65 | 66 | for (int i = 0; i < input.size(); ++i) 67 | output[i] = input[i] ^ key; 68 | 69 | return output; 70 | } 71 | }; 72 | 73 | 74 | class EmailShareAutoExpiring: public EmailShare 75 | { 76 | //... 77 | }; 78 | 79 | class SMSShareAutoExpiring: public SMSShare 80 | { 81 | //... 82 | }; 83 | 84 | 85 | 86 | int main() 87 | { 88 | // Create an array of pointers to CloudStorage objects. 89 | const std::unique_ptr sharingServices[] 90 | { 91 | make_unique(), 92 | make_unique(), 93 | make_unique(), 94 | make_unique() 95 | }; 96 | 97 | // Iterate through the array and invoke the uploadContents and getFreeSpace 98 | // methods on each object 99 | const string content = "Beam me up, Scotty!"; 100 | for (const auto& service : sharingServices) 101 | { 102 | service->shareText(content); 103 | cout << endl; 104 | } 105 | 106 | return 0; 107 | } -------------------------------------------------------------------------------- /Ch02-Bridge/02_03/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch02-Bridge/02_03/begin/inheritance-explosion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ITextShare { 5 | public: 6 | 7 | virtual bool shareText(const string& text) = 0; 8 | virtual ~ITextShare() = default; 9 | }; 10 | 11 | class EmailShare : public ITextShare { 12 | public: 13 | bool shareText(const string& text) override { 14 | cout << "EmailShare::shareText() sharing text: " << text << endl; 15 | return true; 16 | } 17 | }; 18 | 19 | class SMSShare : public ITextShare { 20 | public: 21 | bool shareText(const string& text) override { 22 | cout << "SMSShare::shareText() sharing text: " << text << endl; 23 | return true; 24 | } 25 | }; 26 | 27 | class EmailShareEncrypted : public EmailShare 28 | { 29 | public: 30 | bool shareText(const string &text) override 31 | { 32 | cout << "EmailShareEncrypted::shareText() encrypting text..." << endl; 33 | string encrypted = xorEncrypt(text); 34 | return EmailShare::shareText(encrypted); 35 | } 36 | 37 | private: 38 | string xorEncrypt(const string &input) 39 | { 40 | char key = 64; 41 | string output = input; 42 | 43 | for (int i = 0; i < input.size(); ++i) 44 | output[i] = input[i] ^ key; 45 | 46 | return output; 47 | } 48 | }; 49 | 50 | class SMSShareEncrypted : public SMSShare 51 | { 52 | public: 53 | bool shareText(const string &text) override 54 | { 55 | cout << "SMSShareEncrypted::shareText() encrypting text..." << endl; 56 | string encrypted = xorEncrypt(text); 57 | return SMSShare::shareText(encrypted); 58 | } 59 | 60 | private: 61 | string xorEncrypt(const string input) 62 | { 63 | char key = 64; 64 | string output = input; 65 | 66 | for (int i = 0; i < input.size(); ++i) 67 | output[i] = input[i] ^ key; 68 | 69 | return output; 70 | } 71 | }; 72 | 73 | 74 | class EmailShareAutoExpiring: public EmailShare 75 | { 76 | //... 77 | }; 78 | 79 | class SMSShareAutoExpiring: public SMSShare 80 | { 81 | //... 82 | }; 83 | 84 | 85 | 86 | int main() 87 | { 88 | // Create an array of pointers to CloudStorage objects. 89 | const std::unique_ptr sharingServices[] 90 | { 91 | make_unique(), 92 | make_unique(), 93 | make_unique(), 94 | make_unique() 95 | }; 96 | 97 | // Iterate through the array and invoke the uploadContents and getFreeSpace 98 | // methods on each object 99 | const string content = "Beam me up, Scotty!"; 100 | for (const auto& service : sharingServices) 101 | { 102 | service->shareText(content); 103 | cout << endl; 104 | } 105 | 106 | return 0; 107 | } -------------------------------------------------------------------------------- /Ch02-Bridge/02_03/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch02-Bridge/02_03/end/bridge-refactor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class ITextHandler 5 | { 6 | public: 7 | virtual string prepareMessage(const string &text) const = 0; 8 | virtual ~ITextHandler() = default; 9 | }; 10 | 11 | class ITextSharer 12 | { 13 | public: 14 | explicit ITextSharer(const ITextHandler &handler) : m_textHandler(handler) {} 15 | 16 | bool shareText(const string &text) 17 | { 18 | const string preparedText = m_textHandler.prepareMessage(text); 19 | return sharePreparedText(preparedText); 20 | } 21 | 22 | virtual ~ITextSharer() = default; 23 | 24 | protected: 25 | virtual bool sharePreparedText(const string &text) = 0; 26 | 27 | private: 28 | const ITextHandler &m_textHandler; 29 | }; 30 | 31 | class PlainTextHandler : public ITextHandler 32 | { 33 | public: 34 | string prepareMessage(const string &text) const override 35 | { 36 | cout << "PlainTextHandler::prepareMessage() returning original text..." << endl; 37 | return text; 38 | } 39 | }; 40 | 41 | class EmailShare : public ITextSharer 42 | { 43 | public: 44 | explicit EmailShare(const PlainTextHandler &handler) : ITextSharer(handler) {} 45 | 46 | bool sharePreparedText(const string &text) override 47 | { 48 | cout << "EmailShare::shareText() sharing text: " << text << endl; 49 | return true; 50 | } 51 | }; 52 | 53 | class EncryptedTextHandler : public ITextHandler 54 | { 55 | public: 56 | string prepareMessage(const string &text) const override 57 | { 58 | cout << "EncryptedTextHandler::prepareMessage() encrypting text..." << endl; 59 | string encrypted = xorEncrypt(text); 60 | return encrypted; 61 | } 62 | 63 | private: 64 | string xorEncrypt(const string &input) const 65 | { 66 | char key = 64; 67 | string output = input; 68 | 69 | for (int i = 0; i < input.size(); ++i) 70 | output[i] = input[i] ^ key; 71 | 72 | return output; 73 | } 74 | }; 75 | 76 | class EmailShareEncrypted : public ITextSharer 77 | { 78 | public: 79 | explicit EmailShareEncrypted(const EncryptedTextHandler &handler) : ITextSharer(handler) {} 80 | 81 | protected: 82 | bool sharePreparedText(const string &text) override 83 | { 84 | cout << "EmailShareEncrypted::shareText() sharing text: " << text << endl; 85 | return true; 86 | } 87 | }; 88 | 89 | int main() 90 | { 91 | PlainTextHandler handler = PlainTextHandler(); 92 | EncryptedTextHandler encryptHandler = EncryptedTextHandler(); 93 | 94 | // Create an array of pointers to CloudStorage objects. 95 | const std::unique_ptr sharingServices[]{ 96 | make_unique(handler), 97 | make_unique(encryptHandler), 98 | }; 99 | 100 | // Iterate through the array and invoke the uploadContents and getFreeSpace 101 | // methods on each object 102 | const string content = "Beam me up, Scotty!"; 103 | for (const auto &service : sharingServices) 104 | { 105 | service->shareText(content); 106 | cout << endl; 107 | } 108 | 109 | return 0; 110 | } -------------------------------------------------------------------------------- /Ch02-Bridge/02_05/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch02-Bridge/02_05/end/vehicles-engines.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | 5 | class IEngine 6 | { 7 | public: 8 | virtual void start() const = 0; 9 | virtual ~IEngine() = default; 10 | }; 11 | 12 | 13 | class IVehicle 14 | { 15 | public: 16 | explicit IVehicle(const IEngine &engine) : m_engine(engine) {} 17 | 18 | void drive() const 19 | { 20 | m_engine.start(); 21 | driveVehicle(); 22 | } 23 | 24 | virtual ~IVehicle() = default; 25 | 26 | protected: 27 | virtual void driveVehicle() const = 0; 28 | 29 | private: 30 | const IEngine &m_engine; 31 | }; 32 | 33 | // Concrete implementation of Gas engine 34 | class GasEngine : public IEngine 35 | { 36 | public: 37 | void start() const override 38 | { 39 | cout << "Starting gas engine." << endl; 40 | } 41 | }; 42 | 43 | // Concrete implementation of Electric engine 44 | class ElectricEngine : public IEngine 45 | { 46 | public: 47 | void start() const override 48 | { 49 | cout << "Starting electric engine." << endl; 50 | } 51 | }; 52 | 53 | // Concrete implementation of Hybrid engine 54 | class HybridEngine : public IEngine 55 | { 56 | public: 57 | void start() const override 58 | { 59 | cout << "Starting hybrid engine." << endl; 60 | } 61 | }; 62 | 63 | // Concrete implementation of Car 64 | class Car : public IVehicle 65 | { 66 | public: 67 | Car(const IEngine &engine) : IVehicle(engine) {} 68 | 69 | void driveVehicle() const override 70 | { 71 | cout << "Driving a car." << endl; 72 | } 73 | }; 74 | 75 | // Concrete implementation of Truck 76 | class Truck : public IVehicle 77 | { 78 | public: 79 | Truck(const IEngine &engine) : IVehicle(engine) {} 80 | 81 | void driveVehicle() const override 82 | { 83 | cout << "Driving a truck." << endl; 84 | } 85 | }; 86 | 87 | // Concrete implementation of Bike 88 | class Bike : public IVehicle 89 | { 90 | public: 91 | Bike(const IEngine &engine) : IVehicle(engine) {} 92 | 93 | void driveVehicle() const override 94 | { 95 | cout << "Riding a bike." << endl; 96 | } 97 | }; 98 | 99 | int main() 100 | { 101 | auto gasEngine = GasEngine(); 102 | auto electricEngine = ElectricEngine(); 103 | auto hybridEngine = HybridEngine(); 104 | 105 | // Create an array of pointers to Vehicle objects. 106 | const std::unique_ptr vehicles[] { 107 | make_unique(gasEngine), 108 | make_unique(electricEngine), 109 | make_unique(hybridEngine)}; 110 | 111 | for (const auto &vehicle : vehicles) 112 | { 113 | vehicle->drive(); 114 | cout << endl; 115 | } 116 | 117 | return 0; 118 | } 119 | -------------------------------------------------------------------------------- /Ch03-Composite/03_02/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_02/end/boxes-products.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Concrete product classes 8 | class Book 9 | { 10 | public: 11 | Book(const string &title, double price) : m_Title(title), m_Price(price) {} 12 | 13 | double price() const 14 | { 15 | cout << "Getting \"" << m_Title << "\" book price" << endl; 16 | return m_Price; 17 | } 18 | 19 | private: 20 | std::string m_Title; 21 | double m_Price; 22 | }; 23 | 24 | class Toy 25 | { 26 | public: 27 | Toy(const string &name, double price) : m_Name(name), m_PriceTag(price) {} 28 | 29 | double getPrice() const 30 | { 31 | cout << "Getting \"" << m_Name << "\" toy price" << endl; 32 | return m_PriceTag; 33 | } 34 | 35 | private: 36 | std::string m_Name; 37 | double m_PriceTag; 38 | }; 39 | 40 | // Box class that holds products and other boxes 41 | class Box 42 | { 43 | private: 44 | string m_Name; 45 | std::vector m_Books; 46 | std::vector m_Toys; 47 | std::vector m_Boxes; 48 | 49 | public: 50 | explicit Box(const string &name) : m_Name(name) {} 51 | 52 | void addBook(Book &book) 53 | { 54 | m_Books.push_back(&book); 55 | } 56 | 57 | void addToy(Toy &toy) 58 | { 59 | m_Toys.push_back(&toy); 60 | } 61 | 62 | void addBox(Box &box) 63 | { 64 | m_Boxes.push_back(&box); 65 | } 66 | 67 | double totalPrice() const 68 | { 69 | cout << "Opening " << m_Name << endl; 70 | double totalPrice = 0; 71 | 72 | for (const auto &book : m_Books) 73 | { 74 | totalPrice += book->price(); 75 | } 76 | 77 | for (const auto &toy : m_Toys) 78 | { 79 | totalPrice += toy->getPrice(); 80 | } 81 | 82 | for (const auto &box : m_Boxes) 83 | { 84 | totalPrice += box->totalPrice(); 85 | } 86 | 87 | return totalPrice; 88 | } 89 | }; 90 | 91 | int main() 92 | { 93 | // Create some products 94 | Book book1{"Robinson Crusoe", 4.99}; 95 | Toy toy1{"Star Trooper", 39.99}; 96 | Toy toy2{"Barbie Dreamhouse", 59.99}; 97 | 98 | // Create some boxes and add products and other boxes to them 99 | Box smallBox("Small Box"); 100 | 101 | smallBox.addBook(book1); 102 | smallBox.addToy(toy1); 103 | 104 | Box bigBox("Big Box"); 105 | 106 | bigBox.addToy(toy2); 107 | bigBox.addBox(smallBox); 108 | 109 | // Calculate the total price of the top-level box 110 | cout << "Calculating total price. " << endl 111 | << bigBox.totalPrice() << endl; 112 | 113 | return 0; 114 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_03/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_03/begin/boxes-products.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Concrete product classes 8 | class Book 9 | { 10 | public: 11 | Book(const string &title, double price) : m_Title(title), m_Price(price) {} 12 | 13 | double price() const 14 | { 15 | cout << "Getting \"" << m_Title << "\" book price" << endl; 16 | return m_Price; 17 | } 18 | 19 | private: 20 | std::string m_Title; 21 | double m_Price; 22 | }; 23 | 24 | class Toy 25 | { 26 | public: 27 | Toy(const string &name, double price) : m_Name(name), m_PriceTag(price) {} 28 | 29 | double getPrice() const 30 | { 31 | cout << "Getting \"" << m_Name << "\" toy price" << endl; 32 | return m_PriceTag; 33 | } 34 | 35 | private: 36 | std::string m_Name; 37 | double m_PriceTag; 38 | }; 39 | 40 | // Box class that holds products and other boxes 41 | class Box 42 | { 43 | private: 44 | string m_Name; 45 | std::vector m_Books; 46 | std::vector m_Toys; 47 | std::vector m_Boxes; 48 | 49 | public: 50 | explicit Box(const string &name) : m_Name(name) {} 51 | 52 | void addBook(Book &book) 53 | { 54 | m_Books.push_back(&book); 55 | } 56 | 57 | void addToy(Toy &toy) 58 | { 59 | m_Toys.push_back(&toy); 60 | } 61 | 62 | void addBox(Box &box) 63 | { 64 | m_Boxes.push_back(&box); 65 | } 66 | 67 | double totalPrice() const 68 | { 69 | cout << "Opening " << m_Name << endl; 70 | double totalPrice = 0; 71 | 72 | for (const auto &book : m_Books) 73 | { 74 | totalPrice += book->price(); 75 | } 76 | 77 | for (const auto &toy : m_Toys) 78 | { 79 | totalPrice += toy->getPrice(); 80 | } 81 | 82 | for (const auto &box : m_Boxes) 83 | { 84 | totalPrice += box->totalPrice(); 85 | } 86 | 87 | return totalPrice; 88 | } 89 | }; 90 | 91 | int main() 92 | { 93 | // Create some products 94 | Book book1{"Robinson Crusoe", 4.99}; 95 | Toy toy1{"Star Trooper", 39.99}; 96 | Toy toy2{"Barbie Dreamhouse", 59.99}; 97 | 98 | // Create some boxes and add products and other boxes to them 99 | Box smallBox("Small Box"); 100 | 101 | smallBox.addBook(book1); 102 | smallBox.addToy(toy1); 103 | 104 | Box bigBox("Big Box"); 105 | 106 | bigBox.addToy(toy2); 107 | bigBox.addBox(smallBox); 108 | 109 | // Calculate the total price of the top-level box 110 | cout << "Calculating total price. " << endl 111 | << bigBox.totalPrice() << endl; 112 | 113 | return 0; 114 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_03/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_03/end/boxes-products.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Abstract base class for Box and concrete product types 8 | class Product 9 | { 10 | public: 11 | virtual double price() const = 0; 12 | virtual ~Product() = default; 13 | }; 14 | 15 | // Concrete product classes 16 | class Book : public Product 17 | { 18 | public: 19 | Book(const string &title, double price) : m_Title(title), m_Price(price) {} 20 | double price() const override 21 | { 22 | cout << "Getting \"" << m_Title << "\" book price" << endl; 23 | return m_Price; 24 | } 25 | 26 | private: 27 | std::string m_Title; 28 | double m_Price; 29 | }; 30 | 31 | class Toy : public Product 32 | { 33 | public: 34 | Toy(const string &name, double price) : m_Name(name), m_PriceTag(price) {} 35 | 36 | double price() const override 37 | { 38 | cout << "Getting \"" << m_Name << "\" toy price" << endl; 39 | return m_PriceTag; 40 | } 41 | 42 | private: 43 | std::string m_Name; 44 | double m_PriceTag; 45 | }; 46 | 47 | // Box class that holds products and other boxes 48 | class Box : public Product 49 | { 50 | public: 51 | explicit Box(const string &name) : m_Name(name) {} 52 | 53 | void addProduct(Product &product) 54 | { 55 | m_Products.push_back(&product); 56 | } 57 | 58 | double price() const override 59 | { 60 | cout << "Opening " << m_Name << endl; 61 | double totalPrice = 0; 62 | 63 | for (const auto &product : m_Products) 64 | { 65 | totalPrice += product->price(); 66 | } 67 | 68 | return totalPrice; 69 | } 70 | 71 | private: 72 | string m_Name; 73 | std::vector m_Products; 74 | }; 75 | 76 | int main() 77 | { 78 | // Create some products 79 | Book book1{"Robinson Crusoe", 4.99}; 80 | Toy toy1{"Star Trooper", 39.99}; 81 | Toy toy2{"Barbie Dreamhouse", 59.99}; 82 | 83 | // Create some boxes and add products and other boxes to them 84 | Box smallBox("Small Box"); 85 | 86 | smallBox.addProduct(book1); 87 | smallBox.addProduct(toy1); 88 | 89 | Box bigBox("Big Box"); 90 | 91 | bigBox.addProduct(toy2); 92 | bigBox.addProduct(smallBox); 93 | 94 | // Calculate the total price of the top-level box 95 | cout << "Calculating total price. " << endl 96 | << bigBox.price() << endl; 97 | 98 | return 0; 99 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_04/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_04/end/drawing-shapes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Abstract base class for concrete Shape classes 8 | class Shape 9 | { 10 | public: 11 | virtual void draw() const = 0; 12 | virtual ~Shape() = default; 13 | }; 14 | 15 | // Concrete Shape classes 16 | class Circle : public Shape 17 | { 18 | public: 19 | explicit Circle(double radius) : m_Radius(radius) {} 20 | 21 | void draw() const override 22 | { 23 | cout << "Drawing a circle with radius " << m_Radius << endl; 24 | } 25 | 26 | private: 27 | double m_Radius; 28 | }; 29 | 30 | class Rectangle : public Shape 31 | { 32 | public: 33 | Rectangle(double width, double height) : m_Width(width), m_Height(height) {} 34 | 35 | void draw() const override 36 | { 37 | cout << "Drawing a rectangle with width " << m_Width << " and height " << m_Height << endl; 38 | } 39 | 40 | private: 41 | double m_Width; 42 | double m_Height; 43 | }; 44 | 45 | class Triangle : public Shape 46 | { 47 | public: 48 | Triangle(double side1, double side2, double side3) : m_Side1(side1), m_Side2(side2), m_Side3(side3) {} 49 | 50 | void draw() const override 51 | { 52 | cout << "Drawing a triangle with sides " << m_Side1 << ", " << m_Side2 << ", and " << m_Side3 << endl; 53 | } 54 | 55 | private: 56 | double m_Side1; 57 | double m_Side2; 58 | double m_Side3; 59 | }; 60 | 61 | int main() 62 | { 63 | vector shapes{ 64 | new Circle(5), 65 | new Rectangle(10, 20), 66 | new Triangle(3, 4, 5)}; 67 | 68 | for (const auto &shape : shapes) 69 | { 70 | shape->draw(); 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Ch03-Composite/03_05/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_05/begin/drawing-shapes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Abstract base class for concrete Shape classes 8 | class Shape 9 | { 10 | public: 11 | virtual void draw() const = 0; 12 | virtual ~Shape() = default; 13 | }; 14 | 15 | // Concrete Shape classes 16 | class Circle : public Shape 17 | { 18 | public: 19 | explicit Circle(double radius) : m_Radius(radius) {} 20 | 21 | void draw() const override 22 | { 23 | cout << "Drawing a circle with radius " << m_Radius << endl; 24 | } 25 | 26 | private: 27 | double m_Radius; 28 | }; 29 | 30 | class Rectangle : public Shape 31 | { 32 | public: 33 | Rectangle(double width, double height) : m_Width(width), m_Height(height) {} 34 | 35 | void draw() const override 36 | { 37 | cout << "Drawing a rectangle with width " << m_Width << " and height " << m_Height << endl; 38 | } 39 | 40 | private: 41 | double m_Width; 42 | double m_Height; 43 | }; 44 | 45 | class Triangle : public Shape 46 | { 47 | public: 48 | Triangle(double side1, double side2, double side3) : m_Side1(side1), m_Side2(side2), m_Side3(side3) {} 49 | 50 | void draw() const override 51 | { 52 | cout << "Drawing a triangle with sides " << m_Side1 << ", " << m_Side2 << ", and " << m_Side3 << endl; 53 | } 54 | 55 | private: 56 | double m_Side1; 57 | double m_Side2; 58 | double m_Side3; 59 | }; 60 | 61 | int main() 62 | { 63 | vector shapes{ 64 | new Circle(5), 65 | new Rectangle(10, 20), 66 | new Triangle(3, 4, 5)}; 67 | 68 | for (const auto &shape : shapes) 69 | { 70 | shape->draw(); 71 | } 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /Ch03-Composite/03_05/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch03-Composite/03_05/end/drawing-shapes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Abstract base class for concrete Shape classes 8 | class Shape 9 | { 10 | public: 11 | virtual void draw() const = 0; 12 | virtual ~Shape() = default; 13 | }; 14 | 15 | // Concrete Shape classes 16 | class Circle : public Shape 17 | { 18 | public: 19 | Circle(double radius) : m_Radius(radius) {} 20 | 21 | void draw() const override 22 | { 23 | cout << "Drawing a circle with radius " << m_Radius << endl; 24 | } 25 | 26 | private: 27 | double m_Radius; 28 | }; 29 | 30 | class Rectangle : public Shape 31 | { 32 | public: 33 | Rectangle(double width, double height) : m_Width(width), m_Height(height) {} 34 | 35 | void draw() const override 36 | { 37 | cout << "Drawing a rectangle with width " << m_Width << " and height " << m_Height << endl; 38 | } 39 | 40 | private: 41 | double m_Width; 42 | double m_Height; 43 | }; 44 | 45 | class Triangle : public Shape 46 | { 47 | public: 48 | Triangle(double side1, double side2, double side3) : m_Side1(side1), m_Side2(side2), m_Side3(side3) {} 49 | 50 | void draw() const override 51 | { 52 | cout << "Drawing a triangle with sides " << m_Side1 << ", " << m_Side2 << ", and " << m_Side3 << endl; 53 | } 54 | 55 | private: 56 | double m_Side1; 57 | double m_Side2; 58 | double m_Side3; 59 | }; 60 | 61 | class CompositeShape : public Shape 62 | { 63 | public: 64 | void addShape(Shape &shape) 65 | { 66 | m_Shapes.push_back(&shape); 67 | } 68 | 69 | void removeShape(Shape &shape) 70 | { 71 | for (auto it = m_Shapes.begin(); it != m_Shapes.end(); ++it) 72 | { 73 | if (*it == &shape) 74 | { 75 | m_Shapes.erase(it); 76 | break; 77 | } 78 | } 79 | } 80 | 81 | void draw() const override 82 | { 83 | cout << "Drawing a composite shape:" << endl; 84 | for (const auto &shape : m_Shapes) 85 | { 86 | shape->draw(); 87 | } 88 | } 89 | 90 | private: 91 | vector m_Shapes; 92 | }; 93 | 94 | int main() 95 | { 96 | Circle c(5); 97 | Rectangle r(10, 20); 98 | Triangle t(3, 4, 5); 99 | 100 | // Create a composite shape 101 | CompositeShape cs; 102 | cs.addShape(c); 103 | cs.addShape(r); 104 | cs.addShape(t); 105 | 106 | // Draw the composite shape 107 | cs.draw(); 108 | 109 | // Remove a shape from the composite shape 110 | cs.removeShape(r); 111 | 112 | // Draw the composite shape again 113 | cs.draw(); 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /Ch04-Decorator/04_02/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch04-Decorator/04_02/end/computershop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Computer 7 | { 8 | public: 9 | virtual string description() const = 0; 10 | virtual double price() const = 0; 11 | virtual ~Computer() = default; 12 | }; 13 | 14 | class Desktop : public Computer 15 | { 16 | public: 17 | string description() const override 18 | { 19 | return "Desktop"; 20 | } 21 | 22 | double price() const override 23 | { 24 | return 1000.0; 25 | } 26 | }; 27 | 28 | class Laptop : public Computer 29 | { 30 | public: 31 | string description() const override 32 | { 33 | return "Laptop"; 34 | } 35 | 36 | double price() const override 37 | { 38 | return 1500.0; 39 | } 40 | }; 41 | 42 | // Upgrades 43 | class DesktopWithMemoryUpgrade : public Desktop 44 | { 45 | public: 46 | string description() const override 47 | { 48 | return "Desktop with memory upgrade"; 49 | } 50 | 51 | double price() const override 52 | { 53 | return 1700.0; 54 | } 55 | }; 56 | 57 | class LaptopWithMemoryUpgrade : public Laptop 58 | { 59 | public: 60 | string description() const override 61 | { 62 | return "Laptop with memory upgrade"; 63 | } 64 | 65 | double price() const override 66 | { 67 | return 2000.0; 68 | } 69 | }; 70 | 71 | class DesktopWithGraphicsUpgrade : public Desktop 72 | { 73 | public: 74 | string description() const override 75 | { 76 | return "Desktop with graphics upgrade"; 77 | } 78 | 79 | double price() const override 80 | { 81 | return 2000.0; 82 | } 83 | }; 84 | 85 | class LaptopWithGraphicsUpgrade : public Laptop 86 | { 87 | public: 88 | string description() const override 89 | { 90 | return "Laptop with graphics upgrade"; 91 | } 92 | 93 | double price() const override 94 | { 95 | return 2700.0; 96 | } 97 | }; 98 | 99 | int main() 100 | { 101 | Computer *desktop = new Desktop(); 102 | cout << desktop->description() << " costs $" << desktop->price() << endl; 103 | 104 | Computer *laptop = new Laptop(); 105 | cout << laptop->description() << " costs $" << laptop->price() << endl; 106 | 107 | Computer *desktopGraphicsUpgrade = new DesktopWithGraphicsUpgrade(); 108 | cout << desktopGraphicsUpgrade->description() << " costs $" << desktopGraphicsUpgrade->price() << endl; 109 | 110 | Computer *desktopMemoryUpgrade = new DesktopWithMemoryUpgrade(); 111 | cout << desktopMemoryUpgrade->description() << " costs $" << desktopMemoryUpgrade->price() << endl; 112 | 113 | Computer *laptopGraphicsUpgrade = new LaptopWithGraphicsUpgrade(); 114 | cout << laptopGraphicsUpgrade->description() << " costs $" << laptopGraphicsUpgrade->price() << endl; 115 | 116 | Computer *laptopMemoryUpgrade = new LaptopWithMemoryUpgrade(); 117 | cout << laptopMemoryUpgrade->description() << " costs $" << laptopMemoryUpgrade->price() << endl; 118 | 119 | delete desktop; 120 | delete laptop; 121 | delete desktopGraphicsUpgrade; 122 | delete desktopMemoryUpgrade; 123 | delete laptopGraphicsUpgrade; 124 | delete laptopMemoryUpgrade; 125 | 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /Ch04-Decorator/04_03/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch04-Decorator/04_03/begin/computershop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Computer 7 | { 8 | public: 9 | virtual string description() const = 0; 10 | virtual double price() const = 0; 11 | virtual ~Computer() = default; 12 | }; 13 | 14 | class Desktop : public Computer 15 | { 16 | public: 17 | string description() const override 18 | { 19 | return "Desktop"; 20 | } 21 | 22 | double price() const override 23 | { 24 | return 1000.0; 25 | } 26 | }; 27 | 28 | class Laptop : public Computer 29 | { 30 | public: 31 | string description() const override 32 | { 33 | return "Laptop"; 34 | } 35 | 36 | double price() const override 37 | { 38 | return 1500.0; 39 | } 40 | }; 41 | 42 | // Upgrades 43 | class DesktopWithMemoryUpgrade : public Desktop 44 | { 45 | public: 46 | string description() const override 47 | { 48 | return "Desktop with memory upgrade"; 49 | } 50 | 51 | double price() const override 52 | { 53 | return 1700.0; 54 | } 55 | }; 56 | 57 | class LaptopWithMemoryUpgrade : public Laptop 58 | { 59 | public: 60 | string description() const override 61 | { 62 | return "Laptop with memory upgrade"; 63 | } 64 | 65 | double price() const override 66 | { 67 | return 2000.0; 68 | } 69 | }; 70 | 71 | class DesktopWithGraphicsUpgrade : public Desktop 72 | { 73 | public: 74 | string description() const override 75 | { 76 | return "Desktop with graphics upgrade"; 77 | } 78 | 79 | double price() const override 80 | { 81 | return 2000.0; 82 | } 83 | }; 84 | 85 | class LaptopWithGraphicsUpgrade : public Laptop 86 | { 87 | public: 88 | string description() const override 89 | { 90 | return "Laptop with graphics upgrade"; 91 | } 92 | 93 | double price() const override 94 | { 95 | return 2700.0; 96 | } 97 | }; 98 | 99 | int main() 100 | { 101 | Computer *desktop = new Desktop(); 102 | cout << desktop->description() << " costs $" << desktop->price() << endl; 103 | 104 | Computer *laptop = new Laptop(); 105 | cout << laptop->description() << " costs $" << laptop->price() << endl; 106 | 107 | Computer *desktopGraphicsUpgrade = new DesktopWithGraphicsUpgrade(); 108 | cout << desktopGraphicsUpgrade->description() << " costs $" << desktopGraphicsUpgrade->price() << endl; 109 | 110 | Computer *desktopMemoryUpgrade = new DesktopWithMemoryUpgrade(); 111 | cout << desktopMemoryUpgrade->description() << " costs $" << desktopMemoryUpgrade->price() << endl; 112 | 113 | Computer *laptopGraphicsUpgrade = new LaptopWithGraphicsUpgrade(); 114 | cout << laptopGraphicsUpgrade->description() << " costs $" << laptopGraphicsUpgrade->price() << endl; 115 | 116 | Computer *laptopMemoryUpgrade = new LaptopWithMemoryUpgrade(); 117 | cout << laptopMemoryUpgrade->description() << " costs $" << laptopMemoryUpgrade->price() << endl; 118 | 119 | delete desktop; 120 | delete laptop; 121 | delete desktopGraphicsUpgrade; 122 | delete desktopMemoryUpgrade; 123 | delete laptopGraphicsUpgrade; 124 | delete laptopMemoryUpgrade; 125 | 126 | return 0; 127 | } 128 | -------------------------------------------------------------------------------- /Ch04-Decorator/04_03/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch04-Decorator/04_03/end/computershop.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Computer 8 | { 9 | public: 10 | virtual string description() const = 0; 11 | virtual double price() const = 0; 12 | virtual ~Computer() = default; 13 | }; 14 | 15 | class Desktop : public Computer 16 | { 17 | public: 18 | string description() const override 19 | { 20 | return "Desktop"; 21 | } 22 | 23 | double price() const override 24 | { 25 | return 1000.0; 26 | } 27 | }; 28 | 29 | class Laptop : public Computer 30 | { 31 | public: 32 | string description() const override 33 | { 34 | return "Laptop"; 35 | } 36 | 37 | double price() const override 38 | { 39 | return 1500.0; 40 | } 41 | }; 42 | 43 | // Decorator base 44 | class ComputerDecorator : public Computer 45 | { 46 | public: 47 | explicit ComputerDecorator(Computer *computer) : m_Computer(computer) {} 48 | 49 | string description() const override 50 | { 51 | return m_Computer->description(); 52 | } 53 | 54 | double price() const override 55 | { 56 | return m_Computer->price(); 57 | } 58 | 59 | protected: 60 | const Computer *m_Computer; 61 | }; 62 | 63 | // Upgrades 64 | class MemoryUpgradeDecorator : public ComputerDecorator 65 | { 66 | public: 67 | explicit MemoryUpgradeDecorator(Computer *computer) : ComputerDecorator(computer) {} 68 | 69 | string description() const override 70 | { 71 | return ComputerDecorator::description() + " with memory upgrade"; 72 | } 73 | 74 | double price() const override 75 | { 76 | return ComputerDecorator::price() + 500.0; 77 | } 78 | }; 79 | 80 | class GraphicsUpgradeDecorator : public ComputerDecorator 81 | { 82 | public: 83 | explicit GraphicsUpgradeDecorator(Computer *computer) : ComputerDecorator(computer) {} 84 | 85 | string description() const override 86 | { 87 | return ComputerDecorator::description() + " with graphics upgrade"; 88 | } 89 | 90 | double price() const override 91 | { 92 | return ComputerDecorator::price() + 500.0; 93 | } 94 | }; 95 | 96 | int main() 97 | { 98 | auto desktop = new Desktop(); 99 | cout << desktop->description() << " costs $" << desktop->price() << endl; 100 | 101 | auto laptop = new Laptop(); 102 | cout << laptop->description() << " costs $" << laptop->price() << endl; 103 | 104 | auto desktopMemoryUpgrade = new MemoryUpgradeDecorator(desktop); 105 | cout << desktopMemoryUpgrade->description() << " costs $" << desktopMemoryUpgrade->price() << endl; 106 | 107 | auto laptopGraphicsUpgrade = new GraphicsUpgradeDecorator(laptop); 108 | cout << laptopGraphicsUpgrade->description() << " costs $" << laptopGraphicsUpgrade->price() << endl; 109 | 110 | delete desktop; 111 | delete laptop; 112 | delete desktopMemoryUpgrade; 113 | delete laptopGraphicsUpgrade; 114 | 115 | return 0; 116 | } -------------------------------------------------------------------------------- /Ch04-Decorator/04_04/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch04-Decorator/04_04/end/pizza.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Pizza 5 | { 6 | public: 7 | virtual string description() const = 0; 8 | virtual double price() const = 0; 9 | virtual ~Pizza() = default; 10 | }; 11 | 12 | class MargheritaPizza : public Pizza 13 | { 14 | public: 15 | string description() const override 16 | { 17 | return "Margherita Pizza"; 18 | } 19 | 20 | double price() const override 21 | { 22 | return 9.99; 23 | } 24 | }; 25 | 26 | class HawaiianPizza : public Pizza 27 | { 28 | public: 29 | string description() const override 30 | { 31 | return "Hawaiian Pizza"; 32 | } 33 | 34 | double price() const override 35 | { 36 | return 11.99; 37 | } 38 | }; 39 | 40 | class PepperoniPizza : public Pizza 41 | { 42 | public: 43 | string description() const override 44 | { 45 | return "Pepperoni Pizza"; 46 | } 47 | 48 | double price() const override 49 | { 50 | return 12.99; 51 | } 52 | }; 53 | 54 | int main() 55 | { 56 | const std::unique_ptr pizzas[]{ 57 | make_unique(), 58 | make_unique(), 59 | make_unique()}; 60 | 61 | for (const auto &pizza : pizzas) 62 | { 63 | cout << pizza->description() << " costs $" << pizza->price() << endl; 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Ch04-Decorator/04_05/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch04-Decorator/04_05/begin/pizza.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Pizza 5 | { 6 | public: 7 | virtual string description() const = 0; 8 | virtual double price() const = 0; 9 | virtual ~Pizza() = default; 10 | }; 11 | 12 | class MargheritaPizza : public Pizza 13 | { 14 | public: 15 | string description() const override 16 | { 17 | return "Margherita Pizza"; 18 | } 19 | 20 | double price() const override 21 | { 22 | return 9.99; 23 | } 24 | }; 25 | 26 | class HawaiianPizza : public Pizza 27 | { 28 | public: 29 | string description() const override 30 | { 31 | return "Hawaiian Pizza"; 32 | } 33 | 34 | double price() const override 35 | { 36 | return 11.99; 37 | } 38 | }; 39 | 40 | class PepperoniPizza : public Pizza 41 | { 42 | public: 43 | string description() const override 44 | { 45 | return "Pepperoni Pizza"; 46 | } 47 | 48 | double price() const override 49 | { 50 | return 12.99; 51 | } 52 | }; 53 | 54 | int main() 55 | { 56 | const std::unique_ptr pizzas[]{ 57 | make_unique(), 58 | make_unique(), 59 | make_unique()}; 60 | 61 | for (const auto &pizza : pizzas) 62 | { 63 | cout << pizza->description() << " costs $" << pizza->price() << endl; 64 | } 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Ch04-Decorator/04_05/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch04-Decorator/04_05/end/pizza.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | class Pizza 6 | { 7 | public: 8 | virtual string description() const = 0; 9 | virtual double price() const = 0; 10 | virtual ~Pizza() = default; 11 | }; 12 | 13 | class MargheritaPizza : public Pizza 14 | { 15 | public: 16 | string description() const override 17 | { 18 | return "Margherita Pizza"; 19 | } 20 | 21 | double price() const override 22 | { 23 | return 9.99; 24 | } 25 | }; 26 | 27 | class HawaiianPizza : public Pizza 28 | { 29 | public: 30 | string description() const override 31 | { 32 | return "Hawaiian Pizza"; 33 | } 34 | 35 | double price() const override 36 | { 37 | return 11.99; 38 | } 39 | }; 40 | 41 | class PepperoniPizza : public Pizza 42 | { 43 | public: 44 | string description() const override 45 | { 46 | return "Pepperoni Pizza"; 47 | } 48 | 49 | double price() const override 50 | { 51 | return 12.99; 52 | } 53 | }; 54 | 55 | class ToppingDecorator : public Pizza 56 | { 57 | public: 58 | explicit ToppingDecorator(unique_ptr pizza) : m_Pizza(move(pizza)) {} 59 | 60 | string description() const override 61 | { 62 | return m_Pizza->description(); 63 | } 64 | 65 | double price() const override 66 | { 67 | return m_Pizza->price(); 68 | } 69 | 70 | virtual ~ToppingDecorator() = default; 71 | 72 | private: 73 | const unique_ptr m_Pizza; 74 | }; 75 | 76 | class MushroomDecorator : public ToppingDecorator 77 | { 78 | public: 79 | explicit MushroomDecorator(unique_ptr pizza) : ToppingDecorator(move(pizza)) {} 80 | 81 | string description() const override 82 | { 83 | return ToppingDecorator::description() + " with mushrooms"; 84 | } 85 | 86 | double price() const override 87 | { 88 | return ToppingDecorator::price() + 0.99; 89 | } 90 | }; 91 | 92 | class ExtraCheeseDecorator : public ToppingDecorator 93 | { 94 | public: 95 | explicit ExtraCheeseDecorator(unique_ptr pizza) : ToppingDecorator(move(pizza)) {} 96 | 97 | string description() const override 98 | { 99 | return ToppingDecorator::description() + ", plus extra cheese"; 100 | } 101 | 102 | double price() const override 103 | { 104 | return ToppingDecorator::price() + 1.99; 105 | } 106 | }; 107 | 108 | class TomatoDecorator : public ToppingDecorator 109 | { 110 | public: 111 | explicit TomatoDecorator(unique_ptr pizza) : ToppingDecorator(move(pizza)) {} 112 | 113 | string description() const override 114 | { 115 | return ToppingDecorator::description() + ", plus tomatoes"; 116 | } 117 | 118 | double price() const override 119 | { 120 | return ToppingDecorator::price() + 0.79; 121 | } 122 | }; 123 | 124 | int main() 125 | { 126 | // MargheritaPizza with mushrooms and extra cheese 127 | auto margheritaPizza = make_unique(); 128 | auto margheritaWithMushrooms = make_unique(move(margheritaPizza)); 129 | auto margheritaExtraCheeseMushrooms = make_unique(move(margheritaWithMushrooms)); 130 | 131 | cout << margheritaExtraCheeseMushrooms->description() << " costs $" << margheritaExtraCheeseMushrooms->price() << endl; 132 | 133 | // Pepperoni pizza with mushrooms, tomatoes, and extra cheese 134 | auto pepperoniPizza = make_unique(); 135 | auto pepperoniWithMushrooms = make_unique(move(pepperoniPizza)); 136 | auto pepperoniWithTomatoMushrooms = make_unique(move(pepperoniWithMushrooms)); 137 | auto pepperoniTomatoMushroomsExtraCheese = make_unique(move(pepperoniWithTomatoMushrooms)); 138 | 139 | cout << pepperoniTomatoMushroomsExtraCheese->description() << " costs $" << pepperoniTomatoMushroomsExtraCheese->price() << endl; 140 | 141 | return 0; 142 | } 143 | -------------------------------------------------------------------------------- /Ch05-The Facade/05_02/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | }, 28 | { 29 | "type": "cppbuild", 30 | "label": "C/C++: clang build active file", 31 | "command": "/usr/bin/clang", 32 | "args": [ 33 | "-fcolor-diagnostics", 34 | "-fansi-escape-codes", 35 | "-g", 36 | "${file}", 37 | "-o", 38 | "${fileDirname}/${fileBasenameNoExtension}" 39 | ], 40 | "options": { 41 | "cwd": "${fileDirname}" 42 | }, 43 | "problemMatcher": [ 44 | "$gcc" 45 | ], 46 | "group": "build", 47 | "detail": "Task generated by Debugger." 48 | } 49 | ], 50 | "version": "2.0.0" 51 | } -------------------------------------------------------------------------------- /Ch05-The Facade/05_02/end/hotel-reservation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Mock class for database library 8 | class Database 9 | { 10 | public: 11 | void storeReservation(const string &reservation) 12 | { 13 | cout << "Database: Storing reservation: " << reservation << endl; 14 | } 15 | }; 16 | 17 | // Mock class for payment gateway 18 | class PaymentGateway 19 | { 20 | public: 21 | void processPayment(const string &paymentInfo) 22 | { 23 | cout << "Payment Gateway: Processing payment with info: " << paymentInfo << endl; 24 | } 25 | }; 26 | 27 | // Mock class for messaging service 28 | class MessagingService 29 | { 30 | public: 31 | void sendConfirmation(const string &message) 32 | { 33 | cout << "Messaging Service: Sending confirmation message: " << message << endl; 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | Database db; 40 | PaymentGateway paymentGateway; 41 | MessagingService messagingService; 42 | 43 | const string reservation = "Room reservation info"; 44 | db.storeReservation(reservation); 45 | 46 | const string paymentInfo = "Payment info"; 47 | paymentGateway.processPayment(paymentInfo); 48 | 49 | messagingService.sendConfirmation("Reservation confirmed."); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Ch05-The Facade/05_03/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | }, 28 | { 29 | "type": "cppbuild", 30 | "label": "C/C++: clang build active file", 31 | "command": "/usr/bin/clang", 32 | "args": [ 33 | "-fcolor-diagnostics", 34 | "-fansi-escape-codes", 35 | "-g", 36 | "${file}", 37 | "-o", 38 | "${fileDirname}/${fileBasenameNoExtension}" 39 | ], 40 | "options": { 41 | "cwd": "${fileDirname}" 42 | }, 43 | "problemMatcher": [ 44 | "$gcc" 45 | ], 46 | "group": "build", 47 | "detail": "Task generated by Debugger." 48 | } 49 | ], 50 | "version": "2.0.0" 51 | } -------------------------------------------------------------------------------- /Ch05-The Facade/05_03/begin/hotel-reservation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Mock class for database library 8 | class Database 9 | { 10 | public: 11 | void storeReservation(const string &reservation) 12 | { 13 | cout << "Database: Storing reservation: " << reservation << endl; 14 | } 15 | }; 16 | 17 | // Mock class for payment gateway 18 | class PaymentGateway 19 | { 20 | public: 21 | void processPayment(const string &paymentInfo) 22 | { 23 | cout << "Payment Gateway: Processing payment with info: " << paymentInfo << endl; 24 | } 25 | }; 26 | 27 | // Mock class for messaging service 28 | class MessagingService 29 | { 30 | public: 31 | void sendConfirmation(const string &message) 32 | { 33 | cout << "Messaging Service: Sending confirmation message: " << message << endl; 34 | } 35 | }; 36 | 37 | int main() 38 | { 39 | Database db; 40 | PaymentGateway paymentGateway; 41 | MessagingService messagingService; 42 | 43 | const string reservation = "Room reservation info"; 44 | db.storeReservation(reservation); 45 | 46 | const string paymentInfo = "Payment info"; 47 | paymentGateway.processPayment(paymentInfo); 48 | 49 | messagingService.sendConfirmation("Reservation confirmed."); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Ch05-The Facade/05_03/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | }, 28 | { 29 | "type": "cppbuild", 30 | "label": "C/C++: clang build active file", 31 | "command": "/usr/bin/clang", 32 | "args": [ 33 | "-fcolor-diagnostics", 34 | "-fansi-escape-codes", 35 | "-g", 36 | "${file}", 37 | "-o", 38 | "${fileDirname}/${fileBasenameNoExtension}" 39 | ], 40 | "options": { 41 | "cwd": "${fileDirname}" 42 | }, 43 | "problemMatcher": [ 44 | "$gcc" 45 | ], 46 | "group": "build", 47 | "detail": "Task generated by Debugger." 48 | } 49 | ], 50 | "version": "2.0.0" 51 | } -------------------------------------------------------------------------------- /Ch05-The Facade/05_03/end/hotel-reservation.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | // Mock class for database library 8 | class Database 9 | { 10 | public: 11 | void storeReservation(const string &reservation) 12 | { 13 | cout << "Database: Storing reservation: " << reservation << endl; 14 | } 15 | }; 16 | 17 | // Mock class for payment gateway 18 | class PaymentGateway 19 | { 20 | public: 21 | void processPayment(const string &paymentInfo) 22 | { 23 | cout << "Payment Gateway: Processing payment with info: " << paymentInfo << endl; 24 | } 25 | }; 26 | 27 | // Mock class for messaging service 28 | class MessagingService 29 | { 30 | public: 31 | void sendConfirmation(const string &message) 32 | { 33 | cout << "Messaging Service: Sending confirmation message: " << message << endl; 34 | } 35 | }; 36 | 37 | // Hotel reservation system facade 38 | class ReservationSystemFacade 39 | { 40 | public: 41 | ReservationSystemFacade() : m_Database(), m_PaymentGateway(), m_MessagingService() {} 42 | 43 | void reserveRoom(const string &reservation, const string &paymentInfo) 44 | { 45 | m_Database.storeReservation(reservation); 46 | m_PaymentGateway.processPayment(paymentInfo); 47 | m_MessagingService.sendConfirmation("Reservation confirmed."); 48 | } 49 | 50 | private: 51 | Database m_Database; 52 | PaymentGateway m_PaymentGateway; 53 | MessagingService m_MessagingService; 54 | }; 55 | 56 | int main() 57 | { 58 | /* 59 | Database db; 60 | PaymentGateway paymentGateway; 61 | MessagingService messagingService; 62 | 63 | const string reservation = "Room reservation info"; 64 | db.storeReservation(reservation); 65 | 66 | const string paymentInfo = "Payment info"; 67 | paymentGateway.processPayment(paymentInfo); 68 | 69 | messagingService.sendConfirmation("Reservation confirmed."); 70 | */ 71 | ReservationSystemFacade reservationSystem; 72 | 73 | const string reservation = "Room reservation info"; 74 | const string paymentInfo = "Payment info"; 75 | 76 | reservationSystem.reserveRoom(reservation, paymentInfo); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Ch05-The Facade/05_04/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch05-The Facade/05_04/end/weatherapi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class WorldWeatherAPI 7 | { 8 | public: 9 | tuple getWeather(string location) 10 | { 11 | cout << "Calling worldWeather with location: " << location << endl; 12 | float temperature = 20.0f; 13 | float windSpeed = 5.5f; 14 | string shortDescription = "Sunny"; 15 | return make_tuple(temperature, windSpeed, shortDescription); 16 | } 17 | }; 18 | 19 | class FreeWeather 20 | { 21 | public: 22 | tuple retrieve_weather(string location) 23 | { 24 | cout << "Calling freeWeather with location: " << location << endl; 25 | float temperature = 22.0f; 26 | string shortDescription = "Sunny"; 27 | return make_tuple(temperature, shortDescription); 28 | } 29 | }; 30 | 31 | class RealtimeWeatherService 32 | { 33 | public: 34 | tuple weatherConditions(string location) 35 | { 36 | cout << "Calling realtimeWeather with location: " << location << endl; 37 | float temperature = 19.5f; 38 | float humidity = 60.0f; 39 | string shortDescription = "Partly cloudy with a chance of rain"; 40 | return make_tuple(temperature, humidity, shortDescription); 41 | } 42 | }; 43 | 44 | int main() 45 | { 46 | WorldWeatherAPI worldWeather; 47 | FreeWeather freeWeather; 48 | RealtimeWeatherService realtimeWeather; 49 | 50 | auto const location = "San Francisco, CA, US"; 51 | // Call each API and combine the results 52 | tuple worldWeatherResult = worldWeather.getWeather(location); 53 | tuple freeWeatherResult = freeWeather.retrieve_weather(location); 54 | tuple realtimeWeatherResult = realtimeWeather.weatherConditions(location); 55 | 56 | // Combine the results into a single string 57 | float temperature = get<0>(worldWeatherResult); 58 | float humidity = get<1>(realtimeWeatherResult); 59 | string shortDescription = get<1>(freeWeatherResult); 60 | 61 | cout << "\nWeather for " << location << endl 62 | << shortDescription << endl 63 | << "Temperature: " << temperature << " C" << endl 64 | << "Humidity: " << humidity << " %" << endl; 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Ch05-The Facade/05_05/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch05-The Facade/05_05/begin/weatherapi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class WorldWeatherAPI 7 | { 8 | public: 9 | tuple getWeather(string location) 10 | { 11 | cout << "Calling worldWeather with location: " << location << endl; 12 | float temperature = 20.0f; 13 | float windSpeed = 5.5f; 14 | string shortDescription = "Sunny"; 15 | return make_tuple(temperature, windSpeed, shortDescription); 16 | } 17 | }; 18 | 19 | class FreeWeather 20 | { 21 | public: 22 | tuple retrieve_weather(string location) 23 | { 24 | cout << "Calling freeWeather with location: " << location << endl; 25 | float temperature = 22.0f; 26 | string shortDescription = "Sunny"; 27 | return make_tuple(temperature, shortDescription); 28 | } 29 | }; 30 | 31 | class RealtimeWeatherService 32 | { 33 | public: 34 | tuple weatherConditions(string location) 35 | { 36 | cout << "Calling realtimeWeather with location: " << location << endl; 37 | float temperature = 19.5f; 38 | float humidity = 60.0f; 39 | string shortDescription = "Partly cloudy with a chance of rain"; 40 | return make_tuple(temperature, humidity, shortDescription); 41 | } 42 | }; 43 | 44 | int main() 45 | { 46 | WorldWeatherAPI worldWeather; 47 | FreeWeather freeWeather; 48 | RealtimeWeatherService realtimeWeather; 49 | 50 | auto const location = "San Francisco, CA, US"; 51 | // Call each API and combine the results 52 | tuple worldWeatherResult = worldWeather.getWeather(location); 53 | tuple freeWeatherResult = freeWeather.retrieve_weather(location); 54 | tuple realtimeWeatherResult = realtimeWeather.weatherConditions(location); 55 | 56 | // Combine the results into a single string 57 | float temperature = get<0>(worldWeatherResult); 58 | float humidity = get<1>(realtimeWeatherResult); 59 | string shortDescription = get<1>(freeWeatherResult); 60 | 61 | cout << "\nWeather for " << location << endl 62 | << shortDescription << endl 63 | << "Temperature: " << temperature << " C" << endl 64 | << "Humidity: " << humidity << " %" << endl; 65 | 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /Ch05-The Facade/05_05/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch05-The Facade/05_05/end/weatherapi.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class WorldWeatherAPI 8 | { 9 | public: 10 | tuple getWeather(string location) 11 | { 12 | cout << "Calling worldWeather with location: " << location << endl; 13 | float temperature = 20.0f; 14 | float windSpeed = 5.5f; 15 | string shortDescription = "Sunny"; 16 | return make_tuple(temperature, windSpeed, shortDescription); 17 | } 18 | }; 19 | 20 | class FreeWeather 21 | { 22 | public: 23 | tuple retrieve_weather(string location) 24 | { 25 | cout << "Calling freeWeather with location: " << location << endl; 26 | float temperature = 22.0f; 27 | string shortDescription = "Sunny"; 28 | return make_tuple(temperature, shortDescription); 29 | } 30 | }; 31 | 32 | class RealtimeWeatherService 33 | { 34 | public: 35 | tuple weatherConditions(string location) 36 | { 37 | cout << "Calling realtimeWeather with location: " << location << endl; 38 | float temperature = 19.5f; 39 | float humidity = 60.0f; 40 | string shortDescription = "Partly cloudy with a chance of rain"; 41 | return make_tuple(temperature, humidity, shortDescription); 42 | } 43 | }; 44 | 45 | class WeatherFacade 46 | { 47 | public: 48 | WeatherFacade() : worldWeatherAPI(), freeWeather(), realtimeWeatherService() {} 49 | 50 | const string currentWeather(const string &location) 51 | { 52 | // Call each API 53 | tuple worldWeatherResult = worldWeatherAPI.getWeather(location); 54 | tuple freeWeatherResult = freeWeather.retrieve_weather(location); 55 | tuple realtimeWeatherResult = realtimeWeatherService.weatherConditions(location); 56 | 57 | // Get relevant data from the results 58 | float temperature = get<0>(worldWeatherResult); 59 | float humidity = get<1>(realtimeWeatherResult); 60 | string shortDescription = get<1>(freeWeatherResult); 61 | 62 | stringstream result; 63 | 64 | result << "Weather for " << location << endl 65 | << shortDescription << endl 66 | << "Temperature: " << temperature << " C" << endl 67 | << "Humidity: " << humidity << " %" << endl; 68 | 69 | return result.str(); 70 | } 71 | 72 | private: 73 | WorldWeatherAPI worldWeatherAPI; 74 | FreeWeather freeWeather; 75 | RealtimeWeatherService realtimeWeatherService; 76 | }; 77 | 78 | int main() 79 | { 80 | WeatherFacade weatherFacade; 81 | 82 | auto const location = "San Francisco, CA, US"; 83 | // Call each API and combine the results 84 | cout << weatherFacade.currentWeather(location) << endl; 85 | 86 | return 0; 87 | } 88 | -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_02/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_02/end/sprites.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Texture 7 | { 8 | public: 9 | explicit Texture(const string &fileName) : m_FileName(fileName), m_Id(arc4random_uniform(INT_MAX)) {} 10 | 11 | const string description() const 12 | { 13 | return "<" + m_FileName + " id" + to_string(m_Id) + ">"; 14 | } 15 | ~Texture() 16 | { 17 | cout << "Destructing " << description() << endl; 18 | } 19 | // other texture manipulation methods 20 | private: 21 | const string m_FileName; 22 | const int m_Id; 23 | }; 24 | 25 | class Sprite 26 | { 27 | public: 28 | Sprite(int width, int height, int x, int y, const string &textureFileName) : m_Width(width), m_Height(height), m_X(x), m_Y(y), m_Texture(new Texture(textureFileName)) {} 29 | 30 | void render() 31 | { 32 | // draw sprite 33 | cout << "Rendering sprite with texture: " << m_Texture->description() << endl; 34 | } 35 | 36 | ~Sprite() 37 | { 38 | cout << "Destructing sprite with texture " << m_Texture->description() << endl; 39 | delete m_Texture; 40 | } 41 | 42 | private: 43 | const int m_Width; 44 | const int m_Height; 45 | const int m_X; 46 | const int m_Y; 47 | 48 | const Texture *m_Texture; 49 | }; 50 | 51 | int main() 52 | { 53 | // create a list of sprite objects 54 | vector sprites; 55 | const int numSprites = 10; 56 | const string textureFile = "spaceship.png"; 57 | 58 | for (int i = 0; i < numSprites; ++i) 59 | { 60 | auto sprite = new Sprite(10, 10, i * 10, i * 10, textureFile); 61 | sprites.push_back(sprite); 62 | } 63 | 64 | // draw all sprites 65 | for (auto &sprite : sprites) 66 | { 67 | sprite->render(); 68 | } 69 | 70 | // cleanup 71 | for (auto &sprite : sprites) 72 | { 73 | delete(sprite); 74 | } 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_03/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_03/begin/sprites.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Texture 7 | { 8 | public: 9 | explicit Texture(const string &fileName) : m_FileName(fileName), m_Id(arc4random_uniform(INT_MAX)) {} 10 | 11 | const string description() const 12 | { 13 | return "<" + m_FileName + " id" + to_string(m_Id) + ">"; 14 | } 15 | ~Texture() 16 | { 17 | cout << "Destructing " << description() << endl; 18 | } 19 | // other texture manipulation methods 20 | private: 21 | const string m_FileName; 22 | const int m_Id; 23 | }; 24 | 25 | class Sprite 26 | { 27 | public: 28 | Sprite(int width, int height, int x, int y, const string &textureFileName) : m_Width(width), m_Height(height), m_X(x), m_Y(y), m_Texture(new Texture(textureFileName)) {} 29 | 30 | void render() 31 | { 32 | // draw sprite 33 | cout << "Rendering sprite with texture: " << m_Texture->description() << endl; 34 | } 35 | 36 | ~Sprite() 37 | { 38 | cout << "Destructing sprite with texture " << m_Texture->description() << endl; 39 | delete m_Texture; 40 | } 41 | 42 | private: 43 | const int m_Width; 44 | const int m_Height; 45 | const int m_X; 46 | const int m_Y; 47 | 48 | const Texture *m_Texture; 49 | }; 50 | 51 | int main() 52 | { 53 | // create a list of sprite objects 54 | vector sprites; 55 | const int numSprites = 10; 56 | const string textureFile = "spaceship.png"; 57 | 58 | for (int i = 0; i < numSprites; ++i) 59 | { 60 | auto sprite = new Sprite(10, 10, i * 10, i * 10, textureFile); 61 | sprites.push_back(sprite); 62 | } 63 | 64 | // draw all sprites 65 | for (auto &sprite : sprites) 66 | { 67 | sprite->render(); 68 | } 69 | 70 | // cleanup 71 | for (auto &sprite : sprites) 72 | { 73 | delete(sprite); 74 | } 75 | 76 | return 0; 77 | } 78 | -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_03/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_03/end/sprites.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Texture 8 | { 9 | public: 10 | explicit Texture(const string &fileName) : m_FileName(fileName), m_Id(arc4random_uniform(INT_MAX)) 11 | { 12 | cout << "Creating texture from " << m_FileName << endl; 13 | } 14 | 15 | const string description() const 16 | { 17 | return "<" + m_FileName + " id" + to_string(m_Id) + ">"; 18 | } 19 | private: 20 | const string m_FileName; 21 | const int m_Id; 22 | }; 23 | 24 | class Sprite 25 | { 26 | public: 27 | Sprite(const Texture *texture) : m_Texture(texture) 28 | { 29 | cout << "Creating sprite with texture file " << texture->description() << endl; 30 | } 31 | 32 | void setPositionSize(int x, int y, int width, int height) 33 | { 34 | m_X = x; 35 | m_Y = y; 36 | m_Width = width; 37 | m_Height = height; 38 | } 39 | 40 | void render() 41 | { 42 | // draw sprite 43 | cout << "Rendering sprite with texture: " << m_Texture->description() << endl; 44 | } 45 | private: 46 | int m_Width; 47 | int m_Height; 48 | int m_X; 49 | int m_Y; 50 | 51 | const Texture *m_Texture; 52 | }; 53 | 54 | class SpriteFactory 55 | { 56 | public: 57 | Sprite* makeSprite(const string &fileName) 58 | { 59 | auto it = m_SpritePool.find(fileName); 60 | if (it != m_SpritePool.end()) 61 | { 62 | // sprite already exists in pool, return it 63 | return it->second; 64 | } 65 | else 66 | { 67 | // create new texture and add it to the pool 68 | const auto texture = getTexture(fileName); 69 | auto [newIt, _] = m_SpritePool.emplace(fileName, new Sprite(texture)); 70 | return newIt->second; 71 | } 72 | } 73 | 74 | ~SpriteFactory() 75 | { 76 | for (auto &[filename, sprite] : m_SpritePool) 77 | { 78 | delete sprite; 79 | } 80 | 81 | for (auto &[filename, texture] : m_TexturePool) 82 | { 83 | delete texture; 84 | } 85 | } 86 | 87 | private: 88 | unordered_map m_SpritePool; 89 | unordered_map m_TexturePool; 90 | 91 | const Texture* getTexture(const string &fileName) 92 | { 93 | auto it = m_TexturePool.find(fileName); 94 | if (it != m_TexturePool.end()) 95 | { 96 | // texture already exists in pool, return it 97 | return it->second; 98 | } 99 | else 100 | { 101 | // create new texture and add it to the pool 102 | auto [newIt, _] = m_TexturePool.emplace(fileName, new Texture(fileName)); 103 | return newIt->second; 104 | } 105 | } 106 | }; 107 | 108 | int main() 109 | { 110 | // create a list of sprite objects 111 | vector sprites; 112 | const int numSprites = 10; 113 | const string textureFile = "spaceship.png"; 114 | 115 | SpriteFactory spriteFactory; 116 | 117 | for (int i = 0; i < numSprites; ++i) 118 | { 119 | auto sprite = spriteFactory.makeSprite(textureFile); 120 | sprite->setPositionSize(10, 10, i*10, i*10); 121 | sprites.push_back(sprite); 122 | } 123 | 124 | // draw all sprites 125 | for (auto &sprite : sprites) 126 | { 127 | sprite->render(); 128 | } 129 | 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_04/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_04/end/text-editor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Character 7 | { 8 | public: 9 | explicit Character(char c) : m_Char(c) {} 10 | char getChar() const { return m_Char; } 11 | 12 | private: 13 | const char m_Char; 14 | }; 15 | 16 | class Document 17 | { 18 | public: 19 | void insert(char c) 20 | { 21 | m_Chars.push_back(Character(c)); 22 | } 23 | 24 | void print() const 25 | { 26 | for (const auto &c : m_Chars) 27 | { 28 | std::cout << c.getChar(); 29 | } 30 | } 31 | 32 | private: 33 | std::vector m_Chars; 34 | }; 35 | 36 | int main() 37 | { 38 | Document doc; 39 | doc.insert('H'); 40 | doc.insert('e'); 41 | doc.insert('l'); 42 | doc.insert('l'); 43 | doc.insert('o'); 44 | doc.insert(','); 45 | doc.insert(' '); 46 | doc.insert('w'); 47 | doc.insert('o'); 48 | doc.insert('r'); 49 | doc.insert('l'); 50 | doc.insert('d'); 51 | doc.insert('!'); 52 | doc.print(); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_05/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_05/begin/text-editor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Character 7 | { 8 | public: 9 | explicit Character(char c) : m_Char(c) {} 10 | char getChar() const { return m_Char; } 11 | 12 | private: 13 | const char m_Char; 14 | }; 15 | 16 | class Document 17 | { 18 | public: 19 | void insert(char c) 20 | { 21 | m_Chars.push_back(Character(c)); 22 | } 23 | 24 | void print() const 25 | { 26 | for (const auto &c : m_Chars) 27 | { 28 | std::cout << c.getChar(); 29 | } 30 | } 31 | 32 | private: 33 | std::vector m_Chars; 34 | }; 35 | 36 | int main() 37 | { 38 | Document doc; 39 | doc.insert('H'); 40 | doc.insert('e'); 41 | doc.insert('l'); 42 | doc.insert('l'); 43 | doc.insert('o'); 44 | doc.insert(','); 45 | doc.insert(' '); 46 | doc.insert('w'); 47 | doc.insert('o'); 48 | doc.insert('r'); 49 | doc.insert('l'); 50 | doc.insert('d'); 51 | doc.insert('!'); 52 | doc.print(); 53 | return 0; 54 | } -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_05/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch06-The Flyweight/06_05/end/text-editor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Character 8 | { 9 | public: 10 | explicit Character(char c) : m_char(c) {} 11 | char getChar() const { return m_char; } 12 | 13 | private: 14 | const char m_char; 15 | }; 16 | 17 | class CharacterFactory 18 | { 19 | public: 20 | Character *getCharacter(const char c) 21 | { 22 | auto it = m_Characters.find(c); 23 | if (it != m_Characters.end()) 24 | { 25 | return it->second; 26 | } 27 | else 28 | { 29 | auto [newIt, _] = m_Characters.emplace(c, new Character(c)); 30 | return newIt->second; 31 | } 32 | } 33 | 34 | ~CharacterFactory() 35 | { 36 | for (auto &[c, character] : m_Characters) 37 | { 38 | delete character; 39 | } 40 | } 41 | 42 | private: 43 | unordered_map m_Characters; 44 | }; 45 | 46 | class Document 47 | { 48 | public: 49 | explicit Document(CharacterFactory *factory) : m_factory(factory) {} 50 | 51 | void insert(char c) 52 | { 53 | m_chars.emplace_back(m_factory->getCharacter(c)); 54 | } 55 | 56 | void print() const 57 | { 58 | for (const auto &c : m_chars) 59 | { 60 | std::cout << c->getChar(); 61 | } 62 | } 63 | 64 | private: 65 | std::vector m_chars; 66 | CharacterFactory *m_factory; 67 | }; 68 | 69 | int main() 70 | { 71 | CharacterFactory factory; 72 | Document doc(&factory); 73 | doc.insert('H'); 74 | doc.insert('e'); 75 | doc.insert('l'); 76 | doc.insert('l'); 77 | doc.insert('o'); 78 | doc.insert(','); 79 | doc.insert(' '); 80 | doc.insert('w'); 81 | doc.insert('o'); 82 | doc.insert('r'); 83 | doc.insert('l'); 84 | doc.insert('d'); 85 | doc.insert('!'); 86 | doc.print(); 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /Ch07-The Proxy/07_02/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch07-The Proxy/07_02/end/config.txt: -------------------------------------------------------------------------------- 1 | zoom = 2 2 | reader = false 3 | scheme = dark 4 | -------------------------------------------------------------------------------- /Ch07-The Proxy/07_02/end/read-config.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | // Define an abstract base class for our configuration file 10 | class ConfigFile 11 | { 12 | public: 13 | virtual vector getSettings() = 0; 14 | virtual ~ConfigFile() = default; 15 | }; 16 | 17 | // Define a concrete implementation of the ConfigFile 18 | class RealConfigFile : public ConfigFile 19 | { 20 | public: 21 | explicit RealConfigFile(const string &filename) 22 | { 23 | cout << "RealConfigFile created." << endl; 24 | // Open the file and read its contents into a vector of strings 25 | ifstream file(filename); 26 | string line; 27 | while (getline(file, line)) 28 | { 29 | settings.push_back(line); 30 | } 31 | cout << settings.size() << " settings loaded." << endl; 32 | } 33 | 34 | vector getSettings() override 35 | { 36 | return settings; 37 | } 38 | 39 | private: 40 | vector settings; 41 | }; 42 | 43 | 44 | int main() 45 | { 46 | RealConfigFile configFile("config.txt"); 47 | 48 | bool useSettings = true; 49 | 50 | if (useSettings) 51 | { 52 | vector settings = configFile.getSettings(); 53 | for (const auto &setting : settings) 54 | { 55 | cout << setting << endl; 56 | } 57 | } 58 | else 59 | { 60 | cout << "Configuration not used." << endl; 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Ch07-The Proxy/07_03/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch07-The Proxy/07_03/begin/config.txt: -------------------------------------------------------------------------------- 1 | zoom = 2 2 | reader = false 3 | scheme = dark 4 | -------------------------------------------------------------------------------- /Ch07-The Proxy/07_03/begin/read-config.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | // Define an abstract base class for our configuration file 10 | class ConfigFile 11 | { 12 | public: 13 | virtual vector getSettings() = 0; 14 | virtual ~ConfigFile() = default; 15 | }; 16 | 17 | // Define a concrete implementation of the ConfigFile 18 | class RealConfigFile : public ConfigFile 19 | { 20 | public: 21 | explicit RealConfigFile(const string &filename) 22 | { 23 | cout << "RealConfigFile created." << endl; 24 | // Open the file and read its contents into a vector of strings 25 | ifstream file(filename); 26 | string line; 27 | while (getline(file, line)) 28 | { 29 | settings.push_back(line); 30 | } 31 | cout << settings.size() << " settings loaded." << endl; 32 | } 33 | 34 | vector getSettings() override 35 | { 36 | return settings; 37 | } 38 | 39 | private: 40 | vector settings; 41 | }; 42 | 43 | 44 | int main() 45 | { 46 | RealConfigFile configFile("config.txt"); 47 | 48 | bool useSettings = true; 49 | 50 | if (useSettings) 51 | { 52 | vector settings = configFile.getSettings(); 53 | for (const auto &setting : settings) 54 | { 55 | cout << setting << endl; 56 | } 57 | } 58 | else 59 | { 60 | cout << "Configuration not used." << endl; 61 | } 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /Ch07-The Proxy/07_03/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch07-The Proxy/07_03/end/config.txt: -------------------------------------------------------------------------------- 1 | zoom = 2 2 | reader = false 3 | scheme = dark 4 | -------------------------------------------------------------------------------- /Ch07-The Proxy/07_03/end/read-config.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | // Define an abstract base class for our configuration file 10 | class ConfigFile 11 | { 12 | public: 13 | virtual vector getSettings() = 0; 14 | virtual ~ConfigFile() = default; 15 | }; 16 | 17 | // Define a concrete implementation of the ConfigFile 18 | class RealConfigFile : public ConfigFile 19 | { 20 | public: 21 | explicit RealConfigFile(const string &filename) 22 | { 23 | cout << "RealConfigFile created." << endl; 24 | // Open the file and read its contents into a vector of strings 25 | ifstream file(filename); 26 | string line; 27 | while (getline(file, line)) 28 | { 29 | settings.push_back(line); 30 | } 31 | cout << settings.size() << " settings loaded." << endl; 32 | } 33 | 34 | vector getSettings() override 35 | { 36 | return settings; 37 | } 38 | 39 | private: 40 | vector settings; 41 | }; 42 | 43 | // Define a virtual proxy for the ConfigFile 44 | class ConfigFileProxy : public ConfigFile 45 | { 46 | public: 47 | explicit ConfigFileProxy(const string &filename) : filename(filename), realConfigFile(nullptr) 48 | { 49 | cout << "ConfigFileProxy created." << endl; 50 | } 51 | 52 | vector getSettings() override 53 | { 54 | if (realConfigFile == nullptr) 55 | { 56 | // Lazily load the real ConfigFile on first use 57 | realConfigFile = make_unique(filename); 58 | } 59 | return realConfigFile->getSettings(); 60 | } 61 | 62 | private: 63 | string filename; 64 | unique_ptr realConfigFile; 65 | }; 66 | 67 | int main() 68 | { 69 | // RealConfigFile configFile("config.txt"); 70 | 71 | // Create a virtual proxy for the ConfigFile 72 | ConfigFileProxy configFile("config.txt"); 73 | 74 | bool useSettings = false; 75 | 76 | if (useSettings) 77 | { 78 | vector settings = configFile.getSettings(); 79 | for (const auto &setting : settings) 80 | { 81 | cout << setting << endl; 82 | } 83 | } 84 | else 85 | { 86 | cout << "Configuration not used." << endl; 87 | } 88 | 89 | return 0; 90 | } 91 | -------------------------------------------------------------------------------- /Ch07-The Proxy/07_04/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch07-The Proxy/07_04/end/confidential.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Storage 7 | { 8 | public: 9 | virtual const string getContents() = 0; 10 | virtual ~Storage() = default; 11 | }; 12 | 13 | class SecureStorage : public Storage 14 | { 15 | public: 16 | explicit SecureStorage(const string &data) : m_Contents(data) {} 17 | 18 | const string getContents() 19 | { 20 | return m_Contents; 21 | } 22 | 23 | private: 24 | const string m_Contents; 25 | }; 26 | 27 | int main() 28 | { 29 | SecureStorage secureStorage("Top Secret Information"); 30 | 31 | // Limit access to sensitive data 32 | cout << "Sensitive Data: " << secureStorage.getContents() << endl; 33 | 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Ch07-The Proxy/07_05/begin/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch07-The Proxy/07_05/begin/confidential.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Storage 7 | { 8 | public: 9 | virtual const string getContents() = 0; 10 | virtual ~Storage() = default; 11 | }; 12 | 13 | class SecureStorage : public Storage 14 | { 15 | public: 16 | explicit SecureStorage(const string &data) : m_Contents(data) {} 17 | 18 | const string getContents() 19 | { 20 | return m_Contents; 21 | } 22 | 23 | private: 24 | const string m_Contents; 25 | }; 26 | 27 | int main() 28 | { 29 | SecureStorage secureStorage("Top Secret Information"); 30 | 31 | // Limit access to sensitive data 32 | cout << "Sensitive Data: " << secureStorage.getContents() << endl; 33 | 34 | return 0; 35 | } -------------------------------------------------------------------------------- /Ch07-The Proxy/07_05/end/.vscode/tasks.json: -------------------------------------------------------------------------------- 1 | { 2 | "tasks": [ 3 | { 4 | "type": "cppbuild", 5 | "label": "C/C++: clang++ build active file", 6 | "command": "/usr/bin/clang++", 7 | "args": [ 8 | "-std=c++20", 9 | "-fcolor-diagnostics", 10 | "-fansi-escape-codes", 11 | "-g", 12 | "${file}", 13 | "-o", 14 | "${fileDirname}/${fileBasenameNoExtension}" 15 | ], 16 | "options": { 17 | "cwd": "${fileDirname}" 18 | }, 19 | "problemMatcher": [ 20 | "$gcc" 21 | ], 22 | "group": { 23 | "kind": "build", 24 | "isDefault": true 25 | }, 26 | "detail": "Task generated by Debugger." 27 | } 28 | ], 29 | "version": "2.0.0" 30 | } -------------------------------------------------------------------------------- /Ch07-The Proxy/07_05/end/confidential.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Storage 8 | { 9 | public: 10 | virtual const string getContents() = 0; 11 | virtual ~Storage() = default; 12 | }; 13 | 14 | class SecureStorage : public Storage 15 | { 16 | public: 17 | explicit SecureStorage(const string &data) : m_Contents(data) {} 18 | 19 | const string getContents() override 20 | { 21 | return m_Contents; 22 | } 23 | 24 | private: 25 | const string m_Contents; 26 | }; 27 | 28 | class SecureStorageProxy : public Storage 29 | { 30 | public: 31 | explicit SecureStorageProxy(const string &data, const int code) : m_SecureStorage(make_unique(data)), m_SecretCode(code) {} 32 | 33 | const string getContents() override 34 | { 35 | if (authorized() == true) 36 | { 37 | return m_SecureStorage->getContents(); 38 | } 39 | else 40 | { 41 | return "Access denied!"; 42 | } 43 | } 44 | 45 | private: 46 | bool authorized() 47 | { 48 | return m_SecretCode == 42; 49 | } 50 | 51 | unique_ptr m_SecureStorage; 52 | const int m_SecretCode; 53 | }; 54 | 55 | int main() 56 | { 57 | // SecureStorage secureStorage("Top Secret Information"); 58 | SecureStorageProxy secureStorage("Top Secret Information", 41); 59 | 60 | // Limit access to sensitive data 61 | cout << "Sensitive Data: " << secureStorage.getContents() << endl; 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | LinkedIn Learning Exercise Files License Agreement 2 | ================================================== 3 | 4 | This License Agreement (the "Agreement") is a binding legal agreement 5 | between you (as an individual or entity, as applicable) and LinkedIn 6 | Corporation (“LinkedIn”). By downloading or using the LinkedIn Learning 7 | exercise files in this repository (“Licensed Materials”), you agree to 8 | be bound by the terms of this Agreement. If you do not agree to these 9 | terms, do not download or use the Licensed Materials. 10 | 11 | 1. License. 12 | - a. Subject to the terms of this Agreement, LinkedIn hereby grants LinkedIn 13 | members during their LinkedIn Learning subscription a non-exclusive, 14 | non-transferable copyright license, for internal use only, to 1) make a 15 | reasonable number of copies of the Licensed Materials, and 2) make 16 | derivative works of the Licensed Materials for the sole purpose of 17 | practicing skills taught in LinkedIn Learning courses. 18 | - b. Distribution. Unless otherwise noted in the Licensed Materials, subject 19 | to the terms of this Agreement, LinkedIn hereby grants LinkedIn members 20 | with a LinkedIn Learning subscription a non-exclusive, non-transferable 21 | copyright license to distribute the Licensed Materials, except the 22 | Licensed Materials may not be included in any product or service (or 23 | otherwise used) to instruct or educate others. 24 | 25 | 2. Restrictions and Intellectual Property. 26 | - a. You may not to use, modify, copy, make derivative works of, publish, 27 | distribute, rent, lease, sell, sublicense, assign or otherwise transfer the 28 | Licensed Materials, except as expressly set forth above in Section 1. 29 | - b. Linkedin (and its licensors) retains its intellectual property rights 30 | in the Licensed Materials. Except as expressly set forth in Section 1, 31 | LinkedIn grants no licenses. 32 | - c. You indemnify LinkedIn and its licensors and affiliates for i) any 33 | alleged infringement or misappropriation of any intellectual property rights 34 | of any third party based on modifications you make to the Licensed Materials, 35 | ii) any claims arising from your use or distribution of all or part of the 36 | Licensed Materials and iii) a breach of this Agreement. You will defend, hold 37 | harmless, and indemnify LinkedIn and its affiliates (and our and their 38 | respective employees, shareholders, and directors) from any claim or action 39 | brought by a third party, including all damages, liabilities, costs and 40 | expenses, including reasonable attorneys’ fees, to the extent resulting from, 41 | alleged to have resulted from, or in connection with: (a) your breach of your 42 | obligations herein; or (b) your use or distribution of any Licensed Materials. 43 | 44 | 3. Open source. This code may include open source software, which may be 45 | subject to other license terms as provided in the files. 46 | 47 | 4. Warranty Disclaimer. LINKEDIN PROVIDES THE LICENSED MATERIALS ON AN “AS IS” 48 | AND “AS AVAILABLE” BASIS. LINKEDIN MAKES NO REPRESENTATION OR WARRANTY, 49 | WHETHER EXPRESS OR IMPLIED, ABOUT THE LICENSED MATERIALS, INCLUDING ANY 50 | REPRESENTATION THAT THE LICENSED MATERIALS WILL BE FREE OF ERRORS, BUGS OR 51 | INTERRUPTIONS, OR THAT THE LICENSED MATERIALS ARE ACCURATE, COMPLETE OR 52 | OTHERWISE VALID. TO THE FULLEST EXTENT PERMITTED BY LAW, LINKEDIN AND ITS 53 | AFFILIATES DISCLAIM ANY IMPLIED OR STATUTORY WARRANTY OR CONDITION, INCLUDING 54 | ANY IMPLIED WARRANTY OR CONDITION OF MERCHANTABILITY OR FITNESS FOR A 55 | PARTICULAR PURPOSE, AVAILABILITY, SECURITY, TITLE AND/OR NON-INFRINGEMENT. 56 | YOUR USE OF THE LICENSED MATERIALS IS AT YOUR OWN DISCRETION AND RISK, AND 57 | YOU WILL BE SOLELY RESPONSIBLE FOR ANY DAMAGE THAT RESULTS FROM USE OF THE 58 | LICENSED MATERIALS TO YOUR COMPUTER SYSTEM OR LOSS OF DATA. NO ADVICE OR 59 | INFORMATION, WHETHER ORAL OR WRITTEN, OBTAINED BY YOU FROM US OR THROUGH OR 60 | FROM THE LICENSED MATERIALS WILL CREATE ANY WARRANTY OR CONDITION NOT 61 | EXPRESSLY STATED IN THESE TERMS. 62 | 63 | 5. Limitation of Liability. LINKEDIN SHALL NOT BE LIABLE FOR ANY INDIRECT, 64 | INCIDENTAL, SPECIAL, PUNITIVE, CONSEQUENTIAL OR EXEMPLARY DAMAGES, INCLUDING 65 | BUT NOT LIMITED TO, DAMAGES FOR LOSS OF PROFITS, GOODWILL, USE, DATA OR OTHER 66 | INTANGIBLE LOSSES . IN NO EVENT WILL LINKEDIN'S AGGREGATE LIABILITY TO YOU 67 | EXCEED $100. THIS LIMITATION OF LIABILITY SHALL: 68 | - i. APPLY REGARDLESS OF WHETHER (A) YOU BASE YOUR CLAIM ON CONTRACT, TORT, 69 | STATUTE, OR ANY OTHER LEGAL THEORY, (B) WE KNEW OR SHOULD HAVE KNOWN ABOUT 70 | THE POSSIBILITY OF SUCH DAMAGES, OR (C) THE LIMITED REMEDIES PROVIDED IN THIS 71 | SECTION FAIL OF THEIR ESSENTIAL PURPOSE; AND 72 | - ii. NOT APPLY TO ANY DAMAGE THAT LINKEDIN MAY CAUSE YOU INTENTIONALLY OR 73 | KNOWINGLY IN VIOLATION OF THESE TERMS OR APPLICABLE LAW, OR AS OTHERWISE 74 | MANDATED BY APPLICABLE LAW THAT CANNOT BE DISCLAIMED IN THESE TERMS. 75 | 76 | 6. Termination. This Agreement automatically terminates upon your breach of 77 | this Agreement or termination of your LinkedIn Learning subscription. On 78 | termination, all licenses granted under this Agreement will terminate 79 | immediately and you will delete the Licensed Materials. Sections 2-7 of this 80 | Agreement survive any termination of this Agreement. LinkedIn may discontinue 81 | the availability of some or all of the Licensed Materials at any time for any 82 | reason. 83 | 84 | 7. Miscellaneous. This Agreement will be governed by and construed in 85 | accordance with the laws of the State of California without regard to conflict 86 | of laws principles. The exclusive forum for any disputes arising out of or 87 | relating to this Agreement shall be an appropriate federal or state court 88 | sitting in the County of Santa Clara, State of California. If LinkedIn does 89 | not act to enforce a breach of this Agreement, that does not mean that 90 | LinkedIn has waived its right to enforce this Agreement. The Agreement does 91 | not create a partnership, agency relationship, or joint venture between the 92 | parties. Neither party has the power or authority to bind the other or to 93 | create any obligation or responsibility on behalf of the other. You may not, 94 | without LinkedIn’s prior written consent, assign or delegate any rights or 95 | obligations under these terms, including in connection with a change of 96 | control. Any purported assignment and delegation shall be ineffective. The 97 | Agreement shall bind and inure to the benefit of the parties, their respective 98 | successors and permitted assigns. If any provision of the Agreement is 99 | unenforceable, that provision will be modified to render it enforceable to the 100 | extent possible to give effect to the parties’ intentions and the remaining 101 | provisions will not be affected. This Agreement is the only agreement between 102 | you and LinkedIn regarding the Licensed Materials, and supersedes all prior 103 | agreements relating to the Licensed Materials. 104 | 105 | Last Updated: March 2019 106 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Copyright 2023 LinkedIn Corporation 2 | All Rights Reserved. 3 | 4 | Licensed under the LinkedIn Learning Exercise File License (the "License"). 5 | See LICENSE in the project root for license information. 6 | 7 | Please note, this project may automatically load third party code from external 8 | repositories (for example, NPM modules, Composer packages, or other dependencies). 9 | If so, such third party code may be subject to other license terms than as set 10 | forth above. In addition, such third party code may also depend on and load 11 | multiple tiers of dependencies. Please review the applicable licenses of the 12 | additional dependencies. 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ Design Patterns: Structural 2 | This is the repository for the LinkedIn Learning course C++ Design Patterns: Structural. The full course is available from [LinkedIn Learning][lil-course-url]. 3 | 4 | ![C++ Design Patterns: Structural][lil-thumbnail-url] 5 | 6 | Design patterns allow you to solve common software development problems in a reusable way, making your code more robust and scalable. In this course, Károly Nyisztor explores the structural design patterns from the Gang of Four. He shows you how to leverage these patterns in modern C++ code and how they can help you design more robust and scalable software. He also discusses the seven structural design patterns from the original design patterns book—adapter, bridge, composite, decorator, facade, flyweight, and proxy—and shows you how to implement each one in modern C++. For each pattern, he shows you when and how to use it, along with its benefits and drawbacks. 7 | Join Károly in this course to learn how to apply structural design patterns in your own code and bolster your development skills. 8 | 9 | ### Instructions 10 | 11 | 1. To use these exercise files, you must have the following installed: 12 | - [Visual Studio Code](https://code.visualstudio.com) 13 | - C/C++ for Visual Studio Code extension by Microsoft 14 | 2. Clone this repository into your local machine using the terminal (Mac), CMD (Windows), or a GUI tool like SourceTree. 15 | 16 | ### Instructor 17 | 18 | Károly Nyisztor 19 | 20 | Software Engineer, Author, Entrepreneur 21 | 22 | 23 | 24 | Check out my other courses on [LinkedIn Learning](https://www.linkedin.com/learning/instructors/karoly-nyisztor). 25 | 26 | [lil-course-url]: https://www.linkedin.com/learning/c-plus-plus-design-patterns-structural-22183029?dApp=59033956&leis=LAA 27 | [lil-thumbnail-url]: https://media.licdn.com/dms/image/D560DAQH8V651keIfeg/learning-public-crop_675_1200/0/1683141155361?e=2147483647&v=beta&t=kythrS8ITB5VMVbk9XM3rNLKga1p2LIi0kA-gYC2jzk 28 | --------------------------------------------------------------------------------