Skip to content

Commit

Permalink
Factory Method Design Pattern Examples Push
Browse files Browse the repository at this point in the history
  • Loading branch information
rokel committed Dec 10, 2024
1 parent 9b817d5 commit 3b20fbf
Show file tree
Hide file tree
Showing 26 changed files with 389 additions and 236 deletions.
19 changes: 16 additions & 3 deletions Design-Patterns/Creational-Patterns/factory-method/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
# Create an executable for the pattern
add_executable(factory-method_main main.cpp "example1/Document.h" "example1/PDFDocument.h" "example1/Client.h" "example1/AdobeAcrobatClient.h" "example2/Burger.h" "example2/BeefBurger.h" "example2/VeggieBurger.h" "example2/Restaurant.h" "example2/BeefRestaurant.h" "example2/VeggieRestaurant.h" "example3/Message.h" "example3/TextMessage.h" "example3/JsonMessage.h" "example3/MessageFactory.h" "example3/JsonMessageFactory.h" "example3/TextMessageFactory.h")
#add_executable(factory-method_main main.cpp "example1/Document.h" "example1/PDFDocument.h" "example1/Client.h" "example1/AdobeAcrobatClient.h" "example2/Burger.h" "example2/BeefBurger.h" "example2/VeggieBurger.h" "example2/Restaurant.h" "example2/BeefRestaurant.h" "example2/VeggieRestaurant.h" "example3/Message.h" "example3/TextMessage.h" "example3/JsonMessage.h" "example3/MessageFactory.h" "example3/JsonMessageFactory.h" "example3/TextMessageFactory.h" "example4/Manipulator.h" "example4/LineManipulator.h" "example4/TextManipulator.h" "example4/TextManipulator.cpp" "example4/LineManipulator.cpp" "example4/Figure.h" "example4/LineFigure.h")

