├── screenshots ├── BuildAndRun.gif ├── GenerateCode.gif └── VisualStudioInstaller.png ├── structural_patterns ├── facade │ ├── addressbook.txt │ ├── makefile │ ├── main.cpp │ ├── facade.vcxproj.filters │ ├── DataLibrary.cpp │ ├── PageCreator.h │ ├── HtmlWriter.h │ └── DataLibrary.h ├── adapter │ ├── Print.cpp │ ├── makefile │ ├── main.cpp │ ├── adapter.vcxproj.filters │ ├── Print.h │ ├── PrintMessageDisplay.cpp │ ├── MessageDisplay.cpp │ ├── MessageDisplay.h │ └── PrintMessageDisplay.h ├── flyweight │ ├── big0.txt │ ├── big1.txt │ ├── big2.txt │ ├── big3.txt │ ├── big4.txt │ ├── big5.txt │ ├── big6.txt │ ├── big7.txt │ ├── big8.txt │ ├── big9.txt │ ├── makefile │ ├── LargeSizeString.cpp │ ├── LargeSizeChar.h │ ├── flyweight.vcxproj.filters │ ├── LargeSizeString.h │ ├── LargeSizeCharFactory.cpp │ └── LargeSizeChar.cpp ├── proxy │ ├── Printer.cpp │ ├── makefile │ ├── proxy.vcxproj.filters │ ├── Printer.h │ ├── main.cpp │ ├── ProxyPrinter.cpp │ ├── RealPrinter.h │ └── ProxyPrinter.h ├── bridge │ ├── DisplayImpl.cpp │ ├── makefile │ ├── MultiLineDisplay.cpp │ ├── DisplayImpl.h │ ├── bridge.vcxproj.filters │ ├── main.cpp │ ├── Display.cpp │ ├── Display.h │ ├── MultiLineDisplay.h │ ├── TextDisplayImpl.cpp │ └── TextDisplayImpl.h ├── composite │ ├── makefile │ ├── FileSystemElement.cpp │ ├── composite.vcxproj.filters │ ├── File.cpp │ ├── FileSystemElement.h │ ├── File.h │ ├── Directory.cpp │ └── Directory.h └── decorator │ ├── Frame.cpp │ ├── makefile │ ├── Display.cpp │ ├── decorator.vcxproj.filters │ ├── Frame.h │ ├── MessageDisplay.cpp │ ├── Display.h │ ├── SideFrame.cpp │ ├── main.cpp │ ├── SideFrame.h │ ├── MessageDisplay.h │ └── FullFrame.h ├── model ├── DesignPatternExamplesInCpp.asta └── m_plus.conf ├── .gitignore ├── behavioral_patterns ├── interpreter │ ├── Node.cpp │ ├── program.txt │ ├── makefile │ ├── Node.h │ ├── Command.h │ ├── Action.cpp │ ├── interpreter.vcxproj.filters │ ├── Action.h │ ├── Command.cpp │ ├── Program.h │ ├── Repeat.h │ ├── Program.cpp │ ├── CommandList.h │ ├── Context.h │ └── Repeat.cpp ├── state │ ├── State.cpp │ ├── Context.cpp │ ├── main.cpp │ ├── WindowsForm.cpp │ ├── state.vcxproj.filters │ ├── State.h │ ├── Context.h │ └── CLIWrapper.h ├── visitor │ ├── Element.cpp │ ├── Visitor.cpp │ ├── makefile │ ├── FileSystemElement.cpp │ ├── Element.h │ ├── Visitor.h │ ├── File.cpp │ ├── visitor.vcxproj.filters │ ├── FileSystemElement.h │ ├── File.h │ ├── ListVisitor.h │ └── ListVisitor.cpp ├── command │ ├── Command.cpp │ ├── PaintingTarget.cpp │ ├── main.cpp │ ├── WindowsForm.cpp │ ├── Command.h │ ├── PaintingTarget.h │ ├── PaintingCommand.cpp │ ├── CLIWrapper.h │ ├── command.vcxproj.filters │ ├── HistoryCommand.cpp │ ├── PaintingCanvas.h │ ├── HistoryCommand.h │ └── PaintingCommand.h ├── iterator │ ├── Iterator.cpp │ ├── Aggregate.cpp │ ├── Book.cpp │ ├── makefile │ ├── Aggregate.h │ ├── Iterator.h │ ├── iterator.vcxproj.filters │ ├── Book.h │ ├── BookShelfIterator.cpp │ ├── BookShelf.cpp │ ├── BookShelfIterator.h │ └── BookShelf.h ├── observer │ ├── Observer.cpp │ ├── makefile │ ├── NumberSubject.cpp │ ├── Observer.h │ ├── observer.vcxproj.filters │ ├── DigitObserver.cpp │ ├── Subject.cpp │ ├── BarChartObserver.cpp │ ├── NumberSubject.h │ ├── Subject.h │ ├── DigitObserver.h │ ├── BarChartObserver.h │ └── main.cpp ├── mediator │ ├── Mediator.cpp │ ├── Colleague.cpp │ ├── main.cpp │ ├── CLIWrapper.h │ ├── Colleague.h │ ├── Mediator.h │ ├── WindowsForm.cpp │ ├── ColleagueButton.cpp │ ├── mediator.vcxproj.filters │ ├── ColleagueTextField.cpp │ ├── ColleagueRadioButton.cpp │ ├── ColleagueButton.h │ └── ColleagueTextField.h ├── strategy │ ├── Strategy.cpp │ ├── makefile │ ├── GameResultType.h │ ├── MirrorStrategy.cpp │ ├── strategy.vcxproj.filters │ ├── Strategy.h │ ├── RandomStrategy.cpp │ ├── RandomStrategy.h │ ├── MirrorStrategy.h │ ├── Player.cpp │ └── Player.h ├── memento │ ├── makefile │ ├── Memento.cpp │ ├── memento.vcxproj.filters │ ├── Memento.h │ └── Gamer.h ├── template_method │ ├── makefile │ ├── AbstractDisplay.cpp │ ├── main.cpp │ ├── template_method.vcxproj.filters │ ├── AbstractDisplay.h │ ├── CharDisplay.cpp │ ├── CharDisplay.h │ ├── StringDisplay.cpp │ └── StringDisplay.h └── chain_of_responsibility │ ├── makefile │ ├── Trouble.cpp │ ├── LazySupporter.cpp │ ├── MoodySupporter.cpp │ ├── LimitedSupporter.cpp │ ├── SpecialSupporter.cpp │ ├── chain_of_responsibility.vcxproj.filters │ ├── Trouble.h │ ├── LazySupporter.h │ ├── MoodySupporter.h │ ├── Supporter.cpp │ ├── SpecialSupporter.h │ └── LimitedSupporter.h └── creational_patterns ├── builder ├── Builder.cpp ├── makefile ├── builder.vcxproj.filters ├── Builder.h ├── Director.cpp └── Director.h ├── prototype ├── framework │ ├── Display.cpp │ ├── Display.h │ ├── Manager.cpp │ └── Manager.h ├── makefile ├── UnderlineDisplay.cpp ├── FrameDisplay.cpp ├── FrameDisplay.h ├── prototype.vcxproj.filters └── UnderlineDisplay.h ├── abstract_factory ├── factory │ ├── Factory.cpp │ ├── Item.cpp │ ├── Link.cpp │ ├── Data.cpp │ ├── Item.h │ ├── Factory.h │ ├── Link.h │ ├── Page.h │ └── Data.h ├── list_factory │ ├── ListLink.cpp │ ├── ListData.cpp │ ├── ListData.h │ ├── ListLink.h │ ├── ListPage.h │ ├── ListFactory.cpp │ ├── ListPage.cpp │ └── ListFactory.h ├── table_factory │ ├── TableLink.cpp │ ├── TableData.h │ ├── TableLink.h │ ├── TablePage.h │ ├── TableFactory.cpp │ ├── TableData.cpp │ ├── TablePage.cpp │ └── TableFactory.h └── makefile ├── factory_method ├── framework │ ├── Product.cpp │ ├── Product.h │ ├── Factory.cpp │ └── Factory.h ├── makefile ├── credit_card │ ├── CreditCard.cpp │ ├── CreditCardFactory.cpp │ ├── CreditCard.h │ └── CreditCardFactory.h └── main.cpp └── singleton ├── makefile ├── singleton.vcxproj.filters ├── Singleton.cpp ├── main.cpp └── Singleton.h /screenshots/BuildAndRun.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takaakit/design-pattern-examples-in-cpp/HEAD/screenshots/BuildAndRun.gif -------------------------------------------------------------------------------- /screenshots/GenerateCode.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takaakit/design-pattern-examples-in-cpp/HEAD/screenshots/GenerateCode.gif -------------------------------------------------------------------------------- /structural_patterns/facade/addressbook.txt: -------------------------------------------------------------------------------- 1 | william@example.com=William 2 | emily@example.com=Emily 3 | amelia@example.com=Amelia 4 | -------------------------------------------------------------------------------- /model/DesignPatternExamplesInCpp.asta: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takaakit/design-pattern-examples-in-cpp/HEAD/model/DesignPatternExamplesInCpp.asta -------------------------------------------------------------------------------- /screenshots/VisualStudioInstaller.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/takaakit/design-pattern-examples-in-cpp/HEAD/screenshots/VisualStudioInstaller.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.asta.lock 2 | *.user 3 | *.exe 4 | .project 5 | .cproject 6 | Release/ 7 | Debug/ 8 | .vs/ 9 | .settings/ 10 | .idea/ 11 | cmake-build-debug/ 12 | -------------------------------------------------------------------------------- /structural_patterns/adapter/Print.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/adapter/Print.h" 3 | 4 | // ˄ 5 | 6 | Print::~Print() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Node.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/interpreter/Node.h" 3 | 4 | // ˄ 5 | 6 | Node::~Node() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/state/State.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/state/State.h" 3 | 4 | 5 | // ˄ 6 | 7 | State::~State() 8 | { 9 | // ˅ 10 | 11 | // ˄ 12 | } 13 | 14 | // ˅ 15 | 16 | // ˄ 17 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big0.txt: -------------------------------------------------------------------------------- 1 | 2 | ######## 3 | # ### 4 | # ### 5 | # ### 6 | # ### 7 | # ### 8 | ######## 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big1.txt: -------------------------------------------------------------------------------- 1 | 2 | #### 3 | ### 4 | ### 5 | ### 6 | ### 7 | ### 8 | ####### 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big2.txt: -------------------------------------------------------------------------------- 1 | 2 | ######## 3 | ### 4 | ### 5 | ######## 6 | # 7 | # 8 | ########## 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big3.txt: -------------------------------------------------------------------------------- 1 | 2 | ######## 3 | ### 4 | ### 5 | ######## 6 | ### 7 | # ### 8 | ######## 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big4.txt: -------------------------------------------------------------------------------- 1 | 2 | ##### 3 | # ### 4 | # ### 5 | # ### 6 | ########## 7 | ### 8 | ### 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big5.txt: -------------------------------------------------------------------------------- 1 | 2 | ######### 3 | # 4 | # 5 | ######### 6 | ### 7 | # ### 8 | ######## 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big6.txt: -------------------------------------------------------------------------------- 1 | 2 | ######## 3 | # 4 | # 5 | ######### 6 | # ### 7 | # ### 8 | ######## 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big7.txt: -------------------------------------------------------------------------------- 1 | 2 | ########## 3 | ### 4 | ### 5 | ### 6 | ### 7 | ### 8 | ### 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big8.txt: -------------------------------------------------------------------------------- 1 | 2 | ######## 3 | # ### 4 | # ### 5 | ######## 6 | # ### 7 | # ### 8 | ######## 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/big9.txt: -------------------------------------------------------------------------------- 1 | 2 | ######## 3 | # ### 4 | # ### 5 | ######### 6 | ### 7 | ### 8 | ######## 9 | 10 | -------------------------------------------------------------------------------- /structural_patterns/proxy/Printer.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/proxy/Printer.h" 3 | 4 | // ˄ 5 | 6 | Printer::~Printer() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/state/Context.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/state/Context.h" 3 | 4 | 5 | // ˄ 6 | 7 | Context::~Context() 8 | { 9 | // ˅ 10 | 11 | // ˄ 12 | } 13 | 14 | // ˅ 15 | 16 | // ˄ 17 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/Element.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/visitor/Element.h" 3 | 4 | // ˄ 5 | 6 | Element::~Element() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/Visitor.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/visitor/Visitor.h" 3 | 4 | // ˄ 5 | 6 | Visitor::~Visitor() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /creational_patterns/builder/Builder.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/builder/Builder.h" 3 | 4 | // ˄ 5 | 6 | Builder::~Builder() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/command/Command.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/command/Command.h" 3 | 4 | 5 | // ˄ 6 | 7 | Command::~Command() 8 | { 9 | // ˅ 10 | 11 | // ˄ 12 | } 13 | 14 | // ˅ 15 | 16 | // ˄ 17 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/Iterator.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/iterator/Iterator.h" 3 | 4 | // ˄ 5 | 6 | Iterator::~Iterator() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/Observer.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/observer/Observer.h" 3 | 4 | // ˄ 5 | 6 | Observer::~Observer() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/Mediator.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/mediator/Mediator.h" 3 | 4 | 5 | // ˄ 6 | 7 | Mediator::~Mediator() 8 | { 9 | // ˅ 10 | 11 | // ˄ 12 | } 13 | 14 | // ˅ 15 | 16 | // ˄ 17 | -------------------------------------------------------------------------------- /structural_patterns/bridge/DisplayImpl.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/bridge/DisplayImpl.h" 3 | 4 | // ˄ 5 | 6 | DisplayImpl::~DisplayImpl() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /creational_patterns/prototype/framework/Display.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/prototype/framework/Display.h" 3 | 4 | // ˄ 5 | 6 | Display::~Display() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/command/PaintingTarget.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/command/PaintingTarget.h" 3 | 4 | 5 | // ˄ 6 | 7 | PaintingTarget::~PaintingTarget() 8 | { 9 | // ˅ 10 | 11 | // ˄ 12 | } 13 | 14 | // ˅ 15 | 16 | // ˄ 17 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Factory.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/abstract_factory/factory/Factory.h" 3 | 4 | // ˄ 5 | 6 | Factory::~Factory() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/framework/Product.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/factory_method/framework/Product.h" 3 | 4 | // ˄ 5 | 6 | Product::~Product() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | // ˅ 14 | 15 | // ˄ 16 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/program.txt: -------------------------------------------------------------------------------- 1 | program end 2 | program forward right left end 3 | program forward right forward right forward right forward right end 4 | program repeat 4 forward right end end 5 | program repeat 2 repeat 2 forward right forward right end end end 6 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/Aggregate.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/iterator/Aggregate.h" 3 | #include "behavioral_patterns/iterator/Iterator.h" 4 | 5 | // ˄ 6 | 7 | Aggregate::~Aggregate() 8 | { 9 | // ˅ 10 | 11 | // ˄ 12 | } 13 | 14 | // ˅ 15 | 16 | // ˄ 17 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/Strategy.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/strategy/HandSignal.h" 3 | #include "behavioral_patterns/strategy/Strategy.h" 4 | 5 | // ˄ 6 | 7 | Strategy::~Strategy() 8 | { 9 | // ˅ 10 | 11 | // ˄ 12 | } 13 | 14 | // ˅ 15 | 16 | // ˄ 17 | -------------------------------------------------------------------------------- /creational_patterns/singleton/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Singleton.cpp \ 5 | main.cpp 6 | 7 | PROG = singleton.exe 8 | 9 | .PHONY: all 10 | all: $(SRC) 11 | g++ $(CFLAGS) $(SRC) -o $(PROG) 12 | 13 | .PHONY: clean 14 | clean: 15 | rm -rf $(PROG) 16 | -------------------------------------------------------------------------------- /behavioral_patterns/memento/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Gamer.cpp \ 5 | Memento.cpp \ 6 | main.cpp 7 | 8 | PROG = memento.exe 9 | 10 | .PHONY: all 11 | all: $(SRC) 12 | g++ $(CFLAGS) $(SRC) -o $(PROG) 13 | 14 | .PHONY: clean 15 | clean: 16 | rm -rf $(PROG) 17 | -------------------------------------------------------------------------------- /structural_patterns/facade/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | DataLibrary.cpp \ 5 | HtmlWriter.cpp \ 6 | PageCreator.cpp \ 7 | main.cpp 8 | 9 | PROG = facade.exe 10 | 11 | .PHONY: all 12 | all: $(SRC) 13 | g++ $(CFLAGS) $(SRC) -o $(PROG) 14 | 15 | .PHONY: clean 16 | clean: 17 | rm -rf $(PROG) 18 | -------------------------------------------------------------------------------- /structural_patterns/proxy/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Printer.cpp \ 5 | ProxyPrinter.cpp \ 6 | RealPrinter.cpp \ 7 | main.cpp 8 | 9 | PROG = proxy.exe 10 | 11 | .PHONY: all 12 | all: $(SRC) 13 | g++ $(CFLAGS) $(SRC) -o $(PROG) 14 | 15 | .PHONY: clean 16 | clean: 17 | rm -rf $(PROG) 18 | -------------------------------------------------------------------------------- /structural_patterns/composite/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Directory.cpp \ 5 | File.cpp \ 6 | FileSystemElement.cpp \ 7 | main.cpp 8 | 9 | PROG = composite.exe 10 | 11 | .PHONY: all 12 | all: $(SRC) 13 | g++ $(CFLAGS) $(SRC) -o $(PROG) 14 | 15 | .PHONY: clean 16 | clean: 17 | rm -rf $(PROG) 18 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/Book.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/iterator/Book.h" 3 | 4 | // ˄ 5 | 6 | Book::Book(const string& title) 7 | : title(title) 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | Book::~Book() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | // ˅ 25 | 26 | // ˄ 27 | -------------------------------------------------------------------------------- /structural_patterns/adapter/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | MessageDisplay.cpp \ 5 | Print.cpp \ 6 | PrintMessageDisplay.cpp \ 7 | main.cpp 8 | 9 | PROG = adapter.exe 10 | 11 | .PHONY: all 12 | all: $(SRC) 13 | g++ $(CFLAGS) $(SRC) -o $(PROG) 14 | 15 | .PHONY: clean 16 | clean: 17 | rm -rf $(PROG) 18 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | AbstractDisplay.cpp \ 5 | CharDisplay.cpp \ 6 | StringDisplay.cpp \ 7 | main.cpp 8 | 9 | PROG = template_method.exe 10 | 11 | .PHONY: all 12 | all: $(SRC) 13 | g++ $(CFLAGS) $(SRC) -o $(PROG) 14 | 15 | .PHONY: clean 16 | clean: 17 | rm -rf $(PROG) 18 | -------------------------------------------------------------------------------- /creational_patterns/builder/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Builder.cpp \ 5 | Director.cpp \ 6 | HTMLBuilder.cpp \ 7 | PlainTextBuilder.cpp \ 8 | main.cpp 9 | 10 | PROG = builder.exe 11 | 12 | .PHONY: all 13 | all: $(SRC) 14 | g++ $(CFLAGS) $(SRC) -o $(PROG) 15 | 16 | .PHONY: clean 17 | clean: 18 | rm -rf $(PROG) 19 | -------------------------------------------------------------------------------- /structural_patterns/decorator/Frame.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/decorator/Frame.h" 3 | 4 | // ˄ 5 | 6 | Frame::Frame(const Display* display) 7 | : display(display) 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | Frame::~Frame() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | // ˅ 25 | 26 | // ˄ 27 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | LargeSizeChar.cpp \ 5 | LargeSizeCharFactory.cpp \ 6 | LargeSizeString.cpp \ 7 | main.cpp 8 | 9 | PROG = flyweight.exe 10 | 11 | .PHONY: all 12 | all: $(SRC) 13 | g++ $(CFLAGS) $(SRC) -o $(PROG) 14 | 15 | .PHONY: clean 16 | clean: 17 | rm -rf $(PROG) 18 | -------------------------------------------------------------------------------- /structural_patterns/bridge/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Display.cpp \ 5 | DisplayImpl.cpp \ 6 | MultiLineDisplay.cpp \ 7 | TextDisplayImpl.cpp \ 8 | main.cpp 9 | 10 | PROG = bridge.exe 11 | 12 | .PHONY: all 13 | all: $(SRC) 14 | g++ $(CFLAGS) $(SRC) -o $(PROG) 15 | 16 | .PHONY: clean 17 | clean: 18 | rm -rf $(PROG) 19 | -------------------------------------------------------------------------------- /behavioral_patterns/command/main.cpp: -------------------------------------------------------------------------------- 1 | #include "AppMain.h" 2 | 3 | /* 4 | Simple drawing application: 5 | * Draw a path with points by dragging the mouse. 6 | * Revert to one previous drawing by pressing the Undo button. 7 | * Erase all drawing by pressing the Clear button. 8 | */ 9 | 10 | [STAThread] 11 | int main() 12 | { 13 | AppMain(); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Item.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/abstract_factory/factory/Item.h" 3 | 4 | // ˄ 5 | 6 | Item::Item(const string& name) 7 | : name(name) 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | Item::~Item() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | // ˅ 25 | 26 | // ˄ 27 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Aggregate.cpp \ 5 | Book.cpp \ 6 | BookShelf.cpp \ 7 | BookShelfIterator.cpp \ 8 | Iterator.cpp \ 9 | main.cpp 10 | 11 | PROG = iterator.exe 12 | 13 | .PHONY: all 14 | all: $(SRC) 15 | g++ $(CFLAGS) $(SRC) -o $(PROG) 16 | 17 | .PHONY: clean 18 | clean: 19 | rm -rf $(PROG) 20 | -------------------------------------------------------------------------------- /behavioral_patterns/memento/Memento.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/memento/Memento.h" 3 | 4 | // ˄ 5 | 6 | Memento::Memento(const int money) 7 | : money(money) 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | int Memento::getMoney() const 18 | { 19 | // ˅ 20 | return money; 21 | // ˄ 22 | } 23 | 24 | // ˅ 25 | 26 | // ˄ 27 | -------------------------------------------------------------------------------- /structural_patterns/decorator/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Display.cpp \ 5 | Frame.cpp \ 6 | FullFrame.cpp \ 7 | MessageDisplay.cpp \ 8 | SideFrame.cpp \ 9 | main.cpp 10 | 11 | PROG = decorator.exe 12 | 13 | .PHONY: all 14 | all: $(SRC) 15 | g++ $(CFLAGS) $(SRC) -o $(PROG) 16 | 17 | .PHONY: clean 18 | clean: 19 | rm -rf $(PROG) 20 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | HandSignal.cpp \ 5 | Player.cpp \ 6 | Strategy.cpp \ 7 | MirrorStrategy.cpp \ 8 | RandomStrategy.cpp \ 9 | main.cpp 10 | 11 | PROG = strategy.exe 12 | 13 | .PHONY: all 14 | all: $(SRC) 15 | g++ $(CFLAGS) $(SRC) -o $(PROG) 16 | 17 | .PHONY: clean 18 | clean: 19 | rm -rf $(PROG) 20 | -------------------------------------------------------------------------------- /creational_patterns/prototype/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | framework/Display.cpp \ 5 | framework/Manager.cpp \ 6 | FrameDisplay.cpp \ 7 | UnderlineDisplay.cpp \ 8 | main.cpp 9 | 10 | PROG = prototype.exe 11 | 12 | .PHONY: all 13 | all: $(SRC) 14 | g++ $(CFLAGS) $(SRC) -o $(PROG) 15 | 16 | .PHONY: clean 17 | clean: 18 | rm -rf $(PROG) 19 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Observer.cpp \ 5 | BarChartObserver.cpp \ 6 | DigitObserver.cpp \ 7 | Subject.cpp \ 8 | NumberSubject.cpp \ 9 | main.cpp 10 | 11 | PROG = observer.exe 12 | 13 | .PHONY: all 14 | all: $(SRC) 15 | g++ $(CFLAGS) $(SRC) -o $(PROG) 16 | 17 | .PHONY: clean 18 | clean: 19 | rm -rf $(PROG) 20 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Directory.cpp \ 5 | Element.cpp \ 6 | File.cpp \ 7 | FileSystemElement.cpp \ 8 | ListVisitor.cpp \ 9 | Visitor.cpp \ 10 | main.cpp 11 | 12 | PROG = visitor.exe 13 | 14 | .PHONY: all 15 | all: $(SRC) 16 | g++ $(CFLAGS) $(SRC) -o $(PROG) 17 | 18 | .PHONY: clean 19 | clean: 20 | rm -rf $(PROG) 21 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Link.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/abstract_factory/factory/Link.h" 3 | 4 | // ˄ 5 | 6 | Link::Link(const string& name, const string& url) 7 | : url(url) 8 | // ˅ 9 | , Item(name) 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | Link::~Link() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | // ˅ 25 | 26 | // ˄ 27 | -------------------------------------------------------------------------------- /creational_patterns/singleton/singleton.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | Action.cpp \ 5 | Command.cpp \ 6 | CommandList.cpp \ 7 | Context.cpp \ 8 | Node.cpp \ 9 | Program.cpp \ 10 | Repeat.cpp \ 11 | main.cpp 12 | 13 | PROG = interpreter.exe 14 | 15 | .PHONY: all 16 | all: $(SRC) 17 | g++ $(CFLAGS) $(SRC) -o $(PROG) 18 | 19 | .PHONY: clean 20 | clean: 21 | rm -rf $(PROG) 22 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | framework/Factory.cpp \ 5 | framework/Product.cpp \ 6 | credit_card/CreditCard.cpp \ 7 | credit_card/CreditCardFactory.cpp \ 8 | main.cpp 9 | 10 | PROG = factory_method.exe 11 | 12 | .PHONY: all 13 | all: $(SRC) 14 | g++ $(CFLAGS) $(SRC) -o $(PROG) 15 | 16 | .PHONY: clean 17 | clean: 18 | rm -rf $(PROG) 19 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/Colleague.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/mediator/Colleague.h" 3 | #include "behavioral_patterns/mediator/Mediator.h" 4 | 5 | 6 | // ˄ 7 | 8 | Colleague::Colleague(Mediator* mediator) 9 | : mediator(mediator) 10 | // ˅ 11 | 12 | // ˄ 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | } 18 | 19 | Colleague::~Colleague() 20 | { 21 | // ˅ 22 | 23 | // ˄ 24 | } 25 | 26 | // ˅ 27 | 28 | // ˄ 29 | -------------------------------------------------------------------------------- /behavioral_patterns/state/main.cpp: -------------------------------------------------------------------------------- 1 | #include "AppSafe.h" 2 | 3 | /* 4 | Safe security system that the security status changes with time. When you press a button in a dialog, 5 | the message displayed will change depending on whether the time is day or night. 6 | The internal time of the dialog advances one hour for every second of real time. 7 | */ 8 | 9 | [STAThread] 10 | int main() 11 | { 12 | AppSafe(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /structural_patterns/decorator/Display.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "structural_patterns/decorator/Display.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | Display::~Display() 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | void Display::show() const 17 | { 18 | // ˅ 19 | for (int i = 0; i < getRows(); ++i) { 20 | cout << getLineText(i) << endl; 21 | } 22 | // ˄ 23 | } 24 | 25 | // ˅ 26 | 27 | // ˄ 28 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | LazySupporter.cpp \ 5 | LimitedSupporter.cpp \ 6 | MoodySupporter.cpp \ 7 | SpecialSupporter.cpp \ 8 | Supporter.cpp \ 9 | Trouble.cpp \ 10 | main.cpp 11 | 12 | PROG = chain_of_responsibility.exe 13 | 14 | .PHONY: all 15 | all: $(SRC) 16 | g++ $(CFLAGS) $(SRC) -o $(PROG) 17 | 18 | .PHONY: clean 19 | clean: 20 | rm -rf $(PROG) 21 | -------------------------------------------------------------------------------- /behavioral_patterns/command/WindowsForm.cpp: -------------------------------------------------------------------------------- 1 | #include "WindowsForm.h" 2 | 3 | System::Windows::Forms::PictureBox^ command::WindowsForm::getPictureBox() 4 | { 5 | return this->picture_box; 6 | } 7 | 8 | System::Windows::Forms::Button^ command::WindowsForm::getButtonUndo() 9 | { 10 | return this->button_undo; 11 | } 12 | 13 | System::Windows::Forms::Button^ command::WindowsForm::getButtonClear() 14 | { 15 | return this->button_clear; 16 | } 17 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/AbstractDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/template_method/AbstractDisplay.h" 3 | 4 | // ˄ 5 | 6 | AbstractDisplay::~AbstractDisplay() 7 | { 8 | // ˅ 9 | 10 | // ˄ 11 | } 12 | 13 | void AbstractDisplay::output() 14 | { 15 | // ˅ 16 | open(); 17 | for (int i = 0; i < 5; ++i) { // Repeat write 5 times 18 | write(); 19 | } 20 | close(); 21 | // ˄ 22 | } 23 | 24 | // ˅ 25 | 26 | // ˄ 27 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/GameResultType.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_STRATEGY_GAMERESULTTYPE_H_ 6 | #define BEHAVIORAL_PATTERNS_STRATEGY_GAMERESULTTYPE_H_ 7 | 8 | // ˅ 9 | 10 | // ˄ 11 | 12 | typedef enum 13 | { 14 | 15 | Win, 16 | 17 | Loss, 18 | 19 | Draw 20 | 21 | } GameResultType; 22 | 23 | // ˅ 24 | 25 | // ˄ 26 | 27 | #endif // BEHAVIORAL_PATTERNS_STRATEGY_GAMERESULTTYPE_H_ 28 | 29 | // ˅ 30 | 31 | // ˄ 32 | -------------------------------------------------------------------------------- /model/m_plus.conf: -------------------------------------------------------------------------------- 1 | TARGET_DIRECTORY=.. 2 | PROGRAMMING_LANGUAGE=C++ 3 | CPP_HEADER_EXTENSION=h 4 | CPP_SOURCE_EXTENSION=cpp 5 | CSHARP_EXTENSION=cs 6 | CRYSTAL_EXTENSION=cr 7 | GO_EXTENSION=go 8 | JAVA_EXTENSION=java 9 | JAVASCRIPT_EXTENSION=mjs 10 | KOTLIN_EXTENSION=kt 11 | PYTHON_EXTENSION=py 12 | RUBY_EXTENSION=rb 13 | SCALA_EXTENSION=scala 14 | SWIFT_EXTENSION=swift 15 | TYPESCRIPT_EXTENSION=ts 16 | ALLOY_EXTENSION=als 17 | VDMPP_EXTENSION=vdmpp 18 | PYTHON_VERSION= 19 | -------------------------------------------------------------------------------- /behavioral_patterns/memento/memento.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Data.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/abstract_factory/factory/Data.h" 3 | 4 | // ˄ 5 | 6 | Data::Data(const string& name) 7 | // ˅ 8 | : Item(name) 9 | // ˄ 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | Data::~Data() 17 | { 18 | // ˅ 19 | items.clear(); 20 | // ˄ 21 | } 22 | 23 | void Data::add(Item* item) 24 | { 25 | // ˅ 26 | items.push_back(item); 27 | // ˄ 28 | } 29 | 30 | // ˅ 31 | 32 | // ˄ 33 | -------------------------------------------------------------------------------- /structural_patterns/facade/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "structural_patterns/facade/PageCreator.h" 3 | 4 | using namespace std; 5 | 6 | /* 7 | Create a simple homepage through a Facade (PageCreator). The Facade gets info from 8 | the DataLibrary and uses the info to create an HTML file. 9 | */ 10 | 11 | int main(int argc, char* argv[]) { 12 | PageCreator::getInstance()->createSimpleHomepage("emily@example.com", "Homepage.html"); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /creational_patterns/singleton/Singleton.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/singleton/Singleton.h" 3 | 4 | // ˄ 5 | 6 | Singleton* Singleton::getInstance() 7 | { 8 | // ˅ 9 | static Singleton* instance = new Singleton(); 10 | return instance; 11 | // ˄ 12 | } 13 | 14 | Singleton::Singleton() 15 | // ˅ 16 | 17 | // ˄ 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | Singleton::~Singleton() 25 | { 26 | // ˅ 27 | 28 | // ˄ 29 | } 30 | 31 | // ˅ 32 | 33 | // ˄ 34 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/Trouble.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/chain_of_responsibility/Trouble.h" 3 | 4 | // ˄ 5 | 6 | Trouble::Trouble(const int id) 7 | : id(id) 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | Trouble::~Trouble() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | const string Trouble::toString() const 25 | { 26 | // ˅ 27 | return "[Trouble " + to_string(id) + "]"; 28 | // ˄ 29 | } 30 | 31 | // ˅ 32 | 33 | // ˄ 34 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/main.cpp: -------------------------------------------------------------------------------- 1 | #include "AppLogin.h" 2 | 3 | /* 4 | Show a login dialog for entering a username and password. The dialog has the following elements: 5 | * "Guest" and "Login" radio buttons 6 | * "Username" and "Password" text fields 7 | * "OK" and "Cancel" buttons 8 | 9 | And change the editable properties of the elements depending on the state of the radio buttons and text fields. 10 | */ 11 | 12 | [STAThread] 13 | int main() 14 | { 15 | AppLogin(); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/FileSystemElement.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/visitor/FileSystemElement.h" 3 | 4 | // ˄ 5 | 6 | FileSystemElement::FileSystemElement() 7 | // ˅ 8 | 9 | // ˄ 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | FileSystemElement::~FileSystemElement() 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | } 22 | 23 | const string FileSystemElement::toString() const 24 | { 25 | // ˅ 26 | return getName() + " (" + to_string(getSize()) + ")"; 27 | // ˄ 28 | } 29 | 30 | // ˅ 31 | 32 | // ˄ 33 | -------------------------------------------------------------------------------- /structural_patterns/composite/FileSystemElement.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/composite/FileSystemElement.h" 3 | 4 | // ˄ 5 | 6 | FileSystemElement::FileSystemElement() 7 | // ˅ 8 | 9 | // ˄ 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | FileSystemElement::~FileSystemElement() 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | } 22 | 23 | const string FileSystemElement::toString() const 24 | { 25 | // ˅ 26 | return getName() + " (" + to_string(getSize()) + ")"; 27 | // ˄ 28 | } 29 | 30 | // ˅ 31 | 32 | // ˄ 33 | -------------------------------------------------------------------------------- /structural_patterns/adapter/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "structural_patterns/adapter/PrintMessageDisplay.h" 3 | 4 | using namespace std; 5 | 6 | /* 7 | Display the given string as follows 8 | ``` 9 | -- Nice to meet you -- 10 | ``` 11 | or display it as follows. 12 | ``` 13 | [[ Nice to meet you ]] 14 | ``` 15 | */ 16 | 17 | int main(int argc, char* argv[]) { 18 | unique_ptr print(new PrintMessageDisplay("Nice to meet you")); 19 | print->printWeak(); 20 | print->printStrong(); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/LazySupporter.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/chain_of_responsibility/LazySupporter.h" 3 | 4 | // ˄ 5 | 6 | LazySupporter::LazySupporter(const string& name) 7 | // ˅ 8 | : Supporter(name) 9 | // ˄ 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | LazySupporter::~LazySupporter() 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | } 22 | 23 | bool LazySupporter::canHandle(const Trouble* trouble) const 24 | { 25 | // ˅ 26 | return false; 27 | // ˄ 28 | } 29 | 30 | // ˅ 31 | 32 | // ˄ 33 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/MoodySupporter.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/chain_of_responsibility/MoodySupporter.h" 3 | 4 | // ˄ 5 | 6 | MoodySupporter::MoodySupporter(const string& name) 7 | // ˅ 8 | : Supporter(name) 9 | // ˄ 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | MoodySupporter::~MoodySupporter() 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | } 22 | 23 | bool MoodySupporter::canHandle(const Trouble* trouble) const 24 | { 25 | // ˅ 26 | return trouble->id % 2 == 1; 27 | // ˄ 28 | } 29 | 30 | // ˅ 31 | 32 | // ˄ 33 | -------------------------------------------------------------------------------- /behavioral_patterns/command/Command.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_COMMAND_COMMAND_H_ 6 | #define BEHAVIORAL_PATTERNS_COMMAND_COMMAND_H_ 7 | 8 | // ˅ 9 | 10 | // ˄ 11 | 12 | class Command 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | 18 | public: 19 | 20 | virtual ~Command(); 21 | 22 | virtual void execute() const = 0; 23 | 24 | // ˅ 25 | public: 26 | 27 | protected: 28 | 29 | private: 30 | 31 | // ˄ 32 | }; 33 | 34 | // ˅ 35 | 36 | // ˄ 37 | 38 | #endif // BEHAVIORAL_PATTERNS_COMMAND_COMMAND_H_ 39 | 40 | // ˅ 41 | 42 | // ˄ 43 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/list_factory/ListLink.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/abstract_factory/list_factory/ListLink.h" 3 | 4 | // ˄ 5 | 6 | ListLink::ListLink(const string& name, const string& url) 7 | // ˅ 8 | : Link(name, url) 9 | // ˄ 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | ListLink::~ListLink() 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | } 22 | 23 | const string ListLink::toHTML() const 24 | { 25 | // ˅ 26 | return "
  • " + name + "
  • \n"; 27 | // ˄ 28 | } 29 | 30 | // ˅ 31 | 32 | // ˄ 33 | -------------------------------------------------------------------------------- /creational_patterns/singleton/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "creational_patterns/singleton/Singleton.h" 3 | 4 | using namespace std; 5 | 6 | /* 7 | Check whether the same instance is obtained. 8 | */ 9 | 10 | int main(int argc, char* argv[]) { 11 | Singleton* obj1 = Singleton::getInstance(); 12 | Singleton* obj2 = Singleton::getInstance(); 13 | if (obj1 == obj2) { 14 | cout << "obj1 and obj2 are the same instance." << endl; 15 | } 16 | else { 17 | cout << "obj1 and obj2 are different instances." << endl; 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/table_factory/TableLink.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/abstract_factory/table_factory/TableLink.h" 3 | 4 | // ˄ 5 | 6 | TableLink::TableLink(const string& name, const string& url) 7 | // ˅ 8 | : Link(name, url) 9 | // ˄ 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | TableLink::~TableLink() 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | } 22 | 23 | const string TableLink::toHTML() const 24 | { 25 | // ˅ 26 | return " " + name + "\n"; 27 | // ˄ 28 | } 29 | 30 | // ˅ 31 | 32 | // ˄ 33 | -------------------------------------------------------------------------------- /structural_patterns/proxy/proxy.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /structural_patterns/composite/composite.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "behavioral_patterns/template_method/CharDisplay.h" 4 | #include "behavioral_patterns/template_method/StringDisplay.h" 5 | 6 | using namespace std; 7 | 8 | /* 9 | Display a character or string repeatedly 5 times. 10 | */ 11 | 12 | int main(int argc, char* argv[]) { 13 | unique_ptr display1(new CharDisplay('H')); 14 | display1->output(); 15 | 16 | unique_ptr display2(new StringDisplay("Hello world.")); 17 | display2->output(); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/Element.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_VISITOR_ELEMENT_H_ 6 | #define BEHAVIORAL_PATTERNS_VISITOR_ELEMENT_H_ 7 | 8 | // ˅ 9 | class Visitor; 10 | 11 | // ˄ 12 | 13 | class Element 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | 19 | public: 20 | 21 | virtual ~Element(); 22 | 23 | virtual void accept(Visitor* visitor) = 0; 24 | 25 | // ˅ 26 | public: 27 | 28 | protected: 29 | 30 | private: 31 | 32 | // ˄ 33 | }; 34 | 35 | // ˅ 36 | 37 | // ˄ 38 | 39 | #endif // BEHAVIORAL_PATTERNS_VISITOR_ELEMENT_H_ 40 | 41 | // ˅ 42 | 43 | // ˄ 44 | -------------------------------------------------------------------------------- /structural_patterns/adapter/adapter.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /structural_patterns/adapter/Print.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_ADAPTER_PRINT_H_ 6 | #define STRUCTURAL_PATTERNS_ADAPTER_PRINT_H_ 7 | 8 | // ˅ 9 | 10 | // ˄ 11 | 12 | class Print 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | 18 | public: 19 | 20 | virtual ~Print(); 21 | 22 | virtual void printWeak() const = 0; 23 | 24 | virtual void printStrong() const = 0; 25 | 26 | // ˅ 27 | public: 28 | 29 | protected: 30 | 31 | private: 32 | 33 | // ˄ 34 | }; 35 | 36 | // ˅ 37 | 38 | // ˄ 39 | 40 | #endif // STRUCTURAL_PATTERNS_ADAPTER_PRINT_H_ 41 | 42 | // ˅ 43 | 44 | // ˄ 45 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/Aggregate.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_ITERATOR_AGGREGATE_H_ 6 | #define BEHAVIORAL_PATTERNS_ITERATOR_AGGREGATE_H_ 7 | 8 | // ˅ 9 | class Iterator; 10 | 11 | // ˄ 12 | 13 | class Aggregate 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | 19 | public: 20 | 21 | virtual ~Aggregate(); 22 | 23 | virtual Iterator* iterator() const = 0; 24 | 25 | // ˅ 26 | public: 27 | 28 | protected: 29 | 30 | private: 31 | 32 | // ˄ 33 | }; 34 | 35 | // ˅ 36 | 37 | // ˄ 38 | 39 | #endif // BEHAVIORAL_PATTERNS_ITERATOR_AGGREGATE_H_ 40 | 41 | // ˅ 42 | 43 | // ˄ 44 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/Iterator.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_ITERATOR_ITERATOR_H_ 6 | #define BEHAVIORAL_PATTERNS_ITERATOR_ITERATOR_H_ 7 | 8 | // ˅ 9 | 10 | // ˄ 11 | 12 | class Iterator 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | 18 | public: 19 | 20 | virtual ~Iterator(); 21 | 22 | virtual bool hasNext() const = 0; 23 | 24 | virtual void* next() = 0; 25 | 26 | // ˅ 27 | public: 28 | 29 | protected: 30 | 31 | private: 32 | 33 | // ˄ 34 | }; 35 | 36 | // ˅ 37 | 38 | // ˄ 39 | 40 | #endif // BEHAVIORAL_PATTERNS_ITERATOR_ITERATOR_H_ 41 | 42 | // ˅ 43 | 44 | // ˄ 45 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/template_method.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/LimitedSupporter.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/chain_of_responsibility/LimitedSupporter.h" 3 | 4 | // ˄ 5 | 6 | LimitedSupporter::LimitedSupporter(const string& name, const int limit_id) 7 | : limit_id(limit_id) 8 | // ˅ 9 | , Supporter(name) 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | LimitedSupporter::~LimitedSupporter() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | bool LimitedSupporter::canHandle(const Trouble* trouble) const 25 | { 26 | // ˅ 27 | return trouble->id <= limit_id; 28 | // ˄ 29 | } 30 | 31 | // ˅ 32 | 33 | // ˄ 34 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/SpecialSupporter.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/chain_of_responsibility/SpecialSupporter.h" 3 | 4 | // ˄ 5 | 6 | SpecialSupporter::SpecialSupporter(const string& name, const int target_id) 7 | : target_id(target_id) 8 | // ˅ 9 | , Supporter(name) 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | SpecialSupporter::~SpecialSupporter() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | bool SpecialSupporter::canHandle(const Trouble* trouble) const 25 | { 26 | // ˅ 27 | return trouble->id == target_id; 28 | // ˄ 29 | } 30 | 31 | // ˅ 32 | 33 | // ˄ 34 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/framework/Product.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_FACTORY_METHOD_FRAMEWORK_PRODUCT_H_ 6 | #define CREATIONAL_PATTERNS_FACTORY_METHOD_FRAMEWORK_PRODUCT_H_ 7 | 8 | // ˅ 9 | 10 | // ˄ 11 | 12 | class Product 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | 18 | public: 19 | 20 | virtual ~Product(); 21 | 22 | virtual void use() const = 0; 23 | 24 | // ˅ 25 | public: 26 | 27 | protected: 28 | 29 | private: 30 | 31 | // ˄ 32 | }; 33 | 34 | // ˅ 35 | 36 | // ˄ 37 | 38 | #endif // CREATIONAL_PATTERNS_FACTORY_METHOD_FRAMEWORK_PRODUCT_H_ 39 | 40 | // ˅ 41 | 42 | // ˄ 43 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/credit_card/CreditCard.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "creational_patterns/factory_method/credit_card/CreditCard.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | CreditCard::CreditCard(const string& owner) 10 | : owner(owner) 11 | // ˅ 12 | 13 | // ˄ 14 | { 15 | // ˅ 16 | cout << "Make " << owner << "'s card." << endl; 17 | // ˄ 18 | } 19 | 20 | CreditCard::~CreditCard() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | void CreditCard::use() const 28 | { 29 | // ˅ 30 | cout << "Use " << owner << "'s card." << endl; 31 | // ˄ 32 | } 33 | 34 | // ˅ 35 | 36 | // ˄ 37 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/credit_card/CreditCardFactory.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/factory_method/credit_card/CreditCard.h" 3 | #include "creational_patterns/factory_method/credit_card/CreditCardFactory.h" 4 | 5 | // ˄ 6 | 7 | CreditCardFactory::CreditCardFactory() 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | CreditCardFactory::~CreditCardFactory() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | const Product* CreditCardFactory::createProduct(const string& owner) 25 | { 26 | // ˅ 27 | return new CreditCard(owner); 28 | // ˄ 29 | } 30 | 31 | // ˅ 32 | 33 | // ˄ 34 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/CLIWrapper.h: -------------------------------------------------------------------------------- 1 | #ifndef BEHAVIORAL_PATTERNS_MEDIATOR_CLIWRAPPER_H_ 2 | #define BEHAVIORAL_PATTERNS_MEDIATOR_CLIWRAPPER_H_ 3 | 4 | #include "Mediator.h" 5 | 6 | // Wrapper class for calling unmanaged code from managed code 7 | public ref class CLIWrapper 8 | { 9 | public: 10 | CLIWrapper(Mediator* mediator) : mediator(mediator) {} 11 | ~CLIWrapper() {} 12 | void colleagueChanged(Object^ sender, EventArgs^ e) { 13 | mediator->colleagueChanged(sender, e); 14 | } 15 | 16 | private: 17 | Mediator* mediator; // Pointer to the class of unmanaged code 18 | }; 19 | 20 | #endif // BEHAVIORAL_PATTERNS_MEDIATOR_CLIWRAPPER_H_ 21 | -------------------------------------------------------------------------------- /structural_patterns/bridge/MultiLineDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/bridge/MultiLineDisplay.h" 3 | #include "structural_patterns/bridge/DisplayImpl.h" 4 | 5 | // ˄ 6 | 7 | MultiLineDisplay::MultiLineDisplay(DisplayImpl* impl) 8 | // ˅ 9 | : Display(impl) 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | MultiLineDisplay::~MultiLineDisplay() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | void MultiLineDisplay::outputMultiple(const int times) const 25 | { 26 | // ˅ 27 | open(); 28 | for (int i = 0; i < times; ++i) { 29 | write(); 30 | } 31 | close(); 32 | // ˄ 33 | } 34 | 35 | // ˅ 36 | 37 | // ˄ 38 | -------------------------------------------------------------------------------- /behavioral_patterns/command/PaintingTarget.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_COMMAND_PAINTINGTARGET_H_ 6 | #define BEHAVIORAL_PATTERNS_COMMAND_PAINTINGTARGET_H_ 7 | 8 | // ˅ 9 | 10 | // ˄ 11 | 12 | class PaintingTarget 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | 18 | public: 19 | 20 | ~PaintingTarget(); 21 | 22 | virtual void paint(const double x, const double y) const = 0; 23 | 24 | virtual void clear() = 0; 25 | 26 | // ˅ 27 | public: 28 | 29 | protected: 30 | 31 | private: 32 | 33 | // ˄ 34 | }; 35 | 36 | // ˅ 37 | 38 | // ˄ 39 | 40 | #endif // BEHAVIORAL_PATTERNS_COMMAND_PAINTINGTARGET_H_ 41 | 42 | // ˅ 43 | 44 | // ˄ 45 | -------------------------------------------------------------------------------- /structural_patterns/adapter/PrintMessageDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/adapter/PrintMessageDisplay.h" 3 | 4 | // ˄ 5 | 6 | PrintMessageDisplay::PrintMessageDisplay(const string& message) 7 | // ˅ 8 | : MessageDisplay(message) 9 | // ˄ 10 | { 11 | // ˅ 12 | 13 | // ˄ 14 | } 15 | 16 | PrintMessageDisplay::~PrintMessageDisplay() 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | } 22 | 23 | void PrintMessageDisplay::printWeak() const 24 | { 25 | // ˅ 26 | displayWithHyphens(); 27 | // ˄ 28 | } 29 | 30 | void PrintMessageDisplay::printStrong() const 31 | { 32 | // ˅ 33 | displayWithBrackets(); 34 | // ˄ 35 | } 36 | 37 | // ˅ 38 | 39 | // ˄ 40 | -------------------------------------------------------------------------------- /structural_patterns/facade/facade.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/NumberSubject.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/observer/NumberSubject.h" 4 | 5 | // ˄ 6 | 7 | NumberSubject::NumberSubject() 8 | : value(0) 9 | // ˅ 10 | 11 | // ˄ 12 | { 13 | // ˅ 14 | 15 | // ˄ 16 | } 17 | 18 | NumberSubject::~NumberSubject() 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | } 24 | 25 | void NumberSubject::setValue(int value) 26 | { 27 | // ˅ 28 | // Notify observers when the value is set. 29 | this->value = value; 30 | notifyObservers(); 31 | // ˄ 32 | } 33 | 34 | int NumberSubject::getValue() const 35 | { 36 | // ˅ 37 | return value; 38 | // ˄ 39 | } 40 | 41 | // ˅ 42 | 43 | // ˄ 44 | -------------------------------------------------------------------------------- /behavioral_patterns/state/WindowsForm.cpp: -------------------------------------------------------------------------------- 1 | #include "WindowsForm.h" 2 | 3 | System::Windows::Forms::TextBox^ state::WindowsForm::getTextTime() 4 | { 5 | return this->text_time; 6 | } 7 | 8 | System::Windows::Forms::TextBox^ state::WindowsForm::getTextMessage() 9 | { 10 | return this->text_message; 11 | } 12 | 13 | System::Windows::Forms::Button^ state::WindowsForm::getButtonUse() 14 | { 15 | return this->button_use; 16 | } 17 | 18 | System::Windows::Forms::Button^ state::WindowsForm::getButtonAlarm() 19 | { 20 | return this->button_alarm; 21 | } 22 | 23 | System::Windows::Forms::Button^ state::WindowsForm::getButtonPhone() 24 | { 25 | return this->button_phone; 26 | } 27 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/Visitor.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_VISITOR_VISITOR_H_ 6 | #define BEHAVIORAL_PATTERNS_VISITOR_VISITOR_H_ 7 | 8 | // ˅ 9 | class File; 10 | class Directory; 11 | 12 | // ˄ 13 | 14 | class Visitor 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | 20 | public: 21 | 22 | virtual ~Visitor(); 23 | 24 | virtual void visit(const File* file) = 0; 25 | 26 | virtual void visit(const Directory* directory) = 0; 27 | 28 | // ˅ 29 | public: 30 | 31 | protected: 32 | 33 | private: 34 | 35 | // ˄ 36 | }; 37 | 38 | // ˅ 39 | 40 | // ˄ 41 | 42 | #endif // BEHAVIORAL_PATTERNS_VISITOR_VISITOR_H_ 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | -------------------------------------------------------------------------------- /creational_patterns/builder/builder.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I. -I../.. -std=c++11 2 | 3 | SRC = \ 4 | factory/Data.cpp \ 5 | factory/Factory.cpp \ 6 | factory/Item.cpp \ 7 | factory/Link.cpp \ 8 | factory/Page.cpp \ 9 | list_factory/ListData.cpp \ 10 | list_factory/ListFactory.cpp \ 11 | list_factory/ListLink.cpp \ 12 | list_factory/ListPage.cpp \ 13 | table_factory/TableData.cpp \ 14 | table_factory/TableFactory.cpp \ 15 | table_factory/TableLink.cpp \ 16 | table_factory/TablePage.cpp \ 17 | main.cpp 18 | 19 | PROG = abstract_factory.exe 20 | 21 | .PHONY: all 22 | all: $(SRC) 23 | g++ $(CFLAGS) $(SRC) -o $(PROG) 24 | 25 | .PHONY: clean 26 | clean: 27 | rm -rf $(PROG) 28 | -------------------------------------------------------------------------------- /structural_patterns/bridge/DisplayImpl.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_BRIDGE_DISPLAYIMPL_H_ 6 | #define STRUCTURAL_PATTERNS_BRIDGE_DISPLAYIMPL_H_ 7 | 8 | // ˅ 9 | 10 | // ˄ 11 | 12 | class DisplayImpl 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | 18 | public: 19 | 20 | virtual ~DisplayImpl(); 21 | 22 | virtual void implOpen() const = 0; 23 | 24 | virtual void implWrite() const = 0; 25 | 26 | virtual void implClose() const = 0; 27 | 28 | // ˅ 29 | public: 30 | 31 | protected: 32 | 33 | private: 34 | 35 | // ˄ 36 | }; 37 | 38 | // ˅ 39 | 40 | // ˄ 41 | 42 | #endif // STRUCTURAL_PATTERNS_BRIDGE_DISPLAYIMPL_H_ 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | -------------------------------------------------------------------------------- /structural_patterns/bridge/bridge.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/Observer.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_OBSERVER_OBSERVER_H_ 6 | #define BEHAVIORAL_PATTERNS_OBSERVER_OBSERVER_H_ 7 | 8 | // ˅ 9 | class Subject; 10 | 11 | // ˄ 12 | 13 | // Defines an updating interface for objects that should be notified of changes in a subject. 14 | class Observer 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | 20 | public: 21 | 22 | virtual ~Observer(); 23 | 24 | virtual void update(const Subject* changedSubject) const = 0; 25 | 26 | // ˅ 27 | public: 28 | 29 | protected: 30 | 31 | private: 32 | 33 | // ˄ 34 | }; 35 | 36 | // ˅ 37 | 38 | // ˄ 39 | 40 | #endif // BEHAVIORAL_PATTERNS_OBSERVER_OBSERVER_H_ 41 | 42 | // ˅ 43 | 44 | // ˄ 45 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Node.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_INTERPRETER_NODE_H_ 6 | #define BEHAVIORAL_PATTERNS_INTERPRETER_NODE_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | class Context; 12 | 13 | using namespace std; 14 | 15 | // ˄ 16 | 17 | // Node in the syntax tree. 18 | class Node 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | public: 25 | 26 | virtual ~Node(); 27 | 28 | virtual void parse(Context* context) = 0; 29 | 30 | virtual const string toString() const = 0; 31 | 32 | // ˅ 33 | public: 34 | 35 | protected: 36 | 37 | private: 38 | 39 | // ˄ 40 | }; 41 | 42 | // ˅ 43 | 44 | // ˄ 45 | 46 | #endif // BEHAVIORAL_PATTERNS_INTERPRETER_NODE_H_ 47 | 48 | // ˅ 49 | 50 | // ˄ 51 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/AbstractDisplay.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_ABSTRACTDISPLAY_H_ 6 | #define BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_ABSTRACTDISPLAY_H_ 7 | 8 | // ˅ 9 | 10 | // ˄ 11 | 12 | class AbstractDisplay 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | 18 | public: 19 | 20 | virtual ~AbstractDisplay(); 21 | 22 | virtual void open() = 0; 23 | 24 | virtual void write() = 0; 25 | 26 | virtual void close() = 0; 27 | 28 | void output(); 29 | 30 | // ˅ 31 | public: 32 | 33 | protected: 34 | 35 | private: 36 | 37 | // ˄ 38 | }; 39 | 40 | // ˅ 41 | 42 | // ˄ 43 | 44 | #endif // BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_ABSTRACTDISPLAY_H_ 45 | 46 | // ˅ 47 | 48 | // ˄ 49 | -------------------------------------------------------------------------------- /structural_patterns/adapter/MessageDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "structural_patterns/adapter/MessageDisplay.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | MessageDisplay::MessageDisplay(const string& message) 10 | : message(message) 11 | // ˅ 12 | 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | MessageDisplay::~MessageDisplay() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | void MessageDisplay::displayWithHyphens() const 28 | { 29 | // ˅ 30 | cout << "-- " << message << " --" << endl; 31 | // ˄ 32 | } 33 | 34 | void MessageDisplay::displayWithBrackets() const 35 | { 36 | // ˅ 37 | cout << "[[ " << message << " ]]" << endl; 38 | // ˄ 39 | } 40 | 41 | // ˅ 42 | 43 | // ˄ 44 | -------------------------------------------------------------------------------- /structural_patterns/proxy/Printer.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_PROXY_PRINTER_H_ 6 | #define STRUCTURAL_PATTERNS_PROXY_PRINTER_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | class Printer 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | public: 22 | 23 | virtual ~Printer(); 24 | 25 | virtual const string getName() const = 0; 26 | 27 | virtual void changeName(const string& name) = 0; 28 | 29 | virtual void output(const string& content) = 0; 30 | 31 | // ˅ 32 | public: 33 | 34 | protected: 35 | 36 | private: 37 | 38 | // ˄ 39 | }; 40 | 41 | // ˅ 42 | 43 | // ˄ 44 | 45 | #endif // STRUCTURAL_PATTERNS_PROXY_PRINTER_H_ 46 | 47 | // ˅ 48 | 49 | // ˄ 50 | -------------------------------------------------------------------------------- /creational_patterns/prototype/framework/Display.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_PROTOTYPE_FRAMEWORK_DISPLAY_H_ 6 | #define CREATIONAL_PATTERNS_PROTOTYPE_FRAMEWORK_DISPLAY_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | class Display; 12 | 13 | using namespace std; 14 | 15 | // ˄ 16 | 17 | class Display 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | public: 24 | 25 | virtual ~Display(); 26 | 27 | virtual Display* clone() = 0; 28 | 29 | virtual void show(const string& message) const = 0; 30 | 31 | // ˅ 32 | public: 33 | 34 | protected: 35 | 36 | private: 37 | 38 | // ˄ 39 | }; 40 | 41 | // ˅ 42 | 43 | // ˄ 44 | 45 | #endif // CREATIONAL_PATTERNS_PROTOTYPE_FRAMEWORK_DISPLAY_H_ 46 | 47 | // ˅ 48 | 49 | // ˄ 50 | -------------------------------------------------------------------------------- /structural_patterns/decorator/decorator.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/iterator.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/Colleague.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUE_H_ 6 | #define BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUE_H_ 7 | 8 | // ˅ 9 | class Mediator; 10 | 11 | 12 | // ˄ 13 | 14 | class Colleague 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | 20 | protected: 21 | 22 | Mediator* mediator; 23 | 24 | public: 25 | 26 | Colleague(Mediator* mediator); 27 | 28 | ~Colleague(); 29 | 30 | // Set enable/disable from the Mediator 31 | virtual void setActivation(const bool is_enable) = 0; 32 | 33 | // ˅ 34 | public: 35 | 36 | protected: 37 | 38 | private: 39 | 40 | // ˄ 41 | }; 42 | 43 | // ˅ 44 | 45 | // ˄ 46 | 47 | #endif // BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUE_H_ 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Item.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_ITEM_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_ITEM_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | class Item 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | protected: 22 | 23 | const string name; 24 | 25 | public: 26 | 27 | Item(const string& name); 28 | 29 | virtual ~Item(); 30 | 31 | virtual const string toHTML() const = 0; 32 | 33 | // ˅ 34 | public: 35 | 36 | protected: 37 | 38 | private: 39 | 40 | // ˄ 41 | }; 42 | 43 | // ˅ 44 | 45 | // ˄ 46 | 47 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_ITEM_H_ 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | -------------------------------------------------------------------------------- /structural_patterns/composite/File.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "structural_patterns/composite/File.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | File::File(const string& name, const int size) 10 | : name(name) 11 | , size(size) 12 | // ˅ 13 | 14 | // ˄ 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | } 20 | 21 | File::~File() 22 | { 23 | // ˅ 24 | 25 | // ˄ 26 | } 27 | 28 | const string File::getName() const 29 | { 30 | // ˅ 31 | return name; 32 | // ˄ 33 | } 34 | 35 | const int File::getSize() const 36 | { 37 | // ˅ 38 | return size; 39 | // ˄ 40 | } 41 | 42 | void File::print(const string& upper_path) const 43 | { 44 | // ˅ 45 | cout << upper_path << "/" << toString() << endl; 46 | // ˄ 47 | } 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/Book.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_ITERATOR_BOOK_H_ 6 | #define BEHAVIORAL_PATTERNS_ITERATOR_BOOK_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | class Book 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | public: 22 | 23 | const string title; 24 | 25 | Book(const string& title); 26 | 27 | ~Book(); 28 | 29 | // ˅ 30 | public: 31 | 32 | protected: 33 | 34 | private: 35 | Book(const Book&) = delete; 36 | Book& operator=(const Book&) = delete; 37 | Book(Book&&) = delete; 38 | Book& operator=(Book&&) = delete; 39 | 40 | // ˄ 41 | }; 42 | 43 | // ˅ 44 | 45 | // ˄ 46 | 47 | #endif // BEHAVIORAL_PATTERNS_ITERATOR_BOOK_H_ 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | -------------------------------------------------------------------------------- /behavioral_patterns/command/PaintingCommand.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/command/PaintingCommand.h" 3 | #include "behavioral_patterns/command/PaintingTarget.h" 4 | 5 | 6 | // ˄ 7 | 8 | PaintingCommand::PaintingCommand(const PaintingTarget* painting_target, const double painting_pos_x, const double painting_pos_y) 9 | : painting_pos_x(painting_pos_x) 10 | , painting_pos_y(painting_pos_y) 11 | , painting_target(painting_target) 12 | // ˅ 13 | 14 | // ˄ 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | } 20 | 21 | PaintingCommand::~PaintingCommand() 22 | { 23 | // ˅ 24 | 25 | // ˄ 26 | } 27 | 28 | void PaintingCommand::execute() const 29 | { 30 | // ˅ 31 | painting_target->paint(painting_pos_x, painting_pos_y); 32 | // ˄ 33 | } 34 | 35 | // ˅ 36 | 37 | // ˄ 38 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/observer.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/File.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/visitor/File.h" 3 | #include "behavioral_patterns/visitor/Visitor.h" 4 | #include "behavioral_patterns/visitor/FileSystemElement.h" 5 | 6 | // ˄ 7 | 8 | File::File(const string& name, const int size) 9 | : name(name) 10 | , size(size) 11 | // ˅ 12 | 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | File::~File() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | void File::accept(Visitor* visitor) 28 | { 29 | // ˅ 30 | visitor->visit(this); 31 | // ˄ 32 | } 33 | 34 | const string File::getName() const 35 | { 36 | // ˅ 37 | return name; 38 | // ˄ 39 | } 40 | 41 | const int File::getSize() const 42 | { 43 | // ˅ 44 | return size; 45 | // ˄ 46 | } 47 | 48 | // ˅ 49 | 50 | // ˄ 51 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/framework/Factory.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/factory_method/framework/Factory.h" 3 | #include "creational_patterns/factory_method/framework/Product.h" 4 | 5 | // ˄ 6 | 7 | Factory::Factory() 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | Factory::~Factory() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | const Product* Factory::create(const string& owner) 25 | { 26 | // ˅ 27 | // Write pre-creation code here. 28 | 29 | // Encapsulate the knowledge of which Product subclass to create and move this knowledge out of the framework. 30 | const Product* product = createProduct(owner); 31 | 32 | // Write post-creation code here. 33 | 34 | return product; 35 | // ˄ 36 | } 37 | 38 | // ˅ 39 | 40 | // ˄ 41 | -------------------------------------------------------------------------------- /structural_patterns/bridge/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "structural_patterns/bridge/Display.h" 3 | #include "structural_patterns/bridge/MultiLineDisplay.h" 4 | #include "structural_patterns/bridge/TextDisplayImpl.h" 5 | 6 | using namespace std; 7 | 8 | /* 9 | Display only one line or display the specified number of lines. 10 | */ 11 | 12 | int main(int argc, char* argv[]) { 13 | unique_ptr display_impl1(new TextDisplayImpl("Japan")); 14 | unique_ptr d1(new Display(display_impl1.get())); 15 | d1->output(); 16 | 17 | unique_ptr display_impl2(new TextDisplayImpl("The United States of America")); 18 | unique_ptr d2(new MultiLineDisplay(display_impl2.get())); 19 | d2->output(); 20 | d2->outputMultiple(3); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/list_factory/ListData.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "creational_patterns/abstract_factory/list_factory/ListData.h" 4 | #include "creational_patterns/abstract_factory/factory/Item.h" 5 | 6 | using namespace std; 7 | 8 | // ˄ 9 | 10 | ListData::ListData(const string& name) 11 | // ˅ 12 | : Data(name) 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | ListData::~ListData() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | const string ListData::toHTML() const 28 | { 29 | // ˅ 30 | stringstream buffer; 31 | buffer << "
  • " << name << "
      " << endl; 32 | for (Item* item : items) { 33 | buffer << item->toHTML(); 34 | } 35 | buffer << "
  • " << endl; 36 | return buffer.str(); 37 | // ˄ 38 | } 39 | 40 | // ˅ 41 | 42 | // ˄ 43 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/MirrorStrategy.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/strategy/HandSignal.h" 4 | #include "behavioral_patterns/strategy/MirrorStrategy.h" 5 | 6 | // ˄ 7 | 8 | MirrorStrategy::MirrorStrategy() 9 | : preOpponentsHand(HandSignal::getHand(HandSignal::ROCK)) 10 | // ˅ 11 | 12 | // ˄ 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | } 18 | 19 | MirrorStrategy::~MirrorStrategy() 20 | { 21 | // ˅ 22 | 23 | // ˄ 24 | } 25 | 26 | HandSignal* MirrorStrategy::showHandSignal() 27 | { 28 | // ˅ 29 | return preOpponentsHand; 30 | // ˄ 31 | } 32 | 33 | void MirrorStrategy::notifyGameResult(GameResultType result, HandSignal* ownHand, HandSignal* opponentsHand) 34 | { 35 | // ˅ 36 | preOpponentsHand = opponentsHand; 37 | // ˄ 38 | } 39 | 40 | // ˅ 41 | 42 | // ˄ 43 | -------------------------------------------------------------------------------- /structural_patterns/proxy/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "structural_patterns/proxy/ProxyPrinter.h" 4 | 5 | using namespace std; 6 | 7 | /* 8 | Print on a named printer. Setting and changing the printer name is done by Proxy (ProxyPrinter). 9 | At the time of printing, create an instance of the RealSubject (RealPrinter) for the first time. 10 | */ 11 | 12 | int main(int argc, char* argv[]) { 13 | unique_ptr p(new ProxyPrinter("PRINTER-A")); 14 | cout << "The current printer is " << p->getName() << "." << endl; 15 | p->changeName("PRINTER-B"); 16 | cout << "The current printer is " << p->getName() << "." << endl; 17 | 18 | cout << "Print start." << endl; 19 | p->output("Nice to meet you."); 20 | cout << "Print exit." << endl; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /structural_patterns/bridge/Display.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/bridge/Display.h" 3 | #include "structural_patterns/bridge/DisplayImpl.h" 4 | 5 | // ˄ 6 | 7 | Display::Display(const DisplayImpl* impl) 8 | : impl(impl) 9 | // ˅ 10 | 11 | // ˄ 12 | { 13 | // ˅ 14 | 15 | // ˄ 16 | } 17 | 18 | Display::~Display() 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | } 24 | 25 | void Display::output() const 26 | { 27 | // ˅ 28 | open(); 29 | write(); 30 | close(); 31 | // ˄ 32 | } 33 | 34 | void Display::open() const 35 | { 36 | // ˅ 37 | impl->implOpen(); 38 | // ˄ 39 | } 40 | 41 | void Display::write() const 42 | { 43 | // ˅ 44 | impl->implWrite(); 45 | // ˄ 46 | } 47 | 48 | void Display::close() const 49 | { 50 | // ˅ 51 | impl->implClose(); 52 | // ˄ 53 | } 54 | 55 | // ˅ 56 | 57 | // ˄ 58 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/strategy.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /creational_patterns/builder/Builder.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_BUILDER_BUILDER_H_ 6 | #define CREATIONAL_PATTERNS_BUILDER_BUILDER_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class Builder 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | virtual ~Builder(); 25 | 26 | virtual void createTitle(const string& title) = 0; 27 | 28 | virtual void createSection(const string& section) = 0; 29 | 30 | virtual void createItems(const vector items) = 0; 31 | 32 | virtual void close() = 0; 33 | 34 | // ˅ 35 | public: 36 | 37 | protected: 38 | 39 | private: 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // CREATIONAL_PATTERNS_BUILDER_BUILDER_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/framework/Factory.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_FACTORY_METHOD_FRAMEWORK_FACTORY_H_ 6 | #define CREATIONAL_PATTERNS_FACTORY_METHOD_FRAMEWORK_FACTORY_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | class Product; 12 | 13 | using namespace std; 14 | 15 | // ˄ 16 | 17 | class Factory 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | public: 24 | 25 | Factory(); 26 | 27 | virtual ~Factory(); 28 | 29 | const Product* create(const string& owner); 30 | 31 | protected: 32 | 33 | virtual const Product* createProduct(const string& owner) = 0; 34 | 35 | // ˅ 36 | public: 37 | 38 | protected: 39 | 40 | private: 41 | 42 | // ˄ 43 | }; 44 | 45 | // ˅ 46 | 47 | // ˄ 48 | 49 | #endif // CREATIONAL_PATTERNS_FACTORY_METHOD_FRAMEWORK_FACTORY_H_ 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | -------------------------------------------------------------------------------- /structural_patterns/decorator/Frame.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_DECORATOR_FRAME_H_ 6 | #define STRUCTURAL_PATTERNS_DECORATOR_FRAME_H_ 7 | 8 | // ˅ 9 | #include "structural_patterns/decorator/Display.h" 10 | 11 | // ˄ 12 | 13 | class Frame : public Display 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | 19 | protected: 20 | 21 | const Display* display; 22 | 23 | Frame(const Display* display); 24 | 25 | public: 26 | 27 | virtual ~Frame(); 28 | 29 | // ˅ 30 | public: 31 | 32 | protected: 33 | 34 | private: 35 | Frame(const Frame&) = delete; 36 | Frame& operator=(const Frame&) = delete; 37 | Frame(Frame&&) = delete; 38 | Frame& operator=(Frame&&) = delete; 39 | 40 | // ˄ 41 | }; 42 | 43 | // ˅ 44 | 45 | // ˄ 46 | 47 | #endif // STRUCTURAL_PATTERNS_DECORATOR_FRAME_H_ 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | -------------------------------------------------------------------------------- /structural_patterns/decorator/MessageDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/decorator/MessageDisplay.h" 3 | 4 | // ˄ 5 | 6 | MessageDisplay::MessageDisplay(const string& message) 7 | : message(message) 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | MessageDisplay::~MessageDisplay() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | const int MessageDisplay::getColumns() const 25 | { 26 | // ˅ 27 | return static_cast(message.size()); 28 | // ˄ 29 | } 30 | 31 | const int MessageDisplay::getRows() const 32 | { 33 | // ˅ 34 | return 1; 35 | // ˄ 36 | } 37 | 38 | const string MessageDisplay::getLineText(const int row) const 39 | { 40 | // ˅ 41 | if (row == 0) { 42 | return message; 43 | } 44 | else { 45 | return ""; 46 | } 47 | // ˄ 48 | } 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /behavioral_patterns/iterator/BookShelfIterator.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/iterator/Book.h" 3 | #include "behavioral_patterns/iterator/BookShelf.h" 4 | #include "behavioral_patterns/iterator/BookShelfIterator.h" 5 | 6 | // ˄ 7 | 8 | BookShelfIterator::BookShelfIterator(const BookShelf* book_shelf) 9 | : index(0) 10 | , book_shelf(book_shelf) 11 | // ˅ 12 | 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | BookShelfIterator::~BookShelfIterator() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | bool BookShelfIterator::hasNext() const 28 | { 29 | // ˅ 30 | return index < book_shelf->getNumberOfBooks(); 31 | // ˄ 32 | } 33 | 34 | void* BookShelfIterator::next() 35 | { 36 | // ˅ 37 | Book* book = book_shelf->getAt(index); 38 | ++index; 39 | return book; 40 | // ˄ 41 | } 42 | 43 | // ˅ 44 | 45 | // ˄ 46 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/LargeSizeString.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/flyweight/LargeSizeChar.h" 3 | #include "structural_patterns/flyweight/LargeSizeCharFactory.h" 4 | #include "structural_patterns/flyweight/LargeSizeString.h" 5 | 6 | // ˄ 7 | 8 | LargeSizeString::LargeSizeString(const string& string_data) 9 | // ˅ 10 | 11 | // ˄ 12 | { 13 | // ˅ 14 | for (char char_data : string_data) { 15 | large_size_chars.push_back(LargeSizeCharFactory::getInstance()->getLargeSizeChar(char_data)); 16 | } 17 | // ˄ 18 | } 19 | 20 | LargeSizeString::~LargeSizeString() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | void LargeSizeString::display() const 28 | { 29 | // ˅ 30 | for (LargeSizeChar* large_size_char : large_size_chars) { 31 | large_size_char->display(); 32 | } 33 | // ˄ 34 | } 35 | 36 | // ˅ 37 | 38 | // ˄ 39 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/Strategy.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_STRATEGY_STRATEGY_H_ 6 | #define BEHAVIORAL_PATTERNS_STRATEGY_STRATEGY_H_ 7 | 8 | // ˅ 9 | #include "behavioral_patterns/strategy/GameResultType.h" 10 | 11 | class HandSignal; 12 | 13 | // ˄ 14 | 15 | class Strategy 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | public: 22 | 23 | virtual ~Strategy(); 24 | 25 | // Show a hand signal. 26 | virtual HandSignal* showHandSignal() = 0; 27 | 28 | // Notify a game result. 29 | virtual void notifyGameResult(GameResultType result, HandSignal* ownHand, HandSignal* opponentsHand) = 0; 30 | 31 | // ˅ 32 | public: 33 | 34 | protected: 35 | 36 | private: 37 | 38 | // ˄ 39 | }; 40 | 41 | // ˅ 42 | 43 | // ˄ 44 | 45 | #endif // BEHAVIORAL_PATTERNS_STRATEGY_STRATEGY_H_ 46 | 47 | // ˅ 48 | 49 | // ˄ 50 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/CharDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/template_method/CharDisplay.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | CharDisplay::CharDisplay(const char char_value) 10 | : char_value(char_value) 11 | // ˅ 12 | 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | CharDisplay::~CharDisplay() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | void CharDisplay::open() 28 | { 29 | // ˅ 30 | cout << "<<"; // Display "<<" in the start characters. 31 | // ˄ 32 | } 33 | 34 | void CharDisplay::write() 35 | { 36 | // ˅ 37 | cout << char_value; // Display the character. 38 | // ˄ 39 | } 40 | 41 | void CharDisplay::close() 42 | { 43 | // ˅ 44 | cout << ">>" << endl; // Display ">>" in the end characters. 45 | // ˄ 46 | } 47 | 48 | // ˅ 49 | 50 | // ˄ 51 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/visitor.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /structural_patterns/decorator/Display.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_DECORATOR_DISPLAY_H_ 6 | #define STRUCTURAL_PATTERNS_DECORATOR_DISPLAY_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | class Display 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | public: 22 | 23 | virtual ~Display(); 24 | 25 | // Column width 26 | virtual const int getColumns() const = 0; 27 | 28 | // Number of rows 29 | virtual const int getRows() const = 0; 30 | 31 | virtual const string getLineText(const int row) const = 0; 32 | 33 | // Show all 34 | void show() const; 35 | 36 | // ˅ 37 | public: 38 | 39 | protected: 40 | 41 | private: 42 | 43 | // ˄ 44 | }; 45 | 46 | // ˅ 47 | 48 | // ˄ 49 | 50 | #endif // STRUCTURAL_PATTERNS_DECORATOR_DISPLAY_H_ 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | -------------------------------------------------------------------------------- /creational_patterns/builder/Director.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/builder/Builder.h" 3 | #include "creational_patterns/builder/Director.h" 4 | 5 | // ˄ 6 | 7 | Director::Director(Builder* builder) 8 | : builder(builder) 9 | // ˅ 10 | 11 | // ˄ 12 | { 13 | // ˅ 14 | 15 | // ˄ 16 | } 17 | 18 | Director::~Director() 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | } 24 | 25 | void Director::build() 26 | { 27 | // ˅ 28 | builder->createTitle("Greeting"); // Title 29 | builder->createSection("Morning and Afternoon"); // Section 30 | builder->createItems({"Good morning.", "Hello."}); // Items 31 | builder->createSection("Evening"); // Other section 32 | builder->createItems({"Good evening.", "Good night.", "Goodbye."}); // Other items 33 | builder->close(); 34 | // ˄ 35 | } 36 | 37 | // ˅ 38 | 39 | // ˄ 40 | -------------------------------------------------------------------------------- /creational_patterns/builder/Director.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_BUILDER_DIRECTOR_H_ 6 | #define CREATIONAL_PATTERNS_BUILDER_DIRECTOR_H_ 7 | 8 | // ˅ 9 | class Builder; 10 | 11 | // ˄ 12 | 13 | class Director 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | 19 | private: 20 | 21 | Builder* builder; 22 | 23 | public: 24 | 25 | Director(Builder* builder); 26 | 27 | ~Director(); 28 | 29 | // Construct a document 30 | void build(); 31 | 32 | // ˅ 33 | public: 34 | 35 | protected: 36 | 37 | private: 38 | Director(const Director&) = delete; 39 | Director& operator=(const Director&) = delete; 40 | Director(Director&&) = delete; 41 | Director& operator=(Director&&) = delete; 42 | 43 | // ˄ 44 | }; 45 | 46 | // ˅ 47 | 48 | // ˄ 49 | 50 | #endif // CREATIONAL_PATTERNS_BUILDER_DIRECTOR_H_ 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | -------------------------------------------------------------------------------- /behavioral_patterns/memento/Memento.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_MEMENTO_MEMENTO_H_ 6 | #define BEHAVIORAL_PATTERNS_MEMENTO_MEMENTO_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class Memento 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | // Money 25 | const int money; 26 | 27 | public: 28 | 29 | Memento(const int money); 30 | 31 | int getMoney() const; 32 | 33 | // ˅ 34 | public: 35 | 36 | protected: 37 | 38 | private: 39 | Memento(const Memento&) = delete; 40 | Memento& operator=(const Memento&) = delete; 41 | Memento(Memento&&) = delete; 42 | Memento& operator=(Memento&&) = delete; 43 | 44 | // ˄ 45 | }; 46 | 47 | // ˅ 48 | 49 | // ˄ 50 | 51 | #endif // BEHAVIORAL_PATTERNS_MEMENTO_MEMENTO_H_ 52 | 53 | // ˅ 54 | 55 | // ˄ 56 | -------------------------------------------------------------------------------- /behavioral_patterns/command/CLIWrapper.h: -------------------------------------------------------------------------------- 1 | #ifndef BEHAVIORAL_PATTERNS_COMMAND_CLIWRAPPER_H_ 2 | #define BEHAVIORAL_PATTERNS_COMMAND_CLIWRAPPER_H_ 3 | 4 | #include "AppMain.h" 5 | 6 | // Wrapper class for calling unmanaged code from managed code 7 | public ref class CLIWrapper 8 | { 9 | public: 10 | CLIWrapper(AppMain* app_main) : app_main(app_main) {} 11 | ~CLIWrapper() {} 12 | Void clickUndoButton(Object^ sender, EventArgs^ e) { 13 | app_main->clickUndoButton(sender, e); 14 | } 15 | Void clickClearButton(Object^ sender, EventArgs^ e) { 16 | app_main->clickClearButton(sender, e); 17 | } 18 | Void moveMouseOnTheCanvas(Object^ sender, MouseEventArgs^ e) { 19 | app_main->moveMouseOnTheCanvas(sender, e); 20 | } 21 | 22 | private: 23 | AppMain* app_main; // Pointer to the class of unmanaged code 24 | }; 25 | 26 | #endif // BEHAVIORAL_PATTERNS_COMMAND_CLIWRAPPER_H_ 27 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/Mediator.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_MEDIATOR_MEDIATOR_H_ 6 | #define BEHAVIORAL_PATTERNS_MEDIATOR_MEDIATOR_H_ 7 | 8 | // ˅ 9 | using namespace System; 10 | using namespace System::ComponentModel; 11 | using namespace System::Collections; 12 | using namespace System::Windows::Forms; 13 | using namespace System::Data; 14 | using namespace System::Drawing; 15 | 16 | // ˄ 17 | 18 | class Mediator 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | public: 25 | 26 | virtual ~Mediator(); 27 | 28 | virtual void createColleagues() = 0; 29 | 30 | virtual void colleagueChanged(Object^ sender, EventArgs^ e) = 0; 31 | 32 | // ˅ 33 | public: 34 | 35 | protected: 36 | 37 | private: 38 | 39 | // ˄ 40 | }; 41 | 42 | // ˅ 43 | 44 | // ˄ 45 | 46 | #endif // BEHAVIORAL_PATTERNS_MEDIATOR_MEDIATOR_H_ 47 | 48 | // ˅ 49 | 50 | // ˄ 51 | -------------------------------------------------------------------------------- /behavioral_patterns/state/state.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/FileSystemElement.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_VISITOR_FILESYSTEMELEMENT_H_ 6 | #define BEHAVIORAL_PATTERNS_VISITOR_FILESYSTEMELEMENT_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/visitor/Element.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class FileSystemElement : public Element 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | FileSystemElement(); 25 | 26 | virtual ~FileSystemElement(); 27 | 28 | virtual const string getName() const = 0; 29 | 30 | virtual const int getSize() const = 0; 31 | 32 | const string toString() const; 33 | 34 | // ˅ 35 | public: 36 | 37 | protected: 38 | 39 | private: 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // BEHAVIORAL_PATTERNS_VISITOR_FILESYSTEMELEMENT_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /structural_patterns/decorator/SideFrame.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/decorator/SideFrame.h" 3 | #include "structural_patterns/decorator/Display.h" 4 | 5 | // ˄ 6 | 7 | SideFrame::SideFrame(const Display* display, const char frame_char) 8 | : frame_char(frame_char) 9 | // ˅ 10 | , Frame(display) 11 | // ˄ 12 | { 13 | // ˅ 14 | 15 | // ˄ 16 | } 17 | 18 | SideFrame::~SideFrame() 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | } 24 | 25 | const int SideFrame::getColumns() const 26 | { 27 | // ˅ 28 | return 1 + display->getColumns() + 1; 29 | // ˄ 30 | } 31 | 32 | const int SideFrame::getRows() const 33 | { 34 | // ˅ 35 | return display->getRows(); 36 | // ˄ 37 | } 38 | 39 | const string SideFrame::getLineText(const int row) const 40 | { 41 | // ˅ 42 | return frame_char + display->getLineText(row) + frame_char; 43 | // ˄ 44 | } 45 | 46 | // ˅ 47 | 48 | // ˄ 49 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/WindowsForm.cpp: -------------------------------------------------------------------------------- 1 | #include "WindowsForm.h" 2 | 3 | System::Windows::Forms::RadioButton^ mediator::WindowsForm::getFormsRadioGuest() 4 | { 5 | return this->forms_radio_guest; 6 | } 7 | 8 | System::Windows::Forms::RadioButton^ mediator::WindowsForm::getFormsRadioLogin() 9 | { 10 | return this->forms_radio_login; 11 | } 12 | 13 | System::Windows::Forms::TextBox^ mediator::WindowsForm::getFormsTextUsername() 14 | { 15 | return this->forms_text_username; 16 | } 17 | 18 | System::Windows::Forms::TextBox^ mediator::WindowsForm::getFormsTextPassword() 19 | { 20 | return this->forms_text_password; 21 | } 22 | 23 | System::Windows::Forms::Button^ mediator::WindowsForm::getFormsButtonOk() 24 | { 25 | return this->forms_button_ok; 26 | } 27 | 28 | System::Windows::Forms::Button^ mediator::WindowsForm::getFormsButtonCancel() 29 | { 30 | return this->forms_button_cancel; 31 | } 32 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/DigitObserver.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/observer/DigitObserver.h" 4 | #include "behavioral_patterns/observer/Subject.h" 5 | #include "behavioral_patterns/observer/NumberSubject.h" 6 | 7 | using namespace std; 8 | 9 | // ˄ 10 | 11 | DigitObserver::DigitObserver(const NumberSubject* numberSubject) 12 | : numberSubject(numberSubject) 13 | // ˅ 14 | 15 | // ˄ 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | } 21 | 22 | DigitObserver::~DigitObserver() 23 | { 24 | // ˅ 25 | 26 | // ˄ 27 | } 28 | 29 | void DigitObserver::update(const Subject* changedSubject) const 30 | { 31 | // ˅ 32 | // Before processing, it checks to make sure the changed subject is the subject held. 33 | if (changedSubject == numberSubject) { 34 | cout << "Digit : " << numberSubject->getValue() << endl; 35 | } 36 | // ˄ 37 | } 38 | 39 | // ˅ 40 | 41 | // ˄ 42 | -------------------------------------------------------------------------------- /behavioral_patterns/state/State.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_STATE_STATE_H_ 6 | #define BEHAVIORAL_PATTERNS_STATE_STATE_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | class Context; 12 | 13 | using namespace std; 14 | 15 | 16 | // ˄ 17 | 18 | class State 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | public: 25 | 26 | virtual ~State(); 27 | 28 | virtual void setTime(Context* context, const int hour) = 0; 29 | 30 | virtual void use(const Context* context) const = 0; 31 | 32 | virtual void alarm(const Context* context) const = 0; 33 | 34 | virtual void phone(const Context* context) const = 0; 35 | 36 | virtual const string toString() const = 0; 37 | 38 | // ˅ 39 | public: 40 | 41 | protected: 42 | 43 | private: 44 | 45 | // ˄ 46 | }; 47 | 48 | // ˅ 49 | 50 | // ˄ 51 | 52 | #endif // BEHAVIORAL_PATTERNS_STATE_STATE_H_ 53 | 54 | // ˅ 55 | 56 | // ˄ 57 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/RandomStrategy.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/strategy/HandSignal.h" 4 | #include "behavioral_patterns/strategy/RandomStrategy.h" 5 | 6 | // ˄ 7 | 8 | RandomStrategy::RandomStrategy() 9 | // ˅ 10 | 11 | // ˄ 12 | { 13 | // ˅ 14 | 15 | // ˄ 16 | } 17 | 18 | RandomStrategy::~RandomStrategy() 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | } 24 | 25 | HandSignal* RandomStrategy::showHandSignal() 26 | { 27 | // ˅ 28 | std::random_device random; 29 | std::mt19937 mt(random()); 30 | std::uniform_int_distribution value(0, 2); 31 | const int random_hand_value = value(mt); 32 | return HandSignal::getHand(random_hand_value);; 33 | // ˄ 34 | } 35 | 36 | void RandomStrategy::notifyGameResult(GameResultType result, HandSignal* ownHand, HandSignal* opponentsHand) 37 | { 38 | // ˅ 39 | // Do nothing 40 | // ˄ 41 | } 42 | 43 | // ˅ 44 | 45 | // ˄ 46 | -------------------------------------------------------------------------------- /creational_patterns/prototype/framework/Manager.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/prototype/framework/Display.h" 3 | #include "creational_patterns/prototype/framework/Manager.h" 4 | 5 | // ˄ 6 | 7 | Manager::Manager() 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | Manager::~Manager() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | void Manager::registerDisplay(const string& display_name, Display* display) 25 | { 26 | // ˅ 27 | displays.insert(map::value_type(display_name, display)); 28 | // ˄ 29 | } 30 | 31 | const Display* Manager::getDisplay(const string& display_name) const 32 | { 33 | // ˅ 34 | Display* d = displays.at(display_name); 35 | return d->clone(); // Create a new object by asking a concrete class to clone itself. Therefore, do not need to know the concrete Display class name. 36 | // ˄ 37 | } 38 | 39 | // ˅ 40 | 41 | // ˄ 42 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/chain_of_responsibility.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/Trouble.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_TROUBLE_H_ 6 | #define BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_TROUBLE_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | class Trouble 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | public: 22 | 23 | // Trouble number 24 | const int id; 25 | 26 | Trouble(const int id); 27 | 28 | ~Trouble(); 29 | 30 | const string toString() const; 31 | 32 | // ˅ 33 | public: 34 | 35 | protected: 36 | 37 | private: 38 | Trouble(const Trouble&) = delete; 39 | Trouble& operator=(const Trouble&) = delete; 40 | Trouble(Trouble&&) = delete; 41 | Trouble& operator=(Trouble&&) = delete; 42 | 43 | // ˄ 44 | }; 45 | 46 | // ˅ 47 | 48 | // ˄ 49 | 50 | #endif // BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_TROUBLE_H_ 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Factory.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_FACTORY_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_FACTORY_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | class Page; 12 | class Link; 13 | class Data; 14 | 15 | using namespace std; 16 | 17 | // ˄ 18 | 19 | class Factory 20 | { 21 | // ˅ 22 | 23 | // ˄ 24 | 25 | public: 26 | 27 | virtual ~Factory(); 28 | 29 | virtual Page* createPage(const string& title, const string& author) = 0; 30 | 31 | virtual Link* createLink(const string& name, const string& url) = 0; 32 | 33 | virtual Data* createData(const string& name) = 0; 34 | 35 | // ˅ 36 | public: 37 | 38 | protected: 39 | 40 | private: 41 | 42 | // ˄ 43 | }; 44 | 45 | // ˅ 46 | 47 | // ˄ 48 | 49 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_FACTORY_H_ 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/Subject.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/observer/Subject.h" 3 | #include "behavioral_patterns/observer/Observer.h" 4 | 5 | // ˄ 6 | 7 | Subject::Subject() 8 | // ˅ 9 | 10 | // ˄ 11 | { 12 | // ˅ 13 | 14 | // ˄ 15 | } 16 | 17 | Subject::~Subject() 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | } 23 | 24 | void Subject::attach(Observer* observer) 25 | { 26 | // ˅ 27 | observers.push_back(observer); 28 | // ˄ 29 | } 30 | 31 | void Subject::detach(const Observer* observer) 32 | { 33 | // ˅ 34 | vector::iterator it = begin(observers); 35 | while (it != end(observers)) { 36 | if (*it == observer) { 37 | observers.erase(it); 38 | } 39 | ++it; 40 | } 41 | // ˄ 42 | } 43 | 44 | void Subject::notifyObservers() const 45 | { 46 | // ˅ 47 | for (Observer* observer : observers) { 48 | observer->update(this); 49 | } 50 | // ˄ 51 | } 52 | 53 | // ˅ 54 | 55 | // ˄ 56 | -------------------------------------------------------------------------------- /structural_patterns/composite/FileSystemElement.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_COMPOSITE_FILESYSTEMELEMENT_H_ 6 | #define STRUCTURAL_PATTERNS_COMPOSITE_FILESYSTEMELEMENT_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | class FileSystemElement 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | public: 22 | 23 | FileSystemElement(); 24 | 25 | virtual ~FileSystemElement(); 26 | 27 | virtual const string getName() const = 0; 28 | 29 | virtual const int getSize() const = 0; 30 | 31 | // Print this element with the "upper_path". 32 | virtual void print(const string& upper_path) const = 0; 33 | 34 | const string toString() const; 35 | 36 | // ˅ 37 | public: 38 | 39 | protected: 40 | 41 | private: 42 | 43 | // ˄ 44 | }; 45 | 46 | // ˅ 47 | 48 | // ˄ 49 | 50 | #endif // STRUCTURAL_PATTERNS_COMPOSITE_FILESYSTEMELEMENT_H_ 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | -------------------------------------------------------------------------------- /creational_patterns/prototype/UnderlineDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "creational_patterns/prototype/UnderlineDisplay.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | UnderlineDisplay::UnderlineDisplay(const char underline_char) 10 | : underline_char(underline_char) 11 | // ˅ 12 | 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | UnderlineDisplay::~UnderlineDisplay() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | Display* UnderlineDisplay::clone() 28 | { 29 | // ˅ 30 | return new UnderlineDisplay(underline_char); 31 | // ˄ 32 | } 33 | 34 | void UnderlineDisplay::show(const string& message) const 35 | { 36 | // ˅ 37 | const int length = static_cast(message.size()); 38 | cout << "\"" << message << "\"" << endl; 39 | cout << " "; 40 | for (int i = 0; i < length; ++i) { 41 | cout << underline_char; 42 | } 43 | cout << endl; 44 | // ˄ 45 | } 46 | 47 | // ˅ 48 | 49 | // ˄ 50 | -------------------------------------------------------------------------------- /behavioral_patterns/state/Context.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_STATE_CONTEXT_H_ 6 | #define BEHAVIORAL_PATTERNS_STATE_CONTEXT_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | class State; 12 | 13 | using namespace std; 14 | 15 | 16 | // ˄ 17 | 18 | class Context 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | public: 25 | 26 | virtual ~Context(); 27 | 28 | // Set time 29 | virtual void setTime(const int hour) = 0; 30 | 31 | // Change state 32 | virtual void changeState(State* state) = 0; 33 | 34 | // Call a security guard room 35 | virtual void callSecurityGuardsRoom(const string& msg) const = 0; 36 | 37 | // Record security log 38 | virtual void recordSecurityLog(const string& msg) const = 0; 39 | 40 | // ˅ 41 | public: 42 | 43 | protected: 44 | 45 | private: 46 | 47 | // ˄ 48 | }; 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | 54 | #endif // BEHAVIORAL_PATTERNS_STATE_CONTEXT_H_ 55 | 56 | // ˅ 57 | 58 | // ˄ 59 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Link.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_LINK_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_LINK_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Item.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class Link : public Item 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | protected: 23 | 24 | const string url; 25 | 26 | public: 27 | 28 | Link(const string& name, const string& url); 29 | 30 | virtual ~Link(); 31 | 32 | // ˅ 33 | public: 34 | 35 | protected: 36 | 37 | private: 38 | Link(const Link&) = delete; 39 | Link& operator=(const Link&) = delete; 40 | Link(Link&&) = delete; 41 | Link& operator=(Link&&) = delete; 42 | 43 | // ˄ 44 | }; 45 | 46 | // ˅ 47 | 48 | // ˄ 49 | 50 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_LINK_H_ 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | -------------------------------------------------------------------------------- /structural_patterns/bridge/Display.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_BRIDGE_DISPLAY_H_ 6 | #define STRUCTURAL_PATTERNS_BRIDGE_DISPLAY_H_ 7 | 8 | // ˅ 9 | class DisplayImpl; 10 | 11 | // ˄ 12 | 13 | class Display 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | 19 | private: 20 | 21 | const DisplayImpl* impl; 22 | 23 | public: 24 | 25 | Display(const DisplayImpl* impl); 26 | 27 | virtual ~Display(); 28 | 29 | void output() const; 30 | 31 | protected: 32 | 33 | void open() const; 34 | 35 | void write() const; 36 | 37 | void close() const; 38 | 39 | // ˅ 40 | public: 41 | 42 | protected: 43 | 44 | private: 45 | Display(const Display&) = delete; 46 | Display& operator=(const Display&) = delete; 47 | Display(Display&&) = delete; 48 | Display& operator=(Display&&) = delete; 49 | 50 | // ˄ 51 | }; 52 | 53 | // ˅ 54 | 55 | // ˄ 56 | 57 | #endif // STRUCTURAL_PATTERNS_BRIDGE_DISPLAY_H_ 58 | 59 | // ˅ 60 | 61 | // ˄ 62 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Command.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_INTERPRETER_COMMAND_H_ 6 | #define BEHAVIORAL_PATTERNS_INTERPRETER_COMMAND_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/interpreter/Node.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class Command : public Node 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | Node* node; 25 | 26 | public: 27 | 28 | Command(); 29 | 30 | ~Command(); 31 | 32 | void parse(Context* context); 33 | 34 | const string toString() const; 35 | 36 | // ˅ 37 | public: 38 | 39 | protected: 40 | 41 | private: 42 | Command(const Command&) = delete; 43 | Command& operator=(const Command&) = delete; 44 | Command(Command&&) = delete; 45 | Command& operator=(Command&&) = delete; 46 | 47 | // ˄ 48 | }; 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | 54 | #endif // BEHAVIORAL_PATTERNS_INTERPRETER_COMMAND_H_ 55 | 56 | // ˅ 57 | 58 | // ˄ 59 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Page.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_PAGE_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_PAGE_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | 12 | class Item; 13 | 14 | using namespace std; 15 | 16 | // ˄ 17 | 18 | class Page 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | protected: 25 | 26 | const string title; 27 | 28 | const string author; 29 | 30 | vector contents; 31 | 32 | public: 33 | 34 | Page(const string& title, const string& author); 35 | 36 | virtual ~Page(); 37 | 38 | virtual const string toHTML() const = 0; 39 | 40 | void add(Item* item); 41 | 42 | void output() const; 43 | 44 | // ˅ 45 | public: 46 | 47 | protected: 48 | 49 | private: 50 | 51 | // ˄ 52 | }; 53 | 54 | // ˅ 55 | 56 | // ˄ 57 | 58 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_PAGE_H_ 59 | 60 | // ˅ 61 | 62 | // ˄ 63 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Action.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/interpreter/Action.h" 4 | #include "behavioral_patterns/interpreter/Context.h" 5 | 6 | using namespace std; 7 | 8 | // ˄ 9 | 10 | Action::Action() 11 | : name("") 12 | // ˅ 13 | 14 | // ˄ 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | } 20 | 21 | Action::~Action() 22 | { 23 | // ˅ 24 | 25 | // ˄ 26 | } 27 | 28 | void Action::parse(Context* context) 29 | { 30 | // ˅ 31 | string current_token = context->getToken(); 32 | if (current_token != "forward" && current_token != "right" && current_token != "left") { 33 | cerr << current_token << " is unknown" << endl; 34 | exit(1); 35 | } 36 | 37 | name = current_token; // Hold the current token as this action name 38 | 39 | context->slideToken(current_token); 40 | // ˄ 41 | } 42 | 43 | const string Action::toString() const 44 | { 45 | // ˅ 46 | return name; 47 | // ˄ 48 | } 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/ColleagueButton.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/mediator/ColleagueButton.h" 3 | #include "behavioral_patterns/mediator/CLIWrapper.h" 4 | 5 | 6 | // ˄ 7 | 8 | ColleagueButton::ColleagueButton(const msclr::gcroot button, Mediator* mediator) 9 | : button(button) 10 | // ˅ 11 | , Colleague(mediator) 12 | // ˄ 13 | { 14 | // ˅ 15 | // Wrapper class for calling unmanaged code from managed code 16 | CLIWrapper^ cli_wrapper = gcnew CLIWrapper(mediator); 17 | 18 | button->Click += gcnew EventHandler(cli_wrapper, &CLIWrapper::colleagueChanged); 19 | // ˄ 20 | } 21 | 22 | ColleagueButton::~ColleagueButton() 23 | { 24 | // ˅ 25 | 26 | // ˄ 27 | } 28 | 29 | void ColleagueButton::setActivation(const bool is_enable) 30 | { 31 | // ˅ 32 | button->Enabled = is_enable; 33 | // ˄ 34 | } 35 | 36 | bool ColleagueButton::isPressed() const 37 | { 38 | // ˅ 39 | return button->Focused; 40 | // ˄ 41 | } 42 | 43 | // ˅ 44 | 45 | // ˄ 46 | -------------------------------------------------------------------------------- /behavioral_patterns/state/CLIWrapper.h: -------------------------------------------------------------------------------- 1 | #ifndef BEHAVIORAL_PATTERNS_STATE_CLIWRAPPER_H_ 2 | #define BEHAVIORAL_PATTERNS_STATE_CLIWRAPPER_H_ 3 | 4 | #include "AppSafe.h" 5 | 6 | // Wrapper class for calling unmanaged code from managed code 7 | public ref class CLIWrapper 8 | { 9 | public: 10 | CLIWrapper(AppSafe* native_app_safe) : native_app_safe(native_app_safe) {} 11 | ~CLIWrapper() {} 12 | Void pressedUseButton(Object^ sender, EventArgs^ e) { 13 | native_app_safe->pressedUseButton(); 14 | } 15 | Void pressedAlarmButton(Object^ sender, EventArgs^ e) { 16 | native_app_safe->pressedAlarmButton(); 17 | } 18 | Void pressedPhoneButton(Object^ sender, EventArgs^ e) { 19 | native_app_safe->pressedPhoneButton(); 20 | } 21 | Void countUpTime(Object^ sender, EventArgs^ e) { 22 | native_app_safe->countUpTime(); 23 | } 24 | 25 | private: 26 | AppSafe* native_app_safe; // Pointer to the class of unmanaged code 27 | }; 28 | 29 | #endif // BEHAVIORAL_PATTERNS_STATE_CLIWRAPPER_H_ 30 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/interpreter.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/factory/Data.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_DATA_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_DATA_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | #include "creational_patterns/abstract_factory/factory/Item.h" 12 | 13 | using namespace std; 14 | 15 | // ˄ 16 | 17 | class Data : public Item 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | protected: 24 | 25 | vector items; 26 | 27 | public: 28 | 29 | Data(const string& name); 30 | 31 | virtual ~Data(); 32 | 33 | void add(Item* item); 34 | 35 | // ˅ 36 | public: 37 | 38 | protected: 39 | 40 | private: 41 | Data(const Data&) = delete; 42 | Data& operator=(const Data&) = delete; 43 | Data(Data&&) = delete; 44 | Data& operator=(Data&&) = delete; 45 | 46 | // ˄ 47 | }; 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | 53 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_FACTORY_DATA_H_ 54 | 55 | // ˅ 56 | 57 | // ˄ 58 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/list_factory/ListData.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTDATA_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTDATA_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Data.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class ListData : public Data 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | ListData(const string& name); 25 | 26 | ~ListData(); 27 | 28 | const string toHTML() const; 29 | 30 | // ˅ 31 | public: 32 | 33 | protected: 34 | 35 | private: 36 | ListData(const ListData&) = delete; 37 | ListData& operator=(const ListData&) = delete; 38 | ListData(ListData&&) = delete; 39 | ListData& operator=(ListData&&) = delete; 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTDATA_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/BarChartObserver.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/observer/BarChartObserver.h" 4 | #include "behavioral_patterns/observer/Subject.h" 5 | #include "behavioral_patterns/observer/NumberSubject.h" 6 | 7 | using namespace std; 8 | 9 | // ˄ 10 | 11 | BarChartObserver::BarChartObserver(const NumberSubject* numberSubject) 12 | : numberSubject(numberSubject) 13 | // ˅ 14 | 15 | // ˄ 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | } 21 | 22 | BarChartObserver::~BarChartObserver() 23 | { 24 | // ˅ 25 | 26 | // ˄ 27 | } 28 | 29 | void BarChartObserver::update(const Subject* changedSubject) const 30 | { 31 | // ˅ 32 | // Before processing, it checks to make sure the changed subject is the subject held. 33 | if (changedSubject == numberSubject) { 34 | cout << "Bar chart: "; 35 | for (int i = 0; i < numberSubject->getValue(); ++i) { 36 | cout << "*"; 37 | } 38 | cout << endl; 39 | } 40 | // ˄ 41 | } 42 | 43 | // ˅ 44 | 45 | // ˄ 46 | -------------------------------------------------------------------------------- /creational_patterns/prototype/FrameDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "creational_patterns/prototype/FrameDisplay.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | FrameDisplay::FrameDisplay(const char border_char) 10 | : border_char(border_char) 11 | // ˅ 12 | 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | FrameDisplay::~FrameDisplay() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | Display* FrameDisplay::clone() 28 | { 29 | // ˅ 30 | return new FrameDisplay(border_char); 31 | // ˄ 32 | } 33 | 34 | void FrameDisplay::show(const string& message) const 35 | { 36 | // ˅ 37 | const int length = static_cast(message.size()); 38 | for (int i = 0; i < length + 4; ++i) { 39 | cout << border_char; 40 | } 41 | cout << endl; 42 | cout << border_char << " " << message << " " << border_char << endl; 43 | for (int i = 0; i < length + 4; ++i) { 44 | cout << border_char; 45 | } 46 | cout << endl; 47 | // ˄ 48 | } 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "creational_patterns/factory_method/credit_card/CreditCardFactory.h" 4 | #include "creational_patterns/factory_method/credit_card/CreditCard.h" 5 | 6 | using namespace std; 7 | 8 | /* 9 | The subject is a factory to make credit cards. The Factory defines how to create a credit card, 10 | but the actual credit card is created by the CreditCardFactory. 11 | The "createProduct()" is called a Factory Method, and it is responsible for manufacturing an object. 12 | */ 13 | 14 | int main(int argc, char* argv[]) { 15 | unique_ptr factory(new CreditCardFactory()); 16 | 17 | unique_ptr jackson_card(factory->create("Jackson")); 18 | jackson_card->use(); 19 | 20 | unique_ptr sophia_card(factory->create("Sophia")); 21 | sophia_card->use(); 22 | 23 | unique_ptr olivia_card(factory->create("Olivia")); 24 | olivia_card->use(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /behavioral_patterns/command/command.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Action.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_INTERPRETER_ACTION_H_ 6 | #define BEHAVIORAL_PATTERNS_INTERPRETER_ACTION_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/interpreter/Node.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | // A node corresponding to "forward", "left", and "right". 17 | class Action : public Node 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | private: 24 | 25 | string name; 26 | 27 | public: 28 | 29 | Action(); 30 | 31 | ~Action(); 32 | 33 | void parse(Context* context); 34 | 35 | const string toString() const; 36 | 37 | // ˅ 38 | public: 39 | 40 | protected: 41 | 42 | private: 43 | Action(const Action&) = delete; 44 | Action& operator=(const Action&) = delete; 45 | Action(Action&&) = delete; 46 | Action& operator=(Action&&) = delete; 47 | 48 | // ˄ 49 | }; 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | 55 | #endif // BEHAVIORAL_PATTERNS_INTERPRETER_ACTION_H_ 56 | 57 | // ˅ 58 | 59 | // ˄ 60 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/list_factory/ListLink.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTLINK_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTLINK_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Link.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class ListLink : public Link 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | ListLink(const string& name, const string& url); 25 | 26 | ~ListLink(); 27 | 28 | const string toHTML() const; 29 | 30 | // ˅ 31 | public: 32 | 33 | protected: 34 | 35 | private: 36 | ListLink(const ListLink&) = delete; 37 | ListLink& operator=(const ListLink&) = delete; 38 | ListLink(ListLink&&) = delete; 39 | ListLink& operator=(ListLink&&) = delete; 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTLINK_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/table_factory/TableData.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEDATA_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEDATA_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Data.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class TableData : public Data 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | TableData(const string& name); 25 | 26 | ~TableData(); 27 | 28 | const string toHTML() const; 29 | 30 | // ˅ 31 | public: 32 | 33 | protected: 34 | 35 | private: 36 | TableData(const TableData&) = delete; 37 | TableData& operator=(const TableData&) = delete; 38 | TableData(TableData&&) = delete; 39 | TableData& operator=(TableData&&) = delete; 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEDATA_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Command.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/interpreter/Command.h" 3 | #include "behavioral_patterns/interpreter/Context.h" 4 | #include "behavioral_patterns/interpreter/Repeat.h" 5 | #include "behavioral_patterns/interpreter/Action.h" 6 | 7 | // ˄ 8 | 9 | Command::Command() 10 | : node(nullptr) 11 | // ˅ 12 | 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | Command::~Command() 21 | { 22 | // ˅ 23 | if (node != nullptr) { 24 | delete node; 25 | } 26 | // ˄ 27 | } 28 | 29 | void Command::parse(Context* context) 30 | { 31 | // ˅ 32 | Node* a_node; 33 | if (context->getToken() == "repeat") { 34 | a_node = new Repeat(); 35 | a_node->parse(context); 36 | } 37 | else { 38 | a_node = new Action(); 39 | a_node->parse(context); 40 | } 41 | 42 | node = a_node; // Hold the parsed node 43 | // ˄ 44 | } 45 | 46 | const string Command::toString() const 47 | { 48 | // ˅ 49 | return node->toString(); 50 | // ˄ 51 | } 52 | 53 | // ˅ 54 | 55 | // ˄ 56 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Program.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_INTERPRETER_PROGRAM_H_ 6 | #define BEHAVIORAL_PATTERNS_INTERPRETER_PROGRAM_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/interpreter/Node.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | // A node corresponding to "program". 17 | class Program : public Node 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | private: 24 | 25 | Node* command_list; 26 | 27 | public: 28 | 29 | Program(); 30 | 31 | ~Program(); 32 | 33 | void parse(Context* context); 34 | 35 | const string toString() const; 36 | 37 | // ˅ 38 | public: 39 | 40 | protected: 41 | 42 | private: 43 | Program(const Program&) = delete; 44 | Program& operator=(const Program&) = delete; 45 | Program(Program&&) = delete; 46 | Program& operator=(Program&&) = delete; 47 | 48 | // ˄ 49 | }; 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | 55 | #endif // BEHAVIORAL_PATTERNS_INTERPRETER_PROGRAM_H_ 56 | 57 | // ˅ 58 | 59 | // ˄ 60 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/list_factory/ListPage.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTPAGE_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTPAGE_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Page.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class ListPage : public Page 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | ListPage(const string& title, const string& author); 25 | 26 | ~ListPage(); 27 | 28 | const string toHTML() const; 29 | 30 | // ˅ 31 | public: 32 | 33 | protected: 34 | 35 | private: 36 | ListPage(const ListPage&) = delete; 37 | ListPage& operator=(const ListPage&) = delete; 38 | ListPage(ListPage&&) = delete; 39 | ListPage& operator=(ListPage&&) = delete; 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTPAGE_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Repeat.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_INTERPRETER_REPEAT_H_ 6 | #define BEHAVIORAL_PATTERNS_INTERPRETER_REPEAT_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/interpreter/Node.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | // A node corresponding to "repeat". 17 | class Repeat : public Node 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | private: 24 | 25 | int number; 26 | 27 | Node* command_list; 28 | 29 | public: 30 | 31 | Repeat(); 32 | 33 | ~Repeat(); 34 | 35 | void parse(Context* context); 36 | 37 | const string toString() const; 38 | 39 | // ˅ 40 | public: 41 | 42 | protected: 43 | 44 | private: 45 | Repeat(const Repeat&) = delete; 46 | Repeat& operator=(const Repeat&) = delete; 47 | Repeat(Repeat&&) = delete; 48 | Repeat& operator=(Repeat&&) = delete; 49 | 50 | // ˄ 51 | }; 52 | 53 | // ˅ 54 | 55 | // ˄ 56 | 57 | #endif // BEHAVIORAL_PATTERNS_INTERPRETER_REPEAT_H_ 58 | 59 | // ˅ 60 | 61 | // ˄ 62 | -------------------------------------------------------------------------------- /structural_patterns/adapter/MessageDisplay.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_ADAPTER_MESSAGEDISPLAY_H_ 6 | #define STRUCTURAL_PATTERNS_ADAPTER_MESSAGEDISPLAY_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | class MessageDisplay 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | private: 22 | 23 | const string message; 24 | 25 | public: 26 | 27 | MessageDisplay(const string& message); 28 | 29 | virtual ~MessageDisplay(); 30 | 31 | void displayWithHyphens() const; 32 | 33 | void displayWithBrackets() const; 34 | 35 | // ˅ 36 | public: 37 | 38 | protected: 39 | 40 | private: 41 | MessageDisplay(const MessageDisplay&) = delete; 42 | MessageDisplay& operator=(const MessageDisplay&) = delete; 43 | MessageDisplay(MessageDisplay&&) = delete; 44 | MessageDisplay& operator=(MessageDisplay&&) = delete; 45 | 46 | // ˄ 47 | }; 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | 53 | #endif // STRUCTURAL_PATTERNS_ADAPTER_MESSAGEDISPLAY_H_ 54 | 55 | // ˅ 56 | 57 | // ˄ 58 | -------------------------------------------------------------------------------- /structural_patterns/decorator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "structural_patterns/decorator/Display.h" 3 | #include "structural_patterns/decorator/MessageDisplay.h" 4 | #include "structural_patterns/decorator/SideFrame.h" 5 | #include "structural_patterns/decorator/FullFrame.h" 6 | 7 | using namespace std; 8 | 9 | /* 10 | Display a string with decorative frames. The frames can be combined arbitrarily. 11 | */ 12 | 13 | int main(int argc, char* argv[]) { 14 | unique_ptr display_a(new MessageDisplay("Nice to meet you.")); 15 | display_a->show(); 16 | 17 | unique_ptr diaplay_b1(new MessageDisplay("Nice to meet you.")); 18 | unique_ptr display_b2(new SideFrame(diaplay_b1.get(), '!')); 19 | display_b2->show(); 20 | 21 | unique_ptr display_c1(new MessageDisplay("Nice to meet you.")); 22 | unique_ptr display_c2(new SideFrame(display_c1.get(), '!')); 23 | unique_ptr display_c3(new FullFrame(display_c2.get())); 24 | display_c3->show(); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/mediator.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/table_factory/TableLink.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLELINK_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLELINK_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Link.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class TableLink : public Link 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | TableLink(const string& name, const string& url); 25 | 26 | ~TableLink(); 27 | 28 | const string toHTML() const; 29 | 30 | // ˅ 31 | public: 32 | 33 | protected: 34 | 35 | private: 36 | TableLink(const TableLink&) = delete; 37 | TableLink& operator=(const TableLink&) = delete; 38 | TableLink(TableLink&&) = delete; 39 | TableLink& operator=(TableLink&&) = delete; 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLELINK_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/CharDisplay.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_CHARDISPLAY_H_ 6 | #define BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_CHARDISPLAY_H_ 7 | 8 | // ˅ 9 | #include "behavioral_patterns/template_method/AbstractDisplay.h" 10 | 11 | // ˄ 12 | 13 | class CharDisplay : public AbstractDisplay 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | 19 | private: 20 | 21 | const char char_value; 22 | 23 | public: 24 | 25 | CharDisplay(const char char_value); 26 | 27 | ~CharDisplay(); 28 | 29 | void open(); 30 | 31 | void write(); 32 | 33 | void close(); 34 | 35 | // ˅ 36 | public: 37 | 38 | protected: 39 | 40 | private: 41 | CharDisplay(const CharDisplay&) = delete; 42 | CharDisplay& operator=(const CharDisplay&) = delete; 43 | CharDisplay(CharDisplay&&) = delete; 44 | CharDisplay& operator=(CharDisplay&&) = delete; 45 | 46 | // ˄ 47 | }; 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | 53 | #endif // BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_CHARDISPLAY_H_ 54 | 55 | // ˅ 56 | 57 | // ˄ 58 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/list_factory/ListFactory.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/abstract_factory/list_factory/ListData.h" 3 | #include "creational_patterns/abstract_factory/list_factory/ListFactory.h" 4 | #include "creational_patterns/abstract_factory/list_factory/ListLink.h" 5 | #include "creational_patterns/abstract_factory/list_factory/ListPage.h" 6 | 7 | // ˄ 8 | 9 | ListFactory::ListFactory() 10 | // ˅ 11 | 12 | // ˄ 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | } 18 | 19 | ListFactory::~ListFactory() 20 | { 21 | // ˅ 22 | 23 | // ˄ 24 | } 25 | 26 | Page* ListFactory::createPage(const string& title, const string& author) 27 | { 28 | // ˅ 29 | return new ListPage(title, author); 30 | // ˄ 31 | } 32 | 33 | Link* ListFactory::createLink(const string& name, const string& url) 34 | { 35 | // ˅ 36 | return new ListLink(name, url); 37 | // ˄ 38 | } 39 | 40 | Data* ListFactory::createData(const string& name) 41 | { 42 | // ˅ 43 | return new ListData(name); 44 | // ˄ 45 | } 46 | 47 | // ˅ 48 | 49 | // ˄ 50 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/table_factory/TablePage.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEPAGE_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEPAGE_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Page.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class TablePage : public Page 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | TablePage(const string& title, const string& author); 25 | 26 | ~TablePage(); 27 | 28 | const string toHTML() const; 29 | 30 | // ˅ 31 | public: 32 | 33 | protected: 34 | 35 | private: 36 | TablePage(const TablePage&) = delete; 37 | TablePage& operator=(const TablePage&) = delete; 38 | TablePage(TablePage&&) = delete; 39 | TablePage& operator=(TablePage&&) = delete; 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEPAGE_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/Program.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/interpreter/Program.h" 3 | #include "behavioral_patterns/interpreter/Context.h" 4 | #include "behavioral_patterns/interpreter/CommandList.h" 5 | 6 | // ˄ 7 | 8 | Program::Program() 9 | : command_list(nullptr) 10 | // ˅ 11 | 12 | // ˄ 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | } 18 | 19 | Program::~Program() 20 | { 21 | // ˅ 22 | if (command_list != nullptr) { 23 | delete command_list; 24 | } 25 | // ˄ 26 | } 27 | 28 | void Program::parse(Context* context) 29 | { 30 | // ˅ 31 | context->slideToken("program"); 32 | 33 | Node* a_command_list = new CommandList(); 34 | a_command_list->parse(context); 35 | 36 | this->command_list = a_command_list; // Hold the parsed command list 37 | // ˄ 38 | } 39 | 40 | const string Program::toString() const 41 | { 42 | // ˅ 43 | if (command_list != nullptr) { 44 | return "[program " + command_list->toString() + "]"; 45 | } 46 | else { 47 | return ""; 48 | } 49 | // ˄ 50 | } 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/NumberSubject.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_OBSERVER_NUMBERSUBJECT_H_ 6 | #define BEHAVIORAL_PATTERNS_OBSERVER_NUMBERSUBJECT_H_ 7 | 8 | // ˅ 9 | #include "behavioral_patterns/observer/Subject.h" 10 | 11 | // ˄ 12 | 13 | // Holds a value and notifies observers when the value is set. 14 | class NumberSubject : public Subject 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | 20 | private: 21 | 22 | int value; 23 | 24 | public: 25 | 26 | NumberSubject(); 27 | 28 | ~NumberSubject(); 29 | 30 | void setValue(int value); 31 | 32 | int getValue() const; 33 | 34 | // ˅ 35 | public: 36 | 37 | protected: 38 | 39 | private: 40 | NumberSubject(const NumberSubject&) = delete; 41 | NumberSubject& operator=(const NumberSubject&) = delete; 42 | NumberSubject(NumberSubject&&) = delete; 43 | NumberSubject& operator=(NumberSubject&&) = delete; 44 | 45 | // ˄ 46 | }; 47 | 48 | // ˅ 49 | 50 | // ˄ 51 | 52 | #endif // BEHAVIORAL_PATTERNS_OBSERVER_NUMBERSUBJECT_H_ 53 | 54 | // ˅ 55 | 56 | // ˄ 57 | -------------------------------------------------------------------------------- /creational_patterns/factory_method/credit_card/CreditCard.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_FACTORY_METHOD_CREDIT_CARD_CREDITCARD_H_ 6 | #define CREATIONAL_PATTERNS_FACTORY_METHOD_CREDIT_CARD_CREDITCARD_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/factory_method/framework/Product.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class CreditCard : public Product 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | public: 23 | 24 | const string owner; 25 | 26 | CreditCard(const string& owner); 27 | 28 | ~CreditCard(); 29 | 30 | void use() const; 31 | 32 | // ˅ 33 | public: 34 | 35 | protected: 36 | 37 | private: 38 | CreditCard(const CreditCard&) = delete; 39 | CreditCard& operator=(const CreditCard&) = delete; 40 | CreditCard(CreditCard&&) = delete; 41 | CreditCard& operator=(CreditCard&&) = delete; 42 | 43 | // ˄ 44 | }; 45 | 46 | // ˅ 47 | 48 | // ˄ 49 | 50 | #endif // CREATIONAL_PATTERNS_FACTORY_METHOD_CREDIT_CARD_CREDITCARD_H_ 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/LargeSizeChar.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_FLYWEIGHT_LARGESIZECHAR_H_ 6 | #define STRUCTURAL_PATTERNS_FLYWEIGHT_LARGESIZECHAR_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | class LargeSizeChar 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | private: 22 | 23 | // Display data of the large size character 24 | string display_data; 25 | 26 | public: 27 | 28 | LargeSizeChar(const char char_name); 29 | 30 | ~LargeSizeChar(); 31 | 32 | // Display the large size character 33 | void display() const; 34 | 35 | // ˅ 36 | public: 37 | 38 | protected: 39 | 40 | private: 41 | LargeSizeChar(const LargeSizeChar&) = delete; 42 | LargeSizeChar& operator=(const LargeSizeChar&) = delete; 43 | LargeSizeChar(LargeSizeChar&&) = delete; 44 | LargeSizeChar& operator=(LargeSizeChar&&) = delete; 45 | 46 | // ˄ 47 | }; 48 | 49 | // ˅ 50 | 51 | // ˄ 52 | 53 | #endif // STRUCTURAL_PATTERNS_FLYWEIGHT_LARGESIZECHAR_H_ 54 | 55 | // ˅ 56 | 57 | // ˄ 58 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/ColleagueTextField.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/mediator/ColleagueTextField.h" 3 | #include "behavioral_patterns/mediator/CLIWrapper.h" 4 | 5 | using namespace System; 6 | 7 | // ˄ 8 | 9 | ColleagueTextField::ColleagueTextField(const msclr::gcroot text_box, Mediator* mediator) 10 | : text_box(text_box) 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 | text_box->TextChanged += gcnew EventHandler(cli_wrapper, &CLIWrapper::colleagueChanged); 20 | // ˄ 21 | } 22 | 23 | ColleagueTextField::~ColleagueTextField() 24 | { 25 | // ˅ 26 | 27 | // ˄ 28 | } 29 | 30 | void ColleagueTextField::setActivation(const bool is_enable) 31 | { 32 | // ˅ 33 | text_box->Enabled = is_enable; 34 | // ˄ 35 | } 36 | 37 | bool ColleagueTextField::isEmpty() const 38 | { 39 | // ˅ 40 | return text_box->Text == ""; 41 | // ˄ 42 | } 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/list_factory/ListPage.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "creational_patterns/abstract_factory/list_factory/ListPage.h" 4 | #include "creational_patterns/abstract_factory/factory/Item.h" 5 | 6 | using namespace std; 7 | 8 | // ˄ 9 | 10 | ListPage::ListPage(const string& title, const string& author) 11 | // ˅ 12 | : Page(title, author) 13 | // ˄ 14 | { 15 | // ˅ 16 | 17 | // ˄ 18 | } 19 | 20 | ListPage::~ListPage() 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | } 26 | 27 | const string ListPage::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(); 36 | } 37 | buffer << "
    " << endl; 38 | buffer << "
    " << author << "
    "; 39 | buffer << "" << endl; 40 | return buffer.str(); 41 | // ˄ 42 | } 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/flyweight.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /behavioral_patterns/interpreter/CommandList.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_INTERPRETER_COMMANDLIST_H_ 6 | #define BEHAVIORAL_PATTERNS_INTERPRETER_COMMANDLIST_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | #include "behavioral_patterns/interpreter/Node.h" 12 | 13 | using namespace std; 14 | 15 | // ˄ 16 | 17 | class CommandList : public Node 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | private: 24 | 25 | vector nodes; 26 | 27 | public: 28 | 29 | CommandList(); 30 | 31 | ~CommandList(); 32 | 33 | void parse(Context* context); 34 | 35 | const string toString() const; 36 | 37 | // ˅ 38 | public: 39 | 40 | protected: 41 | 42 | private: 43 | CommandList(const CommandList&) = delete; 44 | CommandList& operator=(const CommandList&) = delete; 45 | CommandList(CommandList&&) = delete; 46 | CommandList& operator=(CommandList&&) = delete; 47 | 48 | // ˄ 49 | }; 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | 55 | #endif // BEHAVIORAL_PATTERNS_INTERPRETER_COMMANDLIST_H_ 56 | 57 | // ˅ 58 | 59 | // ˄ 60 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/table_factory/TableFactory.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "creational_patterns/abstract_factory/table_factory/TableData.h" 3 | #include "creational_patterns/abstract_factory/table_factory/TableFactory.h" 4 | #include "creational_patterns/abstract_factory/table_factory/TableLink.h" 5 | #include "creational_patterns/abstract_factory/table_factory/TablePage.h" 6 | 7 | // ˄ 8 | 9 | TableFactory::TableFactory() 10 | // ˅ 11 | 12 | // ˄ 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | } 18 | 19 | TableFactory::~TableFactory() 20 | { 21 | // ˅ 22 | 23 | // ˄ 24 | } 25 | 26 | Page* TableFactory::createPage(const string& title, const string& author) 27 | { 28 | // ˅ 29 | return new TablePage(title, author); 30 | // ˄ 31 | } 32 | 33 | Link* TableFactory::createLink(const string& name, const string& url) 34 | { 35 | // ˅ 36 | return new TableLink(name, url); 37 | // ˄ 38 | } 39 | 40 | Data* TableFactory::createData(const string& name) 41 | { 42 | // ˅ 43 | return new TableData(name); 44 | // ˄ 45 | } 46 | 47 | // ˅ 48 | 49 | // ˄ 50 | -------------------------------------------------------------------------------- /structural_patterns/bridge/MultiLineDisplay.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_BRIDGE_MULTILINEDISPLAY_H_ 6 | #define STRUCTURAL_PATTERNS_BRIDGE_MULTILINEDISPLAY_H_ 7 | 8 | // ˅ 9 | #include "structural_patterns/bridge/Display.h" 10 | 11 | class DisplayImpl; 12 | 13 | // ˄ 14 | 15 | class MultiLineDisplay : public Display 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | 21 | public: 22 | 23 | MultiLineDisplay(DisplayImpl* impl); 24 | 25 | ~MultiLineDisplay(); 26 | 27 | // Repeat display for the specified number of times 28 | void outputMultiple(const int times) const; 29 | 30 | // ˅ 31 | public: 32 | 33 | protected: 34 | 35 | private: 36 | MultiLineDisplay(const MultiLineDisplay&) = delete; 37 | MultiLineDisplay& operator=(const MultiLineDisplay&) = delete; 38 | MultiLineDisplay(MultiLineDisplay&&) = delete; 39 | MultiLineDisplay& operator=(MultiLineDisplay&&) = delete; 40 | 41 | // ˄ 42 | }; 43 | 44 | // ˅ 45 | 46 | // ˄ 47 | 48 | #endif // STRUCTURAL_PATTERNS_BRIDGE_MULTILINEDISPLAY_H_ 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/Subject.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_OBSERVER_SUBJECT_H_ 6 | #define BEHAVIORAL_PATTERNS_OBSERVER_SUBJECT_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | class Observer; 12 | 13 | using namespace std; 14 | 15 | // ˄ 16 | 17 | // Provides an interface for attaching and detaching Observer objects. 18 | class Subject 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | private: 25 | 26 | vector observers; 27 | 28 | public: 29 | 30 | Subject(); 31 | 32 | virtual ~Subject(); 33 | 34 | void attach(Observer* observer); 35 | 36 | void detach(const Observer* observer); 37 | 38 | void notifyObservers() const; 39 | 40 | // ˅ 41 | public: 42 | 43 | protected: 44 | 45 | private: 46 | Subject(const Subject&) = delete; 47 | Subject& operator=(const Subject&) = delete; 48 | Subject(Subject&&) = delete; 49 | Subject& operator=(Subject&&) = delete; 50 | 51 | // ˄ 52 | }; 53 | 54 | // ˅ 55 | 56 | // ˄ 57 | 58 | #endif // BEHAVIORAL_PATTERNS_OBSERVER_SUBJECT_H_ 59 | 60 | // ˅ 61 | 62 | // ˄ 63 | -------------------------------------------------------------------------------- /creational_patterns/prototype/framework/Manager.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_PROTOTYPE_FRAMEWORK_MANAGER_H_ 6 | #define CREATIONAL_PATTERNS_PROTOTYPE_FRAMEWORK_MANAGER_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | 12 | class Display; 13 | 14 | using namespace std; 15 | 16 | // ˄ 17 | 18 | class Manager 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | private: 25 | 26 | map displays; 27 | 28 | public: 29 | 30 | Manager(); 31 | 32 | ~Manager(); 33 | 34 | void registerDisplay(const string& display_name, Display* display); 35 | 36 | const Display* getDisplay(const string& display_name) const; 37 | 38 | // ˅ 39 | public: 40 | 41 | protected: 42 | 43 | private: 44 | Manager(const Manager&) = delete; 45 | Manager& operator=(const Manager&) = delete; 46 | Manager(Manager&&) = delete; 47 | Manager& operator=(Manager&&) = delete; 48 | 49 | // ˄ 50 | }; 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | 56 | #endif // CREATIONAL_PATTERNS_PROTOTYPE_FRAMEWORK_MANAGER_H_ 57 | 58 | // ˅ 59 | 60 | // ˄ 61 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/table_factory/TableData.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include 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 << "" << endl; 34 | buffer << "" << endl; 35 | for (Item* item : items) { 36 | buffer << item->toHTML(); 37 | } 38 | buffer << "" << endl; 39 | buffer << "
    " << name << "
    " << 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 3 | #include 4 | #include "structural_patterns/flyweight/LargeSizeChar.h" 5 | #include "structural_patterns/flyweight/LargeSizeCharFactory.h" 6 | 7 | using namespace std; 8 | 9 | // ˄ 10 | 11 | LargeSizeCharFactory* LargeSizeCharFactory::getInstance() 12 | { 13 | // ˅ 14 | static LargeSizeCharFactory* instance = new LargeSizeCharFactory(); 15 | return instance; 16 | // ˄ 17 | } 18 | 19 | LargeSizeCharFactory::LargeSizeCharFactory() 20 | // ˅ 21 | 22 | // ˄ 23 | { 24 | // ˅ 25 | 26 | // ˄ 27 | } 28 | 29 | LargeSizeCharFactory::~LargeSizeCharFactory() 30 | { 31 | // ˅ 32 | 33 | // ˄ 34 | } 35 | 36 | LargeSizeChar* LargeSizeCharFactory::getLargeSizeChar(const char char_name) 37 | { 38 | // ˅ 39 | LargeSizeChar* lsc = nullptr; 40 | if (pool_chars.find(char_name) == end(pool_chars)) { 41 | lsc = new LargeSizeChar(char_name); // Create an instance 42 | pool_chars.insert(map::value_type(char_name, lsc)); 43 | } 44 | else { 45 | lsc = pool_chars.at(char_name); 46 | } 47 | return lsc; 48 | // ˄ 49 | } 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | -------------------------------------------------------------------------------- /structural_patterns/composite/Directory.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "structural_patterns/composite/Directory.h" 4 | #include "structural_patterns/composite/FileSystemElement.h" 5 | 6 | using namespace std; 7 | 8 | // ˄ 9 | 10 | Directory::Directory(const string& name) 11 | : name(name) 12 | // ˅ 13 | 14 | // ˄ 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | } 20 | 21 | Directory::~Directory() 22 | { 23 | // ˅ 24 | 25 | // ˄ 26 | } 27 | 28 | const string Directory::getName() const 29 | { 30 | // ˅ 31 | return name; 32 | // ˄ 33 | } 34 | 35 | const int Directory::getSize() const 36 | { 37 | // ˅ 38 | int size = 0; 39 | for (FileSystemElement* element : elements) { 40 | size += element->getSize(); 41 | } 42 | return size; 43 | // ˄ 44 | } 45 | 46 | void Directory::print(const string& upper_path) const 47 | { 48 | // ˅ 49 | cout << upper_path << "/" << this->toString() << endl; 50 | for (FileSystemElement* element : elements) { 51 | element->print(upper_path + "/" + name); 52 | } 53 | // ˄ 54 | } 55 | 56 | void Directory::add(FileSystemElement* element) 57 | { 58 | // ˅ 59 | elements.push_back(element); 60 | // ˄ 61 | } 62 | 63 | // ˅ 64 | 65 | // ˄ 66 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/StringDisplay.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/template_method/StringDisplay.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | StringDisplay::StringDisplay(const string& string_value) 10 | : string_value(string_value) 11 | , width(static_cast(string_value.length())) 12 | // ˅ 13 | 14 | // ˄ 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | } 20 | 21 | StringDisplay::~StringDisplay() 22 | { 23 | // ˅ 24 | 25 | // ˄ 26 | } 27 | 28 | void StringDisplay::open() 29 | { 30 | // ˅ 31 | writeLine(); // Write a line 32 | // ˄ 33 | } 34 | 35 | void StringDisplay::write() 36 | { 37 | // ˅ 38 | cout << "|" << string_value << "|" << endl; // Display the character with "|" 39 | // ˄ 40 | } 41 | 42 | void StringDisplay::close() 43 | { 44 | // ˅ 45 | writeLine(); // Write a line 46 | // ˄ 47 | } 48 | 49 | void StringDisplay::writeLine() 50 | { 51 | // ˅ 52 | cout << "+"; // Display an end mark "+" 53 | for (int i = 0; i < width; ++i) { 54 | cout << "-"; // Display a line "-" 55 | } 56 | cout << "+" << endl; // Display an end mark "+" 57 | // ˄ 58 | } 59 | 60 | // ˅ 61 | 62 | // ˄ 63 | -------------------------------------------------------------------------------- /structural_patterns/proxy/ProxyPrinter.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "structural_patterns/proxy/ProxyPrinter.h" 3 | #include "structural_patterns/proxy/RealPrinter.h" 4 | 5 | // ˄ 6 | 7 | ProxyPrinter::ProxyPrinter(const string& name) 8 | : current_name(name) 9 | , real(nullptr) 10 | // ˅ 11 | 12 | // ˄ 13 | { 14 | // ˅ 15 | 16 | // ˄ 17 | } 18 | 19 | ProxyPrinter::~ProxyPrinter() 20 | { 21 | // ˅ 22 | if (real != nullptr) { 23 | delete real; 24 | } 25 | // ˄ 26 | } 27 | 28 | const string ProxyPrinter::getName() const 29 | { 30 | // ˅ 31 | if (real != nullptr) { 32 | return real->getName(); 33 | } 34 | else { 35 | return current_name; 36 | } 37 | // ˄ 38 | } 39 | 40 | void ProxyPrinter::changeName(const string& name) 41 | { 42 | // ˅ 43 | if (real != nullptr) { 44 | real->changeName(name); 45 | } 46 | 47 | current_name = name; 48 | // ˄ 49 | } 50 | 51 | void ProxyPrinter::output(const string& content) 52 | { 53 | // ˅ 54 | // Check to see if the RealPrinter had been created, create it if necessary. 55 | if (real == nullptr) { 56 | real = new RealPrinter(current_name); 57 | } 58 | 59 | real->output(content); 60 | // ˄ 61 | } 62 | 63 | // ˅ 64 | 65 | // ˄ 66 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/MirrorStrategy.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_STRATEGY_MIRRORSTRATEGY_H_ 6 | #define BEHAVIORAL_PATTERNS_STRATEGY_MIRRORSTRATEGY_H_ 7 | 8 | // ˅ 9 | #include "behavioral_patterns/strategy/Strategy.h" 10 | 11 | class HandSignal; 12 | 13 | // ˄ 14 | 15 | // Mirror Strategy: showing a hand signal from the previous opponent's hand signal. 16 | class MirrorStrategy : public Strategy 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | HandSignal* preOpponentsHand; 25 | 26 | public: 27 | 28 | MirrorStrategy(); 29 | 30 | ~MirrorStrategy(); 31 | 32 | HandSignal* showHandSignal(); 33 | 34 | void notifyGameResult(GameResultType result, HandSignal* ownHand, HandSignal* opponentsHand); 35 | 36 | // ˅ 37 | public: 38 | 39 | protected: 40 | 41 | private: 42 | MirrorStrategy(const MirrorStrategy&) = delete; 43 | MirrorStrategy& operator=(const MirrorStrategy&) = delete; 44 | MirrorStrategy(MirrorStrategy&&) = delete; 45 | MirrorStrategy& operator=(MirrorStrategy&&) = delete; 46 | 47 | // ˄ 48 | }; 49 | 50 | // ˅ 51 | 52 | // ˄ 53 | 54 | #endif // BEHAVIORAL_PATTERNS_STRATEGY_MIRRORSTRATEGY_H_ 55 | 56 | // ˅ 57 | 58 | // ˄ 59 | -------------------------------------------------------------------------------- /structural_patterns/bridge/TextDisplayImpl.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "structural_patterns/bridge/TextDisplayImpl.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | TextDisplayImpl::TextDisplayImpl(const string& text) 10 | : text(text) 11 | , width(static_cast(text.size())) 12 | // ˅ 13 | 14 | // ˄ 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | } 20 | 21 | TextDisplayImpl::~TextDisplayImpl() 22 | { 23 | // ˅ 24 | 25 | // ˄ 26 | } 27 | 28 | void TextDisplayImpl::implOpen() const 29 | { 30 | // ˅ 31 | printLine(); 32 | // ˄ 33 | } 34 | 35 | void TextDisplayImpl::implWrite() const 36 | { 37 | // ˅ 38 | cout << ":" << text << ":" << endl; // Enclose a text with ":" and display it. 39 | // ˄ 40 | } 41 | 42 | void TextDisplayImpl::implClose() const 43 | { 44 | // ˅ 45 | printLine(); 46 | // ˄ 47 | } 48 | 49 | void TextDisplayImpl::printLine() const 50 | { 51 | // ˅ 52 | cout << "*"; // Display "*" mark at the beginning of a frame. 53 | for (int i = 0; i < width; ++i) { // Display "." for the number of "width". 54 | cout << "."; 55 | } 56 | cout << "*" << endl; // Display "*" mark at the end of a frame. 57 | // ˄ 58 | } 59 | 60 | // ˅ 61 | 62 | // ˄ 63 | -------------------------------------------------------------------------------- /structural_patterns/decorator/SideFrame.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_DECORATOR_SIDEFRAME_H_ 6 | #define STRUCTURAL_PATTERNS_DECORATOR_SIDEFRAME_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "structural_patterns/decorator/Frame.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class SideFrame : public Frame 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | // Decoration character 25 | const char frame_char; 26 | 27 | public: 28 | 29 | SideFrame(const Display* display, const char frame_char); 30 | 31 | ~SideFrame(); 32 | 33 | // Number of characters added left and right decoration characters 34 | const int getColumns() const; 35 | 36 | // Number of lines 37 | const int getRows() const; 38 | 39 | const string getLineText(const int row) const; 40 | 41 | // ˅ 42 | public: 43 | 44 | protected: 45 | 46 | private: 47 | SideFrame(const SideFrame&) = delete; 48 | SideFrame& operator=(const SideFrame&) = delete; 49 | SideFrame(SideFrame&&) = delete; 50 | SideFrame& operator=(SideFrame&&) = delete; 51 | 52 | // ˄ 53 | }; 54 | 55 | // ˅ 56 | 57 | // ˄ 58 | 59 | #endif // STRUCTURAL_PATTERNS_DECORATOR_SIDEFRAME_H_ 60 | 61 | // ˅ 62 | 63 | // ˄ 64 | -------------------------------------------------------------------------------- /behavioral_patterns/template_method/StringDisplay.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_STRINGDISPLAY_H_ 6 | #define BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_STRINGDISPLAY_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/template_method/AbstractDisplay.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class StringDisplay : public AbstractDisplay 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | const string string_value; 25 | 26 | // String width 27 | const int width; 28 | 29 | public: 30 | 31 | StringDisplay(const string& string_value); 32 | 33 | ~StringDisplay(); 34 | 35 | void open(); 36 | 37 | void write(); 38 | 39 | void close(); 40 | 41 | private: 42 | 43 | void writeLine(); 44 | 45 | // ˅ 46 | public: 47 | 48 | protected: 49 | 50 | private: 51 | StringDisplay(const StringDisplay&) = delete; 52 | StringDisplay& operator=(const StringDisplay&) = delete; 53 | StringDisplay(StringDisplay&&) = delete; 54 | StringDisplay& operator=(StringDisplay&&) = delete; 55 | 56 | // ˄ 57 | }; 58 | 59 | // ˅ 60 | 61 | // ˄ 62 | 63 | #endif // BEHAVIORAL_PATTERNS_TEMPLATE_METHOD_STRINGDISPLAY_H_ 64 | 65 | // ˅ 66 | 67 | // ˄ 68 | -------------------------------------------------------------------------------- /structural_patterns/proxy/RealPrinter.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_PROXY_REALPRINTER_H_ 6 | #define STRUCTURAL_PATTERNS_PROXY_REALPRINTER_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "structural_patterns/proxy/Printer.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class RealPrinter : public Printer 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | string name; 25 | 26 | public: 27 | 28 | RealPrinter(const string& name); 29 | 30 | ~RealPrinter(); 31 | 32 | const string getName() const; 33 | 34 | void changeName(const string& name); 35 | 36 | // Display a content with the name 37 | void output(const string& content); 38 | 39 | private: 40 | 41 | // Heavy task (Please think so...) 42 | void heavyTask(const string& message) const; 43 | 44 | // ˅ 45 | public: 46 | 47 | protected: 48 | 49 | private: 50 | RealPrinter(const RealPrinter&) = delete; 51 | RealPrinter& operator=(const RealPrinter&) = delete; 52 | RealPrinter(RealPrinter&&) = delete; 53 | RealPrinter& operator=(RealPrinter&&) = delete; 54 | 55 | // ˄ 56 | }; 57 | 58 | // ˅ 59 | 60 | // ˄ 61 | 62 | #endif // STRUCTURAL_PATTERNS_PROXY_REALPRINTER_H_ 63 | 64 | // ˅ 65 | 66 | // ˄ 67 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/ColleagueButton.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUEBUTTON_H_ 6 | #define BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUEBUTTON_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/mediator/Colleague.h" 11 | 12 | using namespace System::Windows::Forms; 13 | 14 | // ˄ 15 | 16 | class ColleagueButton : public Colleague 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | const msclr::gcroot button; 25 | 26 | public: 27 | 28 | ColleagueButton(const msclr::gcroot button, Mediator* mediator); 29 | 30 | ~ColleagueButton(); 31 | 32 | // Set enable/disable from the Mediator 33 | void setActivation(const bool is_enable); 34 | 35 | bool isPressed() const; 36 | 37 | // ˅ 38 | public: 39 | 40 | protected: 41 | 42 | private: 43 | ColleagueButton(const ColleagueButton&) = delete; 44 | ColleagueButton& operator=(const ColleagueButton&) = delete; 45 | ColleagueButton(ColleagueButton&&) = delete; 46 | ColleagueButton& operator=(ColleagueButton&&) = delete; 47 | 48 | // ˄ 49 | }; 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | 55 | #endif // BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUEBUTTON_H_ 56 | 57 | // ˅ 58 | 59 | // ˄ 60 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/list_factory/ListFactory.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTFACTORY_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTFACTORY_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Factory.h" 11 | 12 | class Page; 13 | class Link; 14 | class Data; 15 | 16 | using namespace std; 17 | 18 | // ˄ 19 | 20 | class ListFactory : public Factory 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | 26 | public: 27 | 28 | ListFactory(); 29 | 30 | ~ListFactory(); 31 | 32 | Page* createPage(const string& title, const string& author); 33 | 34 | Link* createLink(const string& name, const string& url); 35 | 36 | Data* createData(const string& name); 37 | 38 | // ˅ 39 | public: 40 | 41 | protected: 42 | 43 | private: 44 | ListFactory(const ListFactory&) = delete; 45 | ListFactory& operator=(const ListFactory&) = delete; 46 | ListFactory(ListFactory&&) = delete; 47 | ListFactory& operator=(ListFactory&&) = delete; 48 | 49 | // ˄ 50 | }; 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | 56 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_LIST_FACTORY_LISTFACTORY_H_ 57 | 58 | // ˅ 59 | 60 | // ˄ 61 | -------------------------------------------------------------------------------- /behavioral_patterns/command/PaintingCanvas.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_COMMAND_PAINTINGCANVAS_H_ 6 | #define BEHAVIORAL_PATTERNS_COMMAND_PAINTINGCANVAS_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/command/PaintingTarget.h" 11 | 12 | using namespace System::Windows::Forms; 13 | 14 | // ˄ 15 | 16 | class PaintingCanvas : public PaintingTarget 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | msclr::gcroot picture_box; 25 | 26 | // Radius of the painting point 27 | const double point_radius; 28 | 29 | public: 30 | 31 | PaintingCanvas(msclr::gcroot picture_box); 32 | 33 | ~PaintingCanvas(); 34 | 35 | void paint(const double x, const double y) const; 36 | 37 | void clear(); 38 | 39 | // ˅ 40 | public: 41 | 42 | protected: 43 | 44 | private: 45 | PaintingCanvas(const PaintingCanvas&) = delete; 46 | PaintingCanvas& operator=(const PaintingCanvas&) = delete; 47 | PaintingCanvas(PaintingCanvas&&) = delete; 48 | PaintingCanvas& operator=(PaintingCanvas&&) = delete; 49 | 50 | // ˄ 51 | }; 52 | 53 | // ˅ 54 | 55 | // ˄ 56 | 57 | #endif // BEHAVIORAL_PATTERNS_COMMAND_PAINTINGCANVAS_H_ 58 | 59 | // ˅ 60 | 61 | // ˄ 62 | -------------------------------------------------------------------------------- /structural_patterns/adapter/PrintMessageDisplay.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_ADAPTER_PRINTMESSAGEDISPLAY_H_ 6 | #define STRUCTURAL_PATTERNS_ADAPTER_PRINTMESSAGEDISPLAY_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "structural_patterns/adapter/MessageDisplay.h" 11 | #include "structural_patterns/adapter/Print.h" 12 | 13 | using namespace std; 14 | 15 | // ˄ 16 | 17 | // Adapt the MessageDisplay interface to the Print interface. 18 | class PrintMessageDisplay : public MessageDisplay, public Print 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | public: 25 | 26 | PrintMessageDisplay(const string& message); 27 | 28 | ~PrintMessageDisplay(); 29 | 30 | void printWeak() const; 31 | 32 | void printStrong() const; 33 | 34 | // ˅ 35 | public: 36 | 37 | protected: 38 | 39 | private: 40 | PrintMessageDisplay(const PrintMessageDisplay&) = delete; 41 | PrintMessageDisplay& operator=(const PrintMessageDisplay&) = delete; 42 | PrintMessageDisplay(PrintMessageDisplay&&) = delete; 43 | PrintMessageDisplay& operator=(PrintMessageDisplay&&) = delete; 44 | 45 | // ˄ 46 | }; 47 | 48 | // ˅ 49 | 50 | // ˄ 51 | 52 | #endif // STRUCTURAL_PATTERNS_ADAPTER_PRINTMESSAGEDISPLAY_H_ 53 | 54 | // ˅ 55 | 56 | // ˄ 57 | -------------------------------------------------------------------------------- /structural_patterns/decorator/MessageDisplay.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_DECORATOR_MESSAGEDISPLAY_H_ 6 | #define STRUCTURAL_PATTERNS_DECORATOR_MESSAGEDISPLAY_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "structural_patterns/decorator/Display.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class MessageDisplay : public Display 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | // Message to be displayed 25 | const string message; 26 | 27 | public: 28 | 29 | MessageDisplay(const string& message); 30 | 31 | ~MessageDisplay(); 32 | 33 | // Number of characters 34 | const int getColumns() const; 35 | 36 | // The number of rows is 1 37 | const int getRows() const; 38 | 39 | const string getLineText(const int row) const; 40 | 41 | // ˅ 42 | public: 43 | 44 | protected: 45 | 46 | private: 47 | MessageDisplay(const MessageDisplay&) = delete; 48 | MessageDisplay& operator=(const MessageDisplay&) = delete; 49 | MessageDisplay(MessageDisplay&&) = delete; 50 | MessageDisplay& operator=(MessageDisplay&&) = delete; 51 | 52 | // ˄ 53 | }; 54 | 55 | // ˅ 56 | 57 | // ˄ 58 | 59 | #endif // STRUCTURAL_PATTERNS_DECORATOR_MESSAGEDISPLAY_H_ 60 | 61 | // ˅ 62 | 63 | // ˄ 64 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/Supporter.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include "behavioral_patterns/chain_of_responsibility/Supporter.h" 4 | 5 | using namespace std; 6 | 7 | // ˄ 8 | 9 | Supporter::Supporter(const string& name) 10 | : name(name) 11 | , next(nullptr) 12 | // ˅ 13 | 14 | // ˄ 15 | { 16 | // ˅ 17 | 18 | // ˄ 19 | } 20 | 21 | Supporter::~Supporter() 22 | { 23 | // ˅ 24 | 25 | // ˄ 26 | } 27 | 28 | void Supporter::support(const Trouble* trouble) const 29 | { 30 | // ˅ 31 | if (canHandle(trouble) == true) { 32 | supported(trouble); 33 | } 34 | else if (next != nullptr) { 35 | next->support(trouble); 36 | } 37 | else { 38 | unsupported(trouble); 39 | } 40 | // ˄ 41 | } 42 | 43 | Supporter* Supporter::setNext(Supporter* next) 44 | { 45 | // ˅ 46 | this->next = next; 47 | return this->next; 48 | // ˄ 49 | } 50 | 51 | void Supporter::supported(const Trouble* trouble) const 52 | { 53 | // ˅ 54 | cout << trouble->toString() << " was handled by " << name << "." << endl; 55 | // ˄ 56 | } 57 | 58 | void Supporter::unsupported(const Trouble* trouble) const 59 | { 60 | // ˅ 61 | cout << trouble->toString() << " was not handled." << endl; 62 | // ˄ 63 | } 64 | 65 | // ˅ 66 | 67 | // ˄ 68 | -------------------------------------------------------------------------------- /behavioral_patterns/visitor/ListVisitor.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include 4 | #include "behavioral_patterns/visitor/ListVisitor.h" 5 | #include "behavioral_patterns/visitor/File.h" 6 | #include "behavioral_patterns/visitor/Directory.h" 7 | 8 | using namespace std; 9 | 10 | // ˄ 11 | 12 | ListVisitor::ListVisitor() 13 | : current_directory("") 14 | // ˅ 15 | 16 | // ˄ 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | } 22 | 23 | ListVisitor::~ListVisitor() 24 | { 25 | // ˅ 26 | 27 | // ˄ 28 | } 29 | 30 | void ListVisitor::visit(const File* file) 31 | { 32 | // ˅ 33 | cout << current_directory << "/" << file->toString() << endl; 34 | // ˄ 35 | } 36 | 37 | void ListVisitor::visit(const Directory* directory) 38 | { 39 | // ˅ 40 | cout << current_directory << "/" << directory->toString() << endl; 41 | const string visited_directory = current_directory; 42 | current_directory = current_directory + "/" + directory->getName(); 43 | for (vector::const_iterator it = directory->getBeginIterator(); it != directory->getEndIterator(); it++) { 44 | FileSystemElement* element = *it; 45 | element->accept(this); 46 | } 47 | current_directory = visited_directory; 48 | // ˄ 49 | } 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | -------------------------------------------------------------------------------- /creational_patterns/abstract_factory/table_factory/TableFactory.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEFACTORY_H_ 6 | #define CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEFACTORY_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "creational_patterns/abstract_factory/factory/Factory.h" 11 | 12 | class Page; 13 | class Link; 14 | class Data; 15 | 16 | using namespace std; 17 | 18 | // ˄ 19 | 20 | class TableFactory : public Factory 21 | { 22 | // ˅ 23 | 24 | // ˄ 25 | 26 | public: 27 | 28 | TableFactory(); 29 | 30 | ~TableFactory(); 31 | 32 | Page* createPage(const string& title, const string& author); 33 | 34 | Link* createLink(const string& name, const string& url); 35 | 36 | Data* createData(const string& name); 37 | 38 | // ˅ 39 | public: 40 | 41 | protected: 42 | 43 | private: 44 | TableFactory(const TableFactory&) = delete; 45 | TableFactory& operator=(const TableFactory&) = delete; 46 | TableFactory(TableFactory&&) = delete; 47 | TableFactory& operator=(TableFactory&&) = delete; 48 | 49 | // ˄ 50 | }; 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | 56 | #endif // CREATIONAL_PATTERNS_ABSTRACT_FACTORY_TABLE_FACTORY_TABLEFACTORY_H_ 57 | 58 | // ˅ 59 | 60 | // ˄ 61 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/SpecialSupporter.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_SPECIALSUPPORTER_H_ 6 | #define BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_SPECIALSUPPORTER_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/chain_of_responsibility/Supporter.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class SpecialSupporter : public Supporter 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | const int target_id; 25 | 26 | public: 27 | 28 | SpecialSupporter(const string& name, const int target_id); 29 | 30 | ~SpecialSupporter(); 31 | 32 | protected: 33 | 34 | // Troubles with the specific ID are handled. 35 | bool canHandle(const Trouble* trouble) const; 36 | 37 | // ˅ 38 | public: 39 | 40 | protected: 41 | 42 | private: 43 | SpecialSupporter(const SpecialSupporter&) = delete; 44 | SpecialSupporter& operator=(const SpecialSupporter&) = delete; 45 | SpecialSupporter(SpecialSupporter&&) = delete; 46 | SpecialSupporter& operator=(SpecialSupporter&&) = delete; 47 | 48 | // ˄ 49 | }; 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | 55 | #endif // BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_SPECIALSUPPORTER_H_ 56 | 57 | // ˅ 58 | 59 | // ˄ 60 | -------------------------------------------------------------------------------- /structural_patterns/flyweight/LargeSizeChar.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include "structural_patterns/flyweight/LargeSizeChar.h" 8 | 9 | #ifdef _MSC_VER 10 | #include 11 | #else 12 | #include 13 | #endif 14 | 15 | using namespace std; 16 | 17 | // ˄ 18 | 19 | LargeSizeChar::LargeSizeChar(const char char_name) 20 | : display_data("") 21 | // ˅ 22 | 23 | // ˄ 24 | { 25 | // ˅ 26 | // Get the current directory path. 27 | char current_directory_path[255]; 28 | #ifdef _MSC_VER 29 | GetCurrentDirectory(255, current_directory_path); 30 | #else 31 | getcwd(current_directory_path, 255); 32 | #endif 33 | 34 | ifstream ifs(string(current_directory_path) + "/big" + char_name + ".txt"); 35 | if (ifs.is_open()) { 36 | string line; 37 | while (getline(ifs, line)) { 38 | display_data += line + "\n"; 39 | } 40 | } 41 | else { 42 | display_data = string(1, char_name) + "?"; 43 | } 44 | // ˄ 45 | } 46 | 47 | LargeSizeChar::~LargeSizeChar() 48 | { 49 | // ˅ 50 | 51 | // ˄ 52 | } 53 | 54 | void LargeSizeChar::display() const 55 | { 56 | // ˅ 57 | cout << display_data << endl; 58 | // ˄ 59 | } 60 | 61 | // ˅ 62 | 63 | // ˄ 64 | -------------------------------------------------------------------------------- /behavioral_patterns/chain_of_responsibility/LimitedSupporter.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_LIMITEDSUPPORTER_H_ 6 | #define BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_LIMITEDSUPPORTER_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/chain_of_responsibility/Supporter.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class LimitedSupporter : public Supporter 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | const int limit_id; 25 | 26 | public: 27 | 28 | LimitedSupporter(const string& name, const int limit_id); 29 | 30 | ~LimitedSupporter(); 31 | 32 | protected: 33 | 34 | // Troubles with an ID below the limit are handled. 35 | bool canHandle(const Trouble* trouble) const; 36 | 37 | // ˅ 38 | public: 39 | 40 | protected: 41 | 42 | private: 43 | LimitedSupporter(const LimitedSupporter&) = delete; 44 | LimitedSupporter& operator=(const LimitedSupporter&) = delete; 45 | LimitedSupporter(LimitedSupporter&&) = delete; 46 | LimitedSupporter& operator=(LimitedSupporter&&) = delete; 47 | 48 | // ˄ 49 | }; 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | 55 | #endif // BEHAVIORAL_PATTERNS_CHAIN_OF_RESPONSIBILITY_LIMITEDSUPPORTER_H_ 56 | 57 | // ˅ 58 | 59 | // ˄ 60 | -------------------------------------------------------------------------------- /structural_patterns/proxy/ProxyPrinter.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_PROXY_PROXYPRINTER_H_ 6 | #define STRUCTURAL_PATTERNS_PROXY_PROXYPRINTER_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "structural_patterns/proxy/Printer.h" 11 | 12 | class RealPrinter; 13 | 14 | using namespace std; 15 | 16 | // ˄ 17 | 18 | // ProxyPrinter forwards requests to RealPrinter when appropriate. 19 | class ProxyPrinter : public Printer 20 | { 21 | // ˅ 22 | 23 | // ˄ 24 | 25 | private: 26 | 27 | string current_name; 28 | 29 | // A printer that actually prints 30 | RealPrinter* real; 31 | 32 | public: 33 | 34 | ProxyPrinter(const string& name); 35 | 36 | ~ProxyPrinter(); 37 | 38 | const string getName() const; 39 | 40 | void changeName(const string& name); 41 | 42 | void output(const string& content); 43 | 44 | // ˅ 45 | public: 46 | 47 | protected: 48 | 49 | private: 50 | ProxyPrinter(const ProxyPrinter&) = delete; 51 | ProxyPrinter& operator=(const ProxyPrinter&) = delete; 52 | ProxyPrinter(ProxyPrinter&&) = delete; 53 | ProxyPrinter& operator=(ProxyPrinter&&) = delete; 54 | 55 | // ˄ 56 | }; 57 | 58 | // ˅ 59 | 60 | // ˄ 61 | 62 | #endif // STRUCTURAL_PATTERNS_PROXY_PROXYPRINTER_H_ 63 | 64 | // ˅ 65 | 66 | // ˄ 67 | -------------------------------------------------------------------------------- /structural_patterns/facade/PageCreator.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_FACADE_PAGECREATOR_H_ 6 | #define STRUCTURAL_PATTERNS_FACADE_PAGECREATOR_H_ 7 | 8 | // ˅ 9 | #include 10 | 11 | using namespace std; 12 | 13 | // ˄ 14 | 15 | // Singleton ( based on the example code on Wikipedia ) 16 | // https://en.wikipedia.org/w/index.php?title=Singleton_pattern&oldid=1115882454#C++ 17 | // Note: The latest Wikipedia page has had the C++ code example removed. This link is to the page before it was removed. 18 | class PageCreator 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | public: 25 | 26 | static PageCreator* getInstance(); 27 | 28 | private: 29 | 30 | PageCreator(); 31 | 32 | public: 33 | 34 | ~PageCreator(); 35 | 36 | void createSimpleHomepage(const string& mail_address, const string& html_file_name); 37 | 38 | // ˅ 39 | public: 40 | 41 | protected: 42 | 43 | private: 44 | PageCreator(const PageCreator&) = delete; 45 | PageCreator& operator=(const PageCreator&) = delete; 46 | PageCreator(PageCreator&&) = delete; 47 | PageCreator& operator=(PageCreator&&) = delete; 48 | 49 | // ˄ 50 | }; 51 | 52 | // ˅ 53 | 54 | // ˄ 55 | 56 | #endif // STRUCTURAL_PATTERNS_FACADE_PAGECREATOR_H_ 57 | 58 | // ˅ 59 | 60 | // ˄ 61 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/Player.cpp: -------------------------------------------------------------------------------- 1 | // ˅ 2 | #include "behavioral_patterns/strategy/Player.h" 3 | #include "behavioral_patterns/strategy/Strategy.h" 4 | 5 | // ˄ 6 | 7 | Player::Player(const string& name, Strategy* strategy) 8 | : name(name) 9 | , win_count(0) 10 | , loss_count(0) 11 | , game_count(0) 12 | , strategy(strategy) 13 | // ˅ 14 | 15 | // ˄ 16 | { 17 | // ˅ 18 | 19 | // ˄ 20 | } 21 | 22 | HandSignal* Player::showHandSignal() 23 | { 24 | // ˅ 25 | return strategy->showHandSignal(); 26 | // ˄ 27 | } 28 | 29 | void Player::notifyGameResult(GameResultType result, HandSignal* ownHand, HandSignal* opponentsHand) 30 | { 31 | // ˅ 32 | switch (result) { 33 | case Win: 34 | win_count++; 35 | game_count++; 36 | break; 37 | case Loss: 38 | loss_count++; 39 | game_count++; 40 | break; 41 | case Draw: 42 | game_count++; 43 | break; 44 | } 45 | 46 | strategy->notifyGameResult(result, ownHand, opponentsHand); 47 | // ˄ 48 | } 49 | 50 | const string Player::toString() const 51 | { 52 | // ˅ 53 | return name + " [" + to_string(game_count) + " games, " + to_string(win_count) + " won, " + to_string(loss_count) + " lost, " + to_string(game_count - win_count - loss_count) + " drew]"; 54 | // ˄ 55 | } 56 | 57 | // ˅ 58 | 59 | // ˄ 60 | -------------------------------------------------------------------------------- /structural_patterns/bridge/TextDisplayImpl.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_BRIDGE_TEXTDISPLAYIMPL_H_ 6 | #define STRUCTURAL_PATTERNS_BRIDGE_TEXTDISPLAYIMPL_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "structural_patterns/bridge/DisplayImpl.h" 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class TextDisplayImpl : public DisplayImpl 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | // A string to display 25 | const string text; 26 | 27 | // A number of characters in bytes 28 | const int width; 29 | 30 | public: 31 | 32 | TextDisplayImpl(const string& text); 33 | 34 | ~TextDisplayImpl(); 35 | 36 | void implOpen() const; 37 | 38 | void implWrite() const; 39 | 40 | void implClose() const; 41 | 42 | private: 43 | 44 | void printLine() const; 45 | 46 | // ˅ 47 | public: 48 | 49 | protected: 50 | 51 | private: 52 | TextDisplayImpl(const TextDisplayImpl&) = delete; 53 | TextDisplayImpl& operator=(const TextDisplayImpl&) = delete; 54 | TextDisplayImpl(TextDisplayImpl&&) = delete; 55 | TextDisplayImpl& operator=(TextDisplayImpl&&) = delete; 56 | 57 | // ˄ 58 | }; 59 | 60 | // ˅ 61 | 62 | // ˄ 63 | 64 | #endif // STRUCTURAL_PATTERNS_BRIDGE_TEXTDISPLAYIMPL_H_ 65 | 66 | // ˅ 67 | 68 | // ˄ 69 | -------------------------------------------------------------------------------- /structural_patterns/decorator/FullFrame.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_DECORATOR_FULLFRAME_H_ 6 | #define STRUCTURAL_PATTERNS_DECORATOR_FULLFRAME_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "structural_patterns/decorator/Frame.h" 11 | 12 | class Display; 13 | 14 | using namespace std; 15 | 16 | // ˄ 17 | 18 | class FullFrame : public Frame 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | public: 25 | 26 | FullFrame(const Display* display); 27 | 28 | ~FullFrame(); 29 | 30 | // Number of characters added left and right decoration characters 31 | const int getColumns() const; 32 | 33 | // Number of rows added the upper and lower decoration lines 34 | const int getRows() const; 35 | 36 | const string getLineText(const int row) const; 37 | 38 | private: 39 | 40 | const string createLine(const char ch, const int size) const; 41 | 42 | // ˅ 43 | public: 44 | 45 | protected: 46 | 47 | private: 48 | FullFrame(const FullFrame&) = delete; 49 | FullFrame& operator=(const FullFrame&) = delete; 50 | FullFrame(FullFrame&&) = delete; 51 | FullFrame& operator=(FullFrame&&) = delete; 52 | 53 | // ˄ 54 | }; 55 | 56 | // ˅ 57 | 58 | // ˄ 59 | 60 | #endif // STRUCTURAL_PATTERNS_DECORATOR_FULLFRAME_H_ 61 | 62 | // ˅ 63 | 64 | // ˄ 65 | -------------------------------------------------------------------------------- /structural_patterns/facade/HtmlWriter.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_FACADE_HTMLWRITER_H_ 6 | #define STRUCTURAL_PATTERNS_FACADE_HTMLWRITER_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | class HtmlWriter 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | ofstream writer; 25 | 26 | public: 27 | 28 | HtmlWriter(const string& file_name); 29 | 30 | ~HtmlWriter(); 31 | 32 | // Write a title 33 | void heading(const string& title); 34 | 35 | // Write a paragraph 36 | void paragraph(const string& message); 37 | 38 | // Write a mail address 39 | void mailto(const string& mail_address, const string& user_name); 40 | 41 | void close(); 42 | 43 | private: 44 | 45 | // Write a link 46 | void anchor(const string& url, const string& text); 47 | 48 | // ˅ 49 | public: 50 | 51 | protected: 52 | 53 | private: 54 | HtmlWriter(const HtmlWriter&) = delete; 55 | HtmlWriter& operator=(const HtmlWriter&) = delete; 56 | HtmlWriter(HtmlWriter&&) = delete; 57 | HtmlWriter& operator=(HtmlWriter&&) = delete; 58 | 59 | // ˄ 60 | }; 61 | 62 | // ˅ 63 | 64 | // ˄ 65 | 66 | #endif // STRUCTURAL_PATTERNS_FACADE_HTMLWRITER_H_ 67 | 68 | // ˅ 69 | 70 | // ˄ 71 | -------------------------------------------------------------------------------- /structural_patterns/composite/Directory.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_COMPOSITE_DIRECTORY_H_ 6 | #define STRUCTURAL_PATTERNS_COMPOSITE_DIRECTORY_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | #include "structural_patterns/composite/FileSystemElement.h" 12 | 13 | using namespace std; 14 | 15 | // ˄ 16 | 17 | class Directory : public FileSystemElement 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | private: 24 | 25 | const string name; 26 | 27 | vector elements; 28 | 29 | public: 30 | 31 | Directory(const string& name); 32 | 33 | ~Directory(); 34 | 35 | const string getName() const; 36 | 37 | const int getSize() const; 38 | 39 | // Print this element with the "upper_path". 40 | void print(const string& upper_path) const; 41 | 42 | // Add an element 43 | void add(FileSystemElement* element); 44 | 45 | // ˅ 46 | public: 47 | 48 | protected: 49 | 50 | private: 51 | Directory(const Directory&) = delete; 52 | Directory& operator=(const Directory&) = delete; 53 | Directory(Directory&&) = delete; 54 | Directory& operator=(Directory&&) = delete; 55 | 56 | // ˄ 57 | }; 58 | 59 | // ˅ 60 | 61 | // ˄ 62 | 63 | #endif // STRUCTURAL_PATTERNS_COMPOSITE_DIRECTORY_H_ 64 | 65 | // ˅ 66 | 67 | // ˄ 68 | -------------------------------------------------------------------------------- /behavioral_patterns/command/HistoryCommand.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_COMMAND_HISTORYCOMMAND_H_ 6 | #define BEHAVIORAL_PATTERNS_COMMAND_HISTORYCOMMAND_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/command/Command.h" 11 | 12 | using namespace std; 13 | 14 | 15 | // ˄ 16 | 17 | // Holder of the past commands 18 | class HistoryCommand : public Command 19 | { 20 | // ˅ 21 | 22 | // ˄ 23 | 24 | private: 25 | 26 | // A set of past commands 27 | vector past_commands; 28 | 29 | public: 30 | 31 | HistoryCommand(); 32 | 33 | ~HistoryCommand(); 34 | 35 | // Execute all past commands 36 | void execute() const; 37 | 38 | void add(Command* cmd); 39 | 40 | // Delete the last command 41 | void undo(); 42 | 43 | // Delete all past commands 44 | void clear(); 45 | 46 | // ˅ 47 | public: 48 | 49 | protected: 50 | 51 | private: 52 | HistoryCommand(const HistoryCommand&) = delete; 53 | HistoryCommand& operator=(const HistoryCommand&) = delete; 54 | HistoryCommand(HistoryCommand&&) = delete; 55 | HistoryCommand& operator=(HistoryCommand&&) = delete; 56 | 57 | // ˄ 58 | }; 59 | 60 | // ˅ 61 | 62 | // ˄ 63 | 64 | #endif // BEHAVIORAL_PATTERNS_COMMAND_HISTORYCOMMAND_H_ 65 | 66 | // ˅ 67 | 68 | // ˄ 69 | -------------------------------------------------------------------------------- /behavioral_patterns/mediator/ColleagueTextField.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUETEXTFIELD_H_ 6 | #define BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUETEXTFIELD_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/mediator/Colleague.h" 11 | 12 | using namespace System::Windows::Forms; 13 | 14 | // ˄ 15 | 16 | class ColleagueTextField : public Colleague 17 | { 18 | // ˅ 19 | 20 | // ˄ 21 | 22 | private: 23 | 24 | const msclr::gcroot text_box; 25 | 26 | public: 27 | 28 | ColleagueTextField(const msclr::gcroot text_box, Mediator* mediator); 29 | 30 | ~ColleagueTextField(); 31 | 32 | // Set enable/disable from the Mediator 33 | void setActivation(const bool is_enable); 34 | 35 | bool isEmpty() const; 36 | 37 | // ˅ 38 | public: 39 | 40 | protected: 41 | 42 | private: 43 | ColleagueTextField(const ColleagueTextField&) = delete; 44 | ColleagueTextField& operator=(const ColleagueTextField&) = delete; 45 | ColleagueTextField(ColleagueTextField&&) = delete; 46 | ColleagueTextField& operator=(ColleagueTextField&&) = delete; 47 | 48 | // ˄ 49 | }; 50 | 51 | // ˅ 52 | 53 | // ˄ 54 | 55 | #endif // BEHAVIORAL_PATTERNS_MEDIATOR_COLLEAGUETEXTFIELD_H_ 56 | 57 | // ˅ 58 | 59 | // ˄ 60 | -------------------------------------------------------------------------------- /behavioral_patterns/observer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "behavioral_patterns/observer/NumberSubject.h" 5 | #include "behavioral_patterns/observer/DigitObserver.h" 6 | #include "behavioral_patterns/observer/BarChartObserver.h" 7 | 8 | #ifdef _MSC_VER 9 | #include 10 | #else 11 | #include 12 | #endif 13 | 14 | using namespace std; 15 | 16 | /* 17 | Observers observe a Subject object holding a numerical value and display the value. 18 | The display formats are digits and bar charts. 19 | */ 20 | 21 | int main() { 22 | unique_ptr numberSubject(new NumberSubject()); 23 | unique_ptr digit_observer(new DigitObserver(numberSubject.get())); 24 | numberSubject->attach(digit_observer.get()); 25 | unique_ptr bar_chart_observer(new BarChartObserver(numberSubject.get())); 26 | numberSubject->attach(bar_chart_observer.get()); 27 | 28 | for (int i = 0; i < 20; ++i) { 29 | random_device random; 30 | mt19937 mt(random()); 31 | uniform_int_distribution random_value(0, 49); 32 | numberSubject->setValue(random_value(mt)); 33 | 34 | #ifdef _MSC_VER 35 | Sleep(200); 36 | #else 37 | usleep(0.2 * 1000000); 38 | #endif 39 | } 40 | 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /behavioral_patterns/strategy/Player.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_STRATEGY_PLAYER_H_ 6 | #define BEHAVIORAL_PATTERNS_STRATEGY_PLAYER_H_ 7 | 8 | // ˅ 9 | #include 10 | #include "behavioral_patterns/strategy/GameResultType.h" 11 | 12 | class Strategy; 13 | class HandSignal; 14 | 15 | using namespace std; 16 | 17 | // ˄ 18 | 19 | class Player 20 | { 21 | // ˅ 22 | 23 | // ˄ 24 | 25 | private: 26 | 27 | const string name; 28 | 29 | int win_count; 30 | 31 | int loss_count; 32 | 33 | int game_count; 34 | 35 | Strategy* strategy; 36 | 37 | public: 38 | 39 | Player(const string& name, Strategy* strategy); 40 | 41 | // Show a hand signal from the strategy. 42 | HandSignal* showHandSignal(); 43 | 44 | // Notify a game result. 45 | void notifyGameResult(GameResultType result, HandSignal* ownHand, HandSignal* opponentsHand); 46 | 47 | const string toString() const; 48 | 49 | // ˅ 50 | public: 51 | 52 | protected: 53 | 54 | private: 55 | Player(const Player&) = delete; 56 | Player& operator=(const Player&) = delete; 57 | Player(Player&&) = delete; 58 | Player& operator=(Player&&) = delete; 59 | 60 | // ˄ 61 | }; 62 | 63 | // ˅ 64 | 65 | // ˄ 66 | 67 | #endif // BEHAVIORAL_PATTERNS_STRATEGY_PLAYER_H_ 68 | 69 | // ˅ 70 | 71 | // ˄ 72 | -------------------------------------------------------------------------------- /behavioral_patterns/command/PaintingCommand.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef BEHAVIORAL_PATTERNS_COMMAND_PAINTINGCOMMAND_H_ 6 | #define BEHAVIORAL_PATTERNS_COMMAND_PAINTINGCOMMAND_H_ 7 | 8 | // ˅ 9 | #include "behavioral_patterns/command/Command.h" 10 | 11 | class PaintingTarget; 12 | 13 | 14 | // ˄ 15 | 16 | // Command to paint a single point 17 | class PaintingCommand : public Command 18 | { 19 | // ˅ 20 | 21 | // ˄ 22 | 23 | private: 24 | 25 | // Painting position x 26 | const double painting_pos_x; 27 | 28 | // Painting position y 29 | const double painting_pos_y; 30 | 31 | const PaintingTarget* painting_target; 32 | 33 | public: 34 | 35 | PaintingCommand(const PaintingTarget* painting_target, const double painting_pos_x, const double painting_pos_y); 36 | 37 | ~PaintingCommand(); 38 | 39 | void execute() const; 40 | 41 | // ˅ 42 | public: 43 | 44 | protected: 45 | 46 | private: 47 | PaintingCommand(const PaintingCommand&) = delete; 48 | PaintingCommand& operator=(const PaintingCommand&) = delete; 49 | PaintingCommand(PaintingCommand&&) = delete; 50 | PaintingCommand& operator=(PaintingCommand&&) = delete; 51 | 52 | // ˄ 53 | }; 54 | 55 | // ˅ 56 | 57 | // ˄ 58 | 59 | #endif // BEHAVIORAL_PATTERNS_COMMAND_PAINTINGCOMMAND_H_ 60 | 61 | // ˅ 62 | 63 | // ˄ 64 | -------------------------------------------------------------------------------- /structural_patterns/facade/DataLibrary.h: -------------------------------------------------------------------------------- 1 | // ˅ 2 | 3 | // ˄ 4 | 5 | #ifndef STRUCTURAL_PATTERNS_FACADE_DATALIBRARY_H_ 6 | #define STRUCTURAL_PATTERNS_FACADE_DATALIBRARY_H_ 7 | 8 | // ˅ 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | // ˄ 15 | 16 | // Singleton ( based on the example code on Wikipedia ) 17 | // https://en.wikipedia.org/w/index.php?title=Singleton_pattern&oldid=1115882454#C++ 18 | // Note: The latest Wikipedia page has had the C++ code example removed. This link is to the page before it was removed. 19 | class DataLibrary 20 | { 21 | // ˅ 22 | 23 | // ˄ 24 | 25 | public: 26 | 27 | static DataLibrary* getInstance(); 28 | 29 | private: 30 | 31 | DataLibrary(); 32 | 33 | public: 34 | 35 | ~DataLibrary(); 36 | 37 | // Read a data library file. 38 | const map getProperties(const string& data_library_name) const; 39 | 40 | // ˅ 41 | public: 42 | 43 | protected: 44 | 45 | private: 46 | DataLibrary(const DataLibrary&) = delete; 47 | DataLibrary& operator=(const DataLibrary&) = delete; 48 | DataLibrary(DataLibrary&&) = delete; 49 | DataLibrary& operator=(DataLibrary&&) = delete; 50 | 51 | // ˄ 52 | }; 53 | 54 | // ˅ 55 | 56 | // ˄ 57 | 58 | #endif // STRUCTURAL_PATTERNS_FACADE_DATALIBRARY_H_ 59 | 60 | // ˅ 61 | 62 | // ˄ 63 | --------------------------------------------------------------------------------