-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Factory Method Design Pattern Examples Push
- Loading branch information
rokel
committed
Dec 10, 2024
1 parent
9b817d5
commit 3b20fbf
Showing
26 changed files
with
389 additions
and
236 deletions.
There are no files selected for viewing
19 changes: 16 additions & 3 deletions
19
Design-Patterns/Creational-Patterns/factory-method/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
Design-Patterns/Creational-Patterns/factory-method/example4/Figure.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
}; |
12 changes: 12 additions & 0 deletions
12
Design-Patterns/Creational-Patterns/factory-method/example4/LineFigure.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
}; |
32 changes: 32 additions & 0 deletions
32
Design-Patterns/Creational-Patterns/factory-method/example4/LineManipulator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 22 additions & 0 deletions
22
Design-Patterns/Creational-Patterns/factory-method/example4/LineManipulator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
10 changes: 10 additions & 0 deletions
10
Design-Patterns/Creational-Patterns/factory-method/example4/Manipulator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
12 changes: 12 additions & 0 deletions
12
Design-Patterns/Creational-Patterns/factory-method/example4/TextFigure.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
}; |
37 changes: 37 additions & 0 deletions
37
Design-Patterns/Creational-Patterns/factory-method/example4/TextManipulator.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
22 changes: 22 additions & 0 deletions
22
Design-Patterns/Creational-Patterns/factory-method/example4/TextManipulator.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.