# Include directories if needed
target_include_directories(factory-method_main PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

# Define the executable
add_executable(factory-method_main
main.cpp
"example4/TextManipulator.cpp"
"example4/LineManipulator.cpp"
)
# Include directories for the headers
target_include_directories(factory-method_main PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
"example1"
"example2"
"example3"
"example4"
)

#add sub directories
add_subdirectory("example1")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include "Manipulator.h"
#include <memory>
//abstract creator(factory)
//Figure == ManipulatorFactory
class Figure {
protected:
virtual std::unique_ptr<Manipulator> createManipulator() = 0;
public:
virtual ~Figure() = default;
std::unique_ptr<Manipulator> getManipulator() {
auto figure = createManipulator();
return figure;
}

};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "Figure.h"
#include "LineManipulator.h"

//concrete factory
// LineFigure == LineManipulatorFactory
class LineFigure : public Figure {
public:
std::unique_ptr<Manipulator> createManipulator() override {
return std::make_unique<LineManipulator>();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "LineManipulator.h"
#include <iostream>

LineManipulator::LineManipulator(int width, const std::string& color)
: lineWidth(width), lineColor(color) {
}
//implementing pure virtual methods that we override from parent
void LineManipulator::downClick() {
std::cout << "LineManipulator: downClick called with line width = "
<< lineWidth << ", color = " << lineColor << std::endl;
}
void LineManipulator::upClick() {
std::cout << "LineManipulator: upClick called with line width = "
<< lineWidth << ", color = " << lineColor << std::endl;
}
void LineManipulator::drag() {
std::cout << "LineManipulator: drag called with line width = "
<< lineWidth << ", color = " << lineColor << std::endl;
}
//getter setters
int LineManipulator::getLineWidth() const {
return lineWidth;
}
void LineManipulator::setLineWidth(int width) {
lineWidth = width;
}
std::string LineManipulator::getLineColor() const {
return lineColor;
}
void LineManipulator::setLineColor(const std::string& color) {
lineColor = color;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "Manipulator.h"
#include <string>

//concrete product
class LineManipulator : public Manipulator {
private:
int lineWidth;
std::string lineColor;
public:
LineManipulator() : lineWidth(1), lineColor("black") {}
LineManipulator(int width, const std::string& color);

void downClick() override;
void drag() override;
void upClick() override;

int getLineWidth() const;
std::string getLineColor() const;
void setLineWidth(int width);
void setLineColor(const std::string& color);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <iostream>
//abstract product
class Manipulator {
public:
virtual ~Manipulator() = default;
virtual void downClick() = 0;
virtual void drag() = 0;
virtual void upClick() = 0;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include "Figure.h"
#include "TextManipulator.h"

//concrete factory
// TextFigure == LineManipulatorFactory
class TextFigure : public Figure {
public:
std::unique_ptr<Manipulator> createManipulator() override {
return std::make_unique<TextManipulator>();
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "TextManipulator.h"
#include <iostream>

// Constructor
TextManipulator::TextManipulator(int width, const std::string& color)
: textWidth(width), textColor(color) {
}

//implementing pure virtual methods that we override from parent
void TextManipulator::downClick() {
std::cout << "TextManipulator: downClick called with text width = "
<< textWidth << ", color = " << textColor << std::endl;
}
void TextManipulator::drag() {
std::cout << "TextManipulator: drag called with text width = "
<< textWidth << ", color = " << textColor << std::endl;
}
void TextManipulator::upClick() {
std::cout << "TextManipulator: upClick called with text width = "
<< textWidth << ", color = " << textColor << std::endl;
}

//getter setter
int TextManipulator::getTextWidth() const {
return textWidth;
}

std::string TextManipulator::getTextColor() const {
return textColor;
}
void TextManipulator::setTextWidth(int width) {
textWidth = width;
}

void TextManipulator::setTextColor(const std::string& color) {
textColor = color;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include "Manipulator.h"
#include <string>

//concrete product
class TextManipulator : public Manipulator {
private:
int textWidth;
std::string textColor;
public:
TextManipulator() : textWidth(5), textColor("black") {}
TextManipulator(int width, const std::string& color);

void downClick() override;
void drag() override;
void upClick() override;

int getTextWidth() const;
std::string getTextColor() const;
void setTextWidth(int width);
void setTextColor(const std::string& color);
};
25 changes: 25 additions & 0 deletions Design-Patterns/Creational-Patterns/factory-method/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include <example3/MessageFactory.h>
#include <example3/TextMessageFactory.h>
#include <example3/JsonMessageFactory.h>
#include <example4/Figure.h>
#include <example4/TextFigure.h>
#include <example4/LineFigure.h>

int main() {

Expand Down Expand Up @@ -40,6 +43,28 @@ int main() {
std::unique_ptr<Message> jsonMsg = jsonFactory->getMessage();
std::cout << "JSON Message Content: " << jsonMsg->getContent() << std::endl;

std::cout << "--------------" << std::endl;
std::cout << "--Example 4 ->" << std::endl;
std::cout << "--------------" << std::endl;

//ex4
std::unique_ptr<Figure> textFigure = std::make_unique<TextFigure>(); //Factory for TextManipulator
std::unique_ptr<Manipulator> textManipulator = textFigure->getManipulator();
textManipulator->downClick();
textManipulator->upClick();
textManipulator->drag();

std::unique_ptr<Figure> lineFigure = std::make_unique<LineFigure>(); //Factory for TextManipulator
std::unique_ptr<Manipulator> lineManipulator = lineFigure->getManipulator();
lineManipulator->downClick();
lineManipulator->upClick();
lineManipulator->drag();

std::cout << "--------------" << std::endl;
std::cout << "--Example 5 ->" << std::endl;
std::cout << "--------------" << std::endl;

//ex5

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@
{
"directoryIndex" : 3,
"id" : "factory-method_main::@6fe980a8ea798b5fefc6",
"jsonFile" : "target-factory-method_main-Debug-8f7fb5ba4e87ac899890.json",
"jsonFile" : "target-factory-method_main-Debug-d8aae8f1d699f2ad1e35.json",
"name" : "factory-method_main",
"projectIndex" : 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"objects" :
[
{
"jsonFile" : "codemodel-v2-89ff72fdb5ac853beca2.json",
"jsonFile" : "codemodel-v2-aa40aafc7b0a6c8fbc21.json",
"kind" : "codemodel",
"version" :
{
Expand Down Expand Up @@ -108,7 +108,7 @@
}
},
{
"jsonFile" : "codemodel-v2-89ff72fdb5ac853beca2.json",
"jsonFile" : "codemodel-v2-aa40aafc7b0a6c8fbc21.json",
"kind" : "codemodel",
"version" :
{
Expand Down
Loading

0 comments on commit 3b20fbf

Please sign in to comment.