├── .gitignore
├── umls
└── html-docs
│ ├── assets
│ ├── js
│ │ └── main.js
│ ├── img
│ │ ├── glyphicons-halflings.png
│ │ └── glyphicons-halflings-white.png
│ └── css
│ │ └── jquery.bonsai.css
│ ├── index.html
│ └── diagrams
│ └── 078238f7718b97a7cea2ea60ee53dcd7.svg
├── .gitattributes
├── tests
├── CMakeLists.txt
├── lib
│ └── gtest-1.7.0
│ │ ├── xcode
│ │ ├── Config
│ │ │ ├── TestTarget.xcconfig
│ │ │ ├── FrameworkTarget.xcconfig
│ │ │ ├── StaticLibraryTarget.xcconfig
│ │ │ ├── DebugProject.xcconfig
│ │ │ ├── ReleaseProject.xcconfig
│ │ │ └── General.xcconfig
│ │ ├── Samples
│ │ │ └── FrameworkSample
│ │ │ │ ├── Info.plist
│ │ │ │ ├── widget.h
│ │ │ │ ├── widget.cc
│ │ │ │ ├── runtests.sh
│ │ │ │ └── widget_test.cc
│ │ ├── Resources
│ │ │ └── Info.plist
│ │ └── Scripts
│ │ │ └── runtests.sh
│ │ ├── m4
│ │ ├── ltversion.m4
│ │ └── gtest.m4
│ │ ├── CONTRIBUTORS
│ │ ├── LICENSE
│ │ ├── test
│ │ ├── production.cc
│ │ ├── gtest_main_unittest.cc
│ │ ├── gtest_uninitialized_test_.cc
│ │ ├── gtest_xml_outfile1_test_.cc
│ │ ├── gtest_xml_outfile2_test_.cc
│ │ ├── gtest-typed-test2_test.cc
│ │ ├── gtest_help_test_.cc
│ │ ├── production.h
│ │ ├── gtest_all_test.cc
│ │ ├── gtest_prod_test.cc
│ │ ├── gtest_sole_header_test.cc
│ │ ├── gtest-param-test_test.h
│ │ ├── gtest_no_test_unittest.cc
│ │ ├── gtest-typed-test_test.h
│ │ ├── gtest_uninitialized_test.py
│ │ ├── gtest-param-test2_test.cc
│ │ ├── gtest_color_test_.cc
│ │ ├── gtest_throw_on_failure_test_.cc
│ │ ├── gtest_break_on_failure_unittest_.cc
│ │ ├── gtest_throw_on_failure_ex_test.cc
│ │ └── gtest_shuffle_test_.cc
│ │ ├── src
│ │ ├── gtest_main.cc
│ │ └── gtest-all.cc
│ │ ├── fused-src
│ │ └── gtest
│ │ │ └── gtest_main.cc
│ │ ├── codegear
│ │ ├── gtest_all.cc
│ │ ├── gtest_link.cc
│ │ └── gtest.groupproj
│ │ ├── scripts
│ │ └── test
│ │ │ └── Makefile
│ │ ├── samples
│ │ ├── sample4_unittest.cc
│ │ ├── sample1.h
│ │ ├── sample4.cc
│ │ ├── sample4.h
│ │ ├── sample2.cc
│ │ ├── sample1.cc
│ │ └── sample2.h
│ │ ├── build-aux
│ │ └── config.h.in
│ │ ├── msvc
│ │ ├── gtest.sln
│ │ └── gtest-md.sln
│ │ ├── include
│ │ └── gtest
│ │ │ └── gtest_prod.h
│ │ ├── configure.ac
│ │ └── make
│ │ └── Makefile
└── unit_tests
│ ├── facade_test.cc
│ ├── proxy_test.cc
│ ├── strategy_test.cc
│ ├── singleton_test.cc
│ ├── CMakeLists.txt
│ ├── decorator_test.cc
│ ├── mediator_test.cc
│ ├── template_method_test.cc
│ ├── state_test.cc
│ ├── adapter_test.cc
│ ├── factory_method_test.cc
│ ├── memento_test.cc
│ ├── bridge_test.cc
│ ├── interpreter_test.cc
│ ├── visitor_test.cc
│ ├── observer_test.cc
│ ├── aggregate_test.cc
│ ├── prototype_test.cc
│ ├── flyweight_test.cc
│ ├── builder_test.cc
│ ├── command_test.cc
│ ├── chain_of_responsibility_test.cc
│ ├── abstract_factory_test.cc
│ └── composite_test.cc
├── CMakeLists.txt
├── src
├── singleton.h
├── singleton.cc
├── factory_method.cc
├── facade.h
├── decorator.cc
├── decorator.h
├── template_method.h
├── factory_method.h
├── memento.h
├── proxy.cc
├── interpreter.h
├── template_method.cc
├── CMakeLists.txt
├── state.h
├── flyweight.h
├── prototype.h
├── aggregate.cc
├── proxy.h
├── bridge.cc
├── memento.cc
├── bridge.h
├── aggregate.h
├── builder.h
├── command.h
├── facade.cc
├── strategy.h
├── visitor.h
├── builder.cc
├── flyweight.cc
├── adapter.h
├── chain_of_responsibility.h
├── composite.h
├── mediator.h
├── mediator.cc
├── observer.h
├── state.cc
├── visitor.cc
├── observer.cc
├── adapter.cc
├── interpreter.cc
├── strategy.cc
├── composite.cc
├── abstract_factory.h
├── command.cc
├── prototype.cc
├── abstract_factory.cc
└── chain_of_responsibility.cc
├── README.md
└── main.cc
/.gitignore:
--------------------------------------------------------------------------------
1 | .idea/
--------------------------------------------------------------------------------
/umls/html-docs/assets/js/main.js:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | *.html linguist-language=c++
--------------------------------------------------------------------------------
/tests/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | project(patterns_tests)
2 |
3 | add_subdirectory(lib/gtest-1.7.0)
4 | add_subdirectory(unit_tests)
5 |
--------------------------------------------------------------------------------
/umls/html-docs/assets/img/glyphicons-halflings.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yogykwan/design-patterns-cpp/HEAD/umls/html-docs/assets/img/glyphicons-halflings.png
--------------------------------------------------------------------------------
/umls/html-docs/assets/img/glyphicons-halflings-white.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/yogykwan/design-patterns-cpp/HEAD/umls/html-docs/assets/img/glyphicons-halflings-white.png
--------------------------------------------------------------------------------
/tests/lib/gtest-1.7.0/xcode/Config/TestTarget.xcconfig:
--------------------------------------------------------------------------------
1 | //
2 | // TestTarget.xcconfig
3 | //
4 | // These are Test target settings for the gtest framework and examples. It
5 | // is set in the "Based On:" dropdown in the "Target" info dialog.
6 |
7 | PRODUCT_NAME = $(TARGET_NAME)
8 | HEADER_SEARCH_PATHS = ../include
9 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 3.6)
2 | project(design-patterns)
3 |
4 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
5 |
6 | set(SOURCE_FILES main.cc)
7 | add_executable(patterns_run ${SOURCE_FILES})
8 |
9 | include_directories(src)
10 |
11 | add_subdirectory(src)
12 | add_subdirectory(tests)
13 |
14 | target_link_libraries(patterns_run patterns)
--------------------------------------------------------------------------------
/src/singleton.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_SINGLETON_H
6 | #define DESIGN_PATTERNS_SINGLETON_H
7 |
8 |
9 | #include
10 |
11 | class Singleton {
12 | private:
13 | Singleton() {};
14 | public:
15 | static Singleton* GetInstance();
16 |
17 | private:
18 | static Singleton* instance_;
19 | static pthread_mutex_t mutex_;
20 | };
21 |
22 |
23 | #endif //DESIGN_PATTERNS_SINGLETON_H
24 |
--------------------------------------------------------------------------------
/src/singleton.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "singleton.h"
6 |
7 | // initialize stastic parameters, compile errors occur without them
8 | Singleton* Singleton::instance_ = NULL;
9 | pthread_mutex_t Singleton::mutex_;
10 |
11 | Singleton* Singleton::GetInstance() {
12 | if(instance_ == NULL) {
13 | pthread_mutex_lock(&mutex_);
14 | if(instance_ == NULL) {
15 | instance_ = new Singleton();
16 | }
17 | pthread_mutex_unlock(&mutex_);
18 | }
19 | return instance_;
20 | }
--------------------------------------------------------------------------------
/tests/unit_tests/facade_test.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "gtest/gtest.h"
6 | #include "facade.h"
7 |
8 | class FacadeFixture: public ::testing::Test {
9 | protected:
10 | virtual void TearDown() {};
11 | virtual void SetUp() {};
12 |
13 | public:
14 | FacadeFixture(): Test() {
15 | fund_ = new Fund;
16 | fund_->BuyFund();
17 | fund_->SellFund();
18 | }
19 |
20 | virtual ~FacadeFixture() {
21 | delete fund_;
22 | }
23 |
24 | Fund *fund_;
25 | };
26 |
27 | TEST_F(FacadeFixture, _test) {
28 | }
--------------------------------------------------------------------------------
/src/factory_method.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/26.
3 | //
4 |
5 | #include "factory_method.h"
6 | #include
7 |
8 | void LeiFeng::Wash() {
9 | std::cout << "Wash" << std::endl;
10 | }
11 |
12 | void LeiFeng::Sweep() {
13 | std::cout << "Sweep" << std::endl;
14 | }
15 |
16 | void LeiFeng::BuyRice() {
17 | std::cout << "BuyRice" << std::endl;
18 | }
19 |
20 | LeiFeng* UndergraduateFactory::CreateLeiFeng() {
21 | LeiFeng* leifeng = new Undergraduate();
22 | return leifeng;
23 | }
24 |
25 | LeiFeng* VolunteerFactory::CreateLeiFeng() {
26 | LeiFeng* leifeng = new Volunteer();
27 | return leifeng;
28 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # design-patterns-cpp
2 | 《大话设计模式》中23种设计模式案例的C++实现版本。样例忠于原书,某些地方根据C++特性做了修改。
3 |
4 | ## 组织结构
5 | * src - 每个模式案例的声明(.h)和实现(.cc)
6 | * tests - 每个模式案例的gtest,相当于客户端
7 | * docs - 每个模式案例的UML(.html)
8 |
9 | ## 编译结果
10 | * patterns - src编译得到的模式案例类库
11 | * patterns_run - main输出设计模式字符图
12 | * patterns_test - tests中所有案例的单元测试
13 |
14 | ## 读书笔记
15 | * [创建型模式](http://jennica.space/2016/12/28/design-patterns-creational/)
16 | * [结构型模式](http://jennica.space/2016/12/30/design-patterns-structural/)
17 | * [行为型模式](http://jennica.space/2017/01/03/design-patterns-behavioral/)
18 |
19 | ## Python版
20 | [design-patterns-py](https://github.com/yogykwan/design-patterns-py)
21 |
22 |
--------------------------------------------------------------------------------
/src/facade.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/29.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_FACADE_H
6 | #define DESIGN_PATTERNS_FACADE_H
7 |
8 |
9 | class Stock1 {
10 | public:
11 | void Buy();
12 | void Sell();
13 | };
14 |
15 | class Stock2 {
16 | public:
17 | void Buy();
18 | void Sell();
19 | };
20 |
21 | class Reality1 {
22 | public:
23 | void Buy();
24 | void Sell();
25 | };
26 |
27 | class Fund {
28 | public:
29 | Fund();
30 | ~Fund();
31 | void BuyFund();
32 | void SellFund();
33 |
34 | private:
35 | Stock1 *stock1_;
36 | Stock2 *stock2_;
37 | Reality1 *reality1_;
38 | };
39 |
40 |
41 | #endif //DESIGN_PATTERNS_FACADE_H
42 |
--------------------------------------------------------------------------------
/tests/lib/gtest-1.7.0/xcode/Config/FrameworkTarget.xcconfig:
--------------------------------------------------------------------------------
1 | //
2 | // FrameworkTarget.xcconfig
3 | //
4 | // These are Framework target settings for the gtest framework and examples. It
5 | // is set in the "Based On:" dropdown in the "Target" info dialog.
6 | // This file is based on the Xcode Configuration files in:
7 | // http://code.google.com/p/google-toolbox-for-mac/
8 | //
9 |
10 | // Dynamic libs need to be position independent
11 | GCC_DYNAMIC_NO_PIC = NO
12 |
13 | // Dynamic libs should not have their external symbols stripped.
14 | STRIP_STYLE = non-global
15 |
16 | // Let the user install by specifying the $DSTROOT with xcodebuild
17 | SKIP_INSTALL = NO
18 |
--------------------------------------------------------------------------------
/tests/unit_tests/proxy_test.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "gtest/gtest.h"
6 | #include "proxy.h"
7 |
8 | class ProxyFixture: public ::testing::Test {
9 | protected:
10 | virtual void TearDown() {};
11 | virtual void SetUp() {};
12 |
13 | public:
14 | ProxyFixture(): Test() {
15 | school_girl_ = new SchoolGirl("Alice");
16 | proxy_ = new Proxy(school_girl_);
17 | proxy_->GiveFlowers();
18 | proxy_->GiveDolls();
19 | }
20 |
21 | virtual ~ProxyFixture() {
22 | delete school_girl_;
23 | delete proxy_;
24 | }
25 |
26 | SchoolGirl *school_girl_;
27 | Proxy *proxy_;
28 | };
29 |
30 | TEST_F(ProxyFixture, proxy_test) {
31 | }
--------------------------------------------------------------------------------
/tests/lib/gtest-1.7.0/xcode/Config/StaticLibraryTarget.xcconfig:
--------------------------------------------------------------------------------
1 | //
2 | // StaticLibraryTarget.xcconfig
3 | //
4 | // These are static library target settings for libgtest.a. It
5 | // is set in the "Based On:" dropdown in the "Target" info dialog.
6 | // This file is based on the Xcode Configuration files in:
7 | // http://code.google.com/p/google-toolbox-for-mac/
8 | //
9 |
10 | // Static libs can be included in bundles so make them position independent
11 | GCC_DYNAMIC_NO_PIC = NO
12 |
13 | // Static libs should not have their internal globals or external symbols
14 | // stripped.
15 | STRIP_STYLE = debugging
16 |
17 | // Let the user install by specifying the $DSTROOT with xcodebuild
18 | SKIP_INSTALL = NO
19 |
--------------------------------------------------------------------------------
/src/decorator.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/29.
3 | //
4 |
5 | #include "decorator.h"
6 | #include
7 |
8 | void Person::Show() {
9 | std::cout << "person" << std::endl;
10 | }
11 |
12 | Finery::Finery(Person *component): component_(component) {}
13 |
14 | Tie::Tie(Person *component): Finery(component) {}
15 |
16 | void Tie::Show() {
17 | std::cout << "tie ";
18 | component_->Show();
19 | }
20 |
21 | Suit::Suit(Person *component): Finery(component) {}
22 |
23 | void Suit::Show() {
24 | std::cout << "suit ";
25 | component_->Show();
26 | }
27 |
28 | Shoes::Shoes(Person *component): Finery(component) {}
29 |
30 | void Shoes::Show() {
31 | std::cout << "shoes ";
32 | component_->Show();
33 | }
--------------------------------------------------------------------------------
/tests/unit_tests/strategy_test.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "gtest/gtest.h"
6 | #include "strategy.h"
7 |
8 | class StrategyFixture: public ::testing::Test {
9 | protected:
10 | virtual void TearDown() {};
11 | virtual void SetUp() {};
12 |
13 | public:
14 | StrategyFixture(): Test() {
15 | cash_context_ = new CashContext("rebate", "0.8");
16 | cash_context_->GetResult(1000);
17 |
18 | cash_context_ = new CashContext("return", "300 100");
19 | cash_context_->GetResult(1000);
20 | }
21 |
22 | virtual ~StrategyFixture() {
23 | delete cash_context_;
24 | }
25 |
26 | CashContext *cash_context_;
27 | };
28 |
29 | TEST_F(StrategyFixture, strategy_test) {
30 | }
--------------------------------------------------------------------------------
/umls/html-docs/assets/css/jquery.bonsai.css:
--------------------------------------------------------------------------------
1 | .bonsai,
2 | .bonsai li {
3 | margin: 0;
4 | padding: 0;
5 | list-style: none;
6 | overflow: hidden;
7 | }
8 |
9 | .bonsai li {
10 | position: relative;
11 | padding-left: 1.3em; /* padding for the thumb */
12 | }
13 |
14 | li .thumb {
15 | margin: -1px 0 0 -1em; /* negative margin into the padding of the li */
16 | position: absolute;
17 | cursor: pointer;
18 | }
19 |
20 | li.has-children > .thumb:after {
21 | content: '▸';
22 | }
23 |
24 | li.has-children.expanded > .thumb:after {
25 | content: '▾';
26 | }
27 |
28 | li.collapsed > ol.bonsai {
29 | height: 0;
30 | overflow: hidden;
31 | }
32 |
33 | .bonsai .all,
34 | .bonsai .none {
35 | cursor: pointer;
36 | }
37 |
--------------------------------------------------------------------------------
/tests/unit_tests/singleton_test.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "gtest/gtest.h"
6 | #include "singleton.h"
7 |
8 | class SingletonFixture: public ::testing::Test {
9 | protected:
10 | virtual void TearDown() {};
11 | virtual void SetUp() {};
12 |
13 | public:
14 | SingletonFixture(): Test() {
15 | instance1_ = Singleton::GetInstance();
16 | instance2_ = Singleton::GetInstance();
17 | }
18 |
19 | virtual ~SingletonFixture() {
20 | delete instance1_;
21 | if(instance1_ != instance2_){
22 | delete instance2_;
23 | }
24 | }
25 |
26 | Singleton* instance1_;
27 | Singleton* instance2_;
28 | };
29 |
30 | TEST_F(SingletonFixture, singleton_test) {
31 | EXPECT_EQ(instance1_, instance2_);
32 | }
--------------------------------------------------------------------------------
/tests/unit_tests/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
2 |
3 | set(SOURCE_FILES
4 | abstract_factory_test.cc factory_method_test.cc singleton_test.cc builder_test.cc
5 | prototype_test.cc proxy_test.cc adapter_test.cc bridge_test.cc
6 | facade_test.cc decorator_test.cc flyweight_test.cc composite_test.cc
7 | chain_of_responsibility_test.cc strategy_test.cc state_test.cc observer_test.cc
8 | aggregate_test.cc memento_test.cc command_test.cc template_method_test.cc
9 | mediator_test.cc interpreter_test.cc visitor_test.cc)
10 | add_executable(patterns_test ${SOURCE_FILES})
11 |
12 | target_link_libraries(patterns_test gtest gtest_main)
13 | target_link_libraries(patterns_test patterns)
14 |
15 |
--------------------------------------------------------------------------------
/tests/unit_tests/decorator_test.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "gtest/gtest.h"
6 | #include "decorator.h"
7 |
8 | class DecoratorFixture: public ::testing::Test {
9 | protected:
10 | virtual void TearDown() {};
11 | virtual void SetUp() {};
12 |
13 | public:
14 | DecoratorFixture(): Test() {
15 | person_ = new Person();
16 | tie_ = new Tie(person_);
17 | suit_ = new Suit(tie_);
18 | shoes_ = new Shoes(suit_);
19 | shoes_->Show();
20 | }
21 |
22 | virtual ~DecoratorFixture() {
23 | delete person_;
24 | delete shoes_;
25 | delete suit_;
26 | delete tie_;
27 | }
28 |
29 | Person* person_;
30 | Tie* tie_;
31 | Suit* suit_;
32 | Shoes* shoes_;
33 | };
34 |
35 | TEST_F(DecoratorFixture, decorator_test) {
36 | }
--------------------------------------------------------------------------------
/src/decorator.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/29.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_DECORATOR_H
6 | #define DESIGN_PATTERNS_DECORATOR_H
7 |
8 |
9 | class Person {
10 | public:
11 | virtual void Show();
12 | };
13 |
14 | class Finery: public Person {
15 | public:
16 | Finery() {}
17 | Finery(Person*);
18 | void Show() {}
19 |
20 | protected:
21 | Person *component_;
22 | };
23 |
24 | class Tie: public Finery {
25 | public:
26 | Tie() {}
27 | Tie(Person*);
28 | void Show();
29 | };
30 |
31 | class Suit: public Finery {
32 | public:
33 | Suit() {}
34 | Suit(Person*);
35 | void Show();
36 | };
37 |
38 | class Shoes: public Finery {
39 | public:
40 | Shoes() {}
41 | Shoes(Person*);
42 | void Show();
43 | };
44 |
45 |
46 | #endif //DESIGN_PATTERNS_DECORATOR_H
47 |
--------------------------------------------------------------------------------
/src/template_method.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2017/1/3.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_TEMPLATE_METHOD_H
6 | #define DESIGN_PATTERNS_TEMPLATE_METHOD_H
7 |
8 | #include
9 |
10 | class TestPaper {
11 | public:
12 | void Question1();
13 | void Question2();
14 | void Question3();
15 |
16 | protected:
17 | virtual std::string Answer1() = 0;
18 | virtual std::string Answer2() = 0;
19 | virtual std::string Answer3() = 0;
20 | };
21 |
22 | class TestPaperA: public TestPaper {
23 | std::string Answer1();
24 | std::string Answer2();
25 | std::string Answer3();
26 | };
27 |
28 | class TestPaperB: public TestPaper {
29 | std::string Answer1();
30 | std::string Answer2();
31 | std::string Answer3();
32 | };
33 |
34 |
35 | #endif //DESIGN_PATTERNS_TEMPLATE_METHOD_H
36 |
--------------------------------------------------------------------------------
/tests/lib/gtest-1.7.0/m4/ltversion.m4:
--------------------------------------------------------------------------------
1 | # ltversion.m4 -- version numbers -*- Autoconf -*-
2 | #
3 | # Copyright (C) 2004 Free Software Foundation, Inc.
4 | # Written by Scott James Remnant, 2004
5 | #
6 | # This file is free software; the Free Software Foundation gives
7 | # unlimited permission to copy and/or distribute it, with or without
8 | # modifications, as long as this notice is preserved.
9 |
10 | # @configure_input@
11 |
12 | # serial 3337 ltversion.m4
13 | # This file is part of GNU Libtool
14 |
15 | m4_define([LT_PACKAGE_VERSION], [2.4.2])
16 | m4_define([LT_PACKAGE_REVISION], [1.3337])
17 |
18 | AC_DEFUN([LTVERSION_VERSION],
19 | [macro_version='2.4.2'
20 | macro_revision='1.3337'
21 | _LT_DECL(, macro_version, 0, [Which release of libtool.m4 was used?])
22 | _LT_DECL(, macro_revision, 0)
23 | ])
24 |
--------------------------------------------------------------------------------
/src/factory_method.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/26.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_FACTORY_METHOD_H
6 | #define DESIGN_PATTERNS_FACTORY_METHOD_H
7 |
8 | class LeiFeng {
9 | public:
10 | virtual void Wash();
11 | virtual void Sweep();
12 | virtual void BuyRice();
13 | };
14 |
15 | class Undergraduate: public LeiFeng {
16 |
17 | };
18 |
19 | class Volunteer: public LeiFeng {
20 |
21 | };
22 |
23 | class IFactory {
24 | public:
25 | IFactory() {};
26 | virtual ~IFactory() {};
27 | virtual LeiFeng* CreateLeiFeng() = 0;
28 | };
29 |
30 | class UndergraduateFactory: public IFactory {
31 | public:
32 | LeiFeng* CreateLeiFeng();
33 | };
34 |
35 | class VolunteerFactory: public IFactory {
36 | public:
37 | LeiFeng* CreateLeiFeng();
38 | };
39 |
40 | #endif //DESIGN_PATTERNS_FACTORY_METHOD_H
41 |
--------------------------------------------------------------------------------
/src/memento.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2017/1/3.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_MEMENTO_H
6 | #define DESIGN_PATTERNS_MEMENTO_H
7 |
8 | class StateMemento {
9 | public:
10 | StateMemento() {}
11 | StateMemento(int, int);
12 | int GetHp();
13 | int GetMp();
14 |
15 | private:
16 | int hp_;
17 | int mp_;
18 | };
19 |
20 | class GameRole {
21 | public:
22 | GameRole();
23 | StateMemento* CreateMemento();
24 | void StateDisplay();
25 | void Fight();
26 | void RecoveryState(StateMemento*);
27 |
28 | private:
29 | int hp_;
30 | int mp_;
31 | };
32 |
33 | class StateCaretaker {
34 | public:
35 | StateCaretaker() {}
36 | StateCaretaker(StateMemento*);
37 | ~StateCaretaker();
38 | StateMemento* GetMemento();
39 | private:
40 | StateMemento* memento_;
41 | };
42 |
43 |
44 | #endif //DESIGN_PATTERNS_MEMENTO_H
45 |
--------------------------------------------------------------------------------
/src/proxy.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/29.
3 | //
4 |
5 | #include "proxy.h"
6 |
7 | SchoolGirl::SchoolGirl(std::string name): name_(name) {
8 | }
9 |
10 | std::string SchoolGirl::GetName() {
11 | return name_;
12 | }
13 |
14 | Pursuit::Pursuit(SchoolGirl *school_girl): school_girl_(school_girl){
15 | }
16 |
17 | void Pursuit::GiveFlowers() {
18 | std::cout << "Give flowers to " << school_girl_->GetName() << std::endl;
19 | }
20 |
21 | void Pursuit::GiveDolls() {
22 | std::cout << "Give dolls to " << school_girl_->GetName() << std::endl;
23 | }
24 |
25 | Proxy::Proxy(SchoolGirl *school_girl) {
26 | pursuit_ = new Pursuit(school_girl);
27 | }
28 |
29 | Proxy::~Proxy() {
30 | delete pursuit_;
31 | }
32 |
33 | void Proxy::GiveFlowers() {
34 | pursuit_->GiveFlowers();
35 | }
36 |
37 | void Proxy::GiveDolls() {
38 | pursuit_->GiveDolls();
39 | }
--------------------------------------------------------------------------------
/tests/unit_tests/mediator_test.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "gtest/gtest.h"
6 | #include "mediator.h"
7 |
8 | class MediatorFixture: public ::testing::Test {
9 | protected:
10 | virtual void TearDown() {};
11 | virtual void SetUp() {};
12 |
13 | public:
14 | MediatorFixture(): Test() {
15 | unsc_ = new UnitedNationsSecurityCouncil();
16 | usa_ = new Usa(unsc_);
17 | iraq_ = new Iraq(unsc_);
18 | unsc_->SetUsa(usa_);
19 | unsc_->SetIraq(iraq_);
20 | usa_->Declare("Stop nuclear weapons");
21 | iraq_->Declare("No nuclear here");
22 | }
23 |
24 | virtual ~MediatorFixture() {
25 | delete unsc_;
26 | delete usa_;
27 | delete iraq_;
28 | }
29 |
30 | UnitedNationsSecurityCouncil *unsc_;
31 | Country *usa_;
32 | Country *iraq_;
33 | };
34 |
35 | TEST_F(MediatorFixture, mediator_test) {
36 | }
--------------------------------------------------------------------------------
/umls/html-docs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
13 |
14 |
15 | Frame Alert
16 | This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client.
17 |
18 | Link toNon-frame version.
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/src/interpreter.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2017/1/3.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_INTERPRETER_H
6 | #define DESIGN_PATTERNS_INTERPRETER_H
7 |
8 | #include
9 |
10 | class Context {
11 | public:
12 | void SetText(std::string);
13 | std::string GetText();
14 |
15 | private:
16 | std::string text_;
17 | };
18 |
19 | class Expression {
20 | public:
21 | virtual ~Expression() {}
22 | void Interprete(Context*);
23 |
24 | protected:
25 | virtual void Excute(std::string, double) = 0;
26 | };
27 |
28 | class Scale: public Expression {
29 | private:
30 | void Excute(std::string, double);
31 | };
32 |
33 | class Note: public Expression {
34 | private:
35 | void Excute(std::string, double);
36 | };
37 |
38 | class ExpressionFactory {
39 | public:
40 | Expression* CreateExpression(Context*);
41 | };
42 |
43 | #endif //DESIGN_PATTERNS_INTERPRETER_H
44 |
--------------------------------------------------------------------------------
/tests/unit_tests/template_method_test.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "gtest/gtest.h"
6 | #include "template_method.h"
7 |
8 | class TemplateMethodFixture: public ::testing::Test {
9 | protected:
10 | virtual void TearDown() {};
11 | virtual void SetUp() {};
12 |
13 | public:
14 | TemplateMethodFixture(): Test() {
15 | test_paper_a_ = new TestPaperA();
16 | test_paper_a_->Question1();
17 | test_paper_a_->Question2();
18 | test_paper_a_->Question3();
19 |
20 | test_paper_b_ = new TestPaperB();
21 | test_paper_b_->Question1();
22 | test_paper_b_->Question2();
23 | test_paper_b_->Question3();
24 | }
25 |
26 | virtual ~TemplateMethodFixture() {
27 |
28 | }
29 |
30 | TestPaper *test_paper_a_;
31 | TestPaper *test_paper_b_;
32 | };
33 |
34 | TEST_F(TemplateMethodFixture, template_method_test) {
35 | }
--------------------------------------------------------------------------------
/src/template_method.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2017/1/3.
3 | //
4 |
5 | #include "template_method.h"
6 | #include
7 |
8 | void TestPaper::Question1() {
9 | std::cout << "question 1: " << Answer1() << std::endl;
10 | }
11 |
12 | void TestPaper::Question2() {
13 | std::cout << "question 2: " << Answer2() << std::endl;
14 | }
15 |
16 | void TestPaper::Question3() {
17 | std::cout << "question 3: " << Answer3() << std::endl;
18 | }
19 |
20 | std::string TestPaperA::Answer1() {
21 | return "a";
22 | }
23 |
24 | std::string TestPaperA::Answer2() {
25 | return "a";
26 | }
27 |
28 | std::string TestPaperA::Answer3() {
29 | return "a";
30 | }
31 |
32 | std::string TestPaperB::Answer1() {
33 | return "b";
34 | }
35 |
36 | std::string TestPaperB::Answer2() {
37 | return "b";
38 | }
39 |
40 | std::string TestPaperB::Answer3() {
41 | return "b";
42 | }
43 |
44 |
--------------------------------------------------------------------------------
/tests/unit_tests/state_test.cc:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/28.
3 | //
4 |
5 | #include "gtest/gtest.h"
6 | #include "state.h"
7 |
8 | class StateFixture: public ::testing::Test {
9 | protected:
10 | virtual void TearDown() {};
11 | virtual void SetUp() {};
12 |
13 | public:
14 | StateFixture(): Test() {
15 | work_ = new Work();
16 |
17 | work_->hour_ = 15;
18 | work_->WriteProgram();
19 |
20 | work_->hour_ = 20;
21 | work_->finished_ = false;
22 | work_->WriteProgram();
23 |
24 | work_->hour_ = 22;
25 | work_->WriteProgram();
26 |
27 | delete work_;
28 | work_ = new Work();
29 | work_->hour_ = 20;
30 | work_->finished_ = true;
31 | work_->WriteProgram();
32 | }
33 |
34 | virtual ~StateFixture() {
35 | delete work_;
36 | }
37 |
38 | Work *work_;
39 | };
40 |
41 | TEST_F(StateFixture, state_test) {
42 | }
--------------------------------------------------------------------------------
/src/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | project(patterns)
2 |
3 | set(HEADER_FILES
4 | abstract_factory.h factory_method.h singleton.h builder.h
5 | prototype.h proxy.h adapter.h bridge.h
6 | facade.h decorator.h flyweight.h composite.h
7 | chain_of_responsibility.h strategy.h state.h observer.h
8 | aggregate.h memento.h command.h template_method.h
9 | mediator.h interpreter.h visitor.h)
10 |
11 | set(SOURCE_FILES
12 | abstract_factory.cc factory_method.cc singleton.cc builder.cc
13 | prototype.cc proxy.cc adapter.cc bridge.cc
14 | facade.cc decorator.cc flyweight.cc composite.cc
15 | chain_of_responsibility.cc strategy.cc state.cc observer.cc
16 | aggregate.cc memento.cc command.cc template_method.cc
17 | mediator.cc interpreter.cc visitor.cc)
18 |
19 | add_library(patterns STATIC ${SOURCE_FILES} ${HEADER_FILES})
20 |
--------------------------------------------------------------------------------
/src/state.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2017/1/2.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_STATE_H
6 | #define DESIGN_PATTERNS_STATE_H
7 |
8 | class State;
9 |
10 | class Work {
11 | public:
12 | Work();
13 | ~Work();
14 | void SetState(State*);
15 | void WriteProgram();
16 |
17 | public:
18 | bool finished_;
19 | int hour_;
20 |
21 | private:
22 | State* state_;
23 | };
24 |
25 | class State {
26 | public:
27 | virtual ~State() {}
28 | virtual void WriteProgram(Work*) = 0;
29 | };
30 |
31 | class WorkingState: public State {
32 | void WriteProgram(Work* work);
33 | };
34 |
35 | class OvertimeState: public State {
36 | void WriteProgram(Work* work);
37 | };
38 |
39 | class RestState: public State {
40 | void WriteProgram(Work* work);
41 | };
42 |
43 | class SleepingState: public State {
44 | void WriteProgram(Work* work);
45 | };
46 |
47 |
48 | #endif //DESIGN_PATTERNS_STATE_H
49 |
--------------------------------------------------------------------------------
/src/flyweight.h:
--------------------------------------------------------------------------------
1 | //
2 | // Created by Jennica on 2016/12/29.
3 | //
4 |
5 | #ifndef DESIGN_PATTERNS_FLYWEIGHT_H
6 | #define DESIGN_PATTERNS_FLYWEIGHT_H
7 |
8 | #include