4 | #include "creational_patterns/abstract_factory/table_factory/TableData.h"
5 | #include "creational_patterns/abstract_factory/factory/Item.h"
6 |
7 | using namespace std;
8 |
9 | // ˄
10 |
11 | TableData::TableData(const string& name)
12 | // ˅
13 | : Data(name)
14 | // ˄
15 | {
16 | // ˅
17 |
18 | // ˄
19 | }
20 |
21 | TableData::~TableData()
22 | {
23 | // ˅
24 |
25 | // ˄
26 | }
27 |
28 | const string TableData::toHTML() const
29 | {
30 | // ˅
31 | stringstream buffer;
32 | buffer << "" << endl;
33 | buffer << "| " << name << " | " << endl;
34 | buffer << "" << endl;
35 | for (Item* item : items) {
36 | buffer << item->toHTML();
37 | }
38 | buffer << " " << endl;
39 | buffer << " | " << endl;
40 | return buffer.str();
41 | // ˄
42 | }
43 |
44 | // ˅
45 |
46 | // ˄
47 |
--------------------------------------------------------------------------------
/structural_patterns/flyweight/LargeSizeString.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef STRUCTURAL_PATTERNS_FLYWEIGHT_LARGESIZESTRING_H_
6 | #define STRUCTURAL_PATTERNS_FLYWEIGHT_LARGESIZESTRING_H_
7 |
8 | // ˅
9 | #include
10 | #include
11 |
12 | class LargeSizeChar;
13 |
14 | using namespace std;
15 |
16 | // ˄
17 |
18 | class LargeSizeString
19 | {
20 | // ˅
21 |
22 | // ˄
23 |
24 | private:
25 |
26 | vector large_size_chars;
27 |
28 | public:
29 |
30 | LargeSizeString(const string& string_data);
31 |
32 | ~LargeSizeString();
33 |
34 | void display() const;
35 |
36 | // ˅
37 | public:
38 |
39 | protected:
40 |
41 | private:
42 | LargeSizeString(const LargeSizeString&) = delete;
43 | LargeSizeString& operator=(const LargeSizeString&) = delete;
44 | LargeSizeString(LargeSizeString&&) = delete;
45 | LargeSizeString& operator=(LargeSizeString&&) = delete;
46 |
47 | // ˄
48 | };
49 |
50 | // ˅
51 |
52 | // ˄
53 |
54 | #endif // STRUCTURAL_PATTERNS_FLYWEIGHT_LARGESIZESTRING_H_
55 |
56 | // ˅
57 |
58 | // ˄
59 |
--------------------------------------------------------------------------------
/behavioral_patterns/command/HistoryCommand.cpp:
--------------------------------------------------------------------------------
1 | // ˅
2 | #include "behavioral_patterns/command/HistoryCommand.h"
3 |
4 |
5 | // ˄
6 |
7 | HistoryCommand::HistoryCommand()
8 | // ˅
9 |
10 | // ˄
11 | {
12 | // ˅
13 |
14 | // ˄
15 | }
16 |
17 | HistoryCommand::~HistoryCommand()
18 | {
19 | // ˅
20 | clear();
21 | // ˄
22 | }
23 |
24 | void HistoryCommand::execute() const
25 | {
26 | // ˅
27 | for (Command* past_command : past_commands) {
28 | past_command->execute();
29 | }
30 | // ˄
31 | }
32 |
33 | void HistoryCommand::add(Command* cmd)
34 | {
35 | // ˅
36 | past_commands.push_back(cmd);
37 | // ˄
38 | }
39 |
40 | void HistoryCommand::undo()
41 | {
42 | // ˅
43 | if (past_commands.size() > 0) {
44 | Command* last_command = past_commands.back();
45 | past_commands.pop_back();
46 | delete last_command;
47 | }
48 | // ˄
49 | }
50 |
51 | void HistoryCommand::clear()
52 | {
53 | // ˅
54 | for (Command* past_command : past_commands) {
55 | delete past_command;
56 | }
57 | past_commands.clear();
58 | // ˄
59 | }
60 |
61 | // ˅
62 |
63 | // ˄
64 |
--------------------------------------------------------------------------------
/behavioral_patterns/iterator/BookShelf.cpp:
--------------------------------------------------------------------------------
1 | // ˅
2 | #include "behavioral_patterns/iterator/Book.h"
3 | #include "behavioral_patterns/iterator/BookShelf.h"
4 | #include "behavioral_patterns/iterator/Iterator.h"
5 | #include "behavioral_patterns/iterator/BookShelfIterator.h"
6 |
7 | // ˄
8 |
9 | BookShelf::BookShelf(const int max_size)
10 | : number_of_books(0)
11 | , books(max_size)
12 | // ˅
13 |
14 | // ˄
15 | {
16 | // ˅
17 |
18 | // ˄
19 | }
20 |
21 | BookShelf::~BookShelf()
22 | {
23 | // ˅
24 |
25 | // ˄
26 | }
27 |
28 | Iterator* BookShelf::iterator() const
29 | {
30 | // ˅
31 | return new BookShelfIterator(this);
32 | // ˄
33 | }
34 |
35 | Book* BookShelf::getAt(const int index) const
36 | {
37 | // ˅
38 | return books.at(index);
39 | // ˄
40 | }
41 |
42 | void BookShelf::add(Book* book)
43 | {
44 | // ˅
45 | books.insert(begin(books) + number_of_books, book);
46 | ++number_of_books;
47 | // ˄
48 | }
49 |
50 | int BookShelf::getNumberOfBooks() const
51 | {
52 | // ˅
53 | return number_of_books;
54 | // ˄
55 | }
56 |
57 | // ˅
58 |
59 | // ˄
60 |
--------------------------------------------------------------------------------
/creational_patterns/prototype/FrameDisplay.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef CREATIONAL_PATTERNS_PROTOTYPE_FRAMEDISPLAY_H_
6 | #define CREATIONAL_PATTERNS_PROTOTYPE_FRAMEDISPLAY_H_
7 |
8 | // ˅
9 | #include
10 | #include "creational_patterns/prototype/framework/Display.h"
11 |
12 | using namespace std;
13 |
14 | // ˄
15 |
16 | class FrameDisplay : public Display
17 | {
18 | // ˅
19 |
20 | // ˄
21 |
22 | private:
23 |
24 | const char border_char;
25 |
26 | public:
27 |
28 | FrameDisplay(const char border_char);
29 |
30 | ~FrameDisplay();
31 |
32 | Display* clone();
33 |
34 | void show(const string& message) const;
35 |
36 | // ˅
37 | public:
38 |
39 | protected:
40 |
41 | private:
42 | FrameDisplay(const FrameDisplay&) = delete;
43 | FrameDisplay& operator=(const FrameDisplay&) = delete;
44 | FrameDisplay(FrameDisplay&&) = delete;
45 | FrameDisplay& operator=(FrameDisplay&&) = delete;
46 |
47 | // ˄
48 | };
49 |
50 | // ˅
51 |
52 | // ˄
53 |
54 | #endif // CREATIONAL_PATTERNS_PROTOTYPE_FRAMEDISPLAY_H_
55 |
56 | // ˅
57 |
58 | // ˄
59 |
--------------------------------------------------------------------------------
/behavioral_patterns/interpreter/Context.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_INTERPRETER_CONTEXT_H_
6 | #define BEHAVIORAL_PATTERNS_INTERPRETER_CONTEXT_H_
7 |
8 | // ˅
9 | #include
10 | #include
11 |
12 | using namespace std;
13 |
14 | // ˄
15 |
16 | // Hold data which will be interpreted.
17 | class Context
18 | {
19 | // ˅
20 |
21 | // ˄
22 |
23 | private:
24 |
25 | vector tokens;
26 |
27 | vector::iterator iter;
28 |
29 | public:
30 |
31 | Context(const string& text);
32 |
33 | ~Context();
34 |
35 | string nextToken();
36 |
37 | string getToken();
38 |
39 | void slideToken(const string& token);
40 |
41 | const int getNumber();
42 |
43 | // ˅
44 | public:
45 |
46 | protected:
47 |
48 | private:
49 | Context(const Context&) = delete;
50 | Context& operator=(const Context&) = delete;
51 | Context(Context&&) = delete;
52 | Context& operator=(Context&&) = delete;
53 |
54 | // ˄
55 | };
56 |
57 | // ˅
58 |
59 | // ˄
60 |
61 | #endif // BEHAVIORAL_PATTERNS_INTERPRETER_CONTEXT_H_
62 |
63 | // ˅
64 |
65 | // ˄
66 |
--------------------------------------------------------------------------------
/behavioral_patterns/mediator/ColleagueRadioButton.cpp:
--------------------------------------------------------------------------------
1 | // ˅
2 | #include "behavioral_patterns/mediator/ColleagueRadioButton.h"
3 | #include "behavioral_patterns/mediator/CLIWrapper.h"
4 |
5 | using namespace System;
6 |
7 | // ˄
8 |
9 | ColleagueRadioButton::ColleagueRadioButton(const msclr::gcroot radio_button, Mediator* mediator)
10 | : radio_button(radio_button)
11 | // ˅
12 | , Colleague(mediator)
13 | // ˄
14 | {
15 | // ˅
16 | // Wrapper class for calling unmanaged code from managed code
17 | CLIWrapper^ cli_wrapper = gcnew CLIWrapper(mediator);
18 |
19 | radio_button->CheckedChanged += gcnew EventHandler(cli_wrapper, &CLIWrapper::colleagueChanged);
20 | // ˄
21 | }
22 |
23 | ColleagueRadioButton::~ColleagueRadioButton()
24 | {
25 | // ˅
26 |
27 | // ˄
28 | }
29 |
30 | void ColleagueRadioButton::setActivation(const bool is_enable)
31 | {
32 | // ˅
33 | radio_button->Enabled = is_enable;
34 | // ˄
35 | }
36 |
37 | bool ColleagueRadioButton::isSelected() const
38 | {
39 | // ˅
40 | return radio_button->Checked;
41 | // ˄
42 | }
43 |
44 | // ˅
45 |
46 | // ˄
47 |
--------------------------------------------------------------------------------
/behavioral_patterns/observer/DigitObserver.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_OBSERVER_DIGITOBSERVER_H_
6 | #define BEHAVIORAL_PATTERNS_OBSERVER_DIGITOBSERVER_H_
7 |
8 | // ˅
9 | #include "behavioral_patterns/observer/Observer.h"
10 |
11 | class NumberSubject;
12 |
13 | // ˄
14 |
15 | // Display values as a number.
16 | class DigitObserver : public Observer
17 | {
18 | // ˅
19 |
20 | // ˄
21 |
22 | private:
23 |
24 | const NumberSubject* numberSubject;
25 |
26 | public:
27 |
28 | DigitObserver(const NumberSubject* numberSubject);
29 |
30 | ~DigitObserver();
31 |
32 | void update(const Subject* changedSubject) const;
33 |
34 | // ˅
35 | public:
36 |
37 | protected:
38 |
39 | private:
40 | DigitObserver(const DigitObserver&) = delete;
41 | DigitObserver& operator=(const DigitObserver&) = delete;
42 | DigitObserver(DigitObserver&&) = delete;
43 | DigitObserver& operator=(DigitObserver&&) = delete;
44 |
45 | // ˄
46 | };
47 |
48 | // ˅
49 |
50 | // ˄
51 |
52 | #endif // BEHAVIORAL_PATTERNS_OBSERVER_DIGITOBSERVER_H_
53 |
54 | // ˅
55 |
56 | // ˄
57 |
--------------------------------------------------------------------------------
/behavioral_patterns/visitor/File.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_VISITOR_FILE_H_
6 | #define BEHAVIORAL_PATTERNS_VISITOR_FILE_H_
7 |
8 | // ˅
9 | #include
10 | #include "behavioral_patterns/visitor/FileSystemElement.h"
11 |
12 | class Visitor;
13 |
14 | using namespace std;
15 |
16 | // ˄
17 |
18 | class File : public FileSystemElement
19 | {
20 | // ˅
21 |
22 | // ˄
23 |
24 | private:
25 |
26 | const string name;
27 |
28 | const int size;
29 |
30 | public:
31 |
32 | File(const string& name, const int size);
33 |
34 | ~File();
35 |
36 | void accept(Visitor* visitor);
37 |
38 | // File name
39 | const string getName() const;
40 |
41 | // File size
42 | const int getSize() const;
43 |
44 | // ˅
45 | public:
46 |
47 | protected:
48 |
49 | private:
50 | File(const File&) = delete;
51 | File& operator=(const File&) = delete;
52 | File(File&&) = delete;
53 | File& operator=(File&&) = delete;
54 |
55 | // ˄
56 | };
57 |
58 | // ˅
59 |
60 | // ˄
61 |
62 | #endif // BEHAVIORAL_PATTERNS_VISITOR_FILE_H_
63 |
64 | // ˅
65 |
66 | // ˄
67 |
--------------------------------------------------------------------------------
/creational_patterns/singleton/Singleton.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef CREATIONAL_PATTERNS_SINGLETON_SINGLETON_H_
6 | #define CREATIONAL_PATTERNS_SINGLETON_SINGLETON_H_
7 |
8 | // ˅
9 |
10 | // ˄
11 |
12 | // Singleton ( based on the example code on Wikipedia )
13 | // https://en.wikipedia.org/w/index.php?title=Singleton_pattern&oldid=1115882454#C++
14 | // Note: The latest Wikipedia page has had the C++ code example removed. This link is to the page before it was removed.
15 | class Singleton
16 | {
17 | // ˅
18 |
19 | // ˄
20 |
21 | public:
22 |
23 | static Singleton* getInstance();
24 |
25 | private:
26 |
27 | Singleton();
28 |
29 | public:
30 |
31 | ~Singleton();
32 |
33 | // ˅
34 | public:
35 |
36 | protected:
37 |
38 | private:
39 | Singleton(const Singleton&) = delete;
40 | Singleton& operator=(const Singleton&) = delete;
41 | Singleton(Singleton&&) = delete;
42 | Singleton& operator=(Singleton&&) = delete;
43 |
44 | // ˄
45 | };
46 |
47 | // ˅
48 |
49 | // ˄
50 |
51 | #endif // CREATIONAL_PATTERNS_SINGLETON_SINGLETON_H_
52 |
53 | // ˅
54 |
55 | // ˄
56 |
--------------------------------------------------------------------------------
/behavioral_patterns/interpreter/Repeat.cpp:
--------------------------------------------------------------------------------
1 | // ˅
2 | #include
3 | #include "behavioral_patterns/interpreter/Repeat.h"
4 | #include "behavioral_patterns/interpreter/Context.h"
5 | #include "behavioral_patterns/interpreter/CommandList.h"
6 |
7 | // ˄
8 |
9 | Repeat::Repeat()
10 | : number(0)
11 | , command_list(nullptr)
12 | // ˅
13 |
14 | // ˄
15 | {
16 | // ˅
17 |
18 | // ˄
19 | }
20 |
21 | Repeat::~Repeat()
22 | {
23 | // ˅
24 | if (command_list != nullptr) {
25 | delete command_list;
26 | }
27 | // ˄
28 | }
29 |
30 | void Repeat::parse(Context* context)
31 | {
32 | // ˅
33 | context->slideToken("repeat");
34 |
35 | number = context->getNumber();
36 | context->slideToken(to_string(number));
37 |
38 | CommandList* a_command_list = new CommandList();
39 | a_command_list->parse(context);
40 |
41 | command_list = a_command_list; // Hold the parsed command list
42 | // ˄
43 | }
44 |
45 | const string Repeat::toString() const
46 | {
47 | // ˅
48 | return "repeat " + to_string(number) + " " + command_list->toString();
49 | // ˄
50 | }
51 |
52 | // ˅
53 |
54 | // ˄
55 |
--------------------------------------------------------------------------------
/behavioral_patterns/memento/Gamer.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_MEMENTO_GAMER_H_
6 | #define BEHAVIORAL_PATTERNS_MEMENTO_GAMER_H_
7 |
8 | // ˅
9 | #include
10 | #include
11 | #include
12 |
13 | class Memento;
14 |
15 | using namespace std;
16 |
17 | // ˄
18 |
19 | class Gamer
20 | {
21 | // ˅
22 |
23 | // ˄
24 |
25 | private:
26 |
27 | // Gamer's money
28 | int money;
29 |
30 | public:
31 |
32 | Gamer(const int money);
33 |
34 | // Get current status
35 | Memento* createMemento();
36 |
37 | // Undo status
38 | void setMemento(Memento* memento);
39 |
40 | // Play a game
41 | void play();
42 |
43 | int getMoney() const;
44 |
45 | const string toString() const;
46 |
47 | // ˅
48 | public:
49 |
50 | protected:
51 |
52 | private:
53 | Gamer(const Gamer&) = delete;
54 | Gamer& operator=(const Gamer&) = delete;
55 | Gamer(Gamer&&) = delete;
56 | Gamer& operator=(Gamer&&) = delete;
57 |
58 | // ˄
59 | };
60 |
61 | // ˅
62 |
63 | // ˄
64 |
65 | #endif // BEHAVIORAL_PATTERNS_MEMENTO_GAMER_H_
66 |
67 | // ˅
68 |
69 | // ˄
70 |
--------------------------------------------------------------------------------
/structural_patterns/composite/File.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef STRUCTURAL_PATTERNS_COMPOSITE_FILE_H_
6 | #define STRUCTURAL_PATTERNS_COMPOSITE_FILE_H_
7 |
8 | // ˅
9 | #include
10 | #include "structural_patterns/composite/FileSystemElement.h"
11 |
12 | using namespace std;
13 |
14 | // ˄
15 |
16 | class File : public FileSystemElement
17 | {
18 | // ˅
19 |
20 | // ˄
21 |
22 | private:
23 |
24 | const string name;
25 |
26 | const int size;
27 |
28 | public:
29 |
30 | File(const string& name, const int size);
31 |
32 | ~File();
33 |
34 | const string getName() const;
35 |
36 | const int getSize() const;
37 |
38 | // Print this element with the "upper_path".
39 | void print(const string& upper_path) const;
40 |
41 | // ˅
42 | public:
43 |
44 | protected:
45 |
46 | private:
47 | File(const File&) = delete;
48 | File& operator=(const File&) = delete;
49 | File(File&&) = delete;
50 | File& operator=(File&&) = delete;
51 |
52 | // ˄
53 | };
54 |
55 | // ˅
56 |
57 | // ˄
58 |
59 | #endif // STRUCTURAL_PATTERNS_COMPOSITE_FILE_H_
60 |
61 | // ˅
62 |
63 | // ˄
64 |
--------------------------------------------------------------------------------
/behavioral_patterns/iterator/BookShelfIterator.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_ITERATOR_BOOKSHELFITERATOR_H_
6 | #define BEHAVIORAL_PATTERNS_ITERATOR_BOOKSHELFITERATOR_H_
7 |
8 | // ˅
9 | #include "behavioral_patterns/iterator/Iterator.h"
10 |
11 | class BookShelf;
12 |
13 | // ˄
14 |
15 | class BookShelfIterator : public Iterator
16 | {
17 | // ˅
18 |
19 | // ˄
20 |
21 | private:
22 |
23 | int index;
24 |
25 | const BookShelf* book_shelf;
26 |
27 | public:
28 |
29 | BookShelfIterator(const BookShelf* book_shelf);
30 |
31 | ~BookShelfIterator();
32 |
33 | bool hasNext() const;
34 |
35 | void* next();
36 |
37 | // ˅
38 | public:
39 |
40 | protected:
41 |
42 | private:
43 | BookShelfIterator(const BookShelfIterator&) = delete;
44 | BookShelfIterator& operator=(const BookShelfIterator&) = delete;
45 | BookShelfIterator(BookShelfIterator&&) = delete;
46 | BookShelfIterator& operator=(BookShelfIterator&&) = delete;
47 |
48 | // ˄
49 | };
50 |
51 | // ˅
52 |
53 | // ˄
54 |
55 | #endif // BEHAVIORAL_PATTERNS_ITERATOR_BOOKSHELFITERATOR_H_
56 |
57 | // ˅
58 |
59 | // ˄
60 |
--------------------------------------------------------------------------------
/behavioral_patterns/chain_of_responsibility/LazySupporter.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_LAZYSUPPORTER_H_
6 | #define BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_LAZYSUPPORTER_H_
7 |
8 | // ˅
9 | #include
10 | #include "behavioral_patterns/chain_of_responsibility/Supporter.h"
11 |
12 | using namespace std;
13 |
14 | // ˄
15 |
16 | class LazySupporter : public Supporter
17 | {
18 | // ˅
19 |
20 | // ˄
21 |
22 | public:
23 |
24 | LazySupporter(const string& name);
25 |
26 | ~LazySupporter();
27 |
28 | protected:
29 |
30 | // No troubles are handled.
31 | bool canHandle(const Trouble* trouble) const;
32 |
33 | // ˅
34 | public:
35 |
36 | protected:
37 |
38 | private:
39 | LazySupporter(const LazySupporter&) = delete;
40 | LazySupporter& operator=(const LazySupporter&) = delete;
41 | LazySupporter(LazySupporter&&) = delete;
42 | LazySupporter& operator=(LazySupporter&&) = delete;
43 |
44 | // ˄
45 | };
46 |
47 | // ˅
48 |
49 | // ˄
50 |
51 | #endif // BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_LAZYSUPPORTER_H_
52 |
53 | // ˅
54 |
55 | // ˄
56 |
--------------------------------------------------------------------------------
/creational_patterns/prototype/prototype.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 | framework
9 |
10 |
11 | framework
12 |
13 |
14 |
15 |
16 |
17 |
18 | framework
19 |
20 |
21 | framework
22 |
23 |
24 |
25 |
26 | {8474984c-3f8f-4ebe-bbe4-69ddbf323cbe}
27 |
28 |
29 |
--------------------------------------------------------------------------------
/behavioral_patterns/strategy/RandomStrategy.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_STRATEGY_RANDOMSTRATEGY_H_
6 | #define BEHAVIORAL_PATTERNS_STRATEGY_RANDOMSTRATEGY_H_
7 |
8 | // ˅
9 | #include
10 | #include "behavioral_patterns/strategy/Strategy.h"
11 |
12 | class HandSignal;
13 |
14 | // ˄
15 |
16 | // Random Strategy: showing a random hand signal.
17 | class RandomStrategy : public Strategy
18 | {
19 | // ˅
20 |
21 | // ˄
22 |
23 | public:
24 |
25 | RandomStrategy();
26 |
27 | ~RandomStrategy();
28 |
29 | HandSignal* showHandSignal();
30 |
31 | void notifyGameResult(GameResultType result, HandSignal* ownHand, HandSignal* opponentsHand);
32 |
33 | // ˅
34 | public:
35 |
36 | protected:
37 |
38 | private:
39 | RandomStrategy(const RandomStrategy&) = delete;
40 | RandomStrategy& operator=(const RandomStrategy&) = delete;
41 | RandomStrategy(RandomStrategy&&) = delete;
42 | RandomStrategy& operator=(RandomStrategy&&) = delete;
43 |
44 | // ˄
45 | };
46 |
47 | // ˅
48 |
49 | // ˄
50 |
51 | #endif // BEHAVIORAL_PATTERNS_STRATEGY_RANDOMSTRATEGY_H_
52 |
53 | // ˅
54 |
55 | // ˄
56 |
--------------------------------------------------------------------------------
/creational_patterns/abstract_factory/table_factory/TablePage.cpp:
--------------------------------------------------------------------------------
1 | // ˅
2 | #include
3 | #include "creational_patterns/abstract_factory/table_factory/TablePage.h"
4 | #include "creational_patterns/abstract_factory/factory/Item.h"
5 |
6 | using namespace std;
7 |
8 | // ˄
9 |
10 | TablePage::TablePage(const string& title, const string& author)
11 | // ˅
12 | : Page(title, author)
13 | // ˄
14 | {
15 | // ˅
16 |
17 | // ˄
18 | }
19 |
20 | TablePage::~TablePage()
21 | {
22 | // ˅
23 |
24 | // ˄
25 | }
26 |
27 | const string TablePage::toHTML() const
28 | {
29 | // ˅
30 | stringstream buffer;
31 | buffer << "" << title << "" << endl;
32 | buffer << "" << title << "
" << endl;
33 | buffer << "" << endl;
34 | for (Item* content : contents) {
35 | buffer << "" << content->toHTML() << "
" << endl;
36 | }
37 | buffer << "
" << endl;
38 | buffer << "
" << author << "" << endl;
39 | buffer << "" << endl;
40 | return buffer.str();
41 | // ˄
42 | }
43 |
44 | // ˅
45 |
46 | // ˄
47 |
--------------------------------------------------------------------------------
/behavioral_patterns/observer/BarChartObserver.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_OBSERVER_BARCHARTOBSERVER_H_
6 | #define BEHAVIORAL_PATTERNS_OBSERVER_BARCHARTOBSERVER_H_
7 |
8 | // ˅
9 | #include "behavioral_patterns/observer/Observer.h"
10 |
11 | class NumberSubject;
12 |
13 | // ˄
14 |
15 | // Display values as a bar chart.
16 | class BarChartObserver : public Observer
17 | {
18 | // ˅
19 |
20 | // ˄
21 |
22 | private:
23 |
24 | const NumberSubject* numberSubject;
25 |
26 | public:
27 |
28 | BarChartObserver(const NumberSubject* numberSubject);
29 |
30 | ~BarChartObserver();
31 |
32 | void update(const Subject* changedSubject) const;
33 |
34 | // ˅
35 | public:
36 |
37 | protected:
38 |
39 | private:
40 | BarChartObserver(const BarChartObserver&) = delete;
41 | BarChartObserver& operator=(const BarChartObserver&) = delete;
42 | BarChartObserver(BarChartObserver&&) = delete;
43 | BarChartObserver& operator=(BarChartObserver&&) = delete;
44 |
45 | // ˄
46 | };
47 |
48 | // ˅
49 |
50 | // ˄
51 |
52 | #endif // BEHAVIORAL_PATTERNS_OBSERVER_BARCHARTOBSERVER_H_
53 |
54 | // ˅
55 |
56 | // ˄
57 |
--------------------------------------------------------------------------------
/creational_patterns/prototype/UnderlineDisplay.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef CREATIONAL_PATTERNS_PROTOTYPE_UNDERLINEDISPLAY_H_
6 | #define CREATIONAL_PATTERNS_PROTOTYPE_UNDERLINEDISPLAY_H_
7 |
8 | // ˅
9 | #include
10 | #include "creational_patterns/prototype/framework/Display.h"
11 |
12 | using namespace std;
13 |
14 | // ˄
15 |
16 | class UnderlineDisplay : public Display
17 | {
18 | // ˅
19 |
20 | // ˄
21 |
22 | private:
23 |
24 | const char underline_char;
25 |
26 | public:
27 |
28 | UnderlineDisplay(const char underline_char);
29 |
30 | ~UnderlineDisplay();
31 |
32 | Display* clone();
33 |
34 | void show(const string& message) const;
35 |
36 | // ˅
37 | public:
38 |
39 | protected:
40 |
41 | private:
42 | UnderlineDisplay(const UnderlineDisplay&) = delete;
43 | UnderlineDisplay& operator=(const UnderlineDisplay&) = delete;
44 | UnderlineDisplay(UnderlineDisplay&&) = delete;
45 | UnderlineDisplay& operator=(UnderlineDisplay&&) = delete;
46 |
47 | // ˄
48 | };
49 |
50 | // ˅
51 |
52 | // ˄
53 |
54 | #endif // CREATIONAL_PATTERNS_PROTOTYPE_UNDERLINEDISPLAY_H_
55 |
56 | // ˅
57 |
58 | // ˄
59 |
--------------------------------------------------------------------------------
/behavioral_patterns/chain_of_responsibility/MoodySupporter.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_MOODYSUPPORTER_H_
6 | #define BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_MOODYSUPPORTER_H_
7 |
8 | // ˅
9 | #include
10 | #include "behavioral_patterns/chain_of_responsibility/Supporter.h"
11 |
12 | using namespace std;
13 |
14 | // ˄
15 |
16 | class MoodySupporter : public Supporter
17 | {
18 | // ˅
19 |
20 | // ˄
21 |
22 | public:
23 |
24 | MoodySupporter(const string& name);
25 |
26 | ~MoodySupporter();
27 |
28 | protected:
29 |
30 | // Troubles with an odd ID are handled.
31 | bool canHandle(const Trouble* trouble) const;
32 |
33 | // ˅
34 | public:
35 |
36 | protected:
37 |
38 | private:
39 | MoodySupporter(const MoodySupporter&) = delete;
40 | MoodySupporter& operator=(const MoodySupporter&) = delete;
41 | MoodySupporter(MoodySupporter&&) = delete;
42 | MoodySupporter& operator=(MoodySupporter&&) = delete;
43 |
44 | // ˄
45 | };
46 |
47 | // ˅
48 |
49 | // ˄
50 |
51 | #endif // BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_MOODYSUPPORTER_H_
52 |
53 | // ˅
54 |
55 | // ˄
56 |
--------------------------------------------------------------------------------
/behavioral_patterns/visitor/ListVisitor.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_VISITOR_LISTVISITOR_H_
6 | #define BEHAVIORAL_PATTERNS_VISITOR_LISTVISITOR_H_
7 |
8 | // ˅
9 | #include
10 | #include "behavioral_patterns/visitor/Visitor.h"
11 |
12 | class File;
13 | class Directory;
14 |
15 | using namespace std;
16 |
17 | // ˄
18 |
19 | class ListVisitor : public Visitor
20 | {
21 | // ˅
22 |
23 | // ˄
24 |
25 | private:
26 |
27 | // Currently visited directory
28 | string current_directory;
29 |
30 | public:
31 |
32 | ListVisitor();
33 |
34 | ~ListVisitor();
35 |
36 | // Visit a file
37 | void visit(const File* file);
38 |
39 | // Visit a directory
40 | void visit(const Directory* directory);
41 |
42 | // ˅
43 | public:
44 |
45 | protected:
46 |
47 | private:
48 | ListVisitor(const ListVisitor&) = delete;
49 | ListVisitor& operator=(const ListVisitor&) = delete;
50 | ListVisitor(ListVisitor&&) = delete;
51 | ListVisitor& operator=(ListVisitor&&) = delete;
52 |
53 | // ˄
54 | };
55 |
56 | // ˅
57 |
58 | // ˄
59 |
60 | #endif // BEHAVIORAL_PATTERNS_VISITOR_LISTVISITOR_H_
61 |
62 | // ˅
63 |
64 | // ˄
65 |
--------------------------------------------------------------------------------
/structural_patterns/facade/DataLibrary.cpp:
--------------------------------------------------------------------------------
1 | // ˅
2 | #include
3 | #include
4 | #include
5 | #include "structural_patterns/facade/DataLibrary.h"
6 |
7 | using namespace std;
8 |
9 | // ˄
10 |
11 | DataLibrary* DataLibrary::getInstance()
12 | {
13 | // ˅
14 | static DataLibrary* instance = new DataLibrary();
15 | return instance;
16 | // ˄
17 | }
18 |
19 | DataLibrary::DataLibrary()
20 | // ˅
21 |
22 | // ˄
23 | {
24 | // ˅
25 |
26 | // ˄
27 | }
28 |
29 | DataLibrary::~DataLibrary()
30 | {
31 | // ˅
32 |
33 | // ˄
34 | }
35 |
36 | const map DataLibrary::getProperties(const string& data_library_name) const
37 | {
38 | // ˅
39 | map ret;
40 | ifstream ifs(data_library_name);
41 | if (ifs.is_open()) {
42 | string line;
43 | while (getline(ifs, line)) {
44 | string::size_type pos = line.find("=");
45 | if (pos != string::npos) {
46 | ret[line.substr(0, pos)] = line.substr(pos + 1);
47 | }
48 | }
49 | }
50 | else {
51 | cerr << "Failed to read file: " << data_library_name << endl;
52 | }
53 |
54 | return ret;
55 | // ˄
56 | }
57 |
58 | // ˅
59 |
60 | // ˄
61 |
--------------------------------------------------------------------------------
/behavioral_patterns/iterator/BookShelf.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef BEHAVIORAL_PATTERNS_ITERATOR_BOOKSHELF_H_
6 | #define BEHAVIORAL_PATTERNS_ITERATOR_BOOKSHELF_H_
7 |
8 | // ˅
9 | #include
10 | #include "behavioral_patterns/iterator/Aggregate.h"
11 |
12 | class Book;
13 | class Iterator;
14 |
15 | using namespace std;
16 |
17 | // ˄
18 |
19 | class BookShelf : public Aggregate
20 | {
21 | // ˅
22 |
23 | // ˄
24 |
25 | private:
26 |
27 | int number_of_books;
28 |
29 | vector books;
30 |
31 | public:
32 |
33 | BookShelf(const int max_size);
34 |
35 | ~BookShelf();
36 |
37 | Iterator* iterator() const;
38 |
39 | Book* getAt(const int index) const;
40 |
41 | void add(Book* book);
42 |
43 | int getNumberOfBooks() const;
44 |
45 | // ˅
46 | public:
47 |
48 | protected:
49 |
50 | private:
51 | BookShelf(const BookShelf&) = delete;
52 | BookShelf& operator=(const BookShelf&) = delete;
53 | BookShelf(BookShelf&&) = delete;
54 | BookShelf& operator=(BookShelf&&) = delete;
55 |
56 | // ˄
57 | };
58 |
59 | // ˅
60 |
61 | // ˄
62 |
63 | #endif // BEHAVIORAL_PATTERNS_ITERATOR_BOOKSHELF_H_
64 |
65 | // ˅
66 |
67 | // ˄
68 |
--------------------------------------------------------------------------------
/creational_patterns/factory_method/credit_card/CreditCardFactory.h:
--------------------------------------------------------------------------------
1 | // ˅
2 |
3 | // ˄
4 |
5 | #ifndef CREATIONAL_PATTERNS_FACTORY_METHOD_CREDIT_CARD_CREDITCARDFACTORY_H_
6 | #define CREATIONAL_PATTERNS_FACTORY_METHOD_CREDIT_CARD_CREDITCARDFACTORY_H_
7 |
8 | // ˅
9 | #include
10 | #include
11 | #include "creational_patterns/factory_method/framework/Factory.h"
12 |
13 | using namespace std;
14 |
15 | // ˄
16 |
17 | class CreditCardFactory : public Factory
18 | {
19 | // ˅
20 |
21 | // ˄
22 |
23 | public:
24 |
25 | CreditCardFactory();
26 |
27 | ~CreditCardFactory();
28 |
29 | protected:
30 |
31 | const Product* createProduct(const string& owner);
32 |
33 | // ˅
34 | public:
35 |
36 | protected:
37 |
38 | private:
39 | CreditCardFactory(const CreditCardFactory&) = delete;
40 | CreditCardFactory& operator=(const CreditCardFactory&) = delete;
41 | CreditCardFactory(CreditCardFactory&&) = delete;
42 | CreditCardFactory& operator=(CreditCardFactory&&) = delete;
43 |
44 | // ˄
45 | };
46 |
47 | // ˅
48 |
49 | // ˄
50 |
51 | #endif // CREATIONAL_PATTERNS_FACTORY_METHOD_CREDIT_CARD_CREDITCARDFACTORY_H_
52 |
53 | // ˅
54 |
55 | // ˄
56 |
--------------------------------------------------------------------------------
/structural_patterns/flyweight/LargeSizeCharFactory.cpp:
--------------------------------------------------------------------------------
1 | // ˅
2 | #include