├── CMakeLists.txt ├── COPYING.txt ├── README.md ├── _cmake_scripts └── DocumentationTargets.cmake ├── doxygen.config ├── doxygenate.py └── src ├── CMakeLists.txt ├── abstract_factory ├── CMakeLists.txt ├── cheese.h ├── cheese_pizza.cc ├── cheese_pizza.h ├── chicago_pizza_ingredient_factory.cc ├── chicago_pizza_ingredient_factory.h ├── chicago_pizza_store.cc ├── chicago_pizza_store.h ├── clam_pizza.cc ├── clam_pizza.h ├── clams.h ├── dough.h ├── fresh_clams.cc ├── fresh_clams.h ├── frozen_clams.cc ├── frozen_clams.h ├── marinara_sauce.cc ├── marinara_sauce.h ├── mozzarella_cheese.cc ├── mozzarella_cheese.h ├── ny_pizza_ingredient_factory.cc ├── ny_pizza_ingredient_factory.h ├── ny_pizza_store.cc ├── ny_pizza_store.h ├── pizza.cc ├── pizza.h ├── pizza_ingredient_factory.h ├── pizza_store.cc ├── pizza_store.h ├── plum_tomato_sauce.cc ├── plum_tomato_sauce.h ├── reggiano_cheese.cc ├── reggiano_cheese.h ├── sauce.h ├── test │ └── abstract_method_pizza_test.cc ├── thick_crust_dough.cc ├── thick_crust_dough.h ├── thin_crust_dough.cc └── thin_crust_dough.h ├── adapter ├── CMakeLists.txt ├── duck.h ├── duck_adapter.cc ├── duck_adapter.h ├── mallard_duck.cc ├── mallard_duck.h ├── test │ ├── duck_adapter_test.cc │ └── turkey_adapter_test.cc ├── turkey.h ├── turkey_adapter.cc ├── turkey_adapter.h ├── wild_turkey.cc └── wild_turkey.h ├── base └── macros.h ├── bridge ├── CMakeLists.txt ├── circle_shape.cc ├── circle_shape.h ├── drawing.h ├── drawing_clockwise.h ├── drawing_counter_clockwise.h ├── shape.h └── test │ └── bridge_circle_shape_test.cc ├── builder ├── CMakeLists.txt ├── cheese_pizza_builder.h ├── clam_pizza_builder.h ├── pizza.h ├── pizza_builder.h ├── test │ └── builder_pizza_test.cc ├── waiter.cc └── waiter.h ├── chain_of_responsibility ├── CMakeLists.txt ├── director.cc ├── director.h ├── manager.cc ├── manager.h ├── request.h ├── request.h~ ├── staff.h ├── teacher.cc ├── teacher.h └── test │ └── staff_responsibility_test.cc ├── command ├── CMakeLists.txt ├── ceiling_fan.cc ├── ceiling_fan.h ├── ceiling_fan_high_command.cc ├── ceiling_fan_high_command.h ├── ceiling_fan_low_command.cc ├── ceiling_fan_low_command.h ├── ceiling_fan_medium_command.cc ├── ceiling_fan_medium_command.h ├── ceiling_fan_off_command.cc ├── ceiling_fan_off_command.h ├── command.h ├── dimmer_light_off_command.cc ├── dimmer_light_off_command.h ├── dimmer_light_on_command.cc ├── dimmer_light_on_command.h ├── light.cc ├── light.h ├── light_off_command.cc ├── light_off_command.h ├── light_on_command.cc ├── light_on_command.h ├── no_command.h ├── remote_control.cc ├── remote_control.h └── test │ └── remote_control_test.cc ├── composite ├── CMakeLists.txt ├── composite_iterator.cc ├── composite_iterator.h ├── iterator.h ├── menu.cc ├── menu.h ├── menu_component.h ├── menu_item.cc ├── menu_item.h ├── menu_iterator.cc ├── menu_iterator.h ├── null_iterator.h ├── test │ └── menu_composite_test.cc ├── waitress.cc └── waitress.h ├── decorator ├── CMakeLists.txt ├── beverage.cc ├── beverage.h ├── condiment_decorator.h ├── darkroast.cc ├── darkroast.h ├── decaf.cc ├── decaf.h ├── espresso.cc ├── espresso.h ├── house_blend.cc ├── house_blend.h ├── milk.cc ├── milk.h ├── mocha.cc ├── mocha.h ├── soy.cc ├── soy.h ├── test │ └── starbuzz_coffee_test.cc ├── whip.cc └── whip.h ├── facade ├── CMakeLists.txt ├── amplifier.cc ├── amplifier.h ├── cd_player.cc ├── cd_player.h ├── dvd_player.cc ├── dvd_player.h ├── home_theater_facade.cc ├── home_theater_facade.h ├── popcorn_popper.cc ├── popcorn_popper.h ├── projector.cc ├── projector.h ├── screen.cc ├── screen.h ├── test │ └── home_theater_test.cc ├── theater_lights.cc ├── theater_lights.h ├── tuner.cc └── tuner.h ├── factory_method ├── CMakeLists.txt ├── factory_method │ ├── CMakeLists.txt │ ├── chicago_pizza_store.cc │ ├── chicago_pizza_store.h │ ├── chicago_style_cheese_pizza.cc │ ├── chicago_style_cheese_pizza.h │ ├── chicago_style_clam_pizza.cc │ ├── chicago_style_clam_pizza.h │ ├── chicago_style_pepperoni_pizza.cc │ ├── chicago_style_pepperoni_pizza.h │ ├── ny_pizza_store.cc │ ├── ny_pizza_store.h │ ├── ny_style_cheese_pizza.cc │ ├── ny_style_cheese_pizza.h │ ├── ny_style_clam_pizza.cc │ ├── ny_style_clam_pizza.h │ ├── ny_style_pepperoni_pizza.cc │ ├── ny_style_pepperoni_pizza.h │ ├── pizza.cc │ ├── pizza.h │ ├── pizza_store.cc │ ├── pizza_store.h │ └── test │ │ └── pizza_test.cc └── simple_factory │ ├── CMakeLists.txt │ ├── cheese_pizza.cc │ ├── cheese_pizza.h │ ├── clam_pizza.cc │ ├── clam_pizza.h │ ├── pepperoni_pizza.cc │ ├── pepperoni_pizza.h │ ├── pizza.cc │ ├── pizza.h │ ├── pizza_store.cc │ ├── pizza_store.h │ ├── simple_pizza_factory.cc │ ├── simple_pizza_factory.h │ ├── test │ └── simple_pizza_test.cc │ ├── veggie_pizza.cc │ └── veggie_pizza.h ├── flyweight ├── CMakeLists.txt ├── circle.cc ├── circle.h ├── shape.h ├── shape_factory.cc ├── shape_factory.h └── test │ └── flyweight_shape_test.cc ├── interpreter ├── CMakeLists.txt ├── add_expression.cc ├── add_expression.h ├── expression.h ├── null_expression.h ├── number_expression.cc ├── number_expression.h ├── subtract_expression.cc ├── subtract_expression.h ├── test │ └── interpreter_token_test.cc ├── token_reader.cc └── token_reader.h ├── iterator ├── CMakeLists.txt ├── diner_menu.cc ├── diner_menu.h ├── diner_menu_iterator.cc ├── diner_menu_iterator.h ├── iterator.h ├── menu.h ├── menu_item.cc ├── menu_item.h ├── pancake_house_menu.cc ├── pancake_house_menu.h ├── pancake_house_menu_iterator.cc ├── pancake_house_menu_iterator.h ├── test │ └── menu_test.cc ├── waitress.cc └── waitress.h ├── mediator ├── CMakeLists.txt ├── colleague.h ├── colleague_event.h ├── employee.h ├── general_staff.cc ├── general_staff.h ├── manager.cc ├── manager.h ├── mediator.h ├── sales_men.h ├── staff_msg.h ├── sys_admin.cc ├── sys_admin.h └── test │ └── mediator_staff_test.cc ├── memento ├── CMakeLists.txt ├── caretaker.h ├── memento.h ├── originator.h └── test │ └── memento_test.cc ├── observer ├── CMakeLists.txt ├── current_conditions_display.cc ├── current_conditions_display.h ├── display_element.h ├── forecast_display.cc ├── forecast_display.h ├── heatindex_display.cc ├── heatindex_display.h ├── observer.h ├── statistics_display.cc ├── statistics_display.h ├── subject.h ├── test │ ├── weather_station_heatindex_test.cc │ └── weather_station_test.cc ├── weather_data.cc └── weather_data.h ├── prototype ├── CMakeLists.txt ├── configuration.cc ├── configuration.h ├── prototype.h ├── prototype_manager.cc ├── prototype_manager.h ├── test │ └── prototype_test.cc ├── user_profile.cc └── user_profile.h ├── proxy ├── CMakeLists.txt ├── bear.h ├── drinker.h ├── proxy_bear.cc ├── proxy_bear.h ├── test │ └── proxy_drink_test.cc └── wine.h ├── singleton ├── CMakeLists.txt ├── mutex.h ├── singleton.cc ├── singleton.h └── test │ └── singleton_test.cc ├── state ├── CMakeLists.txt ├── gumball_machine.cc ├── gumball_machine.h ├── has_quarter_state.cc ├── has_quarter_state.h ├── no_quarter_state.cc ├── no_quarter_state.h ├── sold_out_state.cc ├── sold_out_state.h ├── sold_state.cc ├── sold_state.h ├── state.h └── test │ └── gumball_machine_test.cc ├── strategy ├── CMakeLists.txt ├── decoy_duck.cc ├── decoy_duck.h ├── duck.cc ├── duck.h ├── fly_behavior.h ├── fly_noway.cc ├── fly_noway.h ├── fly_rocketpowerd.cc ├── fly_rocketpowered.h ├── fly_with_wings.cc ├── fly_with_wings.h ├── mallard_duck.cc ├── mallard_duck.h ├── model_duck.cc ├── model_duck.h ├── mute_quack.cc ├── mute_quack.h ├── quack.cc ├── quack.h ├── quack_behavior.h ├── rubber_duck.cc ├── rubber_duck.h ├── squeak.cc ├── squeak.h └── test │ └── duck_test.cc ├── templatemethod ├── CMakeLists.txt ├── caffeine_beverage.cc ├── caffeine_beverage.h ├── caffeine_beverage_with_hook.cc ├── caffeine_beverage_with_hook.h ├── coffee.cc ├── coffee.h ├── coffee_with_hook.cc ├── coffee_with_hook.h ├── tea.cc ├── tea.h ├── tea_with_hook.cc ├── tea_with_hook.h └── test │ └── beverage_test.cc └── visitor ├── CMakeLists.txt ├── body.cc ├── body.h ├── car.cc ├── car.h ├── car_element.h ├── car_element_do_visitor.cc ├── car_element_do_visitor.h ├── car_element_print_visitor.cc ├── car_element_print_visitor.h ├── car_element_visitor.h ├── engine.cc ├── engine.h ├── test └── visitor_car_test.cc ├── wheel.cc └── wheel.h /README.md: -------------------------------------------------------------------------------- 1 | README 2 | ====== 3 | This project contains design pattern using C++ for my articles [浅谈模式设计](http://dreamrunner.org/blog/2014/05/03/%E6%B5%85%E8%B0%88%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/). 4 | 5 | Dependencies 6 | ============ 7 | 8 | * C++ compiler and Make 9 | - Mac OSX https://developer.apple.com/xcode/ 10 | - Linux http://gcc.gnu.org/ 11 | * CMake 12 | - http://www.cmake.org/cmake/resources/software.html 13 | 14 | BUILDING 15 | ======== 16 | 17 | mkdir build 18 | cd build && cmake .. 19 | make 20 | 21 | EXECUTE 22 | ========== 23 | 24 | Once the project has been built (see "BUILDING"), go to folder `bin` to execute different test. 25 | 26 | 27 | -------------------------------------------------------------------------------- /doxygenate.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | """INPUT_FILTER for Doxygen that converts C++ comments to Doxygen comments.""" 3 | import sys 4 | import re 5 | 6 | 7 | def Doxygenate(text): 8 | """Change commenting style to the one recognized by Doxygen.""" 9 | # /* -> /** 10 | text = re.sub(r'(/\*)([ \t\n]) ?', r'/**\2', text) 11 | 12 | # // -> /// 13 | text = re.sub(r'(//)([ \t\n]) ?', r'///\2', text) 14 | 15 | # // Author:-> /** \author */ 16 | text = re.sub(r'(//[ ]+Author:?[ ]*)([^\n]+)', r'/** \\author \2 */', text) 17 | print text 18 | 19 | if __name__ == '__main__': 20 | f = open(sys.argv[1], 'r') 21 | src = f.read() 22 | f.close() 23 | Doxygenate(src) 24 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(strategy) 2 | add_subdirectory(observer) 3 | add_subdirectory(decorator) 4 | add_subdirectory(factory_method) 5 | add_subdirectory(abstract_factory) 6 | add_subdirectory(singleton) 7 | add_subdirectory(command) 8 | add_subdirectory(adapter) 9 | add_subdirectory(facade) 10 | add_subdirectory(templatemethod) 11 | add_subdirectory(iterator) 12 | add_subdirectory(composite) 13 | add_subdirectory(state) 14 | add_subdirectory(builder) 15 | add_subdirectory(proxy) 16 | add_subdirectory(bridge) 17 | add_subdirectory(prototype) 18 | add_subdirectory(flyweight) 19 | add_subdirectory(chain_of_responsibility) 20 | add_subdirectory(interpreter) 21 | add_subdirectory(mediator) 22 | add_subdirectory(memento) 23 | add_subdirectory(visitor) -------------------------------------------------------------------------------- /src/abstract_factory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_abstractfactory STATIC 5 | cheese_pizza.cc 6 | ny_pizza_ingredient_factory.cc 7 | chicago_pizza_ingredient_factory.cc 8 | pizza.cc 9 | chicago_pizza_store.cc 10 | pizza_store.cc 11 | fresh_clams.cc 12 | plum_tomato_sauce.cc 13 | frozen_clams.cc 14 | clam_pizza.cc 15 | reggiano_cheese.cc 16 | marinara_sauce.cc 17 | ny_pizza_store.cc 18 | thick_crust_dough.cc 19 | mozzarella_cheese.cc 20 | thin_crust_dough.cc) 21 | 22 | install(TARGETS designpattern_abstractfactory DESTINATION ${LIBRARY_OUTPUT_PATH}) 23 | 24 | add_executable(abstract_method_pizza_test ./test/abstract_method_pizza_test.cc) 25 | target_link_libraries(abstract_method_pizza_test designpattern_abstractfactory) 26 | add_test(abstract_method_pizza_test ${EXECUTABLE_OUTPUT_PATH}/pizza_test) 27 | -------------------------------------------------------------------------------- /src/abstract_factory/cheese.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_CHEESE_H_ 9 | #define ABSTRACT_FACTORY_CHEESE_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace abstract_factory { 15 | class Cheese { 16 | public: 17 | virtual string toString() = 0; 18 | }; 19 | } // namespace abstract_factory 20 | #endif // ABSTRACT_FACTORY_CHEESE_H_ 21 | -------------------------------------------------------------------------------- /src/abstract_factory/cheese_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "abstract_factory/cheese_pizza.h" 12 | 13 | namespace abstract_factory { 14 | CheesePizza::CheesePizza(PizzaIngredientFactory *ingredient_factory) : 15 | ingredient_factory_(ingredient_factory) { 16 | } 17 | void CheesePizza::prepare() { 18 | cout << "Preparing " << name_ << endl; 19 | dough_ = ingredient_factory_->createDough(); 20 | sauce_ = ingredient_factory_->createSauce(); 21 | cheese_ = ingredient_factory_->createCheese(); 22 | } 23 | } // namespace abstract_factory 24 | -------------------------------------------------------------------------------- /src/abstract_factory/cheese_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_CHEESE_PIZZA_H_ 9 | #define ABSTRACT_FACTORY_CHEESE_PIZZA_H_ 10 | #include "abstract_factory/pizza.h" 11 | #include "abstract_factory/pizza_ingredient_factory.h" 12 | 13 | namespace abstract_factory { 14 | class CheesePizza : public Pizza { 15 | public: 16 | explicit CheesePizza(PizzaIngredientFactory *ingredient_factory); 17 | void prepare(); 18 | private: 19 | PizzaIngredientFactory *ingredient_factory_; 20 | }; 21 | } // namespace abstract_factory 22 | #endif // ABSTRACT_FACTORY_CHEESE_PIZZA_H_ 23 | -------------------------------------------------------------------------------- /src/abstract_factory/chicago_pizza_ingredient_factory.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/chicago_pizza_ingredient_factory.h" 9 | #include "abstract_factory/thick_crust_dough.h" 10 | #include "abstract_factory/plum_tomato_sauce.h" 11 | #include "abstract_factory/mozzarella_cheese.h" 12 | #include "abstract_factory/frozen_clams.h" 13 | 14 | namespace abstract_factory { 15 | Dough* ChicagoPizzaIngredientFactory::createDough() { 16 | return new ThickCrustDough(); 17 | } 18 | 19 | Sauce* ChicagoPizzaIngredientFactory::createSauce() { 20 | return new PlumTomatoSauce(); 21 | } 22 | 23 | Cheese* ChicagoPizzaIngredientFactory::createCheese() { 24 | return new MozzarellaCheese(); 25 | } 26 | 27 | Clams* ChicagoPizzaIngredientFactory::createClam() { 28 | return new FrozenClams(); 29 | } 30 | } // namespace abstract_factory 31 | -------------------------------------------------------------------------------- /src/abstract_factory/chicago_pizza_ingredient_factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_CHICAGO_PIZZA_INGREDIENT_FACTORY_H_ 9 | #define ABSTRACT_FACTORY_CHICAGO_PIZZA_INGREDIENT_FACTORY_H_ 10 | 11 | #include "abstract_factory/pizza_ingredient_factory.h" 12 | 13 | namespace abstract_factory { 14 | class ChicagoPizzaIngredientFactory : public PizzaIngredientFactory { 15 | public: 16 | virtual Dough* createDough(); 17 | virtual Sauce* createSauce(); 18 | virtual Cheese* createCheese(); 19 | virtual Clams* createClam(); 20 | }; 21 | } // namespace abstract_factory 22 | #endif // ABSTRACT_FACTORY_CHICAGO_PIZZA_INGREDIENT_FACTORY_H_ 23 | -------------------------------------------------------------------------------- /src/abstract_factory/chicago_pizza_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_CHICAGO_PIZZA_STORE_H_ 9 | #define ABSTRACT_FACTORY_CHICAGO_PIZZA_STORE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "base/macros.h" 14 | #include "abstract_factory/pizza.h" 15 | #include "abstract_factory/pizza_store.h" 16 | 17 | namespace abstract_factory { 18 | class ChicagoPizzaStore : public PizzaStore { 19 | public: 20 | virtual ~ChicagoPizzaStore(); 21 | Pizza* createPizza(string item); 22 | }; 23 | } // namespace abstract_factory 24 | #endif // ABSTRACT_FACTORY_CHICAGO_PIZZA_STORE_H_ 25 | -------------------------------------------------------------------------------- /src/abstract_factory/clam_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "abstract_factory/clam_pizza.h" 12 | 13 | namespace abstract_factory { 14 | ClamPizza::ClamPizza(PizzaIngredientFactory *ingredient_factory) : 15 | ingredient_factory_(ingredient_factory) { 16 | } 17 | void ClamPizza::prepare() { 18 | cout << "Preparing " << name_ << endl; 19 | dough_ = ingredient_factory_->createDough(); 20 | sauce_ = ingredient_factory_->createSauce(); 21 | cheese_ = ingredient_factory_->createCheese(); 22 | clam_ = ingredient_factory_->createClam(); 23 | } 24 | } // namespace abstract_factory 25 | -------------------------------------------------------------------------------- /src/abstract_factory/clam_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_CLAM_PIZZA_H_ 9 | #define ABSTRACT_FACTORY_CLAM_PIZZA_H_ 10 | #include "abstract_factory/pizza.h" 11 | #include "abstract_factory/pizza_ingredient_factory.h" 12 | 13 | namespace abstract_factory { 14 | class ClamPizza : public Pizza { 15 | public: 16 | explicit ClamPizza(PizzaIngredientFactory *ingredient_factory); 17 | void prepare(); 18 | private: 19 | PizzaIngredientFactory *ingredient_factory_; 20 | }; 21 | } // namespace abstract_factory 22 | #endif // ABSTRACT_FACTORY_CLAM_PIZZA_H_ 23 | -------------------------------------------------------------------------------- /src/abstract_factory/clams.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_CLAMS_H_ 9 | #define ABSTRACT_FACTORY_CLAMS_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace abstract_factory { 15 | class Clams { 16 | public: 17 | virtual string toString() = 0; 18 | }; 19 | } // namespace abstract_factory 20 | #endif // ABSTRACT_FACTORY_CLAMS_H_ 21 | -------------------------------------------------------------------------------- /src/abstract_factory/dough.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_DOUGH_H_ 9 | #define ABSTRACT_FACTORY_DOUGH_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace abstract_factory { 15 | class Dough { 16 | public: 17 | virtual string toString() = 0; 18 | }; 19 | } // namespace abstract_factory 20 | #endif // ABSTRACT_FACTORY_DOUGH_H_ 21 | -------------------------------------------------------------------------------- /src/abstract_factory/fresh_clams.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/fresh_clams.h" 9 | 10 | namespace abstract_factory { 11 | string FreshClams::toString() { 12 | return "Fresh Clams from Long Island Sound"; 13 | } 14 | } // namespace abstract_factory 15 | -------------------------------------------------------------------------------- /src/abstract_factory/fresh_clams.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_FRESH_CLAMS_H_ 9 | #define ABSTRACT_FACTORY_FRESH_CLAMS_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "abstract_factory/clams.h" 14 | 15 | namespace abstract_factory { 16 | class FreshClams : public Clams { 17 | public: 18 | string toString(); 19 | }; 20 | } // namespace abstract_factory 21 | #endif // ABSTRACT_FACTORY_FRESH_CLAMS_H_ 22 | -------------------------------------------------------------------------------- /src/abstract_factory/frozen_clams.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/frozen_clams.h" 9 | 10 | namespace abstract_factory { 11 | string FrozenClams::toString() { 12 | return "Frozen Clams from Chesapeake Bay"; 13 | } 14 | } // namespace abstract_factory 15 | -------------------------------------------------------------------------------- /src/abstract_factory/frozen_clams.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_FROZEN_CLAMS_H_ 9 | #define ABSTRACT_FACTORY_FROZEN_CLAMS_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "abstract_factory/clams.h" 14 | 15 | namespace abstract_factory { 16 | class FrozenClams : public Clams { 17 | public: 18 | string toString(); 19 | }; 20 | } // namespace abstract_factory 21 | #endif // ABSTRACT_FACTORY_FROZWN_CLAMS_H_ 22 | -------------------------------------------------------------------------------- /src/abstract_factory/marinara_sauce.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/marinara_sauce.h" 9 | 10 | namespace abstract_factory { 11 | string MarinaraSauce::toString() { 12 | return "Marinara Sauce"; 13 | } 14 | } // namespace abstract_factory 15 | -------------------------------------------------------------------------------- /src/abstract_factory/marinara_sauce.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_MARINARA_SAUCE_H_ 9 | #define ABSTRACT_FACTORY_MARINARA_SAUCE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "abstract_factory/sauce.h" 14 | 15 | namespace abstract_factory { 16 | class MarinaraSauce : public Sauce { 17 | public: 18 | string toString(); 19 | }; 20 | } // namespace abstract_factory 21 | #endif // ABSTRACT_FACTORY_MARINARA_SAUCE_H_ 22 | -------------------------------------------------------------------------------- /src/abstract_factory/mozzarella_cheese.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/mozzarella_cheese.h" 9 | 10 | namespace abstract_factory { 11 | string MozzarellaCheese::toString() { 12 | return "Shredded Mozzarella"; 13 | } 14 | } // namespace abstract_factory 15 | -------------------------------------------------------------------------------- /src/abstract_factory/mozzarella_cheese.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_MOZZARELLA_CHEESE_H_ 9 | #define ABSTRACT_FACTORY_MOZZARELLA_CHEESE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "abstract_factory/cheese.h" 14 | 15 | namespace abstract_factory { 16 | class MozzarellaCheese : public Cheese { 17 | public: 18 | string toString(); 19 | }; 20 | } // namespace abstract_factory 21 | #endif // ABSTRACT_FACTORY_MOZZARELLA_CHEESE_H_ 22 | -------------------------------------------------------------------------------- /src/abstract_factory/ny_pizza_ingredient_factory.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/ny_pizza_ingredient_factory.h" 9 | #include "abstract_factory/thin_crust_dough.h" 10 | #include "abstract_factory/marinara_sauce.h" 11 | #include "abstract_factory/reggiano_cheese.h" 12 | #include "abstract_factory/fresh_clams.h" 13 | 14 | namespace abstract_factory { 15 | Dough* NYPizzaIngredientFactory::createDough() { 16 | return new ThinCrustDough(); 17 | } 18 | 19 | Sauce* NYPizzaIngredientFactory::createSauce() { 20 | return new MarinaraSauce(); 21 | } 22 | 23 | Cheese* NYPizzaIngredientFactory::createCheese() { 24 | return new ReggianoCheese(); 25 | } 26 | 27 | Clams* NYPizzaIngredientFactory::createClam() { 28 | return new FreshClams(); 29 | } 30 | } // namespace abstract_factory 31 | -------------------------------------------------------------------------------- /src/abstract_factory/ny_pizza_ingredient_factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_NY_PIZZA_INGREDIENT_FACTORY_H_ 9 | #define ABSTRACT_FACTORY_NY_PIZZA_INGREDIENT_FACTORY_H_ 10 | 11 | #include "abstract_factory/pizza_ingredient_factory.h" 12 | 13 | namespace abstract_factory { 14 | class NYPizzaIngredientFactory : public PizzaIngredientFactory { 15 | public: 16 | virtual Dough* createDough(); 17 | virtual Sauce* createSauce(); 18 | virtual Cheese* createCheese(); 19 | virtual Clams* createClam(); 20 | }; 21 | } // namespace abstract_factory 22 | #endif // ABSTRACT_FACTORY_NY_PIZZA_INGREDIENT_FACTORY_H_ 23 | -------------------------------------------------------------------------------- /src/abstract_factory/ny_pizza_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_NY_PIZZA_STORE_H_ 9 | #define ABSTRACT_FACTORY_NY_PIZZA_STORE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "base/macros.h" 14 | #include "abstract_factory/pizza.h" 15 | #include "abstract_factory/pizza_store.h" 16 | 17 | namespace abstract_factory { 18 | class NYPizzaStore : public PizzaStore { 19 | public: 20 | virtual ~NYPizzaStore(); 21 | Pizza* createPizza(string item); 22 | }; 23 | } // namespace abstract_factory 24 | #endif // ABSTRACT_FACTORY_NY_PIZZA_STORE_H_ 25 | -------------------------------------------------------------------------------- /src/abstract_factory/pizza_ingredient_factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_PIZZA_INGREDIENT_FACTORY_H_ 9 | #define ABSTRACT_FACTORY_PIZZA_INGREDIENT_FACTORY_H_ 10 | 11 | #include "abstract_factory/dough.h" 12 | #include "abstract_factory/sauce.h" 13 | #include "abstract_factory/cheese.h" 14 | #include "abstract_factory/clams.h" 15 | 16 | namespace abstract_factory { 17 | class PizzaIngredientFactory { 18 | public: 19 | virtual ~PizzaIngredientFactory() {} 20 | virtual Dough* createDough() = 0; 21 | virtual Sauce* createSauce() = 0; 22 | virtual Cheese* createCheese() = 0; 23 | virtual Clams* createClam() = 0; 24 | }; 25 | } // namespace abstract_factory 26 | #endif // ABSTRACT_FACTORY_PIZZA_INGREDIENT_FACTORY_H_ 27 | -------------------------------------------------------------------------------- /src/abstract_factory/pizza_store.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/pizza_store.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | namespace abstract_factory { 13 | PizzaStore::~PizzaStore() { 14 | } 15 | 16 | Pizza* PizzaStore::orderPizza(string type) { 17 | Pizza *pizza; 18 | pizza = createPizza(type); 19 | cout << "--- Making a " + pizza->name() + " ---" << endl; 20 | pizza->prepare(); 21 | pizza->bake(); 22 | pizza->cut(); 23 | pizza->box(); 24 | return pizza; 25 | } 26 | } // namespace abstract_factory 27 | -------------------------------------------------------------------------------- /src/abstract_factory/pizza_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_PIZZA_STORE_H_ 9 | #define ABSTRACT_FACTORY_PIZZA_STORE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "base/macros.h" 14 | #include "pizza.h" 15 | 16 | namespace abstract_factory { 17 | class PizzaStore { 18 | public: 19 | virtual ~PizzaStore(); 20 | Pizza* orderPizza(string type); 21 | protected: 22 | virtual Pizza* createPizza(string item) = 0; 23 | }; 24 | } // namespace abstract_factory 25 | #endif // ABSTRACT_FACTORY_PIZZA_STORE_H_ 26 | 27 | -------------------------------------------------------------------------------- /src/abstract_factory/plum_tomato_sauce.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/plum_tomato_sauce.h" 9 | 10 | namespace abstract_factory { 11 | string PlumTomatoSauce::toString() { 12 | return "Tomato sauce with plum tomatoes"; 13 | } 14 | } // namespace abstract_factory 15 | -------------------------------------------------------------------------------- /src/abstract_factory/plum_tomato_sauce.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_PLUM_TOMATO_SAUCE_H_ 9 | #define ABSTRACT_FACTORY_PLUM_TOMATO_SAUCE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "abstract_factory/sauce.h" 14 | 15 | namespace abstract_factory { 16 | class PlumTomatoSauce : public Sauce { 17 | public: 18 | string toString(); 19 | }; 20 | } // namespace abstract_factory 21 | #endif // ABSTRACT_FACTORY_PLUM_TOMATO_SAUCE_H_ 22 | -------------------------------------------------------------------------------- /src/abstract_factory/reggiano_cheese.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/reggiano_cheese.h" 9 | 10 | namespace abstract_factory { 11 | string ReggianoCheese::toString() { 12 | return "Reggiano Cheese"; 13 | } 14 | } // namespace abstract_factory 15 | -------------------------------------------------------------------------------- /src/abstract_factory/reggiano_cheese.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_REGGIANO_CHEESE_H_ 9 | #define ABSTRACT_FACTORY_REGGIANO_CHEESE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "abstract_factory/cheese.h" 14 | 15 | namespace abstract_factory { 16 | class ReggianoCheese : public Cheese { 17 | public: 18 | string toString(); 19 | }; 20 | } // namespace abstract_factory 21 | #endif // ABSTRACT_FACTORY_REGGIANO_CHEESE_H_ 22 | -------------------------------------------------------------------------------- /src/abstract_factory/sauce.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_SAUCE_H_ 9 | #define ABSTRACT_FACTORY_SAUCE_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace abstract_factory { 15 | class Sauce { 16 | public: 17 | virtual string toString() = 0; 18 | }; 19 | } // namespace abstract_factory 20 | #endif // ABSTRACT_FACTORY_SAUCE_H_ 21 | -------------------------------------------------------------------------------- /src/abstract_factory/thick_crust_dough.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | #include "abstract_factory/thick_crust_dough.h" 8 | 9 | namespace abstract_factory { 10 | string ThickCrustDough::toString() { 11 | return "ThickCrust style extra thick crust dough"; 12 | } 13 | } // namespace abstract_factory 14 | -------------------------------------------------------------------------------- /src/abstract_factory/thick_crust_dough.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_THICK_CRUST_DOUGH_H_ 9 | #define ABSTRACT_FACTORY_THICK_CRUST_DOUGH_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "abstract_factory/dough.h" 14 | 15 | namespace abstract_factory { 16 | class ThickCrustDough : public Dough { 17 | public: 18 | string toString(); 19 | }; 20 | } // namespace abstract_factory 21 | #endif // ABSTRACT_FACTORY_THICK_CRUST_DOUGH_H_ 22 | -------------------------------------------------------------------------------- /src/abstract_factory/thin_crust_dough.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "abstract_factory/thin_crust_dough.h" 9 | 10 | namespace abstract_factory { 11 | string ThinCrustDough::toString() { 12 | return "Thin Crust Dough"; 13 | } 14 | } // namespace abstract_factory 15 | -------------------------------------------------------------------------------- /src/abstract_factory/thin_crust_dough.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ABSTRACT_FACTORY_THIN_CRUST_DOUGH_H_ 9 | #define ABSTRACT_FACTORY_THIN_CRUST_DOUGH_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "abstract_factory/dough.h" 14 | 15 | namespace abstract_factory { 16 | class ThinCrustDough : public Dough { 17 | public: 18 | string toString(); 19 | }; 20 | } // namespace abstract_factory 21 | #endif // ABSTRACT_FACTORY_THIN_CRUST_DOUGH_H_ 22 | -------------------------------------------------------------------------------- /src/adapter/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_adapter STATIC 5 | duck_adapter.cc 6 | mallard_duck.cc 7 | turkey_adapter.cc 8 | wild_turkey.cc) 9 | 10 | install(TARGETS designpattern_adapter DESTINATION ${LIBRARY_OUTPUT_PATH}) 11 | 12 | # Define an executable and adds a test for it using the most basic libraries 13 | # Args: 14 | # name - name of test. Must have a source file in test/.cc 15 | # ... - optional list of additional library dependencies 16 | function(project_test name) 17 | add_executable(${name} test/${name}.cc) 18 | foreach (lib "${ARGN}") 19 | target_link_libraries(${name} ${lib}) 20 | endforeach() 21 | add_test(${name} ${EXECUTABLE_OUTPUT_PATH}/${name}) 22 | endfunction() 23 | 24 | project_test(duck_adapter_test designpattern_adapter) 25 | project_test(turkey_adapter_test designpattern_adapter) 26 | -------------------------------------------------------------------------------- /src/adapter/duck.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ADAPTER_DUCK_H_ 9 | #define ADAPTER_DUCK_H_ 10 | 11 | namespace adapter { 12 | class Duck { 13 | public: 14 | virtual ~Duck() {} 15 | virtual void quack() {}; 16 | virtual void fly() {}; 17 | }; 18 | } // namespace adapter 19 | #endif // ADAPTER_DUCK_H_ 20 | -------------------------------------------------------------------------------- /src/adapter/duck_adapter.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "adapter/duck_adapter.h" 9 | #include 10 | 11 | namespace adapter { 12 | DuckAdapter::DuckAdapter(Duck* duck) : duck_(duck) { 13 | } 14 | 15 | DuckAdapter::~DuckAdapter() { 16 | } 17 | 18 | void DuckAdapter::gobble() { 19 | duck_->quack(); 20 | } 21 | 22 | void DuckAdapter::fly() { 23 | if (rand() % 5 == 0) { 24 | duck_->fly(); 25 | } 26 | } 27 | } // namespace adapter 28 | -------------------------------------------------------------------------------- /src/adapter/duck_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ADAPTER_DUCK_ADAPTER_H_ 9 | #define ADAPTER_DUCK_ADAPTER_H_ 10 | 11 | #include "adapter/turkey.h" 12 | #include "adapter/duck.h" 13 | 14 | namespace adapter { 15 | class DuckAdapter : public Turkey { 16 | public: 17 | explicit DuckAdapter(Duck* duck); 18 | virtual ~DuckAdapter(); 19 | void gobble(); 20 | void fly(); 21 | private: 22 | Duck* duck_; 23 | }; 24 | } // namespace adapter 25 | #endif // ADAPTER_DUCK_ADAPTER_H_ 26 | -------------------------------------------------------------------------------- /src/adapter/mallard_duck.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "adapter/mallard_duck.h" 12 | 13 | namespace adapter { 14 | MallardDuck::~MallardDuck() { 15 | } 16 | 17 | void MallardDuck::quack() { 18 | cout << "Quack" << endl; 19 | } 20 | 21 | void MallardDuck::fly() { 22 | cout << "I'm flying" << endl; 23 | } 24 | } // namespace adapter 25 | -------------------------------------------------------------------------------- /src/adapter/mallard_duck.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ADAPTER_MALLARD_DUCK_H_ 9 | #define ADAPTER_MALLARD_DUCK_H_ 10 | 11 | #include "adapter/duck.h" 12 | 13 | namespace adapter { 14 | class MallardDuck : public Duck { 15 | public: 16 | virtual ~MallardDuck(); 17 | void quack(); 18 | void fly(); 19 | }; 20 | } // namespace adapter 21 | #endif // ADAPTER_MALLARD_DUCK_H_ 22 | -------------------------------------------------------------------------------- /src/adapter/test/turkey_adapter_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "adapter/mallard_duck.h" 12 | #include "adapter/wild_turkey.h" 13 | #include "adapter/duck_adapter.h" 14 | #include "adapter/turkey.h" 15 | using namespace adapter; // NOLINT 16 | 17 | int main(int argc, char* argv[]) { 18 | MallardDuck* duck = new MallardDuck(); 19 | Turkey* duckAdapter = new DuckAdapter(duck); 20 | for (int i = 0; i < 10; ++i) { 21 | cout << "The DuckAdapter says..." << endl; 22 | duckAdapter->gobble(); 23 | duckAdapter->fly(); 24 | } 25 | delete duck; 26 | delete duckAdapter; 27 | } 28 | -------------------------------------------------------------------------------- /src/adapter/turkey.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ADAPTER_TURKEY_H_ 9 | #define ADAPTER_TURKEY_H_ 10 | 11 | namespace adapter { 12 | class Turkey { 13 | public: 14 | virtual ~Turkey() {} 15 | virtual void gobble() {}; 16 | virtual void fly() {}; 17 | }; 18 | } // namespace adapter 19 | #endif // ADAPTER_TURKEY_H_ 20 | -------------------------------------------------------------------------------- /src/adapter/turkey_adapter.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "adapter/turkey_adapter.h" 9 | 10 | namespace adapter { 11 | TurkeyAdapter::TurkeyAdapter(Turkey* turkey) : turkey_(turkey) { 12 | } 13 | 14 | TurkeyAdapter::~TurkeyAdapter() { 15 | } 16 | 17 | void TurkeyAdapter::quack() { 18 | turkey_->gobble(); 19 | } 20 | 21 | void TurkeyAdapter::fly() { 22 | for (int i = 0; i < 5; ++i) { 23 | turkey_->fly(); 24 | } 25 | } 26 | } // namespace adapter 27 | -------------------------------------------------------------------------------- /src/adapter/turkey_adapter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ADAPTER_TURKEY_ADAPTER_H_ 9 | #define ADAPTER_TURKEY_ADAPTER_H_ 10 | 11 | #include "adapter/duck.h" 12 | #include "adapter/turkey.h" 13 | 14 | namespace adapter { 15 | class TurkeyAdapter : public Duck { 16 | public: 17 | explicit TurkeyAdapter(Turkey* turkey); 18 | virtual ~TurkeyAdapter(); 19 | void quack(); 20 | void fly(); 21 | private: 22 | Turkey* turkey_; 23 | }; 24 | } // namespace adapter 25 | #endif // ADAPTER_TURKEY_ADAPTER_H_ 26 | -------------------------------------------------------------------------------- /src/adapter/wild_turkey.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "adapter/wild_turkey.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace adapter { 14 | WildTurkey::~WildTurkey() { 15 | } 16 | 17 | void WildTurkey::gobble() { 18 | cout << "Gobble gobble" << endl; 19 | } 20 | 21 | void WildTurkey::fly() { 22 | cout << "I'm flying a short distance" << endl; 23 | } 24 | } // namespace adapter 25 | -------------------------------------------------------------------------------- /src/adapter/wild_turkey.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ADAPTER_WILD_TURKEY_H_ 9 | #define ADAPTER_WILD_TURKEY_H_ 10 | 11 | #include "adapter/turkey.h" 12 | 13 | namespace adapter { 14 | class WildTurkey : public Turkey { 15 | public: 16 | virtual ~WildTurkey(); 17 | void gobble(); 18 | void fly(); 19 | }; 20 | } // namespace addapter 21 | #endif // ADAPTER_WILD_TURKEY_H_ 22 | -------------------------------------------------------------------------------- /src/bridge/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_bridge STATIC 5 | circle_shape.cc) 6 | 7 | install(TARGETS designpattern_bridge DESTINATION ${LIBRARY_OUTPUT_PATH}) 8 | 9 | add_executable(bridge_circle_shape_test ./test/bridge_circle_shape_test.cc) 10 | target_link_libraries(bridge_circle_shape_test designpattern_bridge) 11 | add_test(bridge_circle_shape_test ${EXECUTABLE_OUTPUT_PATH}/bridge_circle_shape_test) 12 | -------------------------------------------------------------------------------- /src/bridge/circle_shape.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "bridge/circle_shape.h" 9 | 10 | namespace bridge { 11 | CircleShape::CircleShape(double x, double y, double radius, Drawing *drawing) : 12 | x_(x), y_(y), radius_(radius), drawing_(drawing) { 13 | } 14 | 15 | CircleShape::~CircleShape() { 16 | delete drawing_; 17 | } 18 | 19 | void CircleShape::draw() { 20 | drawing_->drawCircle(x_, y_, radius_); 21 | } 22 | 23 | void CircleShape::resizeByPercentage(double pct) { 24 | radius_ *= pct; 25 | } 26 | } // namespace bridge 27 | -------------------------------------------------------------------------------- /src/bridge/circle_shape.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BRIDGE_CIRCLE_SHAPE_H_ 9 | #define BRIDGE_CIRCLE_SHAPE_H_ 10 | 11 | #include "bridge/shape.h" 12 | #include "bridge/drawing.h" 13 | 14 | namespace bridge { 15 | class CircleShape : public Shape { 16 | public: 17 | virtual ~CircleShape(); 18 | CircleShape(double x, double y, double radius, Drawing *drawing); 19 | virtual void draw(); 20 | virtual void resizeByPercentage(double pct); 21 | private: 22 | double x_; 23 | double y_; 24 | double radius_; 25 | Drawing *drawing_; 26 | }; 27 | } // namespace bridge 28 | #endif // BRIDGE_CIRCLE_SHAPE_H_ 29 | -------------------------------------------------------------------------------- /src/bridge/drawing.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BRIDGE_DRAWING_H_ 9 | #define BRIDGE_DRAWING_H_ 10 | 11 | namespace bridge { 12 | class Drawing { 13 | public: 14 | virtual ~Drawing() {} 15 | virtual void drawCircle(double x, double y, double radius) = 0; 16 | }; 17 | } // namespace bridge 18 | #endif // BRIDGE_DRAWING_H_ 19 | -------------------------------------------------------------------------------- /src/bridge/drawing_clockwise.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BRIDGE_DRAWING_CLOCKWISE_H_ 9 | #define BRIDGE_DRAWING_CLOCKWISE_H_ 10 | 11 | #include 12 | #include "bridge/drawing.h" 13 | 14 | namespace bridge { 15 | class DrawingClockwise : public Drawing { 16 | public: 17 | virtual ~DrawingClockwise() {} 18 | virtual void drawCircle(double x, double y, double radius) { 19 | printf("draw the circle at %f,%f, radius %f by clockwise\n", x, y, radius); 20 | } 21 | }; 22 | } // namespace bridge 23 | #endif // BRIDGE_DRAWING_CLOCKWISE_H_ 24 | -------------------------------------------------------------------------------- /src/bridge/drawing_counter_clockwise.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BRIDGE_DRAWING_COUNTER_CLOCKwISE_H_ 9 | #define BRIDGE_DRAWING_COUNTER_CLOCKwISE_H_ 10 | 11 | #include 12 | #include "bridge/drawing.h" 13 | 14 | namespace bridge { 15 | class DrawingCounterClockwise : public Drawing { 16 | public: 17 | virtual ~DrawingCounterClockwise() {} 18 | virtual void drawCircle(double x, double y, double radius) { 19 | printf("draw the circle at %f,%f, radius %f by counter clockwise\n", 20 | x, y, radius); 21 | } 22 | }; 23 | } // namespace bridge 24 | #endif // BRIDGE_DRAWING_COUNTER_CLOCKwISE_H_ 25 | -------------------------------------------------------------------------------- /src/bridge/shape.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BRIDGE_SHAPE_H_ 9 | #define BRIDGE_SHAPE_H_ 10 | 11 | #include "bridge/drawing.h" 12 | 13 | namespace bridge { 14 | class Shape { 15 | public: 16 | virtual ~Shape() {} 17 | virtual void draw() = 0; 18 | virtual void resizeByPercentage(double pct) = 0; 19 | }; 20 | } // namespace bridge 21 | #endif // BRIDGE_SHAPE_H_ 22 | -------------------------------------------------------------------------------- /src/bridge/test/bridge_circle_shape_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "bridge/drawing.h" 9 | #include "bridge/shape.h" 10 | #include "bridge/circle_shape.h" 11 | #include "bridge/drawing_clockwise.h" 12 | #include "bridge/drawing_counter_clockwise.h" 13 | using namespace bridge; 14 | 15 | int main(int argc, char *argv[]) { 16 | Shape *shape_clockwise = new CircleShape(1, 2, 3, new DrawingClockwise()); 17 | Shape *shape_counter_clockwise = new CircleShape( 18 | 5, 7, 11, new DrawingCounterClockwise()); 19 | shape_clockwise->draw(); 20 | shape_counter_clockwise->resizeByPercentage(2.5); 21 | shape_counter_clockwise->draw(); 22 | } 23 | -------------------------------------------------------------------------------- /src/builder/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_builder STATIC 5 | waiter.cc) 6 | 7 | install(TARGETS designpattern_builder DESTINATION ${LIBRARY_OUTPUT_PATH}) 8 | 9 | add_executable(builder_pizza_test ./test/builder_pizza_test.cc) 10 | target_link_libraries(builder_pizza_test designpattern_builder) 11 | add_test(builder_pizza_test ${EXECUTABLE_OUTPUT_PATH}/builder_pizza_test) 12 | -------------------------------------------------------------------------------- /src/builder/cheese_pizza_builder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BUILDER_CHEESE_PIZZA_BUILDER_H_ 9 | #define BUILDER_CHEESE_PIZZA_BUILDER_H_ 10 | 11 | #include "builder/pizza_builder.h" 12 | 13 | namespace builder { 14 | class CheesePizzaBuilder : public PizzaBuilder { 15 | public: 16 | virtual ~CheesePizzaBuilder() {} 17 | virtual void buildDough() { pizza_->set_dough("thickCrust"); } 18 | virtual void buildSauce() { pizza_->set_sauce("plumTomato"); } 19 | virtual void buildTopping() { pizza_->set_topping("pineapple"); } 20 | }; 21 | } // namespace builder 22 | #endif // BUILDER_CHEESE_PIZZA_BUILDER_H_ 23 | -------------------------------------------------------------------------------- /src/builder/clam_pizza_builder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BUILDER_CLAM_PIZZA_BUILDER_H_ 9 | #define BUILDER_CLAM_PIZZA_BUILDER_H_ 10 | 11 | #include "builder/pizza_builder.h" 12 | 13 | namespace builder { 14 | class ClamPizzaBuilder : public PizzaBuilder { 15 | public: 16 | virtual ~ClamPizzaBuilder() {} 17 | virtual void buildDough() { pizza_->set_dough("thinCrust"); } 18 | virtual void buildSauce() { pizza_->set_sauce("marinaSauce"); } 19 | virtual void buildTopping() { pizza_->set_topping("pepperoni"); } 20 | }; 21 | } // namespace builder 22 | #endif // BUILDER_CLAM_PIZZA_BUILDER_H_ 23 | -------------------------------------------------------------------------------- /src/builder/pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BUILDER_PIZZA_H_ 9 | #define BUILDER_PIZZA_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace builder { 15 | class Pizza { 16 | public: 17 | Pizza() {} 18 | virtual ~Pizza() {} 19 | void set_dough(string dough) { dough_ = dough; } 20 | void set_sauce(string sauce) { sauce_ = sauce; } 21 | void set_topping(string topping) { topping_ = topping; } 22 | string toString() { 23 | string display; 24 | display.append("--- pizza ---\n"); 25 | display.append(dough_ + "\n"); 26 | display.append(sauce_ + "\n"); 27 | display.append(topping_ + "\n"); 28 | return display; 29 | } 30 | private: 31 | string dough_; 32 | string sauce_; 33 | string topping_; 34 | }; 35 | } // namespace builder 36 | #endif // BUILDER_PIZZA_H_ 37 | -------------------------------------------------------------------------------- /src/builder/pizza_builder.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BUILDER_PIZZA_BUILDER_H_ 9 | #define BUILDER_PIZZA_BUILDER_H_ 10 | 11 | #include "builder/pizza.h" 12 | 13 | namespace builder { 14 | class PizzaBuilder { 15 | public: 16 | virtual ~PizzaBuilder() {} 17 | Pizza* get_pizza() { return pizza_; } 18 | void createPizza() { pizza_ = new Pizza(); } 19 | virtual void buildDough() = 0; 20 | virtual void buildSauce() = 0; 21 | virtual void buildTopping() = 0; 22 | protected: 23 | Pizza *pizza_; 24 | }; 25 | } // namespace builder 26 | #endif // BUILDER_PIZZA_BUILDER_H_ 27 | -------------------------------------------------------------------------------- /src/builder/test/builder_pizza_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "builder/waiter.h" 12 | #include "builder/pizza_builder.h" 13 | #include "builder/cheese_pizza_builder.h" 14 | #include "builder/clam_pizza_builder.h" 15 | using namespace builder; // NOLINT 16 | 17 | int main(int argc, char *argv[]) { 18 | Waiter *waiter = new Waiter(); 19 | PizzaBuilder *cheese_pizza_builder = new CheesePizzaBuilder(); 20 | PizzaBuilder *clam_pizza_builder = new ClamPizzaBuilder(); 21 | 22 | waiter->set_pizza_builder(cheese_pizza_builder); 23 | waiter->constructPizza(); 24 | 25 | Pizza *pizza = waiter->get_pizza(); 26 | cout << pizza->toString() << endl; 27 | } 28 | -------------------------------------------------------------------------------- /src/builder/waiter.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "builder/waiter.h" 9 | 10 | namespace builder { 11 | Waiter::~Waiter() { 12 | } 13 | 14 | void Waiter::constructPizza() { 15 | pizza_builder_->createPizza(); 16 | pizza_builder_->buildDough(); 17 | pizza_builder_->buildSauce(); 18 | pizza_builder_->buildTopping(); 19 | } 20 | } // namespace builder 21 | -------------------------------------------------------------------------------- /src/builder/waiter.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef BUILDER_WAITER_H_ 9 | #define BUILDER_WAITER_H_ 10 | 11 | #include "builder/pizza_builder.h" 12 | 13 | namespace builder { 14 | class Waiter { 15 | public: 16 | virtual ~Waiter(); 17 | void set_pizza_builder(PizzaBuilder *pizza_builder) { 18 | pizza_builder_ = pizza_builder; 19 | } 20 | Pizza* get_pizza() { return pizza_builder_->get_pizza(); } 21 | void constructPizza(); 22 | private: 23 | PizzaBuilder *pizza_builder_; 24 | }; 25 | } // namespace builder 26 | #endif // BUILDER_WAITER_H_ 27 | -------------------------------------------------------------------------------- /src/chain_of_responsibility/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_chainresponsibility STATIC 5 | director.cc 6 | manager.cc 7 | teacher.cc) 8 | 9 | install(TARGETS designpattern_chainresponsibility DESTINATION ${LIBRARY_OUTPUT_PATH}) 10 | 11 | add_executable(staff_responsibility_test ./test/staff_responsibility_test.cc) 12 | target_link_libraries(staff_responsibility_test designpattern_chainresponsibility) 13 | add_test(staff_responsibility_test ${EXECUTABLE_OUTPUT_PATH}/staff_responsibility_test) 14 | -------------------------------------------------------------------------------- /src/chain_of_responsibility/director.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef CHAINRESPONSIBILITY_DIRECTOR_H_ 9 | #define CHAINRESPONSIBILITY_DIRECTOR_H_ 10 | 11 | #include "chain_of_responsibility/staff.h" 12 | #include "chain_of_responsibility/request.h" 13 | 14 | namespace chain_of_responsibility { 15 | class Director : public Staff { 16 | public: 17 | virtual ~Director(); 18 | virtual void ProcessRequest(Request *a_request); 19 | }; 20 | } // namespace chain_fo_responsibility 21 | #endif // CHAINRESPONSIBILITY_DIRECTOR_H_ 22 | -------------------------------------------------------------------------------- /src/chain_of_responsibility/manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef CHAINRESPONSIBILITY_MANAGER_H_ 9 | #define CHAINRESPONSIBILITY_MANAGER_H_ 10 | 11 | #include "chain_of_responsibility/staff.h" 12 | #include "chain_of_responsibility/request.h" 13 | 14 | namespace chain_of_responsibility { 15 | class Manager : public Staff { 16 | public: 17 | virtual ~Manager(); 18 | virtual void ProcessRequest(Request *a_request); 19 | }; 20 | } // namespace chain_of_responsibility 21 | #endif // CHAINRESPONSIBILITY_MANAGER_H_ 22 | -------------------------------------------------------------------------------- /src/chain_of_responsibility/request.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef CHAINRESPONSIBILITY_REQUEST_H_ 9 | #define CHAINRESPONSIBILITY_REQUEST_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace chain_of_responsibility { 15 | enum ResponsiableLevel { 16 | kLow, 17 | kMedium, 18 | kHigh 19 | }; 20 | 21 | class Request { 22 | public: 23 | virtual ~Request() {} 24 | void set_level(ResponsiableLevel level) {level_ = level;} 25 | ResponsiableLevel level() {return level_;} 26 | void set_description(string description) {description_ = description;} 27 | string description() {return description_;} 28 | private: 29 | string description_; 30 | ResponsiableLevel level_; 31 | }; 32 | } // namespace chain_of_responsibility 33 | #endif // CHAINRESPONSIBILITY_REQUEST_H_ 34 | -------------------------------------------------------------------------------- /src/chain_of_responsibility/request.h~: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef CHAINRESPONSIBILITY_REQUEST_H_ 9 | #define CHAINRESPONSIBILITY_REQUEST_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace chain_of_responsibility { 15 | enum ResponsiableLevel { 16 | kLow, 17 | kMedim, 18 | kHigh 19 | }; 20 | 21 | class Request { 22 | public: 23 | virtual ~Request() {} 24 | void set_level(ResponsiableLevel level) {level_ = level;} 25 | ResponsiableLevel level() {return level_;} 26 | void set_description(string description) {description_ = description;} 27 | string description() {return description_;} 28 | private: 29 | string description_; 30 | ResponsiableLevel level_; 31 | }; 32 | } // namespace chain_of_responsibility 33 | #endif // CHAINRESPONSIBILITY_REQUEST_H_ 34 | -------------------------------------------------------------------------------- /src/chain_of_responsibility/staff.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef CHAINRESPONSIBILITY_STAFF_H_ 9 | #define CHAINRESPONSIBILITY_STAFF_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "chain_of_responsibility/request.h" 14 | 15 | namespace chain_of_responsibility { 16 | class Staff { 17 | public: 18 | virtual ~Staff() {} 19 | void set_name(string name) {name_ = name;} 20 | string name() {return name_;} 21 | void set_boss(Staff *boss) {boss_ = boss;} 22 | Staff* boss() {return boss_;} 23 | virtual void ProcessRequest(Request *a_request) = 0; 24 | protected: 25 | string name_; 26 | Staff *boss_; 27 | }; 28 | } // namespace chain_of_responsibility 29 | #endif // CHAINRESPONSIBILITY_STAFF_H_ 30 | -------------------------------------------------------------------------------- /src/chain_of_responsibility/teacher.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef CHAINRESPONSIBILITY_TEACHER_H_ 9 | #define CHAINRESPONSIBILITY_TEACHER_H_ 10 | 11 | #include "chain_of_responsibility/staff.h" 12 | #include "chain_of_responsibility/request.h" 13 | 14 | namespace chain_of_responsibility { 15 | class Teacher : public Staff { 16 | public: 17 | virtual ~Teacher(); 18 | virtual void ProcessRequest(Request *a_request); 19 | }; 20 | } // namespace chain_of_responsibility 21 | #endif // CHAINRESPONSIBILITY_TEACHER_H_ 22 | -------------------------------------------------------------------------------- /src/command/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_command STATIC 5 | ceiling_fan.cc 6 | dimmer_light_on_command.cc 7 | ceiling_fan_high_command.cc 8 | light.cc 9 | ceiling_fan_low_command.cc 10 | light_off_command.cc 11 | ceiling_fan_medium_command.cc 12 | light_on_command.cc 13 | ceiling_fan_off_command.cc 14 | remote_control.cc 15 | dimmer_light_off_command.cc) 16 | 17 | install(TARGETS designpattern_command DESTINATION ${LIBRARY_OUTPUT_PATH}) 18 | 19 | add_executable(remote_control_test ./test/remote_control_test.cc) 20 | target_link_libraries(remote_control_test designpattern_command) 21 | add_test(remote_control_test ${EXECUTABLE_OUTPUT_PATH}/remote_control_test) 22 | -------------------------------------------------------------------------------- /src/command/ceiling_fan.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_CEILING_FAN_H_ 9 | #define COMMAND_CEILING_FAN_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace command { 15 | class CeilingFan { 16 | public: 17 | explicit CeilingFan(string location); 18 | virtual ~CeilingFan(); 19 | 20 | enum SpeedType { 21 | HIGH, 22 | MEDIUM, 23 | LOW, 24 | OFF, 25 | }; 26 | 27 | void high(); 28 | void medium(); 29 | void low(); 30 | void off(); 31 | int speed(); 32 | private: 33 | string location_; 34 | int speed_; 35 | }; 36 | } // namespace command 37 | #endif // COMMAND_CEILING_FAN_H_ 38 | -------------------------------------------------------------------------------- /src/command/ceiling_fan_high_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_CEILING_FAN_HIGH_COMMAND_H_ 9 | #define COMMAND_CEILING_FAN_HIGH_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | #include "command/ceiling_fan.h" 13 | 14 | namespace command { 15 | class CeilingFanHighCommand : public Command { 16 | public: 17 | explicit CeilingFanHighCommand(CeilingFan *ceiling_fan); 18 | virtual ~CeilingFanHighCommand(); 19 | void execute(); 20 | void undo(); 21 | private: 22 | CeilingFan* ceiling_fan_; 23 | int prev_speed_; 24 | }; 25 | } // namespace command 26 | #endif // COMMAND_CEILING_FAN_HIGH_COMMAND_H_ 27 | -------------------------------------------------------------------------------- /src/command/ceiling_fan_low_command.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "command/ceiling_fan_low_command.h" 9 | 10 | namespace command { 11 | CeilingFanLowCommand::CeilingFanLowCommand(CeilingFan* ceiling_fan) : 12 | ceiling_fan_(ceiling_fan) { 13 | } 14 | 15 | CeilingFanLowCommand::~CeilingFanLowCommand() { 16 | } 17 | 18 | void CeilingFanLowCommand::execute() { 19 | prev_speed_ = ceiling_fan_->speed(); 20 | ceiling_fan_->low(); 21 | } 22 | 23 | void CeilingFanLowCommand::undo() { 24 | if (prev_speed_ == CeilingFan::HIGH) { 25 | ceiling_fan_->high(); 26 | } else if (prev_speed_ == CeilingFan::MEDIUM) { 27 | ceiling_fan_->medium(); 28 | } else if (prev_speed_ == CeilingFan::LOW) { 29 | ceiling_fan_->low(); 30 | } else if (prev_speed_ == CeilingFan::OFF) { 31 | ceiling_fan_->off(); 32 | } 33 | } 34 | } // namespace command 35 | -------------------------------------------------------------------------------- /src/command/ceiling_fan_low_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_CEILING_FAN_LOW_COMMAND_H_ 9 | #define COMMAND_CEILING_FAN_LOW_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | #include "command/ceiling_fan.h" 13 | 14 | namespace command { 15 | class CeilingFanLowCommand : public Command { 16 | public: 17 | explicit CeilingFanLowCommand(CeilingFan* ceiling_fan); 18 | virtual ~CeilingFanLowCommand(); 19 | void execute(); 20 | void undo(); 21 | private: 22 | CeilingFan* ceiling_fan_; 23 | int prev_speed_; 24 | }; 25 | } // namespace command 26 | #endif // COMMAND_CEILING_FAN_LOW_COMMAND_H_ 27 | -------------------------------------------------------------------------------- /src/command/ceiling_fan_medium_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_CEILING_FAN_MEDIUM_COMMAND_H_ 9 | #define COMMAND_CEILING_FAN_MEDIUM_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | #include "command/ceiling_fan.h" 13 | 14 | namespace command { 15 | class CeilingFanMediumCommand : public Command { 16 | public: 17 | explicit CeilingFanMediumCommand(CeilingFan *ceiling_fan); 18 | virtual ~CeilingFanMediumCommand(); 19 | void execute(); 20 | void undo(); 21 | private: 22 | CeilingFan* ceiling_fan_; 23 | int prev_speed_; 24 | }; 25 | } // namespace command 26 | #endif // COMMAND_CEILING_FAN_MEDIUM_COMMAND_H_ 27 | -------------------------------------------------------------------------------- /src/command/ceiling_fan_off_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_CEILING_FAN_OFF_COMMAND_H_ 9 | #define COMMAND_CEILING_FAN_OFF_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | #include "command/ceiling_fan.h" 13 | 14 | namespace command { 15 | class CeilingFanOffCommand : public Command { 16 | public: 17 | explicit CeilingFanOffCommand(CeilingFan *ceiling_fan); 18 | virtual ~CeilingFanOffCommand(); 19 | void execute(); 20 | void undo(); 21 | private: 22 | CeilingFan* ceiling_fan_; 23 | int prev_speed_; 24 | }; 25 | } // namespace command 26 | #endif // COMMAND_CEILING_FAN_OFF_COMMAND_H_ 27 | -------------------------------------------------------------------------------- /src/command/command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_COMMAND_H_ 9 | #define COMMAND_COMMAND_H_ 10 | namespace command { 11 | class Command { 12 | public: 13 | virtual void execute() = 0; 14 | virtual void undo() = 0; 15 | }; 16 | } // namespace command 17 | #endif // COMMAND_COMMAND_H_ 18 | -------------------------------------------------------------------------------- /src/command/dimmer_light_off_command.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "command/dimmer_light_off_command.h" 9 | 10 | namespace command { 11 | DimmerLightOffCommand::DimmerLightOffCommand(Light* light) : 12 | light_(light), level_(100) { 13 | } 14 | 15 | DimmerLightOffCommand::~DimmerLightOffCommand() { 16 | } 17 | 18 | void DimmerLightOffCommand::execute() { 19 | level_ = light_->level(); 20 | light_->off(); 21 | } 22 | 23 | void DimmerLightOffCommand::undo() { 24 | light_->dim(level_); 25 | } 26 | } // namespace command 27 | -------------------------------------------------------------------------------- /src/command/dimmer_light_off_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_DIMMER_LIGHT_OFF_COMMAND_H_ 9 | #define COMMAND_DIMMER_LIGHT_OFF_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | #include "command/light.h" 13 | 14 | namespace command { 15 | class DimmerLightOffCommand : public Command { 16 | public: 17 | explicit DimmerLightOffCommand(Light* light); 18 | virtual ~DimmerLightOffCommand(); 19 | void execute(); 20 | void undo(); 21 | private: 22 | Light* light_; 23 | int level_; 24 | }; 25 | } // namespace command 26 | #endif // COMMAND_DIMMER_LIGHT_OFF_COMMAND_H_ 27 | -------------------------------------------------------------------------------- /src/command/dimmer_light_on_command.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "command/dimmer_light_on_command.h" 9 | 10 | namespace command { 11 | DimmerLightOnCommand::DimmerLightOnCommand(Light* light) : light_(light) { 12 | } 13 | 14 | void DimmerLightOnCommand::execute() { 15 | level_ = light_->level(); 16 | light_->dim(75); 17 | } 18 | 19 | void DimmerLightOnCommand::undo() { 20 | light_->dim(level_); 21 | } 22 | } // namespace command 23 | -------------------------------------------------------------------------------- /src/command/dimmer_light_on_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_DIMMER_LIGHT_ON_COMMAND_H_ 9 | #define COMMAND_DIMMER_LIGHT_ON_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | #include "command/light.h" 13 | 14 | namespace command { 15 | class DimmerLightOnCommand : public Command { 16 | public: 17 | explicit DimmerLightOnCommand(Light* light); 18 | void execute(); 19 | void undo(); 20 | private: 21 | Light* light_; 22 | int level_; 23 | }; 24 | } // namespace command 25 | #endif // COMMAND_DIMMER_LIGHT_ON_COMMAND_H_ 26 | -------------------------------------------------------------------------------- /src/command/light.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "command/light.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace command { 14 | Light::Light(string location) : location_(location) { 15 | } 16 | 17 | Light::~Light() { 18 | } 19 | 20 | void Light::on() { 21 | level_ = 100; 22 | cout << "Light is on" << endl; 23 | } 24 | 25 | void Light::off() { 26 | level_ = 0; 27 | cout << "Light is off" << endl; 28 | } 29 | 30 | void Light::dim(int level) { 31 | level_ = level; 32 | if (level_ == 0) { 33 | off(); 34 | } else { 35 | cout << "Light is dimmed to " << level_ << "%" << endl; 36 | } 37 | } 38 | 39 | int Light::level() { 40 | return level_; 41 | } 42 | } // namespace command 43 | -------------------------------------------------------------------------------- /src/command/light.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_LIGHT_H_ 9 | #define COMMAND_LIGHT_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace command { 15 | class Light { 16 | public: 17 | explicit Light(string location); 18 | virtual ~Light(); 19 | void on(); 20 | void off(); 21 | void dim(int level); 22 | int level(); 23 | private: 24 | string location_; 25 | int level_; 26 | }; 27 | } // namespace command 28 | #endif // COMMAND_LIGHT_H_ 29 | -------------------------------------------------------------------------------- /src/command/light_off_command.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "command/light_off_command.h" 9 | 10 | namespace command { 11 | LightOffCommand::LightOffCommand(Light* light) : light_(light) { 12 | } 13 | 14 | LightOffCommand::~LightOffCommand() { 15 | } 16 | 17 | void LightOffCommand::execute() { 18 | level_ = light_->level(); 19 | light_->off(); 20 | } 21 | 22 | void LightOffCommand::undo() { 23 | light_->dim(level_); 24 | } 25 | } // namespace command 26 | -------------------------------------------------------------------------------- /src/command/light_off_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_LIGHT_OFF_COMMAND_H_ 9 | #define COMMAND_LIGHT_OFF_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | #include "command/light.h" 13 | 14 | namespace command { 15 | class LightOffCommand : public Command { 16 | public: 17 | explicit LightOffCommand(Light* light); 18 | virtual ~LightOffCommand(); 19 | void execute(); 20 | void undo(); 21 | private: 22 | Light* light_; 23 | int level_; 24 | }; 25 | } // namespace command 26 | #endif // COMMAND_LIGHT_OFF_COMMAND_H_ 27 | -------------------------------------------------------------------------------- /src/command/light_on_command.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "command/light_on_command.h" 9 | 10 | namespace command { 11 | LightOnCommand::LightOnCommand(Light* light) : light_(light) { 12 | } 13 | 14 | void LightOnCommand::execute() { 15 | level_ = light_->level(); 16 | light_->on(); 17 | } 18 | 19 | void LightOnCommand::undo() { 20 | light_->dim(level_); 21 | } 22 | } // namespace command 23 | -------------------------------------------------------------------------------- /src/command/light_on_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_LIGHT_ON_COMMAND_H_ 9 | #define COMMAND_LIGHT_ON_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | #include "command/light.h" 13 | 14 | namespace command { 15 | class LightOnCommand : public Command { 16 | public: 17 | explicit LightOnCommand(Light* light); 18 | void execute(); 19 | void undo(); 20 | private: 21 | Light* light_; 22 | int level_; 23 | }; 24 | } // namespace command 25 | #endif // COMMAND_LIGHT_ON_COMMAND_H_ 26 | -------------------------------------------------------------------------------- /src/command/no_command.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_NO_COMMAND_H_ 9 | #define COMMAND_NO_COMMAND_H_ 10 | 11 | #include "command/command.h" 12 | 13 | namespace command { 14 | class NoCommand : public Command { 15 | public: 16 | void execute() {} 17 | void undo() {} 18 | }; 19 | } // namespace command 20 | #endif // COMMAND_NO_COMMAND_H_ 21 | -------------------------------------------------------------------------------- /src/command/remote_control.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMMAND_REMOTE_CONTROL_H_ 9 | #define COMMAND_REMOTE_CONTROL_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "command/command.h" 14 | #include "command/no_command.h" 15 | 16 | namespace command { 17 | class RemoteControl { 18 | public: 19 | RemoteControl(); 20 | virtual ~RemoteControl(); 21 | void setCommand(int slot, Command *on_command, Command *off_command); 22 | void onButtonWasPushed(int slot); 23 | void offButtonWasPushed(int slot); 24 | void undoButtonWasPushed(); 25 | string toString(); 26 | private: 27 | Command* on_commands_[7]; 28 | Command* off_commands_[7]; 29 | Command* undo_command_; 30 | }; 31 | } // namespace command 32 | #endif // COMMAND_REMOTE_CONTROL_H_ 33 | -------------------------------------------------------------------------------- /src/composite/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_composite STATIC 5 | composite_iterator.cc 6 | menu.cc 7 | menu_item.cc 8 | menu_iterator.cc 9 | waitress.cc) 10 | 11 | install(TARGETS designpattern_composite DESTINATION ${LIBRARY_OUTPUT_PATH}) 12 | 13 | add_executable(menu_composite_test ./test/menu_composite_test.cc) 14 | target_link_libraries(menu_composite_test designpattern_composite) 15 | add_test(menu_composite_test ${EXECUTABLE_OUTPUT_PATH}/menu_composite_test) 16 | -------------------------------------------------------------------------------- /src/composite/composite_iterator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMPOSITE_COMPOSITE_ITERATOR_H_ 9 | #define COMPOSITE_COMPOSITE_ITERATOR_H_ 10 | 11 | #include 12 | using std::stack; 13 | #include "composite/iterator.h" 14 | 15 | namespace composite { 16 | class CompositeIterator : public Iterator { 17 | public: 18 | explicit CompositeIterator(Iterator *iterator); 19 | ~CompositeIterator(); 20 | virtual bool hasNext(); 21 | virtual void* next(); 22 | private: 23 | stack *stack_; 24 | }; 25 | } // namespace composite 26 | #endif // COMPOSITE_COMPOSITE_ITERATOR_H_ 27 | -------------------------------------------------------------------------------- /src/composite/iterator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMPOSITE_ITERATOR_H_ 9 | #define COMPOSITE_ITERATOR_H_ 10 | 11 | namespace composite { 12 | class Iterator { 13 | public: 14 | virtual ~Iterator() {} 15 | virtual bool hasNext() = 0; 16 | virtual void* next() = 0; 17 | }; 18 | } // namespace composite 19 | #endif // COMPOSITE_ITERATOR_H_ 20 | -------------------------------------------------------------------------------- /src/composite/menu_item.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "composite/menu_item.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | #include "composite/null_iterator.h" 13 | 14 | namespace composite { 15 | MenuItem::MenuItem (const string &name, const string &description, 16 | bool vegetarian, double price) 17 | : name_(name), description_(description), 18 | vegetarian_(vegetarian), price_(price) { 19 | } 20 | 21 | MenuItem::~MenuItem() { 22 | } 23 | 24 | void MenuItem::print() const { 25 | cout << " " << name(); 26 | if (isVegetarian()) { 27 | cout << "(v)"; 28 | } 29 | cout << ", " << price() << endl; 30 | cout << " -- " << description() << endl; 31 | } 32 | 33 | Iterator* MenuItem::createIterator() { 34 | return new NullIterator(); 35 | } 36 | } // namespace composite 37 | -------------------------------------------------------------------------------- /src/composite/menu_iterator.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "composite/menu_iterator.h" 9 | 10 | namespace composite { 11 | MenuIterator::MenuIterator(vector *menu_components) 12 | : position_(0), menu_components_(menu_components) { 13 | } 14 | 15 | MenuIterator::~MenuIterator() { 16 | } 17 | 18 | bool MenuIterator::hasNext() { 19 | if (position_ >= menu_components_->size()) { 20 | return false; 21 | } 22 | return true; 23 | } 24 | 25 | void* MenuIterator::next() { 26 | MenuComponent *item = menu_components_->at(position_); 27 | position_++; 28 | return dynamic_cast(item); 29 | } 30 | } // namespace composite 31 | -------------------------------------------------------------------------------- /src/composite/menu_iterator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMPOSITE_MENU_ITERATOR_H_ 9 | #define COMPOSITE_MENU_ITERATOR_H_ 10 | 11 | #include 12 | using std::vector; 13 | #include "composite/menu_component.h" 14 | #include "composite/iterator.h" 15 | 16 | namespace composite { 17 | class MenuIterator : public Iterator { 18 | public: 19 | MenuIterator(vector *menu_components); 20 | virtual ~MenuIterator(); 21 | virtual bool hasNext(); 22 | virtual void* next(); 23 | private: 24 | vector *menu_components_; 25 | int position_; 26 | }; 27 | } // namespace composite 28 | #endif // COMPOSITE_MENU_ITERATOR_H_ 29 | -------------------------------------------------------------------------------- /src/composite/null_iterator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMPOSITE_NULL_ITERATOR_H_ 9 | #define COMPOSITE_NULL_ITERATOR_H_ 10 | 11 | #include "composite/iterator.h" 12 | 13 | namespace composite { 14 | class NullIterator : public Iterator { 15 | public: 16 | virtual ~NullIterator() {} 17 | virtual bool hasNext() { 18 | return false; 19 | } 20 | virtual void* next() { 21 | return NULL; 22 | } 23 | }; 24 | } // namespace composite 25 | #endif // COMPOSITE_NULL_ITERATOR_H_ 26 | -------------------------------------------------------------------------------- /src/composite/waitress.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef COMPOSITE_WAITRESS_H_ 9 | #define COMPOSITE_WAITRESS_H_ 10 | 11 | #include "composite/menu_component.h" 12 | 13 | namespace composite { 14 | class Waitress { 15 | public: 16 | explicit Waitress(MenuComponent *all_menus); 17 | virtual ~Waitress(); 18 | void printMenu(); 19 | void printVegetarianMenu(); 20 | private: 21 | MenuComponent *all_menus_; 22 | }; 23 | } // namespace composite 24 | #endif // COMPOSITE_WAITRESS_H_ 25 | -------------------------------------------------------------------------------- /src/decorator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_decorator STATIC 5 | beverage.cc 6 | decaf.cc 7 | house_blend.cc 8 | mocha.cc 9 | whip.cc 10 | darkroast.cc 11 | espresso.cc 12 | milk.cc 13 | soy.cc) 14 | 15 | install(TARGETS designpattern_decorator DESTINATION ${LIBRARY_OUTPUT_PATH}) 16 | 17 | add_executable(starbuzz_coffee_test ./test/starbuzz_coffee_test.cc) 18 | target_link_libraries(starbuzz_coffee_test designpattern_decorator) 19 | add_test(starbuzz_coffee_test ${EXECUTABLE_OUTPUT_PATH}/starbuzz_coffee_test) 20 | -------------------------------------------------------------------------------- /src/decorator/beverage.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/beverage.h" 9 | 10 | namespace decorator { 11 | Beverage::Beverage() : description_("Unknown Beverage") { 12 | } 13 | 14 | Beverage::~Beverage() { 15 | } 16 | 17 | string Beverage::description() { 18 | return description_; 19 | } 20 | } // namespace decorator 21 | -------------------------------------------------------------------------------- /src/decorator/beverage.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_BEVERAGE_H_ 9 | #define DECORATOR_BEVERAGE_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace decorator { 15 | class Beverage { 16 | public: 17 | Beverage(); 18 | virtual ~Beverage(); 19 | virtual string description(); 20 | virtual double cost() = 0; 21 | protected: 22 | string description_; 23 | }; 24 | } // namespace decorator 25 | #endif // DECORATOR_BEVERAGE_H_ 26 | -------------------------------------------------------------------------------- /src/decorator/condiment_decorator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_CONDIMENT_DECORATOR_H_ 9 | #define DECORATOR_CONDIMENT_DECORATOR_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "decorator/beverage.h" 14 | 15 | namespace decorator { 16 | class CondimentDecorator : public Beverage { 17 | public: 18 | virtual ~CondimentDecorator() {} 19 | virtual string description() = 0; 20 | }; 21 | } // namespace decorator 22 | #endif // DECORATOR_CONDIMENT_DECORATOR_H_ 23 | -------------------------------------------------------------------------------- /src/decorator/darkroast.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/darkroast.h" 9 | namespace decorator { 10 | DarkRoast::DarkRoast() { 11 | description_ = "Dark Roast Coffee"; 12 | } 13 | 14 | DarkRoast::~DarkRoast() { 15 | } 16 | 17 | double DarkRoast::cost() { 18 | return 0.99; 19 | } 20 | } // namespace decorator 21 | -------------------------------------------------------------------------------- /src/decorator/darkroast.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_DARKROAST_H_ 9 | #define DECORATOR_DARKROAST_H_ 10 | 11 | #include "decorator/beverage.h" 12 | 13 | namespace decorator { 14 | class DarkRoast : public Beverage { 15 | public: 16 | DarkRoast(); 17 | virtual ~DarkRoast(); 18 | virtual double cost(); 19 | }; 20 | } // namespace decorator 21 | #endif // DECORATOR_DARKROAST_H_ 22 | -------------------------------------------------------------------------------- /src/decorator/decaf.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/decaf.h" 9 | 10 | namespace decorator { 11 | Decaf::Decaf() { 12 | description_ = "Decaf Coffee"; 13 | } 14 | 15 | Decaf::~Decaf() { 16 | } 17 | 18 | double Decaf::cost() { 19 | return 1.05; 20 | } 21 | } // namespace decorator 22 | -------------------------------------------------------------------------------- /src/decorator/decaf.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_DECAF_H_ 9 | #define DECORATOR_DECAF_H_ 10 | 11 | #include "decorator/beverage.h" 12 | 13 | namespace decorator { 14 | class Decaf : public Beverage { 15 | public: 16 | Decaf(); 17 | virtual ~Decaf(); 18 | virtual double cost(); 19 | }; 20 | } // namespace decorator 21 | #endif // DECORATOR_DECAF_H_ 22 | -------------------------------------------------------------------------------- /src/decorator/espresso.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/espresso.h" 9 | 10 | namespace decorator { 11 | Espresso::Espresso() { 12 | description_ = "Espresso"; 13 | } 14 | 15 | Espresso::~Espresso() { 16 | } 17 | 18 | double Espresso::cost() { 19 | return 1.99; 20 | } 21 | } // namespace decorator 22 | -------------------------------------------------------------------------------- /src/decorator/espresso.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_ESPRESSO_H_ 9 | #define DECORATOR_ESPRESSO_H_ 10 | 11 | #include "decorator/beverage.h" 12 | 13 | namespace decorator { 14 | class Espresso: public Beverage { 15 | public: 16 | Espresso(); 17 | virtual ~Espresso(); 18 | virtual double cost(); 19 | }; 20 | } // namespace decorator 21 | #endif // DECORATOR_ESPRESSO_H_ 22 | -------------------------------------------------------------------------------- /src/decorator/house_blend.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/house_blend.h" 9 | 10 | namespace decorator { 11 | HouseBlend::HouseBlend() { 12 | description_ = "House Blend Coffee"; 13 | } 14 | 15 | HouseBlend::~HouseBlend() { 16 | } 17 | 18 | double HouseBlend::cost() { 19 | return 0.89; 20 | } 21 | } // namespace decorator 22 | -------------------------------------------------------------------------------- /src/decorator/house_blend.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_HOUSE_BLEND_H_ 9 | #define DECORATOR_HOUSE_BLEND_H_ 10 | 11 | #include "decorator/beverage.h" 12 | 13 | namespace decorator { 14 | class HouseBlend : public Beverage { 15 | public: 16 | HouseBlend(); 17 | virtual ~HouseBlend(); 18 | virtual double cost(); 19 | }; 20 | } // namespace decorator 21 | #endif // DECORATOR_HOUSE_BLEND_H_ 22 | -------------------------------------------------------------------------------- /src/decorator/milk.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/milk.h" 9 | 10 | namespace decorator { 11 | Milk::Milk(Beverage *beverage) : beverage_(beverage) { 12 | } 13 | 14 | Milk::~Milk() { 15 | } 16 | 17 | string Milk::description() { 18 | return beverage_->description() + ", Milk"; 19 | } 20 | 21 | double Milk::cost() { 22 | return 0.10 + beverage_->cost(); 23 | } 24 | } // namespace decorator 25 | -------------------------------------------------------------------------------- /src/decorator/milk.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | #ifndef DECORATOR_MILK_H_ 8 | #define DECORATOR_MILK_H_ 9 | 10 | #include 11 | using std::string; 12 | #include "decorator/condiment_decorator.h" 13 | #include "decorator/beverage.h" 14 | 15 | namespace decorator { 16 | class Milk : public CondimentDecorator { 17 | public: 18 | explicit Milk(Beverage *beverage); 19 | virtual ~Milk(); 20 | virtual string description(); 21 | virtual double cost(); 22 | private: 23 | Beverage *beverage_; 24 | }; 25 | } // namespace decorator 26 | #endif // DECORATOR_MILK_H_ 27 | -------------------------------------------------------------------------------- /src/decorator/mocha.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/mocha.h" 9 | 10 | namespace decorator { 11 | Mocha::Mocha(Beverage *beverage) : beverage_(beverage) { 12 | } 13 | 14 | Mocha::~Mocha() { 15 | } 16 | 17 | string Mocha::description() { 18 | return beverage_->description() + ", Mocha"; 19 | } 20 | 21 | double Mocha::cost() { 22 | return 0.20 + beverage_->cost(); 23 | } 24 | } // namespace decorator 25 | -------------------------------------------------------------------------------- /src/decorator/mocha.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_MACHA_H_ 9 | #define DECORATOR_MACHA_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "decorator/condiment_decorator.h" 14 | #include "decorator/beverage.h" 15 | 16 | namespace decorator { 17 | class Mocha: public CondimentDecorator { 18 | public: 19 | explicit Mocha(Beverage *beverage); 20 | virtual ~Mocha(); 21 | virtual string description(); 22 | virtual double cost(); 23 | private: 24 | Beverage *beverage_; 25 | }; 26 | } // namespace decorator 27 | #endif // DECORATOR_MACHA_H_ 28 | -------------------------------------------------------------------------------- /src/decorator/soy.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/soy.h" 9 | 10 | namespace decorator { 11 | Soy::Soy(Beverage *beverage) : beverage_(beverage) { 12 | } 13 | 14 | Soy::~Soy() { 15 | } 16 | 17 | string Soy::description() { 18 | return beverage_->description() + ", Soy"; 19 | } 20 | 21 | double Soy::cost() { 22 | return 0.15 + beverage_->cost(); 23 | } 24 | } // namespace decorator 25 | -------------------------------------------------------------------------------- /src/decorator/soy.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_SOY_H_ 9 | #define DECORATOR_SOY_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "decorator/condiment_decorator.h" 14 | #include "decorator/beverage.h" 15 | 16 | namespace decorator { 17 | class Soy : public CondimentDecorator { 18 | public: 19 | explicit Soy(Beverage *beverage); 20 | virtual ~Soy(); 21 | virtual string description(); 22 | virtual double cost(); 23 | private: 24 | Beverage *beverage_; 25 | }; 26 | } // namespace decorator 27 | #endif // DECORATOR_SOY_H_ 28 | -------------------------------------------------------------------------------- /src/decorator/whip.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "decorator/whip.h" 9 | 10 | namespace decorator { 11 | Whip::Whip(Beverage *beverage) : beverage_(beverage) { 12 | } 13 | 14 | Whip::~Whip() { 15 | } 16 | 17 | string Whip::description() { 18 | return beverage_->description() + ", Whip"; 19 | } 20 | 21 | double Whip::cost() { 22 | return 0.10 + beverage_->cost(); 23 | } 24 | } // namespace decorator 25 | -------------------------------------------------------------------------------- /src/decorator/whip.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef DECORATOR_WHIP_H_ 9 | #define DECORATOR_WHIP_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "decorator/condiment_decorator.h" 14 | #include "decorator/beverage.h" 15 | 16 | namespace decorator { 17 | class Whip : public CondimentDecorator { 18 | public: 19 | explicit Whip(Beverage *beverage); 20 | virtual ~Whip(); 21 | virtual string description(); 22 | virtual double cost(); 23 | private: 24 | Beverage *beverage_; 25 | }; 26 | } // namespace decorator 27 | #endif // DECORATOR_WHIP_H_ 28 | -------------------------------------------------------------------------------- /src/facade/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_facade STATIC 5 | amplifier.cc 6 | home_theater_facade.cc 7 | screen.cc 8 | cd_player.cc 9 | popcorn_popper.cc 10 | theater_lights.cc 11 | dvd_player.cc 12 | projector.cc 13 | tuner.cc) 14 | 15 | install(TARGETS designpattern_facade DESTINATION ${LIBRARY_OUTPUT_PATH}) 16 | 17 | add_executable(home_theater_test ./test/home_theater_test.cc) 18 | target_link_libraries(home_theater_test designpattern_facade) 19 | add_test(home_theater_test ${EXECUTABLE_OUTPUT_PATH}/home_theater_test) 20 | -------------------------------------------------------------------------------- /src/facade/cd_player.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACADE_CD_PLAYER_H_ 9 | #define FACADE_CD_PLAYER_H_ 10 | 11 | #include 12 | using std::string; 13 | #include // NOLINT 14 | using std::ostream; 15 | #include "facade/amplifier.h" 16 | 17 | namespace facade { 18 | class CdPlayer { 19 | public: 20 | explicit CdPlayer(string description, Amplifier* amplifier); 21 | virtual ~CdPlayer(); 22 | void on(); 23 | void off(); 24 | void eject(); 25 | void play(string title); 26 | void play(int track); 27 | void stop(); 28 | void pause(); 29 | friend ostream& operator<<(ostream& out, const CdPlayer& cd_player); 30 | private: 31 | string description_; 32 | int current_track_; 33 | Amplifier* amplifier_; 34 | string title_; 35 | }; 36 | } // namespace facade 37 | #endif // FACADE_CD_PLAYER_H_ 38 | -------------------------------------------------------------------------------- /src/facade/popcorn_popper.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "facade/popcorn_popper.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace facade { 14 | PopcornPopper::PopcornPopper(string description) : description_(description) { 15 | } 16 | 17 | PopcornPopper::~PopcornPopper() { 18 | } 19 | 20 | void PopcornPopper::on() { 21 | cout << description_ << " on" << endl; 22 | } 23 | 24 | void PopcornPopper::off() { 25 | cout << description_ << " off" << endl; 26 | } 27 | 28 | void PopcornPopper::pop() { 29 | cout << description_ << " popping popcorn!" << endl; 30 | } 31 | 32 | string PopcornPopper::toString() { 33 | return description_; 34 | } 35 | } // namespace facade 36 | -------------------------------------------------------------------------------- /src/facade/popcorn_popper.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACADE_POPCORN_POPPER_H_ 9 | #define FACADE_POPCORN_POPPER_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace facade { 15 | class PopcornPopper { 16 | public: 17 | explicit PopcornPopper(string description); 18 | virtual ~PopcornPopper(); 19 | void on(); 20 | void off(); 21 | void pop(); 22 | string toString(); 23 | private: 24 | string description_; 25 | }; 26 | } // namespace facade 27 | #endif // FACADE_POPCORN_POPPER_H_ 28 | -------------------------------------------------------------------------------- /src/facade/projector.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "facade/projector.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace facade { 14 | Projector::Projector(string description, DvdPlayer* dvd_player) : 15 | description_(description), dvd_player_(dvd_player) { 16 | } 17 | 18 | Projector::~Projector() { 19 | } 20 | 21 | void Projector::on() { 22 | cout << description_ << " on" << endl; 23 | } 24 | 25 | void Projector::off() { 26 | cout << description_ << " off" << endl; 27 | } 28 | 29 | void Projector::wideScreenMode() { 30 | cout << description_ << " in widescreen mode" << endl; 31 | } 32 | 33 | void Projector::tvMode() { 34 | cout << description_ << " in tv mode" << endl; 35 | } 36 | 37 | string Projector::toString() { 38 | return description_; 39 | } 40 | } // namespace facade 41 | -------------------------------------------------------------------------------- /src/facade/projector.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACADE_PROJECTOR_H_ 9 | #define FACADE_PROJECTOR_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "facade/dvd_player.h" 14 | 15 | namespace facade { 16 | class Projector { 17 | public: 18 | explicit Projector(string description, DvdPlayer* dvd_player); 19 | virtual ~Projector(); 20 | void on(); 21 | void off(); 22 | void wideScreenMode(); 23 | void tvMode(); 24 | string toString(); 25 | private: 26 | string description_; 27 | DvdPlayer* dvd_player_; 28 | }; 29 | } // namespace facade 30 | #endif // FACADE_PROJECTOR_H_ 31 | -------------------------------------------------------------------------------- /src/facade/screen.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "facade/screen.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace facade { 14 | Screen::Screen(string description) : description_(description) { 15 | } 16 | 17 | Screen::~Screen() { 18 | } 19 | 20 | void Screen::up() { 21 | cout << description_ << " going up" << endl; 22 | } 23 | 24 | void Screen::down() { 25 | cout << description_ << " going down" << endl; 26 | } 27 | 28 | string Screen::toString() { 29 | return description_; 30 | } 31 | } // namespace facade 32 | -------------------------------------------------------------------------------- /src/facade/screen.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACADE_SCREEN_H_ 9 | #define FACADE_SCREEN_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace facade { 15 | class Screen { 16 | public: 17 | explicit Screen(string description); 18 | virtual ~Screen(); 19 | void up(); 20 | void down(); 21 | string toString(); 22 | private: 23 | string description_; 24 | }; 25 | } // namespace facade 26 | #endif // FACADE_SCREEN_H_ 27 | -------------------------------------------------------------------------------- /src/facade/theater_lights.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "facade/theater_lights.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace facade { 14 | TheaterLights::TheaterLights(string description) : 15 | description_(description) { 16 | } 17 | 18 | TheaterLights::~TheaterLights() { 19 | } 20 | 21 | void TheaterLights::on() { 22 | cout << description_ << " on" << endl; 23 | } 24 | 25 | void TheaterLights::off() { 26 | cout << description_ << " off" << endl; 27 | } 28 | 29 | void TheaterLights::dim(int level) { 30 | cout << description_ << " diming to " << level << "%" << endl; 31 | } 32 | 33 | string TheaterLights::toString() { 34 | return description_; 35 | } 36 | } // namespace facade 37 | -------------------------------------------------------------------------------- /src/facade/theater_lights.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACADE_THEATER_LIGHTS_H_ 9 | #define FACADE_THEATER_LIGHTS_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace facade { 15 | class TheaterLights { 16 | public: 17 | explicit TheaterLights(string description); 18 | virtual ~TheaterLights(); 19 | void on(); 20 | void off(); 21 | void dim(int level); 22 | string toString(); 23 | private: 24 | string description_; 25 | }; 26 | } // namespace facade 27 | #endif // FACADE_THEATER_LIGHTS_H_ 28 | -------------------------------------------------------------------------------- /src/facade/tuner.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACADE_TUNER_H_ 9 | #define FACADE_TUNER_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "facade/amplifier.h" 14 | 15 | namespace facade { 16 | class Amplifier; 17 | class Tuner { 18 | public: 19 | explicit Tuner(string description, Amplifier* amplifier); 20 | virtual ~Tuner(); 21 | void on(); 22 | void off(); 23 | void set_frequency(double frequency); 24 | void setAM(); 25 | void setFM(); 26 | string toString(); 27 | private: 28 | string description_; 29 | Amplifier* amplifier_; 30 | double frequency_; 31 | }; 32 | } // namespace facade 33 | #endif // FACADE_TUNER_H_ 34 | -------------------------------------------------------------------------------- /src/factory_method/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(factory_method) 2 | add_subdirectory(simple_factory) -------------------------------------------------------------------------------- /src/factory_method/factory_method/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_factorymethod STATIC 5 | chicago_pizza_store.cc 6 | ny_pizza_store.cc 7 | pizza.cc 8 | chicago_style_cheese_pizza.cc 9 | ny_style_cheese_pizza.cc 10 | pizza_store.cc 11 | chicago_style_clam_pizza.cc 12 | ny_style_clam_pizza.cc 13 | chicago_style_pepperoni_pizza.cc 14 | ny_style_pepperoni_pizza.cc) 15 | 16 | install(TARGETS designpattern_factorymethod DESTINATION ${LIBRARY_OUTPUT_PATH}) 17 | 18 | add_executable(pizza_test ./test/pizza_test.cc) 19 | target_link_libraries(pizza_test designpattern_factorymethod) 20 | add_test(pizza_test ${EXECUTABLE_OUTPUT_PATH}/pizza_test) 21 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/chicago_pizza_store.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/factory_method/chicago_pizza_store.h" 9 | #include "factory_method/factory_method/chicago_style_cheese_pizza.h" 10 | #include "factory_method/factory_method/chicago_style_clam_pizza.h" 11 | #include "factory_method/factory_method/chicago_style_pepperoni_pizza.h" 12 | 13 | namespace factory_method { 14 | ChicagoPizzaStore::~ChicagoPizzaStore() { 15 | } 16 | 17 | Pizza* ChicagoPizzaStore::createPizza(string item) { 18 | if (item == "cheese") { 19 | return new ChicagoStyleCheesePizza(); 20 | } else if (item == "clam") { 21 | return new ChicagoStyleClamPizza(); 22 | } else if (item == "pepperoni") { 23 | return new ChicagoStylePepperoniPizza(); 24 | } else { 25 | return NULL; 26 | } 27 | } 28 | } // namespace factory_method 29 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/chicago_pizza_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_CHICAGO_PIZZA_STORE_H 9 | #define FACTORY_METHOD_CHICAGO_PIZZA_STORE_H 10 | 11 | #include 12 | using std::string; 13 | #include "base/macros.h" 14 | #include "factory_method/factory_method/pizza.h" 15 | #include "factory_method/factory_method/pizza_store.h" 16 | 17 | namespace factory_method { 18 | class ChicagoPizzaStore : public PizzaStore { 19 | public: 20 | virtual ~ChicagoPizzaStore(); 21 | Pizza* createPizza(string item); 22 | }; 23 | } // namespace factory_method 24 | #endif // FACTORY_METHOD_CHICAGO_PIZZA_STORE_H 25 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/chicago_style_cheese_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/factory_method/chicago_style_cheese_pizza.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | namespace factory_method { 13 | ChicagoStyleCheesePizza::ChicagoStyleCheesePizza() { 14 | name_ = "Chicago Style Deep Dish Cheese Pizza"; 15 | dough_ = "Extra Thick Crust Dough"; 16 | sauce_ = "Plum Tomato Sauce"; 17 | toppings_.push_back("Shredded Mozzarella Cheese"); 18 | } 19 | 20 | ChicagoStyleCheesePizza::~ChicagoStyleCheesePizza() { 21 | } 22 | 23 | void ChicagoStyleCheesePizza::cut() { 24 | cout << "Cutting the pizza into square slices" << endl; 25 | } 26 | } // namespace factory_method 27 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/chicago_style_cheese_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_CHICAGO_STYLE_CHEESE_PIZZA_H 9 | #define FACTORY_METHOD_CHICAGO_STYLE_CHEESE_PIZZA_H 10 | 11 | #include "factory_method/factory_method/pizza.h" 12 | 13 | namespace factory_method { 14 | class ChicagoStyleCheesePizza : public Pizza { 15 | public: 16 | ChicagoStyleCheesePizza(); 17 | virtual ~ChicagoStyleCheesePizza(); 18 | virtual void cut(); 19 | }; 20 | } // namespace factory_method 21 | #endif // FACTORY_METHOD_CHICAGO_STYLE_CHEESE_PIZZA_H 22 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/chicago_style_clam_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/factory_method/chicago_style_clam_pizza.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | namespace factory_method { 13 | ChicagoStyleClamPizza::ChicagoStyleClamPizza() { 14 | name_ = "Chicago Style Clam Pizza"; 15 | dough_ = "Extra Thick Crust Dough"; 16 | sauce_ = "Plum Tomato Sauce"; 17 | toppings_.push_back("Shredded Mozzarella Cheese"); 18 | toppings_.push_back("Frozen Clams from Chesapeake Bay"); 19 | } 20 | 21 | ChicagoStyleClamPizza::~ChicagoStyleClamPizza() { 22 | } 23 | 24 | void ChicagoStyleClamPizza::cut() { 25 | cout << "Cutting the pizza into square slices" << endl; 26 | } 27 | } // namespace factory_method 28 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/chicago_style_clam_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_CHICAGO_STYLE_CLAM_PIZZA_H 9 | #define FACTORY_METHOD_CHICAGO_STYLE_CLAM_PIZZA_H 10 | 11 | #include "factory_method/factory_method/pizza.h" 12 | 13 | namespace factory_method { 14 | class ChicagoStyleClamPizza : public Pizza { 15 | public: 16 | ChicagoStyleClamPizza(); 17 | virtual ~ChicagoStyleClamPizza(); 18 | virtual void cut(); 19 | }; 20 | } // namespace factory_method 21 | #endif // FACTORY_METHOD_CHICAGO_STYLE_CLAM_PIZZA_H 22 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/chicago_style_pepperoni_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_CHICAGO_STYLE_PEPPERONI_PIZZA_H 9 | #define FACTORY_METHOD_CHICAGO_STYLE_PEPPERONI_PIZZA_H 10 | 11 | #include "factory_method/factory_method/pizza.h" 12 | 13 | namespace factory_method { 14 | class ChicagoStylePepperoniPizza : public Pizza { 15 | public: 16 | ChicagoStylePepperoniPizza(); 17 | virtual ~ChicagoStylePepperoniPizza(); 18 | virtual void cut(); 19 | }; 20 | } // namespace factory_method 21 | #endif // FACTORY_METHOD_CHICAGO_STYLE_PEPPERONI_PIZZA_H 22 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/ny_pizza_store.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/factory_method/ny_pizza_store.h" 9 | #include "factory_method/factory_method/ny_style_cheese_pizza.h" 10 | #include "factory_method/factory_method/ny_style_clam_pizza.h" 11 | #include "factory_method/factory_method/ny_style_pepperoni_pizza.h" 12 | 13 | namespace factory_method { 14 | NYPizzaStore::~NYPizzaStore() { 15 | } 16 | 17 | Pizza* NYPizzaStore::createPizza(string item) { 18 | if (item == "cheese") { 19 | return new NYStyleCheesePizza(); 20 | } else if (item == "clam") { 21 | return new NYStyleClamPizza(); 22 | } else if (item == "pepperoni") { 23 | return new NYStylePepperoniPizza(); 24 | } else { 25 | return NULL; 26 | } 27 | } 28 | } // namespace factory_method 29 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/ny_pizza_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_NY_PIZZA_STORE_H 9 | #define FACTORY_METHOD_NY_PIZZA_STORE_H 10 | 11 | #include 12 | using std::string; 13 | #include "base/macros.h" 14 | #include "factory_method/factory_method/pizza.h" 15 | #include "factory_method/factory_method/pizza_store.h" 16 | 17 | namespace factory_method { 18 | class NYPizzaStore : public PizzaStore { 19 | public: 20 | virtual ~NYPizzaStore(); 21 | Pizza* createPizza(string item); 22 | }; 23 | } // namespace factory_method 24 | #endif // FACTORY_METHOD_NY_PIZZA_STORE_H 25 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/ny_style_cheese_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/factory_method/ny_style_cheese_pizza.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | namespace factory_method { 13 | NYStyleCheesePizza::NYStyleCheesePizza() { 14 | name_ = "NY Style Sauce and Cheese Pizza"; 15 | dough_ = "Thin Crust Dough"; 16 | sauce_ = "Marinara Sauce"; 17 | toppings_.push_back("Grated Reggiano Cheese"); 18 | } 19 | 20 | NYStyleCheesePizza::~NYStyleCheesePizza() { 21 | } 22 | } // namespace factory_method 23 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/ny_style_cheese_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_NY_STYLE_CHEESE_PIZZA_H 9 | #define FACTORY_METHOD_NY_STYLE_CHEESE_PIZZA_H 10 | 11 | #include "factory_method/factory_method/pizza.h" 12 | 13 | namespace factory_method { 14 | class NYStyleCheesePizza : public Pizza { 15 | public: 16 | NYStyleCheesePizza(); 17 | virtual ~NYStyleCheesePizza(); 18 | }; 19 | } // namespace factory_method 20 | #endif // FACTORY_METHOD_NY_STYLE_CHEESE_PIZZA_H 21 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/ny_style_clam_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/factory_method/ny_style_clam_pizza.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | namespace factory_method { 13 | NYStyleClamPizza::NYStyleClamPizza() { 14 | name_ = "NY Style Clam Pizza"; 15 | dough_ = "Thin Crust Dough"; 16 | sauce_ = "Marinara Sauce"; 17 | toppings_.push_back("Grated Reggiano Cheese"); 18 | toppings_.push_back("Fresh Clams from Long Island Sound"); 19 | } 20 | 21 | NYStyleClamPizza::~NYStyleClamPizza() { 22 | } 23 | } // namespace factory_method 24 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/ny_style_clam_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_NY_STYLE_CLAM_PIZZA_H 9 | #define FACTORY_METHOD_NY_STYLE_CLAM_PIZZA_H 10 | 11 | #include "factory_method/factory_method/pizza.h" 12 | 13 | namespace factory_method { 14 | class NYStyleClamPizza : public Pizza { 15 | public: 16 | NYStyleClamPizza(); 17 | virtual ~NYStyleClamPizza(); 18 | }; 19 | } // namespace factory_method 20 | #endif // FACTORY_METHOD_NY_STYLE_CLAM_PIZZA_H 21 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/ny_style_pepperoni_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/factory_method/ny_style_pepperoni_pizza.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | namespace factory_method { 13 | NYStylePepperoniPizza::NYStylePepperoniPizza() { 14 | name_ = "NY Style Pepperoni Pizza"; 15 | dough_ = "Thin Crust Dough"; 16 | sauce_ = "Marinara Sauce"; 17 | toppings_.push_back("Grated Reggiano Cheese"); 18 | toppings_.push_back("Sliced Pepperoni"); 19 | toppings_.push_back("Garlic"); 20 | toppings_.push_back("Onion"); 21 | toppings_.push_back("Mushrooms"); 22 | toppings_.push_back("Red Pepper"); 23 | } 24 | 25 | NYStylePepperoniPizza::~NYStylePepperoniPizza() { 26 | } 27 | } // namespace factory_method 28 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/ny_style_pepperoni_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_NY_STYLE_PEPPERONI_PIZZA_H 9 | #define FACTORY_METHOD_NY_STYLE_PEPPERONI_PIZZA_H 10 | 11 | #include "factory_method/factory_method/pizza.h" 12 | 13 | namespace factory_method { 14 | class NYStylePepperoniPizza : public Pizza { 15 | public: 16 | NYStylePepperoniPizza(); 17 | virtual ~NYStylePepperoniPizza(); 18 | }; 19 | } // namespace factory_method 20 | #endif // FACTORY_METHOD_NY_STYLE_PEPPERONI_PIZZA_H 21 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHO_PIZZA_H_ 9 | #define FACTORY_METHO_PIZZA_H_ 10 | 11 | #include 12 | #include 13 | #include // NOLINT 14 | using std::string; 15 | using std::vector; 16 | using std::cout; 17 | using std::endl; 18 | 19 | namespace factory_method { 20 | class Pizza { 21 | public: 22 | Pizza(); 23 | virtual ~Pizza(); 24 | string name(); 25 | virtual void prepare(); 26 | virtual void bake(); 27 | virtual void cut(); 28 | virtual void box(); 29 | string toString(); 30 | protected: 31 | string name_; 32 | string dough_; 33 | string sauce_; 34 | vector toppings_; 35 | }; 36 | } // namespace factory_method 37 | #endif // FACTORY_METHO_PIZZA_H_ 38 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/pizza_store.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/factory_method/pizza_store.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | namespace factory_method { 13 | PizzaStore::~PizzaStore() { 14 | } 15 | 16 | Pizza* PizzaStore::orderPizza(string type) { 17 | Pizza *pizza; 18 | pizza = createPizza(type); 19 | cout << "--- Making a " + pizza->name() + " ---" << endl; 20 | pizza->prepare(); 21 | pizza->bake(); 22 | pizza->cut(); 23 | pizza->box(); 24 | return pizza; 25 | } 26 | } // namespace factory_method 27 | -------------------------------------------------------------------------------- /src/factory_method/factory_method/pizza_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_PIZZA_STORE_H_ 9 | #define FACTORY_METHOD_PIZZA_STORE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "base/macros.h" 14 | #include "factory_method/factory_method/pizza.h" 15 | 16 | namespace factory_method { 17 | class PizzaStore { 18 | public: 19 | virtual ~PizzaStore(); 20 | virtual Pizza* createPizza(string item) = 0; 21 | Pizza* orderPizza(string type); 22 | }; 23 | } // namespace factory_method 24 | #endif // FACTORY_METHOD_PIZZA_STORE_H_ 25 | 26 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_simplefactory STATIC 5 | cheese_pizza.cc 6 | pepperoni_pizza.cc 7 | pizza_store.cc 8 | veggie_pizza.cc 9 | clam_pizza.cc 10 | pizza.cc 11 | simple_pizza_factory.cc) 12 | 13 | install(TARGETS designpattern_simplefactory DESTINATION ${LIBRARY_OUTPUT_PATH}) 14 | 15 | add_executable(simple_pizza_test ./test/simple_pizza_test.cc) 16 | target_link_libraries(simple_pizza_test designpattern_simplefactory) 17 | add_test(simple_pizza_test ${EXECUTABLE_OUTPUT_PATH}/simple_pizza_test) 18 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/cheese_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/simple_factory/cheese_pizza.h" 9 | 10 | namespace factory_method { 11 | CheesePizza::CheesePizza() { 12 | name_ = "Cheese Pizza"; 13 | dough_ = "Regular Crust"; 14 | sauce_ = "Marinara Pizza Sauce"; 15 | toppings_.push_back("Fresh Mozzarella"); 16 | toppings_.push_back("Parmesan"); 17 | } 18 | 19 | CheesePizza::~CheesePizza() { 20 | } 21 | } // namespace factory_method 22 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/cheese_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_CHEESE_PIZZA_H_ 9 | #define FACTORY_METHOD_CHEESE_PIZZA_H_ 10 | 11 | #include "base/macros.h" 12 | #include "factory_method/simple_factory/pizza.h" 13 | 14 | namespace factory_method { 15 | class CheesePizza : public Pizza { 16 | public: 17 | CheesePizza(); 18 | virtual ~CheesePizza(); 19 | }; 20 | } // namespace factory_method 21 | #endif // FACTORY_METHOD_CHEESE_PIZZA_H_ 22 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/clam_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/simple_factory/clam_pizza.h" 9 | 10 | namespace factory_method { 11 | ClamPizza::ClamPizza() { 12 | name_ = "Clam Pizza"; 13 | dough_ = "Thin crust"; 14 | sauce_ = "White garlic sauce"; 15 | toppings_.push_back("Clams"); 16 | toppings_.push_back("Grated parmesan cheese"); 17 | } 18 | 19 | ClamPizza::~ClamPizza() { 20 | } 21 | } // namespace factory_method 22 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/clam_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_CLAM_PIZZA_H_ 9 | #define FACTORY_METHOD_CLAM_PIZZA_H_ 10 | 11 | #include "base/macros.h" 12 | #include "factory_method/simple_factory/pizza.h" 13 | 14 | namespace factory_method { 15 | class ClamPizza : public Pizza { 16 | public: 17 | ClamPizza(); 18 | virtual ~ClamPizza(); 19 | }; 20 | } // namespace factory_method 21 | #endif // FACTORY_METHOD_CLAM_PIZZA_H_ 22 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/pepperoni_pizza.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/simple_factory/pepperoni_pizza.h" 9 | 10 | namespace factory_method { 11 | PepperoniPizza::PepperoniPizza() { 12 | name_ = "Pepperoni Pizza"; 13 | dough_ = "Crust"; 14 | sauce_ = "Marinara sauce"; 15 | toppings_.push_back("Sliced Pepperoni"); 16 | toppings_.push_back("Sliced Onion"); 17 | toppings_.push_back("Grated parmesan cheese"); 18 | } 19 | 20 | PepperoniPizza::~PepperoniPizza() { 21 | } 22 | } // namespace factory_method 23 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/pepperoni_pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_PEPPERONI_PIZZA_H_ 9 | #define FACTORY_METHOD_PEPPERONI_PIZZA_H_ 10 | 11 | #include "base/macros.h" 12 | #include "factory_method/simple_factory/pizza.h" 13 | 14 | namespace factory_method { 15 | class PepperoniPizza : public Pizza { 16 | public: 17 | PepperoniPizza(); 18 | virtual ~PepperoniPizza(); 19 | }; 20 | } // namespace factory_method 21 | #endif // FACTORY_METHOD_PEPPERONI_PIZZA_H_ 22 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/pizza.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHO_PIZZA_H_ 9 | #define FACTORY_METHO_PIZZA_H_ 10 | 11 | #include 12 | #include 13 | #include // NOLINT 14 | using std::string; 15 | using std::vector; 16 | using std::cout; 17 | using std::endl; 18 | 19 | namespace factory_method { 20 | class Pizza { 21 | public: 22 | Pizza(); 23 | virtual ~Pizza(); 24 | string name(); 25 | void prepare(); 26 | void bake(); 27 | void cut(); 28 | void box(); 29 | string toString(); 30 | protected: 31 | string name_; 32 | string dough_; 33 | string sauce_; 34 | vector toppings_; 35 | }; 36 | } // namespace factory_method 37 | #endif // FACTORY_METHO_PIZZA_H_ 38 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/pizza_store.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "factory_method/simple_factory/pizza_store.h" 9 | 10 | namespace factory_method { 11 | PizzaStore::~PizzaStore() { 12 | } 13 | 14 | PizzaStore::PizzaStore(SimplePizzaFactory *factory) : factory_(factory) { 15 | } 16 | 17 | Pizza* PizzaStore::orderPizza(string type) { 18 | Pizza *pizza; 19 | pizza = factory_->createPizza(type); 20 | 21 | pizza->prepare(); 22 | pizza->bake(); 23 | pizza->cut(); 24 | pizza->box(); 25 | return pizza; 26 | } 27 | } // namespace factory_method 28 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/pizza_store.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_PIZZA_STORE_H_ 9 | #define FACTORY_METHOD_PIZZA_STORE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "base/macros.h" 14 | #include "factory_method/simple_factory/simple_pizza_factory.h" 15 | #include "factory_method/simple_factory/pizza.h" 16 | 17 | namespace factory_method { 18 | class PizzaStore { 19 | public: 20 | virtual ~PizzaStore(); 21 | explicit PizzaStore(SimplePizzaFactory* factory); 22 | Pizza* orderPizza(string type); 23 | private: 24 | SimplePizzaFactory *factory_; 25 | DISALLOW_COPY_AND_ASSIGN(PizzaStore); 26 | }; 27 | } // namespace factory_method 28 | #endif // FACTORY_METHOD_PIZZA_STORE_H_ 29 | 30 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/simple_pizza_factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FACTORY_METHOD_SIMPLE_PIZZA_FACTORY_H_ 9 | #define FACTORY_METHOD_SIMPLE_PIZZA_FACTORY_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "base/macros.h" 14 | #include "factory_method/simple_factory/pizza.h" 15 | 16 | namespace factory_method { 17 | class SimplePizzaFactory { 18 | public: 19 | explicit SimplePizzaFactory(); 20 | virtual ~SimplePizzaFactory(); 21 | Pizza* createPizza(string type); 22 | }; 23 | } // namespace factory_method 24 | #endif // FACTORY_METHOD_SIMPLE_PIZZA_FACTORY_H_ 25 | -------------------------------------------------------------------------------- /src/factory_method/simple_factory/test/simple_pizza_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "factory_method/simple_factory/simple_pizza_factory.h" 12 | #include "factory_method/simple_factory/pizza.h" 13 | #include "factory_method/simple_factory/pizza_store.h" 14 | using namespace factory_method; // NOLINT 15 | 16 | int main(int argc, char *argv[]) { 17 | SimplePizzaFactory *factory = new SimplePizzaFactory(); 18 | PizzaStore *store = new PizzaStore(factory); 19 | 20 | Pizza *pizza = store->orderPizza("cheese"); 21 | cout << "We ordered a " << pizza->name() << endl; 22 | 23 | pizza = store->orderPizza("veggie"); 24 | cout << "We ordered a " << pizza->name() < // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace flyweight { 14 | Circle::Circle(string color) { 15 | color_ = color; 16 | } 17 | 18 | Circle::~Circle() { 19 | } 20 | 21 | void Circle::draw() { 22 | cout << "Circle:Draw() Color:" << color_ << ",x:" << x_ << ",y:" << y_ << 23 | ",radius:" << radius_ << endl; 24 | } 25 | } // namespace flyweight 26 | -------------------------------------------------------------------------------- /src/flyweight/circle.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FLYWEIGHT_CIRCLE_H_ 9 | #define FLYWEIGHT_CIRCLE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "flyweight/shape.h" 14 | 15 | namespace flyweight { 16 | class Circle : public Shape { 17 | public: 18 | explicit Circle(string color); 19 | virtual ~Circle(); 20 | void set_x(int x) {x_ = x;} 21 | void set_y(int y) {y_ = y;} 22 | void set_radius(int radius) {radius_ = radius;} 23 | virtual void draw(); 24 | private: 25 | string color_; 26 | int x_; 27 | int y_; 28 | int radius_; 29 | }; 30 | } // namespace flyweight 31 | #endif // FLYWEIGHT_CIRCLE_H_ 32 | -------------------------------------------------------------------------------- /src/flyweight/shape.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FLYWEIGHT_SHAPE_H_ 9 | #define FLYWEIGHT_SHAPE_H_ 10 | 11 | namespace flyweight { 12 | class Shape { 13 | public: 14 | virtual ~Shape() {} 15 | virtual void draw() = 0; 16 | }; 17 | } // namespace flyweight 18 | #endif // FLYWEIGHT_SHAPE_H_ 19 | -------------------------------------------------------------------------------- /src/flyweight/shape_factory.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "flyweight/shape_factory.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace flyweight { 14 | map *ShapeFactory::map_ = new map(); 15 | 16 | ShapeFactory::~ShapeFactory() { 17 | } 18 | 19 | Shape* ShapeFactory::getCircle(string color) { 20 | map::iterator it; 21 | it = map_->find(color); 22 | if (it != map_->end()) { 23 | return it->second; 24 | } 25 | Circle *circle = new Circle(color); 26 | map_->insert(std::pair(color, circle)); 27 | cout << "Creating circle of color:" << color << endl; 28 | return circle; 29 | } 30 | } // namespace flyweight 31 | -------------------------------------------------------------------------------- /src/flyweight/shape_factory.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef FLYWEIGHT_SHAPE_FACTORY_H_ 9 | #define FLYWEIGHT_SHAPE_FACTORY_H_ 10 | 11 | #include 12 | using std::map; 13 | #include 14 | using std::string; 15 | #include "flyweight/circle.h" 16 | #include "flyweight/shape.h" 17 | 18 | namespace flyweight { 19 | class ShapeFactory { 20 | public: 21 | virtual ~ShapeFactory(); 22 | static Shape* getCircle(string color); 23 | private: 24 | static map *map_; 25 | }; 26 | } // namespace flyweight 27 | #endif // FLYWEIGHT_SHAPE_FACTORY_H_ 28 | -------------------------------------------------------------------------------- /src/interpreter/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_interpreter STATIC 5 | add_expression.cc 6 | subtract_expression.cc 7 | number_expression.cc 8 | token_reader.cc) 9 | 10 | install(TARGETS designpattern_interpreter DESTINATION ${LIBRARY_OUTPUT_PATH}) 11 | 12 | add_executable(interpreter_token_test ./test/interpreter_token_test.cc) 13 | target_link_libraries(interpreter_token_test designpattern_interpreter) 14 | add_test(interpreter_token_test ${EXECUTABLE_OUTPUT_PATH}/interpreter_token_test) 15 | -------------------------------------------------------------------------------- /src/interpreter/add_expression.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "interpreter/add_expression.h" 9 | 10 | namespace interpreter { 11 | AddExpression::AddExpression(Expression *left, Expression *right) : 12 | left_expression_(left), right_expression_(right) { 13 | } 14 | 15 | AddExpression::~AddExpression() { 16 | delete left_expression_; 17 | delete right_expression_; 18 | } 19 | 20 | int AddExpression::Interpret() { 21 | return left_expression_->Interpret() + right_expression_->Interpret(); 22 | } 23 | } // namespace interpreter 24 | -------------------------------------------------------------------------------- /src/interpreter/add_expression.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef INTERPRETER_ADD_EXPRESSION_H_ 9 | #define INTERPRETER_ADD_EXPRESSION_H_ 10 | 11 | #include "interpreter/expression.h" 12 | 13 | namespace interpreter { 14 | class AddExpression : public Expression { 15 | public: 16 | AddExpression(Expression *left, Expression *right); 17 | virtual ~AddExpression(); 18 | virtual int Interpret(); 19 | private: 20 | Expression *left_expression_; 21 | Expression *right_expression_; 22 | }; 23 | } // namespace interpreter 24 | #endif // INTERPRETER_ADD_EXPRESSION_H_ 25 | -------------------------------------------------------------------------------- /src/interpreter/expression.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef INTERPRETER_EXPRESSION_H_ 9 | #define INTERPRETER_EXPRESSION_H_ 10 | 11 | namespace interpreter { 12 | class Expression { 13 | public: 14 | virtual ~Expression() {} 15 | virtual int Interpret() = 0; 16 | }; 17 | } // namespace interpreter 18 | #endif // INTERPRETER_EXPRESSION_H_ 19 | -------------------------------------------------------------------------------- /src/interpreter/null_expression.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef INTERPRETER_NULL_EXPRESSION_H_ 9 | #define INTERPRETER_NULL_EXPRESSION_H_ 10 | 11 | #include 12 | using std::runtime_error; 13 | #include "interpreter/expression.h" 14 | 15 | namespace interpreter { 16 | class NullExpression : public Expression { 17 | public: 18 | virtual ~NullExpression() {} 19 | virtual int Interpret() { 20 | throw runtime_error("Null Expression"); 21 | } 22 | }; 23 | } // namespace interpreter 24 | #endif // INTERPRETER_NULL_EXPRESSION_H_ 25 | -------------------------------------------------------------------------------- /src/interpreter/number_expression.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "interpreter/number_expression.h" 9 | 10 | namespace interpreter { 11 | NumberExpression::NumberExpression(int num) : number_(num) { 12 | } 13 | 14 | NumberExpression::~NumberExpression() { 15 | } 16 | 17 | int NumberExpression::Interpret() { 18 | return number_; 19 | } 20 | } // namespace interpreter 21 | -------------------------------------------------------------------------------- /src/interpreter/number_expression.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef INTERPRETER_NUMBER_EXPRESSION_H_ 9 | #define INTERPRETER_NUMBER_EXPRESSION_H_ 10 | 11 | #include "interpreter/expression.h" 12 | 13 | namespace interpreter { 14 | class NumberExpression : public Expression { 15 | public: 16 | explicit NumberExpression(int num); 17 | virtual ~NumberExpression(); 18 | virtual int Interpret(); 19 | private: 20 | int number_; 21 | }; 22 | } // namespace interpreter 23 | #endif // INTERPRETER_NUMBER_EXPRESSION_H_ 24 | -------------------------------------------------------------------------------- /src/interpreter/subtract_expression.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "interpreter/subtract_expression.h" 9 | 10 | namespace interpreter { 11 | SubtractExpression::SubtractExpression(Expression *left, Expression *right) : 12 | left_expression_(left), right_expression_(right) { 13 | } 14 | 15 | SubtractExpression::~SubtractExpression() { 16 | } 17 | 18 | int SubtractExpression::Interpret() { 19 | return left_expression_->Interpret() - right_expression_->Interpret(); 20 | } 21 | } // namespace interpreter 22 | -------------------------------------------------------------------------------- /src/interpreter/subtract_expression.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef INTERPRETER_SUBTRACT_EXPRESSION_H_ 9 | #define INTERPRETER_SUBTRACT_EXPRESSION_H_ 10 | 11 | #include "interpreter/expression.h" 12 | 13 | namespace interpreter { 14 | class SubtractExpression : public Expression { 15 | public: 16 | SubtractExpression(Expression *left, Expression *right); 17 | virtual ~SubtractExpression(); 18 | virtual int Interpret(); 19 | private: 20 | Expression *left_expression_; 21 | Expression *right_expression_; 22 | }; 23 | } // namespace interpreter 24 | #endif // INTERPRETER_SUBTRACT_EXPRESSION_H_ 25 | -------------------------------------------------------------------------------- /src/interpreter/token_reader.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef INTERPRETER_TOKEN_READER_H_ 9 | #define INTERPRETER_TOKEN_READER_H_ 10 | 11 | #include 12 | using std::vector; 13 | #include 14 | using std::string; 15 | #include "interpreter/expression.h" 16 | 17 | namespace interpreter { 18 | class TokenReader { 19 | public: 20 | virtual ~TokenReader(); 21 | Expression* ReadToken(vector *token_vector); 22 | Expression* ReadNextToken(vector *token_vector); 23 | Expression* ReadNonTerminal(vector *token_vector); 24 | }; 25 | } // namespace interpreter 26 | #endif // INTERPRETER_TOKEN_READER_H_ 27 | -------------------------------------------------------------------------------- /src/iterator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_iterator STATIC 5 | diner_menu.cc 6 | menu_item.cc 7 | pancake_house_menu_iterator.cc 8 | diner_menu_iterator.cc 9 | pancake_house_menu.cc 10 | waitress.cc) 11 | 12 | install(TARGETS designpattern_iterator DESTINATION ${LIBRARY_OUTPUT_PATH}) 13 | 14 | add_executable(menu_test ./test/menu_test.cc) 15 | target_link_libraries(menu_test designpattern_iterator) 16 | add_test(menu_test ${EXECUTABLE_OUTPUT_PATH}/menu_test) 17 | -------------------------------------------------------------------------------- /src/iterator/diner_menu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ITERATOR_DINER_MENU_H_ 9 | #define ITERATOR_DINER_MENU_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "iterator/iterator.h" 14 | #include "iterator/menu.h" 15 | #include "iterator/menu_item.h" 16 | 17 | namespace iterator { 18 | class DinerMenu : public Menu { 19 | public: 20 | enum { kMaxItems = 6 }; 21 | DinerMenu(); 22 | virtual ~DinerMenu(); 23 | void addItem(const string &name, const string &description, bool vegetarian, 24 | double price); 25 | MenuItem* menu_items() const { return *menu_items_; } 26 | virtual Iterator* createIterator(); 27 | private: 28 | int number_of_items_; 29 | MenuItem *menu_items_[kMaxItems]; 30 | }; 31 | } // namespace iterator 32 | #endif // ITERATOR_DINER_MENU_H_ 33 | -------------------------------------------------------------------------------- /src/iterator/diner_menu_iterator.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "iterator/diner_menu_iterator.h" 9 | 10 | namespace iterator { 11 | DinerMenuIterator::DinerMenuIterator(MenuItem **items) : 12 | items_(items), position_(0) { 13 | } 14 | 15 | DinerMenuIterator::~DinerMenuIterator() { 16 | } 17 | 18 | bool DinerMenuIterator::hasNext() { 19 | if (position_ >= kMaxItems || items_[position_] == NULL) { 20 | return false; 21 | } 22 | return true; 23 | } 24 | 25 | void* DinerMenuIterator::next() { 26 | MenuItem* menu_item = items_[position_]; 27 | position_++; 28 | return reinterpret_cast(menu_item); 29 | } 30 | } // namespace iterator 31 | -------------------------------------------------------------------------------- /src/iterator/diner_menu_iterator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ITERATOR_DINER_MENU_ITERATOR_H_ 9 | #define ITERATOR_DINER_MENU_ITERATOR_H_ 10 | 11 | #include "iterator/iterator.h" 12 | #include "iterator/menu_item.h" 13 | 14 | namespace iterator { 15 | class DinerMenuIterator : public Iterator { 16 | public: 17 | enum { kMaxItems = 6 }; 18 | DinerMenuIterator(MenuItem **items); 19 | virtual ~DinerMenuIterator(); 20 | virtual bool hasNext(); 21 | virtual void* next(); 22 | private: 23 | MenuItem **items_; 24 | int position_; 25 | }; 26 | } // namespace iterator 27 | #endif // ITERATOR_DINER_MENU_ITERATOR_H_ 28 | -------------------------------------------------------------------------------- /src/iterator/iterator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ITERATOR_ITERATOR_H_ 9 | #define ITERATOR_ITERATOR_H_ 10 | 11 | namespace iterator { 12 | class Iterator { 13 | public: 14 | virtual ~Iterator() {} 15 | virtual bool hasNext() = 0; 16 | virtual void* next() = 0; 17 | }; 18 | } // namespace iterator 19 | #endif // ITERATOR_ITERATOR_H_ 20 | -------------------------------------------------------------------------------- /src/iterator/menu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ITERATOR_MENU_H_ 9 | #define ITERATOR_MENU_H_ 10 | 11 | #include "iterator/iterator.h" 12 | 13 | namespace iterator { 14 | class Menu { 15 | public: 16 | virtual ~Menu() {} 17 | virtual Iterator* createIterator() = 0; 18 | }; 19 | } // namespace iterator 20 | #endif // ITERATOR_MENU_H_ 21 | -------------------------------------------------------------------------------- /src/iterator/menu_item.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "iterator/menu_item.h" 9 | 10 | namespace iterator { 11 | MenuItem::MenuItem(const string &name, const string &description, 12 | bool vegetarian, double price) : 13 | name_(name), description_(description), vegetarian_(vegetarian), 14 | price_(price) { 15 | } 16 | 17 | MenuItem::~MenuItem() { 18 | } 19 | } // namespace iterator 20 | -------------------------------------------------------------------------------- /src/iterator/menu_item.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ITERATOR_MENU_ITEM_H_ 9 | #define ITERATOR_MENU_ITEM_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace iterator { 15 | class MenuItem { 16 | public: 17 | MenuItem(const string &name, const string &description, 18 | bool vegetarian, double price); 19 | virtual ~MenuItem(); 20 | string name() const { return name_; } 21 | string description() const { return description_; } 22 | double price() const { return price_; } 23 | bool isVegetarian() const { return vegetarian_; } 24 | private: 25 | string name_; 26 | string description_; 27 | bool vegetarian_; 28 | double price_; 29 | }; 30 | } // namespace iterator 31 | #endif // ITERATOR_MENU_ITEM_H_ 32 | -------------------------------------------------------------------------------- /src/iterator/pancake_house_menu.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ITERATOR_PANCAKE_HOUSE_MENU_H_ 9 | #define ITERATOR_PANCAKE_HOUSE_MENU_H_ 10 | 11 | #include 12 | using std::string; 13 | #include 14 | using std::vector; 15 | #include "iterator/menu.h" 16 | #include "iterator/menu_item.h" 17 | #include "iterator/iterator.h" 18 | 19 | namespace iterator { 20 | class PancakeHouseMenu : public Menu { 21 | public: 22 | PancakeHouseMenu(); 23 | virtual ~PancakeHouseMenu(); 24 | void addItem(const string &name, const string &description, bool vegetarian, 25 | double price); 26 | vector* menu_items() const { return menu_items_; } 27 | virtual Iterator* createIterator(); 28 | private: 29 | vector *menu_items_; 30 | }; 31 | } // namespace iterator 32 | #endif // ITERATOR_PANCAKE_HOUSE_MENU_H_ 33 | -------------------------------------------------------------------------------- /src/iterator/pancake_house_menu_iterator.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "iterator/pancake_house_menu_iterator.h" 9 | 10 | namespace iterator { 11 | PancakeHouseMenuIterator::PancakeHouseMenuIterator(vector *items) 12 | : items_(items), position_(0) { 13 | } 14 | 15 | PancakeHouseMenuIterator::~PancakeHouseMenuIterator() { 16 | } 17 | 18 | bool PancakeHouseMenuIterator::hasNext() { 19 | if (position_ >= items_->size() || items_->at(position_) == NULL) { 20 | return false; 21 | } 22 | return true; 23 | } 24 | 25 | void* PancakeHouseMenuIterator::next() { 26 | MenuItem *menu_item = items_->at(position_); 27 | position_++; 28 | return reinterpret_cast(menu_item); 29 | } 30 | } // namespace iterator 31 | -------------------------------------------------------------------------------- /src/iterator/pancake_house_menu_iterator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ITERATOR_PANCAKE_HOUSE_MENU_ITERATOR_H_ 9 | #define ITERATOR_PANCAKE_HOUSE_MENU_ITERATOR_H_ 10 | 11 | #include 12 | using std::vector; 13 | #include "iterator/iterator.h" 14 | #include "iterator/menu_item.h" 15 | 16 | namespace iterator { 17 | class PancakeHouseMenuIterator : public Iterator { 18 | public: 19 | explicit PancakeHouseMenuIterator(vector *items); 20 | virtual ~PancakeHouseMenuIterator(); 21 | bool hasNext(); 22 | void* next(); 23 | private: 24 | vector *items_; 25 | int position_; 26 | }; 27 | } // namespace iterator 28 | #endif // ITERATOR_PANCAKE_HOUSE_MENU_ITERATOR_H_ 29 | -------------------------------------------------------------------------------- /src/iterator/waitress.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef ITERATOR_WAITRESS_H_ 9 | #define ITERATOR_WAITRESS_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "iterator/menu.h" 14 | #include "iterator/iterator.h" 15 | 16 | namespace iterator { 17 | class Waitress { 18 | public: 19 | Waitress(Menu *pancake_house_menu, Menu *diner_menu); 20 | virtual ~Waitress(); 21 | void printMenu() const; 22 | void printVegetarianMenu() const; 23 | bool isItemVegetarian(const string &name) const; 24 | private: 25 | void printMenu(Iterator *iterator) const; 26 | void printVegetarianMenu(Iterator *iterator) const; 27 | bool isVegetarian(const string &name, Iterator *iterator) const; 28 | Menu *pancake_house_menu_; 29 | Menu *diner_menu_; 30 | }; 31 | } // namespace iterator 32 | #endif // ITERATOR_WAITRESS_H_ 33 | -------------------------------------------------------------------------------- /src/mediator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_mediator STATIC 5 | general_staff.cc 6 | manager.cc 7 | sys_admin.cc) 8 | 9 | install(TARGETS designpattern_mediator DESTINATION ${LIBRARY_OUTPUT_PATH}) 10 | 11 | add_executable(mediator_staff_test ./test/mediator_staff_test.cc) 12 | target_link_libraries(mediator_staff_test designpattern_mediator) 13 | add_test(mediator_staff_test ${EXECUTABLE_OUTPUT_PATH}/mediator_staff_test) 14 | -------------------------------------------------------------------------------- /src/mediator/colleague.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEDIATOR_COLLEAGUE_H_ 9 | #define MEDIATOR_COLLEAGUE_H_ 10 | 11 | namespace mediator { 12 | class Colleague { 13 | protected: 14 | Colleague() {} 15 | virtual ~Colleague() {} 16 | }; 17 | } // namespace mediator 18 | #endif // MEDIATOR_COLLEAGUE_H_ 19 | -------------------------------------------------------------------------------- /src/mediator/employee.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEDIATOR_EMPLOEE_H_ 9 | #define MEDIATOR_EMPLOEE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "mediator/colleague.h" 14 | 15 | namespace mediator { 16 | class Employee : public Colleague { 17 | public: 18 | Employee(string title, string name) : title_(title), name_(name) { 19 | } 20 | virtual ~Employee() {} 21 | string title() {return title_;} 22 | string name() {return name_;} 23 | protected: 24 | string title_; 25 | string name_; 26 | }; 27 | } // namespace mediator 28 | #endif // MEDIATOR_EMPLOEE_H_ 29 | -------------------------------------------------------------------------------- /src/mediator/general_staff.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEDIATOR_GENERAL_STAFF_H_ 9 | #define MEDIATOR_GENERAL_STAFF_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "mediator/employee.h" 14 | #include "mediator/colleague_event.h" 15 | #include "mediator/staff_msg.h" 16 | 17 | namespace mediator { 18 | class GeneralStaff : public Employee { 19 | public: 20 | GeneralStaff(string title, string name); 21 | virtual ~GeneralStaff(); 22 | static void OnColleagueEvent(Colleague *source, StaffMsg data, 23 | Colleague* context); 24 | protected: 25 | ColleagueEvent general_staff_event_; 26 | }; 27 | } // namespace mediator 28 | #endif // MEDIATOR_GENERAL_STAFF_H_ 29 | -------------------------------------------------------------------------------- /src/mediator/manager.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "mediator/manager.h" 9 | #include "mediator/staff_msg.h" 10 | 11 | namespace mediator { 12 | Manager::Manager(string name) : GeneralStaff("Manager", name) { 13 | } 14 | 15 | Manager::~Manager() { 16 | } 17 | 18 | void Manager::BookMeetingRoom(string meeting_room_name) { 19 | general_staff_event_.FireEvent(StaffMsg("Meeting Room Booking", 20 | meeting_room_name)); 21 | } 22 | } // namespace mediator 23 | -------------------------------------------------------------------------------- /src/mediator/manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEDIATOR_MANAGER_H_ 9 | #define MEDIATOR_MANAGER_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "mediator/general_staff.h" 14 | #include "mediator/colleague_event.h" 15 | 16 | namespace mediator { 17 | class Manager : public GeneralStaff { 18 | public: 19 | explicit Manager(string name); 20 | virtual ~Manager(); 21 | void BookMeetingRoom(string meeting_room_name); 22 | }; 23 | } // namespace mediator 24 | #endif // MEDIATOR_MANAGER_H_ 25 | -------------------------------------------------------------------------------- /src/mediator/sales_men.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEDIATOR_SALES_MEN_H_ 9 | #define MEDIATOR_SALES_MEN_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "mediator/general_staff.h" 14 | 15 | namespace mediator { 16 | class SalesMen : public GeneralStaff { 17 | public: 18 | explicit SalesMen(string name) : 19 | GeneralStaff("Sales Man", name) { 20 | } 21 | virtual ~SalesMen() {} 22 | }; 23 | } // namespace mediator 24 | #endif // MEDIATOR_SALES_MEN_H_ 25 | -------------------------------------------------------------------------------- /src/mediator/staff_msg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEDIATOR_STAFF_MSG_H_ 9 | #define MEDIATOR_STAFF_MSG_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace mediator { 15 | class StaffMsg { 16 | public: 17 | StaffMsg(string name, string data) : msg_name_(name), msg_data_(data) { 18 | } 19 | virtual ~StaffMsg() {} 20 | string msg_name() {return msg_name_;} 21 | string msg_data() {return msg_data_;} 22 | private: 23 | string msg_name_; 24 | string msg_data_; 25 | }; 26 | } // namespace mediator 27 | #endif // MEDIATOR_STAFF_MSG_H_ 28 | -------------------------------------------------------------------------------- /src/mediator/sys_admin.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "mediator/sys_admin.h" 9 | #include "mediator/staff_msg.h" 10 | 11 | namespace mediator { 12 | SysAdmin::SysAdmin(string name) : GeneralStaff("Sys Admin", name) { 13 | } 14 | 15 | SysAdmin::~SysAdmin() { 16 | } 17 | 18 | void SysAdmin::AdviceForSoftwareUpdate(string sw_name) { 19 | general_staff_event_.FireEvent(StaffMsg("Software Update Advice", sw_name)); 20 | } 21 | } // namespace mediator 22 | -------------------------------------------------------------------------------- /src/mediator/sys_admin.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEDIATOR_SYS_ADMIN_H_ 9 | #define MEDIATOR_SYS_ADMIN_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "mediator/general_staff.h" 14 | 15 | namespace mediator { 16 | class SysAdmin : public GeneralStaff { 17 | public: 18 | explicit SysAdmin(string name); 19 | virtual ~SysAdmin(); 20 | void AdviceForSoftwareUpdate(string sw_name); 21 | }; 22 | } // namespace mediator 23 | #endif // MEDIATOR_SYS_ADMIN_H_ 24 | -------------------------------------------------------------------------------- /src/mediator/test/mediator_staff_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "mediator/manager.h" 12 | #include "mediator/sys_admin.h" 13 | #include "mediator/sales_men.h" 14 | using namespace mediator; // NOLINT 15 | 16 | int main(int argc, char *argv[]) { 17 | Manager mng1("Vivek"), mng2("Pradeep"); 18 | SysAdmin sys_admin("Sony"); 19 | SalesMen sl1("Dave"), s12("Mike"), s13("Allen"); 20 | 21 | mng1.BookMeetingRoom("Big Room"); 22 | cout << endl; 23 | sys_admin.AdviceForSoftwareUpdate("Win7"); 24 | cout << endl; 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /src/memento/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_executable(memento_test ./test/memento_test.cc) 5 | add_test(memento_test ${EXECUTABLE_OUTPUT_PATH}/memento_test) 6 | -------------------------------------------------------------------------------- /src/memento/caretaker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEMENTO_CARETAKER_H_ 9 | #define MEMENTO_CARETAKER_H_ 10 | #include 11 | using std::vector; 12 | #include "memento/memento.h" 13 | #include "memento/originator.h" 14 | 15 | namespace memento { 16 | template 17 | class Caretaker { 18 | public: 19 | virtual ~Caretaker() {} 20 | void SaveState(Originator *orig) { 21 | memento_array_.push_back(orig->CreateMemento()); 22 | } 23 | void RestoreState(Originator *orig, int state_number) { 24 | orig->SetMemento(memento_array_[state_number]); 25 | } 26 | private: 27 | vector *> memento_array_; 28 | }; 29 | } // namespace memento 30 | #endif // MEMENTO_CARETAKER_H_ 31 | -------------------------------------------------------------------------------- /src/memento/memento.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEMENTO_MEMENTO_H_ 9 | #define MEMENTO_MEMENTO_H_ 10 | 11 | namespace memento { 12 | template 13 | class Memento { 14 | public: 15 | virtual ~Memento() {} 16 | T state() const {return state_;} 17 | void set_state(T state) {state_ = state;} 18 | private: 19 | T state_; 20 | }; 21 | } // namespace memento 22 | #endif // MEMENTO_MEMENTO_H_ 23 | -------------------------------------------------------------------------------- /src/memento/originator.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef MEMENOTO_ORIGINATOR_H_ 9 | #define MEMENOTO_ORIGINATOR_H_ 10 | 11 | #include "memento/memento.h" 12 | #include // NOLINT 13 | using std::cout; 14 | using std::endl; 15 | #include "memento/originator.h" 16 | 17 | namespace memento { 18 | template 19 | class Originator { 20 | public: 21 | virtual ~Originator() {} 22 | Memento* CreateMemento() { 23 | Memento *m = new Memento(); 24 | m->set_state(state_); 25 | return m; 26 | } 27 | void SetMemento(Memento *m) { 28 | state_ = m->state(); 29 | } 30 | void set_state(const T &state) {state_ = state;} 31 | void ShowState() const { 32 | cout << state_ << endl; 33 | } 34 | private: 35 | T state_; 36 | }; 37 | } // namespace memento 38 | #endif // MEMENOTO_ORIGINATOR_H_ 39 | -------------------------------------------------------------------------------- /src/memento/test/memento_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include 9 | using std::string; 10 | #include "memento/originator.h" 11 | #include "memento/caretaker.h" 12 | using namespace memento; 13 | 14 | int main(int argc, char *argv[]) { 15 | Originator *orig = new Originator(); 16 | Caretaker *care_taker = new Caretaker (); 17 | 18 | orig->set_state("state #0"); 19 | care_taker->SaveState(orig); 20 | orig->ShowState(); 21 | 22 | orig->set_state("state #1"); 23 | care_taker->SaveState(orig); 24 | orig->ShowState(); 25 | 26 | orig->set_state("state #2"); 27 | care_taker->SaveState(orig); 28 | orig->ShowState(); 29 | 30 | care_taker->RestoreState(orig, 0); 31 | orig->ShowState(); 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /src/observer/current_conditions_display.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "observer/current_conditions_display.h" 9 | #include 10 | 11 | namespace observer { 12 | CurrentConditionsDisplay::CurrentConditionsDisplay(Subject *weather_data) 13 | : weather_data_(weather_data) { 14 | weather_data->registerObserver(this); 15 | } 16 | 17 | CurrentConditionsDisplay::~CurrentConditionsDisplay() { 18 | } 19 | 20 | void CurrentConditionsDisplay::update(float temp, float humidity, 21 | float pressure) { 22 | temperature_ = temp; 23 | humidity_ = humidity; 24 | display(); 25 | } 26 | 27 | void CurrentConditionsDisplay::display() { 28 | printf("Current conditions: %fF degrees and %f%% humidity\n", temperature_, 29 | humidity_); 30 | } 31 | } // namespace observer 32 | -------------------------------------------------------------------------------- /src/observer/display_element.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef OBSERVER_DISPLAY_ELEMENT_H_ 9 | #define OBSERVER_DISPLAY_ELEMENT_H_ 10 | 11 | namespace observer { 12 | class DisplayElement { 13 | public: 14 | virtual ~DisplayElement() {} 15 | virtual void display() = 0; 16 | }; 17 | } // namespace observer 18 | 19 | #endif // OBSERVER_DISPLAY_ELEMENT_H_ 20 | -------------------------------------------------------------------------------- /src/observer/forecast_display.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef OBSERVER_FORECAST_DISPLAY_H_ 9 | #define OBSERVER_FORECAST_DISPLAY_H_ 10 | 11 | #include "base/macros.h" 12 | #include "observer/observer.h" 13 | #include "observer/display_element.h" 14 | #include "observer/subject.h" 15 | 16 | namespace observer { 17 | class ForecastDisplay : public Observer, public DisplayElement { 18 | public: 19 | explicit ForecastDisplay(Subject *weather_data); 20 | virtual ~ForecastDisplay(); 21 | virtual void update(float temp, float humidity, float pressure); 22 | virtual void display(); 23 | private: 24 | float current_pressure_; 25 | float last_pressure_; 26 | Subject *weather_data_; 27 | DISALLOW_COPY_AND_ASSIGN(ForecastDisplay); 28 | }; 29 | } // namespace observer 30 | #endif // OBSERVER_FORECAST_DISPLAY_H_ 31 | -------------------------------------------------------------------------------- /src/observer/heatindex_display.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef OBSERVER_HEATINDEX_DISPLAY_H_ 9 | #define OBSERVER_HEATINDEX_DISPLAY_H_ 10 | 11 | #include "base/macros.h" 12 | #include "observer/observer.h" 13 | #include "observer/display_element.h" 14 | #include "observer/subject.h" 15 | 16 | namespace observer { 17 | class HeatIndexDisplay : public Observer, public DisplayElement { 18 | public: 19 | explicit HeatIndexDisplay(Subject *weather_data); 20 | virtual ~HeatIndexDisplay(); 21 | float computeHeatIndex(float t, float rh); 22 | virtual void update(float temp, float humidity, float pressure); 23 | virtual void display(); 24 | private: 25 | float heat_index_; 26 | Subject *weather_data_; 27 | DISALLOW_COPY_AND_ASSIGN(HeatIndexDisplay); 28 | }; 29 | } // namespace observer 30 | #endif // OBSERVER_HEATINDEX_DISPLAY_H_ 31 | 32 | -------------------------------------------------------------------------------- /src/observer/observer.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef OBSERVER_OBSERVER_H_ 9 | #define OBSERVER_OBSERVER_H_ 10 | 11 | namespace observer { 12 | class Observer { 13 | public: 14 | virtual ~Observer() {} 15 | virtual void update(float temp, float humdity, float pressure) = 0; 16 | }; 17 | } // namespace observer 18 | 19 | #endif // OBSERVER_OBSERVER_H_ 20 | -------------------------------------------------------------------------------- /src/observer/subject.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef OBSERVER_SUBJECT_H_ 9 | #define OBSERVER_SUBJECT_H_ 10 | 11 | #include "observer/observer.h" 12 | 13 | namespace observer { 14 | class Subject { 15 | public: 16 | virtual ~Subject() {} 17 | virtual void registerObserver(Observer *o) = 0; 18 | virtual void removeObserver(Observer *o) = 0; 19 | virtual void notifyObservers() = 0; 20 | }; 21 | } // namespace observer 22 | #endif // OBSERVER_SUBJECT_H_ 23 | -------------------------------------------------------------------------------- /src/prototype/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_prototype STATIC 5 | configuration.cc 6 | prototype_manager.cc 7 | user_profile.cc) 8 | 9 | install(TARGETS designpattern_prototype DESTINATION ${LIBRARY_OUTPUT_PATH}) 10 | 11 | add_executable(prototype_test ./test/prototype_test.cc) 12 | target_link_libraries(prototype_test designpattern_prototype) 13 | add_test(prototype_test ${EXECUTABLE_OUTPUT_PATH}/prototype_test) 14 | -------------------------------------------------------------------------------- /src/prototype/configuration.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "prototype/configuration.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace prototype { 14 | Configuration::~Configuration() { 15 | } 16 | 17 | void Configuration::GetFileInformation() { 18 | cout << "takes 5 seconds to get information" << endl; 19 | file_information_ = "Long file information"; 20 | } 21 | 22 | Prototype* Configuration::Clone() { 23 | Configuration *c = new Configuration(); 24 | c->file_information_ = file_information_; 25 | return c; 26 | } 27 | 28 | void Configuration::ShowInformation() { 29 | cout << "Showing " << file_information_ << endl; 30 | } 31 | } // namespace prototype 32 | -------------------------------------------------------------------------------- /src/prototype/configuration.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef PROTOTYPE_CONFIGURATION_H_ 9 | #define PROTOTYPE_CONFIGURATION_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "prototype/prototype.h" 14 | 15 | namespace prototype { 16 | class Configuration : public Prototype { 17 | public: 18 | virtual ~Configuration(); 19 | void GetFileInformation(); 20 | virtual Prototype* Clone(); 21 | void ShowInformation(); 22 | private: 23 | string file_information_; 24 | }; 25 | } // namespace prototype 26 | #endif // PROTOTYPE_CONFIGURATION_H_ 27 | -------------------------------------------------------------------------------- /src/prototype/prototype.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef PROTOTYPE_PROTOTYPE_H_ 9 | #define PROTOTYPE_PROTOTYPE_H_ 10 | 11 | namespace prototype { 12 | class Prototype { 13 | public: 14 | virtual ~Prototype() {} 15 | virtual Prototype* Clone() = 0; 16 | }; 17 | } // namespace prototype 18 | #endif // PROTOTYPE_PROTOTYPE_H_ 19 | -------------------------------------------------------------------------------- /src/prototype/prototype_manager.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "prototype/prototype_manager.h" 9 | 10 | namespace prototype { 11 | PrototypeManager::PrototypeManager() { 12 | prototype_map_ = new map; 13 | } 14 | 15 | PrototypeManager::~PrototypeManager() { 16 | delete prototype_map_; 17 | } 18 | 19 | void PrototypeManager::AddPrototype(Prototype *p, int index) { 20 | (*prototype_map_)[index] = p; 21 | } 22 | 23 | 24 | Prototype* PrototypeManager::GetPrototype(int index) { 25 | return prototype_map_->at(index)->Clone(); 26 | } 27 | } // namespace prototype 28 | -------------------------------------------------------------------------------- /src/prototype/prototype_manager.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef PROTOTYPE_PROTOTYPE_MANAGER_H_ 9 | #define PROTOTYPE_PROTOTYPE_MANAGER_H_ 10 | 11 | #include 12 | using std::map; 13 | #include "prototype/prototype.h" 14 | 15 | namespace prototype { 16 | class PrototypeManager { 17 | public: 18 | PrototypeManager(); 19 | virtual ~PrototypeManager(); 20 | void AddPrototype(Prototype *p, int index); 21 | Prototype* GetPrototype(int index); 22 | private: 23 | map *prototype_map_; 24 | }; 25 | } // namespace prototype 26 | #endif // PROTOTYPE_PROTOTYPE_MANAGER_H_ 27 | -------------------------------------------------------------------------------- /src/prototype/user_profile.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "prototype/user_profile.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace prototype { 14 | UserProfile::~UserProfile() { 15 | } 16 | 17 | void UserProfile::GetDatabaseInformation() { 18 | cout << "take 5 seconds to get database information" << endl; 19 | database_information_ = "Long database information"; 20 | } 21 | 22 | Prototype* UserProfile::Clone() { 23 | UserProfile *u = new UserProfile(); 24 | u->database_information_ = database_information_; 25 | return u; 26 | } 27 | 28 | void UserProfile::ShowInformation() { 29 | cout << "Showing " << database_information_ << endl; 30 | } 31 | } // namespace prototype 32 | -------------------------------------------------------------------------------- /src/prototype/user_profile.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef PROTOTYPE_USER_PROFILE_H_ 9 | #define PROTOTYPE_USER_PROFILE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "prototype/prototype.h" 14 | 15 | namespace prototype { 16 | class UserProfile : public Prototype { 17 | public: 18 | virtual ~UserProfile(); 19 | void GetDatabaseInformation(); 20 | virtual Prototype* Clone(); 21 | void ShowInformation(); 22 | private: 23 | string database_information_; 24 | }; 25 | } // namespace prototype 26 | #endif // PROTOTYPE_USER_PROFILE_H_ 27 | -------------------------------------------------------------------------------- /src/proxy/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_proxy STATIC 5 | proxy_bear.cc) 6 | 7 | install(TARGETS designpattern_proxy DESTINATION ${LIBRARY_OUTPUT_PATH}) 8 | 9 | add_executable(proxy_drink_test ./test/proxy_drink_test.cc) 10 | target_link_libraries(proxy_drink_test designpattern_proxy) 11 | add_test(proxy_drink_test ${EXECUTABLE_OUTPUT_PATH}/proxy_drink_test) 12 | -------------------------------------------------------------------------------- /src/proxy/bear.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef PROXY_BEAR_H_ 9 | #define PROXY_BEAR_H_ 10 | 11 | #include // NOLINT 12 | using std::cout; 13 | using std::endl; 14 | #include "proxy/wine.h" 15 | 16 | namespace proxy { 17 | class Bear : public Wine { 18 | public: 19 | virtual ~Bear() {} 20 | virtual void drink() { 21 | cout << "drink the bear" << endl; 22 | } 23 | }; 24 | } // namespace proxy 25 | #endif // PROXY_BEAR_H_ 26 | -------------------------------------------------------------------------------- /src/proxy/drinker.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef PROXY_DRINKER_H_ 9 | #define PROXY_DRINKER_H_ 10 | 11 | namespace proxy { 12 | class Drinker { 13 | public: 14 | explicit Drinker(int age) : age_(age) { 15 | } 16 | int age() { return age_; } 17 | void set_age(int age) { age_ = age; } 18 | private: 19 | int age_; 20 | }; 21 | } // namespace proxy 22 | #endif // PROXY_DRINKER_H_ 23 | -------------------------------------------------------------------------------- /src/proxy/proxy_bear.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "proxy/proxy_bear.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | #include "proxy/bear.h" 13 | 14 | namespace proxy { 15 | #define kDrinkAge 16 16 | ProxyBear::ProxyBear(Drinker *drinker) : drinker_(drinker) { 17 | real_bear_ = new Bear(); 18 | } 19 | 20 | ProxyBear::~ProxyBear() { 21 | delete real_bear_; 22 | delete drinker_; 23 | } 24 | 25 | void ProxyBear::drink() { 26 | if (drinker_->age() < kDrinkAge) { 27 | cout << "Sorry the drinker is too young to drink" << endl; 28 | } else { 29 | real_bear_->drink(); 30 | } 31 | } 32 | } // namespace proxy 33 | -------------------------------------------------------------------------------- /src/proxy/proxy_bear.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef PROXY_PROXY_BEAR_H_ 9 | #define PROXY_PROXY_BEAR_H_ 10 | 11 | #include "proxy/wine.h" 12 | #include "proxy/drinker.h" 13 | 14 | namespace proxy { 15 | class ProxyBear : public Wine { 16 | public: 17 | explicit ProxyBear(Drinker *drinker); 18 | virtual ~ProxyBear(); 19 | virtual void drink(); 20 | private: 21 | Wine *real_bear_; 22 | Drinker *drinker_; 23 | }; 24 | } // namespace proxy 25 | #endif // PROXY_PROXY_BEAR_H_ 26 | -------------------------------------------------------------------------------- /src/proxy/test/proxy_drink_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "proxy/wine.h" 9 | #include "proxy/proxy_bear.h" 10 | #include "proxy/drinker.h" 11 | using namespace proxy; // NOLINT 12 | 13 | int main(int argc, char *argv[]) { 14 | Wine *wine = new ProxyBear(new Drinker(15)); 15 | wine->drink(); 16 | delete wine; 17 | wine = new ProxyBear(new Drinker(25)); 18 | wine->drink(); 19 | delete wine; 20 | } 21 | -------------------------------------------------------------------------------- /src/proxy/wine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef PROXY_WINE_H_ 9 | #define PROXY_WINE_H_ 10 | 11 | namespace proxy { 12 | class Wine { 13 | public: 14 | virtual ~Wine() {} 15 | virtual void drink() = 0; 16 | }; 17 | } // namespace proxy 18 | #endif // PROXY_WINE_H_ 19 | -------------------------------------------------------------------------------- /src/singleton/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_singleton STATIC 5 | singleton.cc) 6 | 7 | install(TARGETS designpattern_singleton DESTINATION ${LIBRARY_OUTPUT_PATH}) 8 | 9 | add_executable(singleton_test ./test/singleton_test.cc) 10 | target_link_libraries(singleton_test designpattern_singleton) 11 | find_package(Threads) 12 | target_link_libraries(singleton_test ${CMAKE_THREAD_LIBS_INIT}) 13 | add_test(singleton_test ${EXECUTABLE_OUTPUT_PATH}/singleton_test) 14 | -------------------------------------------------------------------------------- /src/singleton/singleton.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "singleton/singleton.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace singleton { 14 | Singleton* Singleton::pInstance = NULL; 15 | 16 | Singleton::~Singleton() { 17 | } 18 | 19 | Singleton* Singleton::instance() { 20 | cout << "call the singleton instance" << endl; 21 | static Mutex mutex_(base::LINKER_INITIALIZED); 22 | MutexLock l(&mutex_); 23 | if (pInstance == NULL) { 24 | cout << "In the Lock" << endl; 25 | pInstance = new Singleton(); 26 | } 27 | return pInstance; 28 | } // release MutexLock (via MutexLock destructor) 29 | } // namespace singleton 30 | -------------------------------------------------------------------------------- /src/singleton/singleton.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | #ifndef SINGLETON_SINGLETON_H_ 8 | #define SINGLETON_SINGLETON_H_ 9 | 10 | #include "base/macros.h" 11 | #include "singleton/mutex.h" 12 | 13 | namespace singleton { 14 | class Singleton { 15 | public: 16 | Singleton() {} 17 | static Singleton* instance(); 18 | private: 19 | static Singleton* pInstance; 20 | virtual ~Singleton(); 21 | DISALLOW_COPY_AND_ASSIGN(Singleton); 22 | }; 23 | } // namespace singleton 24 | #endif // SINGLETON_SINGLETON_H_ 25 | -------------------------------------------------------------------------------- /src/singleton/test/singleton_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "singleton/singleton.h" 9 | using namespace singleton; // NOLINT 10 | 11 | int main(int argc, char *argv[]) { 12 | Singleton* const instance = Singleton::instance(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /src/state/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_state STATIC 5 | gumball_machine.cc 6 | no_quarter_state.cc 7 | sold_state.cc 8 | has_quarter_state.cc 9 | sold_out_state.cc) 10 | 11 | install(TARGETS designpattern_state DESTINATION ${LIBRARY_OUTPUT_PATH}) 12 | 13 | add_executable(gumball_machine_test ./test/gumball_machine_test.cc) 14 | target_link_libraries(gumball_machine_test designpattern_state) 15 | add_test(gumball_machine_test ${EXECUTABLE_OUTPUT_PATH}/gumball_machine_test) 16 | -------------------------------------------------------------------------------- /src/state/has_quarter_state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STATE_HAS_QUARTER_STATE_H_ 9 | #define STATE_HAS_QUARTER_STATE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "state/gumball_machine.h" 14 | #include "state/state.h" 15 | 16 | namespace state { 17 | class HasQuarterState : public State { 18 | public: 19 | explicit HasQuarterState(GumballMachine *gumball_machine); 20 | virtual ~HasQuarterState(); 21 | virtual void insertQuarter(); 22 | virtual void ejectQuarter(); 23 | virtual void turnCrank(); 24 | virtual void dispense(); 25 | virtual string toString(); 26 | private: 27 | GumballMachine *gumball_machine_; 28 | }; 29 | } // namespace state 30 | #endif // STATE_HAS_QUARTER_STATE_H_ 31 | -------------------------------------------------------------------------------- /src/state/no_quarter_state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STATE_NO_QUARTER_STAET_H_ 9 | #define STATE_NO_QUARTER_STAET_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "state/gumball_machine.h" 14 | #include "state/state.h" 15 | 16 | namespace state { 17 | class NoQuarterState : public State { 18 | public: 19 | explicit NoQuarterState(GumballMachine *gumball_machine); 20 | virtual ~NoQuarterState(); 21 | virtual void insertQuarter(); 22 | virtual void ejectQuarter(); 23 | virtual void turnCrank(); 24 | virtual void dispense(); 25 | virtual string toString(); 26 | private: 27 | GumballMachine *gumball_machine_; 28 | }; 29 | } // namespace state 30 | #endif // STATE_NO_QUARTER_STAET_H_ 31 | -------------------------------------------------------------------------------- /src/state/sold_out_state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STATE_SOLD_OUT_STATE_H_ 9 | #define STATE_SOLD_OUT_STATE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "state/gumball_machine.h" 14 | #include "state/state.h" 15 | 16 | namespace state { 17 | class SoldOutState : public State { 18 | public: 19 | explicit SoldOutState(GumballMachine *gumball_machine); 20 | virtual ~SoldOutState(); 21 | virtual void insertQuarter(); 22 | virtual void ejectQuarter(); 23 | virtual void turnCrank(); 24 | virtual void dispense(); 25 | virtual string toString(); 26 | private: 27 | GumballMachine *gumball_machine_; 28 | }; 29 | } // namespace state 30 | #endif // STATE_SOLD_OUT_STATE_H_ 31 | -------------------------------------------------------------------------------- /src/state/sold_state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STATE_SOLD_STATE_H_ 9 | #define STATE_SOLD_STATE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "state/gumball_machine.h" 14 | #include "state/state.h" 15 | 16 | namespace state { 17 | class SoldState : public State { 18 | public: 19 | explicit SoldState(GumballMachine *gumball_machine); 20 | virtual ~SoldState(); 21 | virtual void insertQuarter(); 22 | virtual void ejectQuarter(); 23 | virtual void turnCrank(); 24 | virtual void dispense(); 25 | virtual string toString(); 26 | private: 27 | GumballMachine *gumball_machine_; 28 | }; 29 | } // namespace state 30 | #endif // STATE_SOLD_STATE_H_ 31 | -------------------------------------------------------------------------------- /src/state/state.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STATE_STATE_H_ 9 | #define STATE_STATE_H_ 10 | 11 | #include 12 | using std::string; 13 | 14 | namespace state { 15 | class State { 16 | public: 17 | virtual ~State() {} 18 | virtual void insertQuarter() = 0; 19 | virtual void ejectQuarter() = 0; 20 | virtual void turnCrank() = 0; 21 | virtual void dispense() = 0; 22 | virtual string toString() = 0; 23 | }; 24 | } // namespace state 25 | #endif // STATE_STATE_H_ 26 | -------------------------------------------------------------------------------- /src/state/test/gumball_machine_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "state/gumball_machine.h" 12 | using namespace state; // NOLINT 13 | 14 | int main(int argc, char *argv[]) { 15 | GumballMachine *gumball_machine = new GumballMachine(5); 16 | cout << *gumball_machine << endl; 17 | gumball_machine->insertQuarter(); 18 | gumball_machine->turnCrank(); 19 | cout << *gumball_machine << endl; 20 | gumball_machine->insertQuarter(); 21 | gumball_machine->turnCrank(); 22 | gumball_machine->insertQuarter(); 23 | gumball_machine->turnCrank(); 24 | cout << *gumball_machine << endl; 25 | return 0; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /src/strategy/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_strategy STATIC 5 | decoy_duck.cc 6 | fly_rocketpowerd.cc 7 | model_duck.cc 8 | rubber_duck.cc 9 | duck.cc 10 | fly_with_wings.cc 11 | mute_quack.cc 12 | squeak.cc 13 | fly_noway.cc 14 | mallard_duck.cc 15 | quack.cc) 16 | install(TARGETS designpattern_strategy DESTINATION ${LIBRARY_OUTPUT_PATH}) 17 | 18 | add_executable(duck_test ./test/duck_test.cc) 19 | target_link_libraries(duck_test designpattern_strategy) 20 | add_test(duck_test ${EXECUTABLE_OUTPUT_PATH}/duck_test) 21 | -------------------------------------------------------------------------------- /src/strategy/decoy_duck.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/decoy_duck.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | #include "strategy/fly_noway.h" 13 | #include "strategy/mute_quack.h" 14 | 15 | namespace strategy { 16 | DecoyDuck::DecoyDuck() { 17 | fly_behavior_ = new FlyNoWay; 18 | quack_behavior_ = new MuteQuack; 19 | } 20 | 21 | DecoyDuck::~DecoyDuck() { 22 | } 23 | 24 | void DecoyDuck::display() { 25 | cout << "I'm a duck Decoy" << endl; 26 | } 27 | } // namespace strategy 28 | -------------------------------------------------------------------------------- /src/strategy/decoy_duck.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_DECOY_DUCK_H_ 9 | #define STRATEGY_DECOY_DUCK_H_ 10 | 11 | #include "base/macros.h" 12 | #include "strategy/duck.h" 13 | 14 | namespace strategy { 15 | class DecoyDuck : public Duck { 16 | public: 17 | DecoyDuck(); 18 | virtual ~DecoyDuck(); 19 | virtual void display(); 20 | private: 21 | DISALLOW_COPY_AND_ASSIGN(DecoyDuck); 22 | }; 23 | } // namespace strategy 24 | #endif // STRATEGY_DECOY_DUCK_H_ 25 | -------------------------------------------------------------------------------- /src/strategy/duck.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_DUCK_H_ 9 | #define STRATEGY_DUCK_H_ 10 | 11 | #include "strategy/fly_behavior.h" 12 | #include "strategy/quack_behavior.h" 13 | 14 | namespace strategy { 15 | 16 | class Duck { 17 | public: 18 | Duck(void); 19 | virtual ~Duck(); 20 | virtual void display() = 0; 21 | virtual void swim(); 22 | void set_fly_behavior(FlyBehavior *fly_behavior); 23 | void set_quack_behavior(QuackBehavior *quack_behavior); 24 | void performFly(); 25 | void performQuack(); 26 | protected: 27 | FlyBehavior *fly_behavior_; 28 | QuackBehavior *quack_behavior_; 29 | }; 30 | } // namespace strategy 31 | #endif // STRATEGY_DUCK_H_ 32 | -------------------------------------------------------------------------------- /src/strategy/fly_behavior.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_FLY_BEHAVIOR_H_ 9 | #define STRATEGY_FLY_BEHAVIOR_H_ 10 | 11 | namespace strategy { 12 | class FlyBehavior { 13 | public: 14 | virtual ~FlyBehavior() {} 15 | virtual void fly() = 0; 16 | }; 17 | } // namespace strategy 18 | #endif // STRATEGY_FLY_BEHAVIOR_H_ 19 | -------------------------------------------------------------------------------- /src/strategy/fly_noway.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/fly_noway.h" 9 | 10 | #include //NOLINT 11 | using std::cout; 12 | using std::endl; 13 | 14 | namespace strategy { 15 | void FlyNoWay::fly() { 16 | cout << "I can't fly" << endl; 17 | } 18 | } // namespace strategy 19 | -------------------------------------------------------------------------------- /src/strategy/fly_noway.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_FLY_NOWAY_H_ 9 | #define STRATEGY_FLY_NOWAY_H_ 10 | 11 | #include "strategy/fly_behavior.h" 12 | 13 | namespace strategy { 14 | class FlyNoWay : public FlyBehavior { 15 | public: 16 | FlyNoWay() {} 17 | virtual ~FlyNoWay() {} 18 | virtual void fly(); 19 | }; 20 | } // namespace strategy 21 | #endif // STRATEGY_FLY_NOWAY_H_ 22 | -------------------------------------------------------------------------------- /src/strategy/fly_rocketpowerd.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/fly_rocketpowered.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace strategy { 14 | void FlyRocketPowered::fly() { 15 | cout << "I'm flying with a rocket" << endl; 16 | } 17 | } // namespace strategy 18 | -------------------------------------------------------------------------------- /src/strategy/fly_rocketpowered.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_FLY_ROCKETPOWERED_H_ 9 | #define STRATEGY_FLY_ROCKETPOWERED_H_ 10 | 11 | #include "strategy/fly_behavior.h" 12 | 13 | namespace strategy { 14 | class FlyRocketPowered : public FlyBehavior { 15 | public: 16 | virtual ~FlyRocketPowered() {} 17 | virtual void fly(); 18 | }; 19 | } // namespace strategy 20 | #endif // STRATEGY_FLY_ROCKETPOWERED_H_ 21 | -------------------------------------------------------------------------------- /src/strategy/fly_with_wings.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/fly_with_wings.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace strategy { 14 | 15 | void FlyWithWings::fly() { 16 | cout << "I'm flying!" << endl; 17 | } 18 | } // namespace strategy 19 | -------------------------------------------------------------------------------- /src/strategy/fly_with_wings.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_FLY_WITH_WINGS_H_ 9 | #define STRATEGY_FLY_WITH_WINGS_H_ 10 | 11 | #include "strategy/fly_behavior.h" 12 | 13 | namespace strategy { 14 | class FlyWithWings : public FlyBehavior { 15 | public: 16 | virtual ~FlyWithWings() {} 17 | virtual void fly(); 18 | }; 19 | } // namespace strategy 20 | #endif // STRATEGY_FLY_WITH_WINGS_H_ 21 | -------------------------------------------------------------------------------- /src/strategy/mallard_duck.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/mallard_duck.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | #include "strategy/fly_with_wings.h" 13 | #include "strategy/quack.h" 14 | 15 | namespace strategy { 16 | MallardDuck::MallardDuck() { 17 | fly_behavior_ = new FlyWithWings(); 18 | quack_behavior_ = new Quack(); 19 | } 20 | 21 | MallardDuck::~MallardDuck() { 22 | } 23 | 24 | void MallardDuck::display() { 25 | cout << "I'm a real Mallard duck" << endl; 26 | } 27 | } // namespace strategy 28 | -------------------------------------------------------------------------------- /src/strategy/mallard_duck.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_MALLARD_DUCK_H_ 9 | #define STRATEGY_MALLARD_DUCK_H_ 10 | 11 | #include "base/macros.h" 12 | #include "strategy/duck.h" 13 | 14 | namespace strategy { 15 | class MallardDuck : public Duck { 16 | public: 17 | MallardDuck(); 18 | virtual ~MallardDuck(); 19 | virtual void display(); 20 | private: 21 | DISALLOW_COPY_AND_ASSIGN(MallardDuck); 22 | }; 23 | } // namespace strategy 24 | #endif // STRATEGY_MALLARD_DUCK_H_ 25 | -------------------------------------------------------------------------------- /src/strategy/model_duck.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/model_duck.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | #include "strategy/fly_noway.h" 13 | #include "strategy/quack.h" 14 | 15 | namespace strategy { 16 | ModelDuck::ModelDuck() { 17 | fly_behavior_ = new FlyNoWay(); 18 | quack_behavior_ = new Quack(); 19 | } 20 | 21 | ModelDuck::~ModelDuck() { 22 | } 23 | 24 | void ModelDuck::display() { 25 | cout << "I'm a model duck" << endl; 26 | } 27 | } // namespace strategy 28 | -------------------------------------------------------------------------------- /src/strategy/model_duck.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_MODEL_DUCK_H_ 9 | #define STRATEGY_MODEL_DUCK_H_ 10 | 11 | #include "base/macros.h" 12 | #include "strategy/duck.h" 13 | 14 | namespace strategy { 15 | class ModelDuck : public Duck { 16 | public: 17 | ModelDuck(); 18 | virtual ~ModelDuck(); 19 | virtual void display(); 20 | private: 21 | DISALLOW_COPY_AND_ASSIGN(ModelDuck); 22 | }; 23 | } // namespace strategy 24 | #endif // STRATEGY_MODEL_DUCK_H_ 25 | -------------------------------------------------------------------------------- /src/strategy/mute_quack.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/mute_quack.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace strategy { 14 | void MuteQuack::quack() { 15 | cout << "<< Slience >>" << endl; 16 | } 17 | } // namespace strategy 18 | -------------------------------------------------------------------------------- /src/strategy/mute_quack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_MUTE_QUACK_H_ 9 | #define STRATEGY_MUTE_QUACK_H_ 10 | 11 | #include "strategy/quack_behavior.h" 12 | 13 | namespace strategy { 14 | class MuteQuack : public QuackBehavior { 15 | public: 16 | virtual ~MuteQuack() {} 17 | virtual void quack(); 18 | }; 19 | } // namespacd strategy 20 | #endif // STRATEGY_MUTE_QUACK_H_ 21 | -------------------------------------------------------------------------------- /src/strategy/quack.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/quack.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace strategy { 14 | void Quack::quack() { 15 | cout << "Quack" << endl; 16 | } 17 | } // strategy 18 | -------------------------------------------------------------------------------- /src/strategy/quack.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_QUACK_H_ 9 | #define STRATEGY_QUACK_H_ 10 | #include "strategy/quack_behavior.h" 11 | 12 | namespace strategy { 13 | class Quack : public QuackBehavior { 14 | public: 15 | virtual ~Quack() {} 16 | virtual void quack(); 17 | }; 18 | } // namespace strategy 19 | #endif // STRATEGY_QUACK_H_ 20 | -------------------------------------------------------------------------------- /src/strategy/quack_behavior.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_QUACK_BEHAVIOR_H_ 9 | #define STRATEGY_QUACK_BEHAVIOR_H_ 10 | 11 | namespace strategy { 12 | 13 | class QuackBehavior { 14 | public: 15 | virtual ~QuackBehavior() {} 16 | virtual void quack() = 0; 17 | }; 18 | } // namespace strategy 19 | #endif // STRATEGY_QUACK_BEHAVIOR_H_ 20 | -------------------------------------------------------------------------------- /src/strategy/rubber_duck.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/rubber_duck.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | #include "strategy/fly_noway.h" 13 | #include "strategy/squeak.h" 14 | 15 | namespace strategy { 16 | RubberDuck::RubberDuck() { 17 | fly_behavior_ = new FlyNoWay(); 18 | quack_behavior_ = new Squeak(); 19 | } 20 | 21 | RubberDuck::~RubberDuck() { 22 | } 23 | 24 | void RubberDuck::display() { 25 | cout << "I'm a rubber duckie" << endl; 26 | } 27 | } // namespace strategy 28 | -------------------------------------------------------------------------------- /src/strategy/rubber_duck.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_RUBBER_DUCK_H_ 9 | #define STRATEGY_RUBBER_DUCK_H_ 10 | 11 | #include "base/macros.h" 12 | #include "strategy/duck.h" 13 | 14 | namespace strategy { 15 | class RubberDuck : public Duck { 16 | public: 17 | RubberDuck(); 18 | virtual ~RubberDuck(); 19 | virtual void display(); 20 | private: 21 | DISALLOW_COPY_AND_ASSIGN(RubberDuck); 22 | }; 23 | } // namespace strategy 24 | #endif // STRATEGY_RUBBER_DUCK_H_ 25 | -------------------------------------------------------------------------------- /src/strategy/squeak.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "strategy/squeak.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace strategy { 14 | void Squeak::quack() { 15 | cout << "Squeak" << endl; 16 | } 17 | } // namespace strategy 18 | -------------------------------------------------------------------------------- /src/strategy/squeak.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef STRATEGY_SQUEAK_H_ 9 | #define STRATEGY_SQUEAK_H_ 10 | 11 | #include "strategy/quack_behavior.h" 12 | 13 | namespace strategy { 14 | class Squeak : public QuackBehavior { 15 | public: 16 | virtual ~Squeak() {} 17 | virtual void quack(); 18 | }; 19 | } // namespacd strategy 20 | #endif // STRATEGY_SQUEAK_H_ 21 | -------------------------------------------------------------------------------- /src/strategy/test/duck_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include // NOLINT 9 | using std::cout; 10 | using std::endl; 11 | #include "strategy/mallard_duck.h" 12 | #include "strategy/rubber_duck.h" 13 | #include "strategy/decoy_duck.h" 14 | #include "strategy/model_duck.h" 15 | #include "strategy/fly_rocketpowered.h" 16 | using namespace strategy; // NOLINT 17 | int main(int argc, char *argv[]) { 18 | MallardDuck *mallard = new MallardDuck; 19 | RubberDuck *rubberDuckie = new RubberDuck; 20 | DecoyDuck *decoy = new DecoyDuck; 21 | ModelDuck *model = new ModelDuck; 22 | 23 | mallard->performQuack(); 24 | rubberDuckie->performQuack(); 25 | decoy->performQuack(); 26 | 27 | model->performFly(); 28 | model->set_fly_behavior(new FlyRocketPowered); 29 | model->performFly(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /src/templatemethod/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_templatemethod STATIC 5 | caffeine_beverage.cc 6 | coffee.cc 7 | tea.cc 8 | caffeine_beverage_with_hook.cc 9 | coffee_with_hook.cc 10 | tea_with_hook.cc) 11 | 12 | install(TARGETS designpattern_templatemethod DESTINATION ${LIBRARY_OUTPUT_PATH}) 13 | 14 | add_executable(beverage_test ./test/beverage_test.cc) 15 | target_link_libraries(beverage_test designpattern_templatemethod) 16 | add_test(beverage_test ${EXECUTABLE_OUTPUT_PATH}/beverage_test) 17 | -------------------------------------------------------------------------------- /src/templatemethod/caffeine_beverage.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "templatemethod/caffeine_beverage.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace templatemethod { 14 | CaffeineBeverage::~CaffeineBeverage() { 15 | } 16 | 17 | void CaffeineBeverage::prepareRecipe() { 18 | boilWater(); 19 | brew(); 20 | pourInCup(); 21 | addCondiments(); 22 | } 23 | 24 | void CaffeineBeverage::boilWater() { 25 | cout << "Boiling water" << endl; 26 | } 27 | 28 | void CaffeineBeverage::pourInCup() { 29 | cout << "Pouring into cup" << endl; 30 | } 31 | } // namespace templatemethod 32 | -------------------------------------------------------------------------------- /src/templatemethod/caffeine_beverage.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef TEMPLATEMETHOD_CAFFEINE_BEVERAGE_H_ 9 | #define TEMPLATEMETHOD_CAFFEINE_BEVERAGE_H_ 10 | 11 | namespace templatemethod { 12 | class CaffeineBeverage { 13 | public: 14 | virtual ~CaffeineBeverage(); 15 | void prepareRecipe(); 16 | protected: 17 | void boilWater(); 18 | void pourInCup(); 19 | virtual void brew() = 0; 20 | virtual void addCondiments() = 0; 21 | }; 22 | } // namespace templatemethod 23 | #endif // TEMPLATEMETHOD_CAFFEINE_BEVERAGE_H_ 24 | -------------------------------------------------------------------------------- /src/templatemethod/caffeine_beverage_with_hook.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "templatemethod/caffeine_beverage_with_hook.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace templatemethod { 14 | CaffeineBeverageWithHook::~CaffeineBeverageWithHook() { 15 | } 16 | 17 | void CaffeineBeverageWithHook::prepareRecipe() { 18 | boilWater(); 19 | brew(); 20 | pourInCup(); 21 | if (customerWantsCondiments()) { 22 | addCondiments(); 23 | } 24 | } 25 | 26 | void CaffeineBeverageWithHook::boilWater() { 27 | cout << "Boiling water" << endl; 28 | } 29 | 30 | void CaffeineBeverageWithHook::pourInCup() { 31 | cout << "Pouring into cup" << endl; 32 | } 33 | 34 | bool CaffeineBeverageWithHook::customerWantsCondiments() { 35 | return true; 36 | } 37 | } // namespace templatemethod 38 | -------------------------------------------------------------------------------- /src/templatemethod/caffeine_beverage_with_hook.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef TEMPLATEMETHOD_CAFFEINE_BEVERAGE_WITH_HOOK_H_ 9 | #define TEMPLATEMETHOD_CAFFEINE_BEVERAGE_WITH_HOOK_H_ 10 | 11 | namespace templatemethod { 12 | class CaffeineBeverageWithHook { 13 | public: 14 | virtual ~CaffeineBeverageWithHook(); 15 | void prepareRecipe(); 16 | protected: 17 | void boilWater(); 18 | void pourInCup(); 19 | virtual bool customerWantsCondiments(); 20 | virtual void brew() = 0; 21 | virtual void addCondiments() = 0; 22 | }; 23 | } // namespace templatemethod 24 | #endif // TEMPLATEMETHOD_CAFFEINE_BEVERAGE_WITH_HOOK_H_ 25 | -------------------------------------------------------------------------------- /src/templatemethod/coffee.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "templatemethod/coffee.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace templatemethod { 14 | Coffee::~Coffee() { 15 | } 16 | 17 | void Coffee::brew() { 18 | cout << "Dripping Coffee through filter" << endl; 19 | } 20 | 21 | void Coffee::addCondiments() { 22 | cout << "Adding Sugar and Milk" << endl; 23 | } 24 | } // namespace templatemethod 25 | -------------------------------------------------------------------------------- /src/templatemethod/coffee.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef TEMPLATEMETHOD_COFFEE_H_ 9 | #define TEMPLATEMETHOD_COFFEE_H_ 10 | 11 | #include "templatemethod/caffeine_beverage.h" 12 | 13 | namespace templatemethod { 14 | class Coffee : public CaffeineBeverage { 15 | public: 16 | virtual ~Coffee(); 17 | protected: 18 | virtual void brew(); 19 | virtual void addCondiments(); 20 | }; 21 | } // namespace templatemethod 22 | #endif // TEMPLATEMETHOD_COFFEE_H_ 23 | -------------------------------------------------------------------------------- /src/templatemethod/coffee_with_hook.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef TEMPLATEMETHOD_COFFEE_WITH_HOOK_H_ 9 | #define TEMPLATEMETHOD_COFFEE_WITH_HOOK_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "templatemethod/caffeine_beverage_with_hook.h" 14 | 15 | namespace templatemethod { 16 | class CoffeeWithHook : public CaffeineBeverageWithHook { 17 | public: 18 | virtual ~CoffeeWithHook(); 19 | protected: 20 | virtual void brew(); 21 | virtual void addCondiments(); 22 | virtual bool customerWantsCondiments(); 23 | private: 24 | string getUserInput(); 25 | }; 26 | } // namespace templatemethod 27 | #endif // TEMPLATEMETHOD_COFFEE_WITH_HOOK_H_ 28 | -------------------------------------------------------------------------------- /src/templatemethod/tea.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "templatemethod/tea.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace templatemethod { 14 | Tea::~Tea() { 15 | } 16 | 17 | void Tea::brew() { 18 | cout << "Steeping the tea" << endl; 19 | } 20 | 21 | void Tea::addCondiments() { 22 | cout << "Adding Lemon" << endl; 23 | } 24 | } // namespace templatemethod 25 | -------------------------------------------------------------------------------- /src/templatemethod/tea.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef TEMPLATEMETHOD_TEA_H_ 9 | #define TEMPLATEMETHOD_TEA_H_ 10 | 11 | #include "templatemethod/caffeine_beverage.h" 12 | 13 | namespace templatemethod { 14 | class Tea : public CaffeineBeverage { 15 | public: 16 | virtual ~Tea(); 17 | protected: 18 | virtual void brew(); 19 | virtual void addCondiments(); 20 | }; 21 | } // namespace templatemethod 22 | #endif // TEMPLATEMETHOD_TEA_H_ 23 | -------------------------------------------------------------------------------- /src/templatemethod/tea_with_hook.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef TEMPLATEMETHOD_TEA_WITH_HOOK_H_ 9 | #define TEMPLATEMETHOD_TEA_WITH_HOOK_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "templatemethod/caffeine_beverage_with_hook.h" 14 | 15 | namespace templatemethod { 16 | class TeaWithHook : public CaffeineBeverageWithHook { 17 | public: 18 | virtual ~TeaWithHook(); 19 | protected: 20 | virtual void brew(); 21 | virtual void addCondiments(); 22 | virtual bool customerWantsCondiments(); 23 | private: 24 | string getUserInput(); 25 | }; 26 | } // namespace templatemethod 27 | #endif // TEMPLATEMETHOD_TEA_WITH_HOOK_H_ 28 | -------------------------------------------------------------------------------- /src/visitor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # just for testing 2 | INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/test) 3 | 4 | add_library(designpattern_visitor STATIC 5 | body.cc 6 | car_element_print_visitor.cc 7 | wheel.cc 8 | car_element_do_visitor.cc 9 | engine.cc 10 | car.cc) 11 | 12 | install(TARGETS designpattern_visitor DESTINATION ${LIBRARY_OUTPUT_PATH}) 13 | 14 | add_executable(visitor_car_test ./test/visitor_car_test.cc) 15 | target_link_libraries(visitor_car_test designpattern_visitor) 16 | add_test(visitor_car_test ${EXECUTABLE_OUTPUT_PATH}/visitor_car_test) 17 | -------------------------------------------------------------------------------- /src/visitor/body.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "visitor/body.h" 9 | 10 | namespace visitor { 11 | Body::~Body() { 12 | } 13 | 14 | void Body::accept(CarElementVisitor *visitor) { 15 | visitor->visit(this); 16 | } 17 | } // namespace visitor 18 | -------------------------------------------------------------------------------- /src/visitor/body.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef VISITOR_BODY_H_ 9 | #define VISITOR_BODY_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "visitor/car_element.h" 14 | 15 | namespace visitor { 16 | class Body : public CarElement { 17 | public: 18 | virtual ~Body(); 19 | virtual void accept(CarElementVisitor *visitor); 20 | }; 21 | } // namespace visitor 22 | #endif // VISITOR_BODY_H_ 23 | -------------------------------------------------------------------------------- /src/visitor/car.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef VISITOR_CAR_H_ 9 | #define VISITOR_CAR_H_ 10 | 11 | #include 12 | using std::vector; 13 | #include "visitor/car_element.h" 14 | #include "visitor/wheel.h" 15 | #include "visitor/engine.h" 16 | #include "visitor/body.h" 17 | #include "visitor/car_element_do_visitor.h" 18 | 19 | namespace visitor { 20 | class Car { 21 | public: 22 | Car(); 23 | virtual ~Car(); 24 | void visit_car(CarElementVisitor *visitor); 25 | private: 26 | void visit_elements(CarElementVisitor *visitor); 27 | vector elements_array_; 28 | }; 29 | } // namespace visitor 30 | #endif // VISITOR_CAR_H_ 31 | -------------------------------------------------------------------------------- /src/visitor/car_element.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef VISITOR_CAR_ELEMENT_H_ 9 | #define VISITOR_CAR_ELEMENT_H_ 10 | 11 | #include "visitor/car_element_visitor.h" 12 | 13 | namespace visitor { 14 | class CarElement { 15 | public: 16 | virtual ~CarElement() {} 17 | virtual void accept(CarElementVisitor *visitor) = 0; 18 | }; 19 | } // namespace visitor 20 | #endif // VISITOR_CAR_ELEMENT_H_ 21 | -------------------------------------------------------------------------------- /src/visitor/car_element_do_visitor.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "visitor/car_element_do_visitor.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace visitor { 14 | CarElementDoVisitor::~CarElementDoVisitor() { 15 | } 16 | 17 | void CarElementDoVisitor::visit(Wheel *wheel) { 18 | cout << "Kicking my " << wheel->name() << " wheel" << endl; 19 | } 20 | 21 | void CarElementDoVisitor::visit(Engine *engine) { 22 | cout << "Starting my engine" << endl; 23 | } 24 | 25 | void CarElementDoVisitor::visit(Body *body) { 26 | cout << "Moving my body" << endl; 27 | } 28 | } // namespace visitor 29 | -------------------------------------------------------------------------------- /src/visitor/car_element_do_visitor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef VISITOR_CAR_ELEMENT_DO_VISITOR_H_ 9 | #define VISITOR_CAR_ELEMENT_DO_VISITOR_H_ 10 | 11 | #include "visitor/car_element_visitor.h" 12 | #include "visitor/wheel.h" 13 | #include "visitor/engine.h" 14 | #include "visitor/body.h" 15 | 16 | namespace visitor { 17 | class CarElementDoVisitor : public CarElementVisitor { 18 | public: 19 | virtual ~CarElementDoVisitor(); 20 | virtual void visit(Wheel *wheel); 21 | virtual void visit(Engine *engine); 22 | virtual void visit(Body *body); 23 | }; 24 | } // namespace visitor 25 | #endif // VISITOR_CAR_ELEMENT_DOVISITOR_H_ 26 | -------------------------------------------------------------------------------- /src/visitor/car_element_print_visitor.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "visitor/car_element_print_visitor.h" 9 | #include // NOLINT 10 | using std::cout; 11 | using std::endl; 12 | 13 | namespace visitor { 14 | CarElementPrintVisitor::~CarElementPrintVisitor() { 15 | } 16 | 17 | void CarElementPrintVisitor::visit(Wheel *wheel) { 18 | cout << "Visiting " << wheel->name() << " wheel" << endl; 19 | } 20 | 21 | void CarElementPrintVisitor::visit(Engine *engine) { 22 | cout << "Visiting engine" << endl; 23 | } 24 | 25 | void CarElementPrintVisitor::visit(Body *body) { 26 | cout << "Visiting body" << endl; 27 | } 28 | } // namespace visitor 29 | -------------------------------------------------------------------------------- /src/visitor/car_element_print_visitor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef VISITOR_CAR_ELEMENT_PRINT_VISITOR_H_ 9 | #define VISITOR_CAR_ELEMENT_PRINT_VISITOR_H_ 10 | 11 | #include "visitor/car_element_visitor.h" 12 | #include "visitor/wheel.h" 13 | #include "visitor/engine.h" 14 | #include "visitor/body.h" 15 | 16 | namespace visitor { 17 | class CarElementPrintVisitor : public CarElementVisitor { 18 | public: 19 | virtual ~CarElementPrintVisitor(); 20 | virtual void visit(Wheel *wheel); 21 | virtual void visit(Engine *engine); 22 | virtual void visit(Body *body); 23 | }; 24 | } // namespace visitor 25 | #endif // VISITOR_CAR_ELEMENT_PRINT_VISITOR_H_ 26 | -------------------------------------------------------------------------------- /src/visitor/car_element_visitor.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef VISITOR_CAR_ELEMENT_VISITOR_H_ 9 | #define VISITOR_CAR_ELEMENT_VISITOR_H_ 10 | 11 | namespace visitor { 12 | class Wheel; 13 | class Engine; 14 | class Body; 15 | 16 | class CarElementVisitor { 17 | public: 18 | virtual ~CarElementVisitor() {} 19 | virtual void visit(Wheel *wheel) = 0; 20 | virtual void visit(Engine *engine) = 0; 21 | virtual void visit(Body *body) = 0; 22 | }; 23 | } // namespace visitor 24 | #endif // VISITOR_CAR_ELEMENT_VISITOR_H_ 25 | -------------------------------------------------------------------------------- /src/visitor/engine.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "visitor/engine.h" 9 | 10 | namespace visitor { 11 | Engine::~Engine() { 12 | } 13 | 14 | void Engine::accept(CarElementVisitor *visitor) { 15 | visitor->visit(this); 16 | } 17 | } // namespace visitor 18 | -------------------------------------------------------------------------------- /src/visitor/engine.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef VISITOR_ENGINE_H_ 9 | #define VISITOR_ENGINE_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "visitor/car_element.h" 14 | 15 | namespace visitor { 16 | class CarElementVisitor; 17 | 18 | class Engine : public CarElement { 19 | public: 20 | virtual ~Engine(); 21 | virtual void accept(CarElementVisitor *visitor); 22 | }; 23 | } // namespace visitor 24 | #endif // VISITOR_ENGINE_H_ 25 | -------------------------------------------------------------------------------- /src/visitor/test/visitor_car_test.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include 9 | using std::vector; 10 | #include "visitor/car.h" 11 | #include "visitor/car_element_print_visitor.h" 12 | #include "visitor/car_element_do_visitor.h" 13 | using namespace visitor; 14 | 15 | int main(int argc, char *argv[]) { 16 | CarElementVisitor *car_element_print_visitor = new CarElementPrintVisitor(); 17 | CarElementVisitor *car_element_do_visitor = new CarElementDoVisitor(); 18 | Car *car = new Car(); 19 | car->visit_car(car_element_print_visitor); 20 | car->visit_car(car_element_do_visitor); 21 | delete car; 22 | delete car_element_print_visitor; 23 | delete car_element_do_visitor; 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /src/visitor/wheel.cc: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #include "visitor/wheel.h" 9 | 10 | namespace visitor { 11 | Wheel::Wheel(string name) : name_(name) { 12 | } 13 | 14 | Wheel::~Wheel() { 15 | } 16 | 17 | void Wheel::accept(CarElementVisitor *visitor) { 18 | visitor->visit(this); 19 | } 20 | } // namespace visitor 21 | -------------------------------------------------------------------------------- /src/visitor/wheel.h: -------------------------------------------------------------------------------- 1 | /* 2 | * \copyright Copyright 2014 3 | * \license @{ 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * @} 6 | */ 7 | 8 | #ifndef VISITOR_WHEEL_H_ 9 | #define VISITOR_WHEEL_H_ 10 | 11 | #include 12 | using std::string; 13 | #include "visitor/car_element.h" 14 | 15 | namespace visitor { 16 | class Wheel : public CarElement { 17 | public: 18 | explicit Wheel(string name); 19 | virtual ~Wheel(); 20 | virtual void accept(CarElementVisitor *visitor); 21 | string name() const {return name_;} 22 | private: 23 | string name_; 24 | }; 25 | } // namespace visitor 26 | #endif // VISITOR_WHEEL_H_ 27 | --------------------------------------------------------------------------------