Add example of bridge pattern in C++
This commit is contained in:
parent
baabfa439a
commit
da9d26cf15
|
@ -0,0 +1,21 @@
|
|||
###############################################################################
|
||||
## Author: Shaun Reed ##
|
||||
## Legal: All Content (c) 2021 Shaun Reed, all rights reserved ##
|
||||
## About: A project for practicing the bridge C++ design pattern ##
|
||||
## ##
|
||||
## Contact: shaunrd0@gmail.com | URL: www.shaunreed.com | GitHub: shaunrd0 ##
|
||||
##############################################################################
|
||||
#
|
||||
cmake_minimum_required(VERSION 3.15)
|
||||
project(
|
||||
#[[NAME]] Bridge
|
||||
VERSION 1.0
|
||||
DESCRIPTION "An example of the bridge design pattern in C++"
|
||||
LANGUAGES CXX
|
||||
)
|
||||
add_compile_options("-Wall")
|
||||
|
||||
add_library(abstraction "abstraction.cpp")
|
||||
add_library(implementation "implementation.cpp")
|
||||
add_executable(bridge-test "main.cpp")
|
||||
target_link_libraries(bridge-test abstraction implementation)
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
#include "abstraction.hpp"
|
||||
|
||||
int VerboseCalculator::doMath(const int &a, const int &b) const
|
||||
{
|
||||
int result = method->math(a, b);
|
||||
std::cout << "Performing " << method->getName() << " on input: a = " << a
|
||||
<< ", b = " << b << std::endl << "Result: " << result << std::endl;
|
||||
return result;
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
|
||||
#ifndef ABSTRACTION_HPP
|
||||
#define ABSTRACTION_HPP
|
||||
|
||||
#include <iostream>
|
||||
#include <utility>
|
||||
|
||||
#include "implementation.hpp"
|
||||
|
||||
// Abstract calculator that can only perform one operation
|
||||
class Calculator {
|
||||
public:
|
||||
explicit Calculator(Implementation *method_) : method(method_) {}
|
||||
|
||||
virtual std::string getName() const = 0;
|
||||
virtual int doMath(const int &a, const int &b) const = 0;
|
||||
|
||||
protected:
|
||||
Implementation *method;
|
||||
};
|
||||
|
||||
// Simple calculator that can only perform one operation
|
||||
class SimpleCalculator : public Calculator {
|
||||
public:
|
||||
explicit SimpleCalculator(Implementation *method_) : Calculator(method_) {}
|
||||
|
||||
inline std::string getName() const override { return method->getName();}
|
||||
inline int doMath(const int &a, const int &b) const override { return method->math(a, b);}
|
||||
};
|
||||
|
||||
// Verbose calculator that prints information on method performed and result
|
||||
class VerboseCalculator : public Calculator {
|
||||
public:
|
||||
explicit VerboseCalculator(Implementation *method_) : Calculator(method_) {}
|
||||
|
||||
inline std::string getName() const override { return method->getName();}
|
||||
int doMath(const int &a, const int &b) const override;
|
||||
};
|
||||
|
||||
#endif // ABSTRACTION_HPP
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
#include "implementation.hpp"
|
|
@ -0,0 +1,38 @@
|
|||
|
||||
#ifndef IMPLEMENTATION_HPP
|
||||
#define IMPLEMENTATION_HPP
|
||||
|
||||
#include <string>
|
||||
|
||||
// Base class for abstract implementation
|
||||
class Implementation {
|
||||
public:
|
||||
explicit Implementation(std::string name_) : name(std::move(name_)) {}
|
||||
virtual ~Implementation() = default;
|
||||
|
||||
inline std::string getName() const { return name;}
|
||||
|
||||
virtual int math(const int &a, const int &b) const = 0;
|
||||
|
||||
private:
|
||||
std::string name;
|
||||
};
|
||||
|
||||
// Concrete Add implementation
|
||||
class Add : public Implementation {
|
||||
public:
|
||||
explicit Add(std::string name_="Add") :
|
||||
Implementation(std::move(name_)) {}
|
||||
|
||||
inline int math(const int &a, const int &b) const override { return a + b;}
|
||||
};
|
||||
|
||||
// Concrete Subtract implementation
|
||||
class Subtract : public Implementation {
|
||||
public:
|
||||
explicit Subtract(std::string name_="Subtract") :
|
||||
Implementation(std::move(name_)) {}
|
||||
inline int math(const int &a, const int &b) const override { return a - b;}
|
||||
};
|
||||
|
||||
#endif // IMPLEMENTATION_HPP
|
|
@ -0,0 +1,28 @@
|
|||
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
#include "abstraction.hpp"
|
||||
#include "implementation.hpp"
|
||||
|
||||
int main(const int argc, const char * argv[]) {
|
||||
Add *add = new Add;
|
||||
SimpleCalculator adder(add);
|
||||
const int a = 5;
|
||||
const int b = 10;
|
||||
std::cout << "A = " << a << "\nB = " << b << std::endl;
|
||||
|
||||
std::cout << "\nTesting " << adder.getName() << ".doMath(a, b)...\n";
|
||||
std::cout << "Result: " << adder.doMath(a, b);
|
||||
|
||||
auto *sub = new Subtract;
|
||||
SimpleCalculator subtractor(sub);
|
||||
std::cout << "\n\nTesting " << subtractor.getName() << ".doMath(a, b)...\n";
|
||||
std::cout << "Result: " << subtractor.doMath(a, b);
|
||||
|
||||
// Testing extension of abstraction
|
||||
std::cout << "\n\nTesting VerboseCalculator.doMath(a, b)...\n";
|
||||
VerboseCalculator verboseCalculator(add);
|
||||
verboseCalculator.doMath(a, b);
|
||||
}
|
Loading…
Reference in New Issue