From 912cb47dcf197e25c9b7bb40124a05ff3b097a65 Mon Sep 17 00:00:00 2001 From: Shaun Reed Date: Tue, 11 May 2021 16:57:21 -0400 Subject: [PATCH] Add example of adapter pattern in C++ --- cpp/patterns/CMakeLists.txt | 4 ++++ cpp/patterns/adapter/CMakeLists.txt | 20 ++++++++++++++++ cpp/patterns/adapter/adapter.cpp | 2 ++ cpp/patterns/adapter/adapter.hpp | 37 +++++++++++++++++++++++++++++ cpp/patterns/adapter/main.cpp | 20 ++++++++++++++++ 5 files changed, 83 insertions(+) create mode 100644 cpp/patterns/adapter/CMakeLists.txt create mode 100644 cpp/patterns/adapter/adapter.cpp create mode 100644 cpp/patterns/adapter/adapter.hpp create mode 100644 cpp/patterns/adapter/main.cpp diff --git a/cpp/patterns/CMakeLists.txt b/cpp/patterns/CMakeLists.txt index f0d98e8..d2f4bde 100644 --- a/cpp/patterns/CMakeLists.txt +++ b/cpp/patterns/CMakeLists.txt @@ -16,3 +16,7 @@ project( ) add_subdirectory(singleton) +add_subdirectory(adapter) +add_subdirectory(bridge) +add_subdirectory(factory) +add_subdirectory(abstract-factory) diff --git a/cpp/patterns/adapter/CMakeLists.txt b/cpp/patterns/adapter/CMakeLists.txt new file mode 100644 index 0000000..9f9cd6a --- /dev/null +++ b/cpp/patterns/adapter/CMakeLists.txt @@ -0,0 +1,20 @@ +############################################################################### +## Author: Shaun Reed ## +## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ## +## About: A project for practicing the adapter C++ design pattern ## +## ## +## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ## +############################################################################## +# +cmake_minimum_required(VERSION 3.15) +project( + #[[NAME]] Adapter + VERSION 1.0 + DESCRIPTION "An example of the adapter design pattern in C++" + LANGUAGES CXX +) +add_compile_options("-Wall") + +add_library(adapter "adapter.cpp") +add_executable(adapter-test "main.cpp") +target_link_libraries(adapter-test adapter) diff --git a/cpp/patterns/adapter/adapter.cpp b/cpp/patterns/adapter/adapter.cpp new file mode 100644 index 0000000..e95c6ad --- /dev/null +++ b/cpp/patterns/adapter/adapter.cpp @@ -0,0 +1,2 @@ + +#include "adapter.hpp" diff --git a/cpp/patterns/adapter/adapter.hpp b/cpp/patterns/adapter/adapter.hpp new file mode 100644 index 0000000..c97da31 --- /dev/null +++ b/cpp/patterns/adapter/adapter.hpp @@ -0,0 +1,37 @@ + +#ifndef ADAPTER_HPP +#define ADAPTER_HPP + +#include + +// Target implementation to adapt to a new interface +class Add { +public: + virtual ~Add() = default; + + virtual int doMath(const int &a, const int &b) const { return a + b;} +}; + +// An adaptee with some useful behavior to adapt to our Add target +class RandomNumber { +public: + explicit RandomNumber(const int &max) : maxNum(max) { srand(time(nullptr));} + + int getRandom() const { return rand() % maxNum;} + +private: + int maxNum; +}; + +// An adapter that combines behaviors of Add and RandomNumber +class RandomAddAdapter : public Add { +public: + explicit RandomAddAdapter(RandomNumber *random_) : random(random_) {} + + int doMath(const int &a, const int &b) const override { return a + b + random->getRandom();} + +private: + RandomNumber *random; +}; + +#endif // ADAPTER_HPP diff --git a/cpp/patterns/adapter/main.cpp b/cpp/patterns/adapter/main.cpp new file mode 100644 index 0000000..3c3ca31 --- /dev/null +++ b/cpp/patterns/adapter/main.cpp @@ -0,0 +1,20 @@ + +#include + +#include "adapter.hpp" + +int main(const int argc, const char * argv[]) { + const int a = 5; + const int b = 10; + std::cout << "A = " << a << "\nB = " << b << std::endl; + + // Testing target implementation + Add adder; + std::cout << "Adding a + b: " << adder.doMath(a, b) << std::endl; + + // Testing RandomAddAdapter using RandomNumber as the adaptee + RandomNumber *adaptee = new RandomNumber(100); + RandomAddAdapter *adapter = new RandomAddAdapter(adaptee); + std::cout << "\nAdding a + b + RandomNumber: " << adapter->doMath(a, b) + << std::endl; +}