├── cp02 └── WeatherData │ ├── Makefile │ ├── Observer.h │ ├── DisplayElement.h │ ├── Subject.h │ ├── main.cpp │ ├── TemperatureOnlyDisplay.h │ ├── CurrentConditionsDisplay.h │ └── WeatherData.h ├── README.md ├── cp05 └── Singleton │ ├── Singleton.cpp │ ├── Makefile │ ├── main.cpp │ └── Singleton.h ├── cp04 ├── AbstractFactory │ ├── Calm.h │ ├── Cheese.h │ ├── Sauce.h │ ├── Veggle.h │ ├── NewYorkCalm.h │ ├── ShenZhenCalm.h │ ├── ShenZhenSauce.h │ ├── NewYorkCheese.h │ ├── NewYorkVeggle.h │ ├── ShenZhenCheese.h │ ├── ShenZhenVeggle.h │ ├── NewYorkSauce.h │ ├── Makefile │ ├── Pizza.h │ ├── main.cpp │ ├── PizzaIngredientFactory.h │ ├── PizzaStore.h │ ├── NewYorkPizzaStore.h │ ├── ShenZhenPizzaStore.h │ ├── NewYorkIngredientFactory.h │ ├── ShenZhenIngredientFactory.h │ ├── CalmPizza.h │ ├── CheesePizza.h │ └── VegglePizza.h ├── Factory │ ├── Makefile │ ├── Pizza.h │ ├── main.cpp │ ├── PizzaStore.h │ ├── CalmPizza.h │ ├── CheesePizza.h │ ├── VegglePizza.h │ ├── NewYorkVeeglePizza.h │ ├── NewYorkCalmPizza.h │ ├── NewYorkCheesePizza.h │ ├── ShenZhenCalmPizza.h │ ├── ShenZhenVeeglePizza.h │ ├── ShenZhenCheesePizza.h │ ├── NewYorkPizzaStore.h │ └── ShenZhenPizzaStore.h └── SimpleFactory │ ├── Makefile │ ├── Pizza.h │ ├── main.cpp │ ├── CalmPizza.h │ ├── VegglePizza.h │ ├── CheesePizza.h │ ├── PizzaStore.h │ └── SimplePizzaFactory.h ├── cp01 └── Duck │ ├── FlyBehavior.h │ ├── QuackBehavior.h │ ├── DuckMallard.h │ ├── DuckRedhead.h │ ├── Makefile │ ├── FlyNoFly.h │ ├── Quack.h │ ├── main.cpp │ ├── QuackSqueak.h │ ├── FlyWithWings.h │ └── Duck.h ├── cp09 ├── Iterator │ ├── Menu.h │ ├── Makefile │ ├── main.cpp │ ├── MenuItem.h │ ├── Breakfast.h │ ├── Diner.h │ └── Iterator.h └── Composite │ ├── Menu.h │ ├── Makefile │ ├── main.cpp │ ├── MenuItem.h │ └── SubMenu.h ├── cp11 └── VirtualProxy │ ├── Common.h │ ├── main.cpp │ ├── Makefile │ ├── Image.h │ ├── Proxy.h │ └── RealImage.h ├── cp03 └── Starbuzz │ ├── BeverageDecorator.h │ ├── CondimentDecorator.h │ ├── Makefile │ ├── Beverage.h │ ├── BeverageBeer.h │ ├── BeverageCoffee.h │ ├── CondimentWhip.h │ ├── CondimentMocha.h │ └── main.cpp ├── cp08 └── TemplateMethod │ ├── main.cpp │ ├── Makefile │ ├── Tea.h │ ├── Coffee.h │ └── CaffeineBeverage.h ├── cp07 ├── Adapter │ ├── main.cpp │ ├── Makefile │ ├── TurkeyAdapter.h │ ├── Duck.h │ └── Turkey.h └── Facade │ ├── Makefile │ ├── main.cpp │ ├── Screen.h │ ├── DvdPlayer.h │ └── HomeTheaterFacade.h ├── cp06 └── RemoteControl │ ├── Makefile │ ├── Light.h │ ├── Stereo.h │ ├── main.cpp │ └── Command.h ├── .gitignore ├── cp10 └── State │ ├── Makefile │ ├── Process.cpp │ ├── State.h │ ├── main.cpp │ ├── Process.h │ └── ConcreteStates.h └── LICENSE /cp02/WeatherData/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | g++ -Wall main.cpp 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Head First设计模式的前11章的主要模式的实现,因为不会Java,所有全部是用C++实现的。 2 | 之前尝试过看GOF的《设计模式》,发现实在是太难了,后来找到这本书,写得非常好,所有模式都用最通俗易懂的语言来描述,而且翻译也是一级棒。 3 | -------------------------------------------------------------------------------- /cp05/Singleton/Singleton.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | 3 | namespace single 4 | { 5 | 6 | Singleton * Singleton::instance = NULL; 7 | } 8 | 9 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/Calm.h: -------------------------------------------------------------------------------- 1 | #ifndef CALM_H 2 | #define CALM_H 3 | 4 | namespace pizza 5 | { 6 | 7 | class Calm 8 | { 9 | public: 10 | Calm(){} 11 | ~Calm(){} 12 | }; 13 | } 14 | 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/Cheese.h: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_H 2 | #define CHEESE_H 3 | 4 | namespace pizza 5 | { 6 | 7 | class Cheese 8 | { 9 | public: 10 | Cheese(){} 11 | ~Cheese(){} 12 | }; 13 | } 14 | 15 | #endif 16 | 17 | 18 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/Sauce.h: -------------------------------------------------------------------------------- 1 | #ifndef SAUCE_H 2 | #define SAUCE_H 3 | 4 | namespace pizza 5 | { 6 | 7 | class Sauce 8 | { 9 | public: 10 | Sauce(){} 11 | ~Sauce(){} 12 | }; 13 | } 14 | 15 | #endif 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /cp01/Duck/FlyBehavior.h: -------------------------------------------------------------------------------- 1 | #ifndef __FLYBEHAVIOR_H__ 2 | #define __FLYBEHAVIOR_H__ 3 | 4 | namespace duck 5 | { 6 | 7 | class FlyBehavior 8 | { 9 | public: 10 | virtual void fly() = 0; 11 | }; 12 | 13 | } 14 | 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp02/WeatherData/Observer.h: -------------------------------------------------------------------------------- 1 | #ifndef __OBSERVER_H__ 2 | #define __OBSERVER_H__ 3 | 4 | namespace weather 5 | { 6 | 7 | class Observer 8 | { 9 | public: 10 | virtual void update() = 0; 11 | }; 12 | } 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/Veggle.h: -------------------------------------------------------------------------------- 1 | #ifndef VEGGLE_H 2 | #define VEGGLE_H 3 | 4 | namespace pizza 5 | { 6 | 7 | class Veggle 8 | { 9 | public: 10 | Veggle(){} 11 | ~Veggle(){} 12 | }; 13 | } 14 | 15 | #endif 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /cp01/Duck/QuackBehavior.h: -------------------------------------------------------------------------------- 1 | #ifndef __QUACKBEHAVIOR_H__ 2 | #define __QUACKBEHAVIOR_H__ 3 | namespace duck 4 | { 5 | 6 | class QuackBehavior 7 | { 8 | public: 9 | virtual void quack() = 0; 10 | }; 11 | 12 | } 13 | 14 | #endif 15 | 16 | -------------------------------------------------------------------------------- /cp02/WeatherData/DisplayElement.h: -------------------------------------------------------------------------------- 1 | #ifndef __DISPLAYELEMENT_H__ 2 | #define __DISPLAYELEMENT_H__ 3 | 4 | namespace weather 5 | { 6 | 7 | class DisplayElement 8 | { 9 | public: 10 | virtual void display() = 0; 11 | }; 12 | } 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/NewYorkCalm.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWYORK_CALM_H 2 | #define NEWYORK_CALM_H 3 | 4 | #include "Calm.h" 5 | 6 | namespace pizza 7 | { 8 | 9 | class NewYorkCalm: public Calm 10 | { 11 | public: 12 | NewYorkCalm(){} 13 | ~NewYorkCalm(){} 14 | }; 15 | } 16 | #endif 17 | -------------------------------------------------------------------------------- /cp09/Iterator/Menu.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_H 2 | #define MENU_H 3 | 4 | #include "Iterator.h" 5 | 6 | namespace menu 7 | { 8 | 9 | class Menu 10 | { 11 | public: 12 | Menu(){} 13 | ~Menu(){} 14 | virtual Iterator* createIterator() = 0; 15 | }; 16 | } 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/ShenZhenCalm.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_CALM_H 2 | #define SHENZHEN_CALM_H 3 | 4 | #include "Calm.h" 5 | 6 | namespace pizza 7 | { 8 | class ShenZhenCalm: public Calm 9 | { 10 | public: 11 | ShenZhenCalm(){} 12 | ~ShenZhenCalm(){} 13 | }; 14 | } 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/ShenZhenSauce.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_SAUCE_H 2 | #define SHENZHEN_SAUCE_H 3 | 4 | #include "Sauce.h" 5 | 6 | namespace pizza 7 | { 8 | class ShenZhenSauce: public Sauce 9 | { 10 | public: 11 | ShenZhenSauce(){} 12 | ~ShenZhenSauce(){} 13 | }; 14 | } 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/NewYorkCheese.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWYORK_CHEESE_H 2 | #define NEWYORK_CHEESE_H 3 | 4 | #include "Cheese.h" 5 | 6 | namespace pizza 7 | { 8 | class NewYorkCheese: public Cheese 9 | { 10 | public: 11 | NewYorkCheese(){} 12 | ~NewYorkCheese(){} 13 | }; 14 | } 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/NewYorkVeggle.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWYORK_VEGGLE_H 2 | #define NEWYORK_VEGGLE_H 3 | 4 | #include "Veggle.h" 5 | 6 | namespace pizza 7 | { 8 | class NewYorkVeggle: public Veggle 9 | { 10 | public: 11 | NewYorkVeggle(){} 12 | ~NewYorkVeggle(){} 13 | }; 14 | } 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp11/VirtualProxy/Common.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMON_H 2 | #define COMMON_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using std::cout; 10 | using std::endl; 11 | using std::string; 12 | using std::vector; 13 | using std::list; 14 | 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/ShenZhenCheese.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_CHEESE_H 2 | #define SHENZHEN_CHEESE_H 3 | 4 | #include "Cheese.h" 5 | 6 | namespace pizza 7 | { 8 | class ShenZhenCheese: public Cheese 9 | { 10 | public: 11 | ShenZhenCheese(){} 12 | ~ShenZhenCheese(){} 13 | }; 14 | } 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/ShenZhenVeggle.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_VEGGLE_H 2 | #define SHENZHEN_VEGGLE_H 3 | 4 | #include "Veggle.h" 5 | 6 | namespace pizza 7 | { 8 | class ShenZhenVeggle: public Veggle 9 | { 10 | public: 11 | ShenZhenVeggle(){} 12 | ~ShenZhenVeggle(){} 13 | }; 14 | } 15 | #endif 16 | 17 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/NewYorkSauce.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWYORK_SAUCE_H 2 | #define NEWYORK_SAUCE_H 3 | 4 | #include "Sauce.h" 5 | #include "Sauce.h" 6 | 7 | namespace pizza 8 | { 9 | class NewYorkSauce: public Sauce 10 | { 11 | public: 12 | NewYorkSauce(){} 13 | ~NewYorkSauce(){} 14 | }; 15 | } 16 | #endif 17 | 18 | -------------------------------------------------------------------------------- /cp01/Duck/DuckMallard.h: -------------------------------------------------------------------------------- 1 | #ifndef __DUCKKREDHEAD_H__ 2 | #define __DUCKKREDHEAD_H__ 3 | #include "Duck.h" 4 | 5 | namespace duck 6 | { 7 | 8 | class DuckRedhead: public Duck 9 | { 10 | public: 11 | DuckRedhead(): Duck(new FlyWithWings(), new QuackSqueak()) 12 | { 13 | } 14 | }; 15 | 16 | } 17 | 18 | #endif 19 | 20 | -------------------------------------------------------------------------------- /cp01/Duck/DuckRedhead.h: -------------------------------------------------------------------------------- 1 | #ifndef __QUACKMALLARD_H__ 2 | #define __QUACKMALLARD_H__ 3 | 4 | #include "Duck.h" 5 | 6 | namespace duck 7 | { 8 | 9 | class DuckMallard: public Duck 10 | { 11 | public: 12 | DuckMallard(): Duck(new FlyNoFly(), new Quack()) 13 | { 14 | } 15 | }; 16 | 17 | } 18 | 19 | #endif 20 | 21 | 22 | -------------------------------------------------------------------------------- /cp03/Starbuzz/BeverageDecorator.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDIMENT_DECORATOR_H 2 | #define __CONDIMENT_DECORATOR_H 3 | 4 | namespace starbuzz 5 | { 6 | 7 | class CondimentDecorator: public Beverage 8 | { 9 | public: 10 | ComdimentDecorator() 11 | { 12 | } 13 | ~ComdimentDecorator() 14 | { 15 | } 16 | }; 17 | } 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /cp03/Starbuzz/CondimentDecorator.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDIMENT_DECORATOR_H 2 | #define __CONDIMENT_DECORATOR_H 3 | 4 | namespace starbuzz 5 | { 6 | 7 | class CondimentDecorator: public Beverage 8 | { 9 | public: 10 | CondimentDecorator() 11 | { 12 | } 13 | ~CondimentDecorator() 14 | { 15 | } 16 | }; 17 | } 18 | 19 | #endif 20 | 21 | -------------------------------------------------------------------------------- /cp01/Duck/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | 9 | .PHONY: all 10 | all: $(target) 11 | 12 | $(target): $(objs) 13 | $(CXX) $(LDFLAGS) -o _main $^ 14 | 15 | $(objs):%.o:%.cpp 16 | $(CXX) $(CFLAGS) -c -o $@ $< 17 | 18 | clean: 19 | rm -f _main *.o 20 | 21 | -------------------------------------------------------------------------------- /cp04/Factory/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | 9 | .PHONY: all 10 | all: $(target) 11 | 12 | $(target): $(objs) 13 | $(CXX) $(LDFLAGS) -o _main $^ 14 | 15 | $(objs):%.o:%.cpp 16 | $(CXX) $(CFLAGS) -c -o $@ $< 17 | 18 | clean: 19 | rm -f _main *.o 20 | 21 | -------------------------------------------------------------------------------- /cp01/Duck/FlyNoFly.h: -------------------------------------------------------------------------------- 1 | #ifndef __FLYNOFLY_H__ 2 | #define __FLYNOFLY_H__ 3 | 4 | #include 5 | #include "FlyBehavior.h" 6 | 7 | 8 | namespace duck 9 | { 10 | class FlyNoFly: public FlyBehavior 11 | { 12 | public: 13 | void fly() 14 | { 15 | std::cout << "I can't fly" << std::endl; 16 | } 17 | }; 18 | } 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /cp01/Duck/Quack.h: -------------------------------------------------------------------------------- 1 | #ifndef __QUACK_H__ 2 | #define __QUACK_H__ 3 | #include "iostream" 4 | #include "QuackBehavior.h" 5 | 6 | namespace duck 7 | { 8 | 9 | class Quack: public QuackBehavior 10 | { 11 | public: 12 | void quack() 13 | { 14 | std::cout << "This is my quack" << std::endl; 15 | } 16 | }; 17 | 18 | } 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /cp04/Factory/Pizza.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_H 2 | #define PIZZA_H 3 | 4 | namespace pizza 5 | { 6 | 7 | class Pizza 8 | { 9 | public: 10 | Pizza(){} 11 | virtual ~Pizza(){} 12 | 13 | virtual void prepare() = 0; 14 | virtual void bake() = 0; 15 | virtual void cut() = 0; 16 | virtual void box() = 0; 17 | }; 18 | } 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /cp04/SimpleFactory/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | 9 | .PHONY: all 10 | all: $(target) 11 | 12 | $(target): $(objs) 13 | $(CXX) $(LDFLAGS) -o _main $^ 14 | 15 | $(objs):%.o:%.cpp 16 | $(CXX) $(CFLAGS) -c -o $@ $< 17 | 18 | clean: 19 | rm -f _main *.o 20 | 21 | -------------------------------------------------------------------------------- /cp01/Duck/main.cpp: -------------------------------------------------------------------------------- 1 | #include "DuckMallard.h" 2 | #include "DuckRedhead.h" 3 | 4 | 5 | int main() 6 | { 7 | duck::Duck *d1 = new duck::DuckMallard(); 8 | duck::Duck *d2 = new duck::DuckRedhead(); 9 | 10 | d1->performFly(); 11 | d1->performQuack(); 12 | 13 | d2->performFly(); 14 | d2->performQuack(); 15 | 16 | return 0; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | 9 | .PHONY: all 10 | all: $(target) 11 | 12 | $(target): $(objs) 13 | $(CXX) $(LDFLAGS) -o _main $^ 14 | 15 | $(objs):%.o:%.cpp 16 | $(CXX) $(CFLAGS) -c -o $@ $< 17 | 18 | clean: 19 | rm -f _main *.o 20 | 21 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/Pizza.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_H 2 | #define PIZZA_H 3 | 4 | namespace pizza 5 | { 6 | 7 | class Pizza 8 | { 9 | public: 10 | Pizza(){} 11 | virtual ~Pizza(){} 12 | 13 | virtual void prepare() = 0; 14 | virtual void bake() = 0; 15 | virtual void cut() = 0; 16 | virtual void box() = 0; 17 | }; 18 | } 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /cp04/SimpleFactory/Pizza.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_H 2 | #define PIZZA_H 3 | 4 | namespace pizza 5 | { 6 | 7 | class Pizza 8 | { 9 | public: 10 | Pizza(){} 11 | virtual ~Pizza(){} 12 | 13 | virtual void prepare() = 0; 14 | virtual void bake() = 0; 15 | virtual void cut() = 0; 16 | virtual void box() = 0; 17 | }; 18 | } 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /cp05/Singleton/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp Singleton.cpp 7 | objs = $(srcs:.cpp=.o) 8 | 9 | .PHONY: all 10 | all: $(target) 11 | 12 | $(target): $(objs) 13 | $(CXX) $(LDFLAGS) -o _main $^ 14 | 15 | $(objs):%.o:%.cpp 16 | $(CXX) $(CFLAGS) -c -o $@ $< 17 | 18 | clean: 19 | rm -f _main *.o 20 | 21 | -------------------------------------------------------------------------------- /cp05/Singleton/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Singleton.h" 4 | 5 | using namespace single; 6 | using std::printf; 7 | 8 | int main() 9 | { 10 | Singleton *p1 = Singleton::getInstance(); 11 | Singleton *p2 = Singleton::getInstance(); 12 | 13 | printf("address of p1: %p\naddress of p2: %p\n", p1, p2); 14 | return 0; 15 | } 16 | 17 | -------------------------------------------------------------------------------- /cp08/TemplateMethod/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Tea.h" 2 | #include "Coffee.h" 3 | 4 | using namespace caffeine; 5 | 6 | int main() 7 | { 8 | CaffeineBeverage *cb = new Coffee(); 9 | 10 | cb->prepareRecipe(); 11 | cout << endl; 12 | delete cb; 13 | cb = new Tea(); 14 | cb->prepareRecipe(); 15 | 16 | delete cb; 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /cp01/Duck/QuackSqueak.h: -------------------------------------------------------------------------------- 1 | #ifndef __QUACKSQUEAK_H__ 2 | #define __QUACKSQUEAK_H__ 3 | #include 4 | #include "QuackBehavior.h" 5 | 6 | namespace duck 7 | { 8 | 9 | class QuackSqueak: public QuackBehavior 10 | { 11 | public: 12 | void quack() 13 | { 14 | std::cout << "This is my squeak" << std::endl; 15 | } 16 | }; 17 | 18 | } 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /cp02/WeatherData/Subject.h: -------------------------------------------------------------------------------- 1 | #ifndef __SUBJECT_H__ 2 | #define __SUBJECT_H__ 3 | 4 | #include "Observer.h" 5 | 6 | namespace weather 7 | { 8 | 9 | class Subject 10 | { 11 | public: 12 | virtual void registerObserver(Observer *ob) = 0; 13 | virtual void removeObserver(Observer *ob) = 0; 14 | virtual void notifyObservers() = 0; 15 | }; 16 | } 17 | 18 | #endif 19 | -------------------------------------------------------------------------------- /cp07/Adapter/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Duck.h" 2 | #include "Turkey.h" 3 | #include "TurkeyAdapter.h" 4 | 5 | using namespace adapter; 6 | 7 | int main() 8 | { 9 | Turkey *wt = new WildTurkey("James"); 10 | TurkeyAdapter *ta = new TurkeyAdapter(wt); 11 | 12 | ta->fly(); 13 | ta->quack(); 14 | 15 | delete ta; 16 | delete wt; 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /cp01/Duck/FlyWithWings.h: -------------------------------------------------------------------------------- 1 | #ifndef __FLYWITHWINGS_H__ 2 | #define __FLYWITHWINGS_H__ 3 | 4 | #include 5 | #include "FlyBehavior.h" 6 | 7 | 8 | namespace duck 9 | { 10 | class FlyWithWings: public FlyBehavior 11 | { 12 | public: 13 | void fly() 14 | { 15 | std::cout << "I am flying with wings" << std::endl; 16 | } 17 | }; 18 | } 19 | 20 | #endif 21 | 22 | -------------------------------------------------------------------------------- /cp03/Starbuzz/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | 9 | .PHONY: all 10 | all: $(target) 11 | 12 | $(target): $(objs) FORCE 13 | $(CXX) $(LDFLAGS) -o _main $(srcs) 14 | 15 | $(objs):%.o:%.cpp 16 | $(CXX) $(CFLAGS) -c -o $@ $< 17 | 18 | .PHONY: FROCE 19 | FORCE: 20 | 21 | clean: 22 | rm -f _main *.o 23 | 24 | -------------------------------------------------------------------------------- /cp11/VirtualProxy/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Common.h" 2 | #include "RealImage.h" 3 | #include "Proxy.h" 4 | 5 | using namespace virtualProxy; 6 | 7 | 8 | int main() 9 | { 10 | Image *ri = new Proxy("/tmp/a.jpg"); 11 | 12 | // if not using this object, never create the RealImage object 13 | //ri->draw(); 14 | //ri->refresh(); 15 | 16 | delete ri; 17 | 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /cp07/Adapter/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | headers = $(wildcard *.h) 9 | 10 | 11 | .PHONY: all 12 | all: $(target) 13 | 14 | $(target): $(objs) $(headers) 15 | $(CXX) $(LDFLAGS) -o _main $(objs) 16 | 17 | $(objs):%.o:%.cpp 18 | $(CXX) $(CFLAGS) -c -o $@ $< 19 | 20 | clean: 21 | rm -f _main *.o 22 | 23 | -------------------------------------------------------------------------------- /cp07/Facade/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | headers = $(wildcard *.h) 9 | 10 | 11 | .PHONY: all 12 | all: $(target) 13 | 14 | $(target): $(objs) $(headers) 15 | $(CXX) $(LDFLAGS) -o _main $(objs) 16 | 17 | $(objs):%.o:%.cpp 18 | $(CXX) $(CFLAGS) -c -o $@ $< 19 | 20 | clean: 21 | rm -f _main *.o 22 | 23 | -------------------------------------------------------------------------------- /cp09/Composite/Menu.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_H 2 | #define MENU_H 3 | 4 | namespace menu 5 | { 6 | 7 | class Menu 8 | { 9 | public: 10 | class UnSupportedOperation{}; 11 | Menu(){} 12 | virtual ~Menu(){} 13 | 14 | virtual void addMenuItem(Menu *m) 15 | { 16 | throw UnSupportedOperation(); 17 | } 18 | virtual void print(int indent = 0) = 0; 19 | }; 20 | } 21 | 22 | #endif 23 | 24 | -------------------------------------------------------------------------------- /cp03/Starbuzz/Beverage.h: -------------------------------------------------------------------------------- 1 | #ifndef __BEVERAGE_H 2 | #define __BEVERAGE_H 3 | #include 4 | 5 | namespace starbuzz 6 | { 7 | using std::string; 8 | 9 | class Beverage 10 | { 11 | public: 12 | Beverage() 13 | { 14 | } 15 | virtual ~Beverage() 16 | { 17 | } 18 | 19 | virtual int getCost() = 0; 20 | virtual string getDescription() = 0; 21 | }; 22 | } 23 | 24 | #endif 25 | 26 | -------------------------------------------------------------------------------- /cp06/RemoteControl/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | headers = $(wildcard *.h) 9 | 10 | 11 | .PHONY: all 12 | all: $(target) 13 | 14 | $(target): $(objs) $(headers) 15 | $(CXX) $(LDFLAGS) -o _main $(objs) 16 | 17 | $(objs):%.o:%.cpp 18 | $(CXX) $(CFLAGS) -c -o $@ $< 19 | 20 | clean: 21 | rm -f _main *.o 22 | 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | *.obj 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Compiled Dynamic libraries 12 | *.so 13 | *.dylib 14 | *.dll 15 | 16 | # Fortran module files 17 | *.mod 18 | 19 | # Compiled Static libraries 20 | *.lai 21 | *.la 22 | *.a 23 | *.lib 24 | 25 | # Executables 26 | *.exe 27 | *.out 28 | *.app 29 | 30 | _main 31 | a.out 32 | *.swp 33 | vs 34 | 35 | -------------------------------------------------------------------------------- /cp07/Facade/main.cpp: -------------------------------------------------------------------------------- 1 | #include "HomeTheaterFacade.h" 2 | 3 | using namespace facade; 4 | 5 | 6 | int main() 7 | { 8 | Screen *screen = new Screen(); 9 | DvdPlayer *dp = new DvdPlayer(); 10 | HomeTheaterFacade *htf = new HomeTheaterFacade(dp, screen); 11 | 12 | htf->watchMovie(); 13 | htf->endMovie(); 14 | 15 | delete htf; 16 | delete dp; 17 | delete screen; 18 | 19 | return 0; 20 | } 21 | 22 | -------------------------------------------------------------------------------- /cp08/TemplateMethod/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | headers = $(wildcard *.h) 9 | 10 | 11 | .PHONY: all 12 | all: $(target) 13 | 14 | $(target): $(objs) $(headers) FORCE 15 | $(CXX) $(LDFLAGS) -o _main $(objs) 16 | 17 | $(objs):%.o:%.cpp 18 | $(CXX) $(CFLAGS) -c -o $@ $< 19 | 20 | .PHONY: FORCE 21 | FORCE: 22 | 23 | clean: 24 | rm -f _main *.o 25 | 26 | -------------------------------------------------------------------------------- /cp04/Factory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "PizzaStore.h" 4 | #include "NewYorkPizzaStore.h" 5 | #include "ShenZhenPizzaStore.h" 6 | 7 | 8 | using std::cout; 9 | using std::endl; 10 | 11 | int main() 12 | { 13 | pizza::PizzaStore *ps = new pizza::NewYorkPizzaStore(); 14 | ps->orderPizza("Cheese"); 15 | cout << endl; 16 | ps = new pizza::ShenZhenPizzaStore(); 17 | ps->orderPizza("Calm"); 18 | return 0; 19 | } 20 | 21 | -------------------------------------------------------------------------------- /cp09/Composite/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | headers = $(wildcard *.h) 9 | 10 | 11 | .PHONY: all 12 | all: $(target) 13 | 14 | $(target): $(objs) $(headers) FORCE 15 | $(CXX) $(LDFLAGS) -o _main $(objs) 16 | -rm -f *.o 17 | 18 | $(objs):%.o:%.cpp 19 | $(CXX) $(CFLAGS) -c -o $@ $< 20 | 21 | .PHONY: FORCE 22 | FORCE: 23 | 24 | clean: 25 | rm -f _main *.o 26 | 27 | -------------------------------------------------------------------------------- /cp09/Iterator/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | headers = $(wildcard *.h) 9 | 10 | 11 | .PHONY: all 12 | all: $(target) 13 | 14 | $(target): $(objs) $(headers) FORCE 15 | $(CXX) $(LDFLAGS) -o _main $(objs) 16 | -rm -f *.o 17 | 18 | $(objs):%.o:%.cpp 19 | $(CXX) $(CFLAGS) -c -o $@ $< 20 | 21 | .PHONY: FORCE 22 | FORCE: 23 | 24 | clean: 25 | rm -f _main *.o 26 | 27 | -------------------------------------------------------------------------------- /cp07/Facade/Screen.h: -------------------------------------------------------------------------------- 1 | #ifndef SCREEN_H 2 | #define SCREEN_H 3 | 4 | #include 5 | 6 | namespace facade 7 | { 8 | using std::cout; 9 | using std::endl; 10 | 11 | class Screen 12 | { 13 | public: 14 | Screen(){} 15 | ~Screen(){} 16 | 17 | void down() 18 | { 19 | cout << "Screen is down" << endl; 20 | } 21 | void up() 22 | { 23 | cout << "Screen is up" << endl; 24 | } 25 | }; 26 | } 27 | 28 | #endif 29 | 30 | 31 | -------------------------------------------------------------------------------- /cp11/VirtualProxy/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp 7 | objs = $(srcs:.cpp=.o) 8 | headers = $(wildcard *.h) 9 | 10 | 11 | .PHONY: all 12 | all: $(target) 13 | 14 | $(target): $(objs) $(headers) FORCE 15 | $(CXX) $(LDFLAGS) -o _main $(objs) 16 | -rm -f *.o 17 | 18 | $(objs):%.o:%.cpp 19 | $(CXX) $(CFLAGS) -c -o $@ $< 20 | 21 | .PHONY: FORCE 22 | FORCE: 23 | 24 | clean: 25 | rm -f _main *.o 26 | 27 | -------------------------------------------------------------------------------- /cp10/State/Makefile: -------------------------------------------------------------------------------- 1 | CXX = g++ 2 | CFLAGS = -Wall 3 | LDFLAGS = 4 | 5 | target = _main 6 | srcs = main.cpp Process.cpp 7 | objs = $(srcs:.cpp=.o) 8 | headers = $(wildcard *.h) 9 | 10 | 11 | .PHONY: all 12 | all: $(target) 13 | 14 | $(target): $(objs) $(headers) FORCE 15 | $(CXX) $(LDFLAGS) -o _main $(objs) 16 | -rm -f *.o 17 | 18 | $(objs):%.o:%.cpp 19 | $(CXX) $(CFLAGS) -c -o $@ $< 20 | 21 | .PHONY: FORCE 22 | FORCE: 23 | 24 | clean: 25 | rm -f _main *.o 26 | 27 | -------------------------------------------------------------------------------- /cp07/Facade/DvdPlayer.h: -------------------------------------------------------------------------------- 1 | #ifndef DVD_PLAYER_H 2 | #define DVD_PLAYER_H 3 | 4 | #include 5 | 6 | namespace facade 7 | { 8 | using std::cout; 9 | using std::endl; 10 | 11 | class DvdPlayer 12 | { 13 | public: 14 | DvdPlayer(){} 15 | ~DvdPlayer(){} 16 | 17 | void on() 18 | { 19 | cout << "Dvd player is on" << endl; 20 | } 21 | void off() 22 | { 23 | cout << "Dvd player is off" << endl; 24 | } 25 | }; 26 | } 27 | 28 | #endif 29 | 30 | -------------------------------------------------------------------------------- /cp05/Singleton/Singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef SINGLETON_H 2 | #define SINGLETON_H 3 | 4 | #include 5 | 6 | namespace single 7 | { 8 | 9 | class Singleton 10 | { 11 | public: 12 | static Singleton *getInstance() 13 | { 14 | if (instance == NULL) 15 | { 16 | instance = new Singleton(); 17 | } 18 | 19 | return instance; 20 | } 21 | private: 22 | Singleton(){} 23 | static Singleton *instance; 24 | }; 25 | } 26 | #endif 27 | 28 | 29 | -------------------------------------------------------------------------------- /cp04/SimpleFactory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "PizzaStore.h" 4 | #include "SimplePizzaFactory.h" 5 | 6 | 7 | using std::cout; 8 | using std::endl; 9 | 10 | int main() 11 | { 12 | pizza::SimplePizzaFactory *spf = new pizza::SimplePizzaFactory(); 13 | pizza::PizzaStore ps = pizza::PizzaStore(spf); 14 | ps.orderPizza("Cheese"); 15 | cout << endl; 16 | ps.orderPizza("Veggle"); 17 | cout << endl; 18 | ps.orderPizza("Calm"); 19 | 20 | return 0; 21 | } 22 | 23 | -------------------------------------------------------------------------------- /cp07/Adapter/TurkeyAdapter.h: -------------------------------------------------------------------------------- 1 | #ifndef TURKEY_ADAPTER_H 2 | #define TURKEY_ADAPTER_H 3 | 4 | #include "Duck.h" 5 | #include "Turkey.h" 6 | 7 | namespace adapter 8 | { 9 | 10 | class TurkeyAdapter: public Duck 11 | { 12 | public: 13 | TurkeyAdapter(Turkey *t): turkey(t) 14 | {} 15 | ~TurkeyAdapter(){} 16 | void quack() 17 | { 18 | turkey->gobble(); 19 | } 20 | void fly() 21 | { 22 | turkey->fly(); 23 | } 24 | private: 25 | Turkey *turkey; 26 | }; 27 | } 28 | 29 | #endif 30 | 31 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "NewYorkPizzaStore.h" 4 | #include "ShenZhenPizzaStore.h" 5 | #include "NewYorkIngredientFactory.h" 6 | #include "ShenZhenIngredientFactory.h" 7 | 8 | 9 | using std::cout; 10 | using std::endl; 11 | 12 | int main() 13 | { 14 | pizza::PizzaStore *ps = new pizza::NewYorkPizzaStore(); 15 | ps->orderPizza("Cheese"); 16 | cout << endl; 17 | delete ps; 18 | ps = new pizza::ShenZhenPizzaStore(); 19 | ps->orderPizza("Calm"); 20 | delete ps; 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /cp10/State/Process.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include "Process.h" 5 | #include "ConcreteStates.h" 6 | 7 | 8 | namespace state 9 | { 10 | using std::string; 11 | using std::cout; 12 | using std::endl; 13 | 14 | Process::Process(string n): name(n) 15 | { 16 | cout << "Creating: " << name << endl << endl; 17 | readyState = new ReadyState(this, "Ready"); 18 | runningState = new RunningState(this, "Running"); 19 | blockedState = new BlockedState(this, "Blocked"); 20 | 21 | curState = readyState; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /cp10/State/State.h: -------------------------------------------------------------------------------- 1 | #ifndef STATE_H 2 | #define STATE_H 3 | 4 | #include 5 | 6 | namespace state 7 | { 8 | using std::string; 9 | 10 | class State 11 | { 12 | public: 13 | State(string n): name(n) 14 | {} 15 | virtual ~State(){} 16 | 17 | string getName() 18 | { 19 | return name; 20 | } 21 | 22 | virtual void resumedByCpu() = 0; 23 | virtual void timeSlotEnd() = 0; 24 | virtual void needIO() = 0; 25 | virtual void finishIO() = 0; 26 | private: 27 | string name; 28 | }; 29 | } 30 | #endif 31 | 32 | -------------------------------------------------------------------------------- /cp06/RemoteControl/Light.h: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_H 2 | #define LIGHT_H 3 | 4 | #include 5 | #include 6 | 7 | namespace control 8 | { 9 | using std::string; 10 | using std::cout; 11 | using std::endl; 12 | 13 | class Light 14 | { 15 | public: 16 | Light(string n): name(n) 17 | {} 18 | ~Light(){} 19 | void on() 20 | { 21 | cout << name << " is on" << endl; 22 | } 23 | void off() 24 | { 25 | cout << name << " is off" << endl; 26 | } 27 | private: 28 | string name; 29 | }; 30 | } 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /cp09/Iterator/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Iterator.h" 2 | #include "Breakfast.h" 3 | #include "Diner.h" 4 | 5 | using namespace menu; 6 | int main() 7 | { 8 | Diner *d = new Diner(); 9 | Iterator *it = d->createIterator(); 10 | Breakfast *b = new Breakfast(); 11 | Iterator *bit = b->createIterator(); 12 | 13 | while (it->hasNext()) 14 | { 15 | (it->next())->print(); 16 | } 17 | while (bit->hasNext()) 18 | { 19 | (bit->next())->print(); 20 | } 21 | 22 | delete it; 23 | delete d; 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /cp06/RemoteControl/Stereo.h: -------------------------------------------------------------------------------- 1 | #ifndef STEREO_H 2 | #define STEREO_H 3 | 4 | #include 5 | #include 6 | 7 | namespace control 8 | { 9 | using std::string; 10 | using std::cout; 11 | using std::endl; 12 | 13 | class Stereo 14 | { 15 | public: 16 | Stereo(string n): name(n) 17 | {} 18 | ~Stereo(){} 19 | void on() 20 | { 21 | cout << name << " is on" << endl; 22 | } 23 | void off() 24 | { 25 | cout << name << " is off" << endl; 26 | } 27 | private: 28 | string name; 29 | }; 30 | } 31 | #endif 32 | 33 | -------------------------------------------------------------------------------- /cp03/Starbuzz/BeverageBeer.h: -------------------------------------------------------------------------------- 1 | #ifndef __BEVERAG_BEER_H 2 | #define __BEVERAG_BEER_H 3 | #include "Beverage.h" 4 | 5 | namespace starbuzz 6 | { 7 | 8 | class BeverageBeer 9 | { 10 | public: 11 | BeverageBeer(string str, int p) : description(str), cost(p) 12 | { 13 | } 14 | ~BeverageBeer() 15 | { 16 | } 17 | 18 | int getCost() 19 | { 20 | return cost; 21 | } 22 | string getDescription() 23 | { 24 | return description; 25 | } 26 | private: 27 | string description; 28 | int cost; 29 | }; 30 | } 31 | 32 | #endif 33 | 34 | -------------------------------------------------------------------------------- /cp02/WeatherData/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Subject.h" 4 | #include "Observer.h" 5 | 6 | #include "WeatherData.h" 7 | #include "CurrentConditionsDisplay.h" 8 | #include "TemperatureOnlyDisplay.h" 9 | 10 | int main() 11 | { 12 | weather::WeatherData wd; 13 | weather::CurrentConditionsDisplay ccd = weather::CurrentConditionsDisplay(&wd); 14 | weather::TemperatureOnlyDisplay tod = weather::TemperatureOnlyDisplay(&wd); 15 | 16 | 17 | wd.setMeasurement(3, 2, 1); 18 | wd.removeObserver(&tod); 19 | wd.setMeasurement(4, 5, 6); 20 | 21 | return 0; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /cp11/VirtualProxy/Image.h: -------------------------------------------------------------------------------- 1 | #ifndef IMAGE_H 2 | #define IMAGE_H 3 | 4 | #include "Common.h" 5 | 6 | 7 | 8 | namespace virtualProxy 9 | { 10 | 11 | class ImageHandle 12 | { 13 | public: 14 | ImageHandle(string p): path(p) 15 | { 16 | path = "\"" + path + "\""; 17 | } 18 | 19 | string getPath() 20 | { 21 | return path; 22 | } 23 | private: 24 | string path; 25 | }; 26 | 27 | class Image 28 | { 29 | public: 30 | virtual ~Image(){} 31 | 32 | virtual void draw() = 0; 33 | virtual void refresh() = 0; 34 | }; 35 | } 36 | 37 | #endif 38 | 39 | -------------------------------------------------------------------------------- /cp08/TemplateMethod/Tea.h: -------------------------------------------------------------------------------- 1 | #ifndef TEA_H 2 | #define TEA_H 3 | 4 | #include 5 | 6 | #include "CaffeineBeverage.h" 7 | 8 | namespace caffeine 9 | { 10 | using std::cout; 11 | using std::endl; 12 | 13 | class Tea: public CaffeineBeverage 14 | { 15 | public: 16 | Tea() 17 | { 18 | cout << "Making tea..." << endl; 19 | } 20 | ~Tea(){} 21 | 22 | void brew() 23 | { 24 | cout << "Brew tea..." << endl; 25 | } 26 | void addCondiments() 27 | { 28 | cout << "Add lemon..." << endl; 29 | } 30 | }; 31 | } 32 | 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /cp09/Composite/main.cpp: -------------------------------------------------------------------------------- 1 | #include "MenuItem.h" 2 | #include "SubMenu.h" 3 | 4 | using namespace menu; 5 | 6 | int main() 7 | { 8 | Menu *m = new SubMenu("Diner"); 9 | 10 | m->addMenuItem(new MenuItem("Meat", 12)); 11 | m->addMenuItem(new MenuItem("Fish", 10)); 12 | m->addMenuItem(new MenuItem("Steak", 9)); 13 | 14 | Menu *mm = new SubMenu("Desert"); 15 | mm->addMenuItem(new MenuItem("Icecream", 9)); 16 | mm->addMenuItem(new MenuItem("Cookie", 3)); 17 | 18 | m->addMenuItem(mm); 19 | 20 | m->print(); 21 | 22 | delete m; 23 | 24 | return 0; 25 | } 26 | 27 | -------------------------------------------------------------------------------- /cp03/Starbuzz/BeverageCoffee.h: -------------------------------------------------------------------------------- 1 | #ifndef __BEVERAG_COFFEE_H 2 | #define __BEVERAG_COFFEE_H 3 | #include "Beverage.h" 4 | 5 | namespace starbuzz 6 | { 7 | 8 | class BeverageCoffee: public Beverage 9 | { 10 | public: 11 | BeverageCoffee(string str, int p) : description(str), cost(p) 12 | { 13 | } 14 | ~BeverageCoffee() 15 | { 16 | } 17 | 18 | int getCost() 19 | { 20 | return cost; 21 | } 22 | string getDescription() 23 | { 24 | return description; 25 | } 26 | private: 27 | string description; 28 | int cost; 29 | }; 30 | } 31 | 32 | #endif 33 | 34 | 35 | -------------------------------------------------------------------------------- /cp08/TemplateMethod/Coffee.h: -------------------------------------------------------------------------------- 1 | #ifndef COFFEE_H 2 | #define COFFEE_H 3 | 4 | #include 5 | 6 | #include "CaffeineBeverage.h" 7 | 8 | namespace caffeine 9 | { 10 | using std::cout; 11 | using std::endl; 12 | 13 | class Coffee: public CaffeineBeverage 14 | { 15 | public: 16 | Coffee() 17 | { 18 | cout << "Making coffee..." << endl; 19 | } 20 | ~Coffee(){} 21 | 22 | void brew() 23 | { 24 | cout << "Brew coffee..." << endl; 25 | } 26 | void addCondiments() 27 | { 28 | cout << "Add sugar..." << endl; 29 | } 30 | }; 31 | } 32 | 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/PizzaIngredientFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_INGREDIENT_FACTORY_H 2 | #define PIZZA_INGREDIENT_FACTORY_H 3 | 4 | #include 5 | 6 | #include "Calm.h" 7 | #include "Cheese.h" 8 | #include "Sauce.h" 9 | #include "Veggle.h" 10 | 11 | 12 | namespace pizza 13 | { 14 | 15 | class PizzaIngredientFactory 16 | { 17 | public: 18 | PizzaIngredientFactory(){} 19 | virtual ~PizzaIngredientFactory(){} 20 | 21 | virtual Calm *createCalm() = 0; 22 | virtual Cheese *createCheese() = 0; 23 | virtual Sauce *createSauce() = 0; 24 | virtual Veggle *createVeggle() = 0; 25 | }; 26 | } 27 | #endif 28 | 29 | -------------------------------------------------------------------------------- /cp07/Facade/HomeTheaterFacade.h: -------------------------------------------------------------------------------- 1 | #ifndef HOME_THEATER_FACADE_H 2 | #define HOME_THEATER_FACADE_H 3 | 4 | #include "DvdPlayer.h" 5 | #include "Screen.h" 6 | 7 | namespace facade 8 | { 9 | 10 | class HomeTheaterFacade 11 | { 12 | public: 13 | HomeTheaterFacade(DvdPlayer *dp, Screen *s): dvdPlayer(dp), screen(s) 14 | {} 15 | ~HomeTheaterFacade(){} 16 | 17 | void watchMovie() 18 | { 19 | screen->down(); 20 | dvdPlayer->on(); 21 | } 22 | void endMovie() 23 | { 24 | dvdPlayer->off(); 25 | screen->up(); 26 | } 27 | 28 | private: 29 | DvdPlayer *dvdPlayer; 30 | Screen *screen; 31 | }; 32 | } 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /cp01/Duck/Duck.h: -------------------------------------------------------------------------------- 1 | #ifndef __DUCK_H__ 2 | #define __DUCK_H__ 3 | 4 | #include "Quack.h" 5 | #include "QuackSqueak.h" 6 | #include "FlyWithWings.h" 7 | #include "FlyNoFly.h" 8 | 9 | 10 | namespace duck 11 | { 12 | 13 | class Duck 14 | { 15 | public: 16 | void performQuack() 17 | { 18 | qb->quack(); 19 | } 20 | void performFly() 21 | { 22 | fb->fly(); 23 | } 24 | ~Duck() 25 | { 26 | delete qb; 27 | delete fb; 28 | } 29 | protected: 30 | QuackBehavior *qb; 31 | FlyBehavior *fb; 32 | Duck(FlyBehavior *f, QuackBehavior *q) 33 | { 34 | qb = q; 35 | fb = f; 36 | } 37 | }; 38 | 39 | } 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /cp09/Composite/MenuItem.h: -------------------------------------------------------------------------------- 1 | #ifndef MENUITEM_H 2 | #define MENUITEM_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Menu.h" 8 | 9 | namespace menu 10 | { 11 | using std::string; 12 | using std::cout; 13 | using std::endl; 14 | 15 | class MenuItem: public Menu 16 | { 17 | public: 18 | MenuItem(string n, int p): name(n), price(p) 19 | {} 20 | ~MenuItem(){} 21 | void print(int indent) 22 | { 23 | for (int i = 0; i < indent; i++) 24 | { 25 | cout << " "; 26 | } 27 | cout << name << ":" << price << endl; 28 | } 29 | 30 | private: 31 | string name; 32 | int price; 33 | }; 34 | } 35 | 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /cp09/Iterator/MenuItem.h: -------------------------------------------------------------------------------- 1 | #ifndef MENUITEM_H 2 | #define MENUITEM_H 3 | 4 | #include 5 | #include 6 | 7 | namespace menu 8 | { 9 | using std::cout; 10 | using std::endl; 11 | using std::string; 12 | 13 | class MenuItem 14 | { 15 | public: 16 | MenuItem(string n, int p): name(n), price(p) 17 | { 18 | } 19 | ~MenuItem(){} 20 | 21 | string getName() 22 | { 23 | return name; 24 | } 25 | int getPrice() 26 | { 27 | return price; 28 | } 29 | void print() 30 | { 31 | cout << getName() << ": " << getPrice() << endl; 32 | } 33 | 34 | private: 35 | string name; 36 | int price; 37 | }; 38 | } 39 | 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /cp04/Factory/PizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZ_STORE_H 2 | #define PIZZ_STORE_H 3 | 4 | #include 5 | #include "Pizza.h" 6 | 7 | using std::string; 8 | 9 | namespace pizza 10 | { 11 | 12 | class PizzaStore 13 | { 14 | public: 15 | PizzaStore(){} 16 | virtual ~PizzaStore(){} 17 | 18 | virtual Pizza *createPizza(string type) = 0; 19 | 20 | Pizza *orderPizza(string type) 21 | { 22 | Pizza *pizza = this->createPizza(type); 23 | if (pizza == NULL) 24 | { 25 | return NULL; 26 | } 27 | pizza->prepare(); 28 | pizza->bake(); 29 | pizza->cut(); 30 | pizza->box(); 31 | 32 | return pizza; 33 | } 34 | }; 35 | } 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/PizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZ_STORE_H 2 | #define PIZZ_STORE_H 3 | 4 | #include 5 | #include "Pizza.h" 6 | 7 | using std::string; 8 | 9 | namespace pizza 10 | { 11 | 12 | class PizzaStore 13 | { 14 | public: 15 | PizzaStore(){} 16 | virtual ~PizzaStore(){} 17 | 18 | virtual Pizza *createPizza(string type) = 0; 19 | 20 | Pizza *orderPizza(string type) 21 | { 22 | Pizza *pizza = this->createPizza(type); 23 | if (pizza == NULL) 24 | { 25 | return NULL; 26 | } 27 | pizza->prepare(); 28 | pizza->bake(); 29 | pizza->cut(); 30 | pizza->box(); 31 | 32 | return pizza; 33 | } 34 | }; 35 | } 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /cp03/Starbuzz/CondimentWhip.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDIMENT_WHIP_H 2 | #define __CONDIMENT_WHIP_H 3 | 4 | #include 5 | #include "Beverage.h" 6 | 7 | namespace starbuzz 8 | { 9 | 10 | class CondimentWhip: public CondimentDecorator 11 | { 12 | public: 13 | CondimentWhip(Beverage *b): cost(10), description("Whip") 14 | { 15 | this->beverage = b; 16 | } 17 | ~CondimentWhip() 18 | { 19 | } 20 | int getCost() 21 | { 22 | return beverage->getCost() + this->cost; 23 | } 24 | string getDescription() 25 | { 26 | return this->description + " " + beverage->getDescription(); 27 | } 28 | private: 29 | int cost; 30 | string description; 31 | Beverage *beverage; 32 | }; 33 | } 34 | 35 | #endif 36 | 37 | -------------------------------------------------------------------------------- /cp07/Adapter/Duck.h: -------------------------------------------------------------------------------- 1 | #ifndef DUCK_H 2 | #define DUCK_H 3 | 4 | #include 5 | #include 6 | 7 | namespace adapter 8 | { 9 | using std::string; 10 | using std::cout; 11 | using std::endl; 12 | 13 | class Duck 14 | { 15 | public: 16 | Duck(){} 17 | ~Duck(){} 18 | virtual void quack() = 0; 19 | virtual void fly() = 0; 20 | }; 21 | 22 | class RedDuck: public Duck 23 | { 24 | public: 25 | RedDuck(string n = "Red duck"): name(n) 26 | { 27 | } 28 | ~RedDuck(){} 29 | 30 | void quack() 31 | { 32 | cout << name << " is quacking" << endl; 33 | } 34 | void fly() 35 | { 36 | cout << name << " is flying" << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /cp03/Starbuzz/CondimentMocha.h: -------------------------------------------------------------------------------- 1 | #ifndef __CONDIMENT_MOCHA_H 2 | #define __CONDIMENT_MOCHA_H 3 | 4 | #include 5 | #include "Beverage.h" 6 | 7 | 8 | namespace starbuzz 9 | { 10 | 11 | class CondimentMocha: public CondimentDecorator 12 | { 13 | public: 14 | CondimentMocha(Beverage *b): cost(3), description("Mocha") 15 | { 16 | this->beverage = b; 17 | } 18 | ~CondimentMocha() 19 | { 20 | } 21 | int getCost() 22 | { 23 | return beverage->getCost() + this->cost; 24 | } 25 | string getDescription() 26 | { 27 | return this->description + " " + beverage->getDescription(); 28 | } 29 | private: 30 | int cost; 31 | string description; 32 | Beverage *beverage; 33 | }; 34 | } 35 | 36 | #endif 37 | 38 | -------------------------------------------------------------------------------- /cp07/Adapter/Turkey.h: -------------------------------------------------------------------------------- 1 | #ifndef TURKEY_H 2 | #define TURKEY_H 3 | 4 | #include 5 | #include 6 | 7 | namespace adapter 8 | { 9 | using std::string; 10 | using std::cout; 11 | using std::endl; 12 | 13 | class Turkey 14 | { 15 | public: 16 | Turkey(){} 17 | ~Turkey(){} 18 | virtual void gobble() = 0; 19 | virtual void fly() = 0; 20 | }; 21 | 22 | class WildTurkey: public Turkey 23 | { 24 | public: 25 | WildTurkey(string n): name(n) 26 | { 27 | } 28 | ~WildTurkey(){} 29 | 30 | void gobble() 31 | { 32 | cout << name << " is goobling" << endl; 33 | } 34 | void fly() 35 | { 36 | cout << name << " is flying" << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /cp08/TemplateMethod/CaffeineBeverage.h: -------------------------------------------------------------------------------- 1 | #ifndef CAFFEINE_BEVERAGE_H 2 | #define CAFFEINE_BEVERAGE_H 3 | 4 | #include 5 | 6 | 7 | namespace caffeine 8 | { 9 | using std::cout; 10 | using std::endl; 11 | 12 | class CaffeineBeverage 13 | { 14 | public: 15 | CaffeineBeverage(){} 16 | virtual ~CaffeineBeverage(){} 17 | 18 | void prepareRecipe() 19 | { 20 | boilWater(); 21 | brew(); 22 | pourInCup(); 23 | addCondiments(); 24 | } 25 | void boilWater() 26 | { 27 | cout << "Boiling warter..." << endl; 28 | } 29 | virtual void brew() = 0; 30 | void pourInCup() 31 | { 32 | cout << "Pour into your cup..." << endl; 33 | } 34 | virtual void addCondiments() = 0; 35 | 36 | }; 37 | } 38 | 39 | #endif 40 | 41 | -------------------------------------------------------------------------------- /cp11/VirtualProxy/Proxy.h: -------------------------------------------------------------------------------- 1 | #ifndef PROXY_H 2 | #define PROXY_H 3 | 4 | #include "Common.h" 5 | #include "RealImage.h" 6 | 7 | 8 | 9 | namespace virtualProxy 10 | { 11 | 12 | class Proxy: public Image 13 | { 14 | public: 15 | Proxy(string p): path(p), realImage(0) 16 | { 17 | } 18 | ~Proxy() 19 | { 20 | // do some clean job 21 | } 22 | 23 | void draw() 24 | { 25 | getImage()->draw(); 26 | } 27 | void refresh() 28 | { 29 | getImage()->refresh(); 30 | } 31 | private: 32 | RealImage *getImage() 33 | { 34 | if (realImage == 0) 35 | { 36 | realImage = new RealImage(path); 37 | } 38 | return realImage; 39 | } 40 | string path; 41 | RealImage *realImage; 42 | }; 43 | } 44 | 45 | #endif 46 | 47 | -------------------------------------------------------------------------------- /cp04/Factory/CalmPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CALM_PIZZA_H 2 | #define CALM_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class CalmPizza: public Pizza 15 | { 16 | public: 17 | CalmPizza(): name("Calm Pizza") 18 | { 19 | }; 20 | ~CalmPizza(){}; 21 | 22 | void prepare() 23 | { 24 | cout << "Preparing " << this->name << endl; 25 | } 26 | void bake() 27 | { 28 | cout << "Baking " << this->name << endl; 29 | } 30 | void cut() 31 | { 32 | cout << "Cutting " << this->name << endl; 33 | } 34 | void box() 35 | { 36 | cout << "Boxing " << this->name << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | 46 | -------------------------------------------------------------------------------- /cp04/Factory/CheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_PIZZA_H 2 | #define CHEESE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class CheesePizza: public Pizza 15 | { 16 | public: 17 | CheesePizza(): name("Cheese Pizza") 18 | { 19 | }; 20 | ~CheesePizza(){}; 21 | 22 | void prepare() 23 | { 24 | cout << "Preparing " << this->name << endl; 25 | } 26 | void bake() 27 | { 28 | cout << "Baking " << this->name << endl; 29 | } 30 | void cut() 31 | { 32 | cout << "Cutting " << this->name << endl; 33 | } 34 | void box() 35 | { 36 | cout << "Boxing " << this->name << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /cp04/Factory/VegglePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef VEGGLE_PIZZA_H 2 | #define VEGGLE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class VeeglePizza: public Pizza 15 | { 16 | public: 17 | VeeglePizza(): name("NewYork Veggle Pizza") 18 | { 19 | }; 20 | ~VeeglePizza(){}; void prepare() 21 | { 22 | cout << "Preparing " << this->name << endl; 23 | } 24 | void bake() 25 | { 26 | cout << "Baking " << this->name << endl; 27 | } 28 | void cut() 29 | { 30 | cout << "Cutting " << this->name << endl; 31 | } 32 | void box() 33 | { 34 | cout << "Boxing " << this->name << endl; 35 | } 36 | private: 37 | string name; 38 | }; 39 | } 40 | 41 | #endif 42 | 43 | 44 | -------------------------------------------------------------------------------- /cp04/SimpleFactory/CalmPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CALM_PIZZA_H 2 | #define CALM_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class CalmPizza: public Pizza 15 | { 16 | public: 17 | CalmPizza(): name("Calm Pizza") 18 | { 19 | }; 20 | ~CalmPizza(){}; 21 | 22 | void prepare() 23 | { 24 | cout << "Preparing " << this->name << endl; 25 | } 26 | void bake() 27 | { 28 | cout << "Baking " << this->name << endl; 29 | } 30 | void cut() 31 | { 32 | cout << "Cutting " << this->name << endl; 33 | } 34 | void box() 35 | { 36 | cout << "Boxing " << this->name << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | 46 | -------------------------------------------------------------------------------- /cp04/SimpleFactory/VegglePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef VEGGLE_PIZZA_H 2 | #define VEGGLE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class VeeglePizza: public Pizza 15 | { 16 | public: 17 | VeeglePizza(): name("Veggle Pizza") 18 | { 19 | }; 20 | ~VeeglePizza(){}; void prepare() 21 | { 22 | cout << "Preparing " << this->name << endl; 23 | } 24 | void bake() 25 | { 26 | cout << "Baking " << this->name << endl; 27 | } 28 | void cut() 29 | { 30 | cout << "Cutting " << this->name << endl; 31 | } 32 | void box() 33 | { 34 | cout << "Boxing " << this->name << endl; 35 | } 36 | private: 37 | string name; 38 | }; 39 | } 40 | 41 | #endif 42 | 43 | 44 | -------------------------------------------------------------------------------- /cp04/SimpleFactory/CheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_PIZZA_H 2 | #define CHEESE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class CheesePizza: public Pizza 15 | { 16 | public: 17 | CheesePizza(): name("Cheese Pizza") 18 | { 19 | }; 20 | ~CheesePizza(){}; 21 | 22 | void prepare() 23 | { 24 | cout << "Preparing " << this->name << endl; 25 | } 26 | void bake() 27 | { 28 | cout << "Baking " << this->name << endl; 29 | } 30 | void cut() 31 | { 32 | cout << "Cutting " << this->name << endl; 33 | } 34 | void box() 35 | { 36 | cout << "Boxing " << this->name << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /cp06/RemoteControl/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Command.h" 2 | 3 | using namespace control; 4 | 5 | int main() 6 | { 7 | Light *light = new Light("Simple light"); 8 | Stereo *stereo = new Stereo("Cool stereo"); 9 | 10 | LightOnCommand *loc = new LightOnCommand(light); 11 | LightOffCommand *lofc = new LightOffCommand(light); 12 | StereoOnCommand *soc = new StereoOnCommand(stereo); 13 | StereoOffCommand *sofc = new StereoOffCommand(stereo); 14 | MacroCommand *mc = new MacroCommand(light, stereo); 15 | 16 | 17 | loc->execute(); 18 | lofc->execute(); 19 | soc->execute(); 20 | sofc->execute(); 21 | sofc->undo(); 22 | 23 | mc->execute(); 24 | 25 | delete mc; 26 | delete sofc; 27 | delete soc; 28 | delete lofc; 29 | delete loc; 30 | delete stereo; 31 | delete light; 32 | return 0; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /cp04/Factory/NewYorkVeeglePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef VEGGLE_PIZZA_H 2 | #define VEGGLE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class NewYorkVeeglePizza: public Pizza 15 | { 16 | public: 17 | NewYorkVeeglePizza(): name("Veggle Pizza") 18 | { 19 | }; 20 | ~NewYorkVeeglePizza(){}; void prepare() 21 | { 22 | cout << "Preparing " << this->name << endl; 23 | } 24 | void bake() 25 | { 26 | cout << "Baking " << this->name << endl; 27 | } 28 | void cut() 29 | { 30 | cout << "Cutting " << this->name << endl; 31 | } 32 | void box() 33 | { 34 | cout << "Boxing " << this->name << endl; 35 | } 36 | private: 37 | string name; 38 | }; 39 | } 40 | 41 | #endif 42 | 43 | 44 | -------------------------------------------------------------------------------- /cp04/Factory/NewYorkCalmPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CALM_PIZZA_H 2 | #define CALM_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class NewYorkCalmPizza: public Pizza 15 | { 16 | public: 17 | NewYorkCalmPizza(): name("NewYork Calm Pizza") 18 | { 19 | }; 20 | ~NewYorkCalmPizza(){}; 21 | 22 | void prepare() 23 | { 24 | cout << "Preparing " << this->name << endl; 25 | } 26 | void bake() 27 | { 28 | cout << "Baking " << this->name << endl; 29 | } 30 | void cut() 31 | { 32 | cout << "Cutting " << this->name << endl; 33 | } 34 | void box() 35 | { 36 | cout << "Boxing " << this->name << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | 46 | -------------------------------------------------------------------------------- /cp04/Factory/NewYorkCheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_PIZZA_H 2 | #define CHEESE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class NewYorkCheesePizza: public Pizza 15 | { 16 | public: 17 | NewYorkCheesePizza(): name("NewYork Cheese Pizza") 18 | { 19 | }; 20 | ~NewYorkCheesePizza(){}; 21 | 22 | void prepare() 23 | { 24 | cout << "Preparing " << this->name << endl; 25 | } 26 | void bake() 27 | { 28 | cout << "Baking " << this->name << endl; 29 | } 30 | void cut() 31 | { 32 | cout << "Cutting " << this->name << endl; 33 | } 34 | void box() 35 | { 36 | cout << "Boxing " << this->name << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /cp03/Starbuzz/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "Beverage.h" 4 | #include "BeverageBeer.h" 5 | #include "BeverageCoffee.h" 6 | #include "CondimentDecorator.h" 7 | #include "CondimentMocha.h" 8 | #include "CondimentWhip.h" 9 | 10 | using std::cout; 11 | using std::endl; 12 | 13 | int main() 14 | { 15 | starbuzz::Beverage *beverage = new starbuzz::BeverageCoffee("Coffee", 3); 16 | starbuzz::Beverage *mocha = new starbuzz::CondimentMocha(beverage); 17 | starbuzz::Beverage *whip = new starbuzz::CondimentWhip(mocha); 18 | 19 | cout << beverage->getDescription() << ":" << beverage->getCost() << endl; 20 | cout << mocha->getDescription() << ":" << mocha->getCost() << endl; 21 | cout << whip->getDescription() << ":" << whip->getCost() << endl; 22 | 23 | delete whip; 24 | delete mocha; 25 | delete beverage; 26 | 27 | return 0; 28 | } 29 | 30 | -------------------------------------------------------------------------------- /cp04/SimpleFactory/PizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef PIZZ_STORE_H 2 | #define PIZZ_STORE_H 3 | 4 | #include 5 | #include "Pizza.h" 6 | #include "SimplePizzaFactory.h" 7 | 8 | using std::string; 9 | 10 | namespace pizza 11 | { 12 | 13 | class PizzaStore 14 | { 15 | public: 16 | PizzaStore(SimplePizzaFactory *spf) 17 | { 18 | this->simplePizzaFactory = spf; 19 | } 20 | ~PizzaStore() 21 | { 22 | } 23 | 24 | Pizza *orderPizza(string type) 25 | { 26 | Pizza *pizza = simplePizzaFactory->createPizza(type); 27 | if (pizza == NULL) 28 | { 29 | return NULL; 30 | } 31 | pizza->prepare(); 32 | pizza->bake(); 33 | pizza->cut(); 34 | pizza->box(); 35 | 36 | return pizza; 37 | } 38 | private: 39 | SimplePizzaFactory *simplePizzaFactory; 40 | }; 41 | } 42 | #endif 43 | 44 | -------------------------------------------------------------------------------- /cp04/SimpleFactory/SimplePizzaFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLE_PIZZA_FACTORY_H 2 | #define SIMPLE_PIZZA_FACTORY_H 3 | 4 | #include "Pizza.h" 5 | #include "CheesePizza.h" 6 | #include "VegglePizza.h" 7 | #include "CalmPizza.h" 8 | 9 | namespace pizza 10 | { 11 | 12 | class SimplePizzaFactory 13 | { 14 | public: 15 | SimplePizzaFactory(){} 16 | ~SimplePizzaFactory(){} 17 | 18 | Pizza *createPizza(string type) 19 | { 20 | if (type == string("Cheese")) 21 | { 22 | return new CheesePizza(); 23 | } 24 | else if (type == string("Calm")) 25 | { 26 | return new CalmPizza(); 27 | } 28 | else if (type == string("Veggle")) 29 | { 30 | return new CalmPizza(); 31 | } 32 | else 33 | { 34 | return NULL; 35 | } 36 | } 37 | }; 38 | } 39 | 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /cp04/Factory/ShenZhenCalmPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_CALM_PIZZA_H 2 | #define SHENZHEN_CALM_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class ShenZhenCalmPizza: public Pizza 15 | { 16 | public: 17 | ShenZhenCalmPizza(): name("ShenZhen Calm Pizza") 18 | { 19 | }; 20 | ~ShenZhenCalmPizza(){}; 21 | 22 | void prepare() 23 | { 24 | cout << "Preparing " << this->name << endl; 25 | } 26 | void bake() 27 | { 28 | cout << "Baking " << this->name << endl; 29 | } 30 | void cut() 31 | { 32 | cout << "Cutting " << this->name << endl; 33 | } 34 | void box() 35 | { 36 | cout << "Boxing " << this->name << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | 46 | -------------------------------------------------------------------------------- /cp04/Factory/ShenZhenVeeglePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_VEEGLE_PIZZA_H 2 | #define SHENZHEN_VEEGLE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class ShenZhenVeeglePizza: public Pizza 15 | { 16 | public: 17 | ShenZhenVeeglePizza(): name("ShenZhen Veggle Pizza") 18 | { 19 | }; 20 | ~ShenZhenVeeglePizza(){}; void prepare() 21 | { 22 | cout << "Preparing " << this->name << endl; 23 | } 24 | void bake() 25 | { 26 | cout << "Baking " << this->name << endl; 27 | } 28 | void cut() 29 | { 30 | cout << "Cutting " << this->name << endl; 31 | } 32 | void box() 33 | { 34 | cout << "Boxing " << this->name << endl; 35 | } 36 | private: 37 | string name; 38 | }; 39 | } 40 | 41 | #endif 42 | 43 | 44 | -------------------------------------------------------------------------------- /cp11/VirtualProxy/RealImage.h: -------------------------------------------------------------------------------- 1 | #ifndef REALIMAGE_H 2 | #define REALIMAGE_H 3 | 4 | #include "Common.h" 5 | #include "Image.h" 6 | 7 | 8 | namespace virtualProxy 9 | { 10 | 11 | class RealImage: public Image 12 | { 13 | public: 14 | RealImage(string p) 15 | { 16 | imageHandle = loadImage(p); 17 | } 18 | ~RealImage() 19 | { 20 | // do some clean job 21 | } 22 | 23 | void draw() 24 | { 25 | cout << "I am drawing " << imageHandle->getPath() << endl; 26 | } 27 | void refresh() 28 | { 29 | cout << "I am refreshing " << imageHandle->getPath() << endl; 30 | } 31 | ImageHandle* loadImage(string p) 32 | { 33 | cout << "Loading image \"" << p << "\", not an easy job..." << endl; 34 | return new ImageHandle(p); 35 | } 36 | private: 37 | ImageHandle *imageHandle; 38 | }; 39 | } 40 | 41 | #endif 42 | 43 | -------------------------------------------------------------------------------- /cp04/Factory/ShenZhenCheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_CHEESE_PIZZA_H 2 | #define SHENZHEN_CHEESE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | using std::string; 8 | using std::cout; 9 | using std::endl; 10 | 11 | namespace pizza 12 | { 13 | 14 | class ShenZhenCheesePizza: public Pizza 15 | { 16 | public: 17 | ShenZhenCheesePizza(): name("ShenZhen Cheese Pizza") 18 | { 19 | }; 20 | ~ShenZhenCheesePizza(){}; 21 | 22 | void prepare() 23 | { 24 | cout << "Preparing " << this->name << endl; 25 | } 26 | void bake() 27 | { 28 | cout << "Baking " << this->name << endl; 29 | } 30 | void cut() 31 | { 32 | cout << "Cutting " << this->name << endl; 33 | } 34 | void box() 35 | { 36 | cout << "Boxing " << this->name << endl; 37 | } 38 | private: 39 | string name; 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /cp02/WeatherData/TemperatureOnlyDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEMPERATUREONLYDISPLAY_H__ 2 | #define __TEMPERATUREONLYDISPLAY_H__ 3 | 4 | #include "WeatherData.h" 5 | #include "Observer.h" 6 | #include "DisplayElement.h" 7 | 8 | namespace weather 9 | { 10 | using std::cout; 11 | using std::endl; 12 | 13 | class TemperatureOnlyDisplay: public Observer, public DisplayElement 14 | { 15 | public: 16 | TemperatureOnlyDisplay(WeatherData *wd) 17 | { 18 | weatherData = wd; 19 | wd->registerObserver(this); 20 | } 21 | 22 | void update() 23 | { 24 | temp = weatherData->getTemp(); 25 | display(); 26 | } 27 | 28 | void display() 29 | { 30 | cout << "TemperatureOnlyDisplay displaying:" << endl; 31 | cout << "Temperuture: " << temp << endl; 32 | cout << endl; 33 | } 34 | private: 35 | WeatherData *weatherData; 36 | int temp; 37 | int humidity; 38 | int pressure; 39 | }; 40 | } 41 | 42 | #endif 43 | -------------------------------------------------------------------------------- /cp04/Factory/NewYorkPizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWYORK_PIZZA_STORE_H 2 | #define NEWYORK_PIZZA_STORE_H 3 | 4 | #include 5 | #include "Pizza.h" 6 | #include "NewYorkCalmPizza.h" 7 | #include "NewYorkCheesePizza.h" 8 | #include "NewYorkVeeglePizza.h" 9 | 10 | using std::string; 11 | 12 | namespace pizza 13 | { 14 | 15 | // NewYork pizza factory 16 | class NewYorkPizzaStore: public PizzaStore { 17 | public: 18 | NewYorkPizzaStore(){} 19 | ~NewYorkPizzaStore(){} 20 | 21 | Pizza *createPizza(string type) 22 | { 23 | if (type == string("Cheese")) 24 | { 25 | return new NewYorkCheesePizza(); 26 | } 27 | else if (type == string("Calm")) 28 | { 29 | return new NewYorkCalmPizza(); 30 | } 31 | else if (type == string("Veggle")) 32 | { 33 | return new NewYorkVeeglePizza(); 34 | } 35 | else 36 | { 37 | return NULL; 38 | } 39 | } 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /cp04/Factory/ShenZhenPizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_PIZZA_STORE_H 2 | #define SHENZHEN_PIZZA_STORE_H 3 | 4 | #include 5 | #include "Pizza.h" 6 | #include "ShenZhenCalmPizza.h" 7 | #include "ShenZhenCheesePizza.h" 8 | #include "ShenZhenVeeglePizza.h" 9 | 10 | using std::string; 11 | 12 | namespace pizza 13 | { 14 | 15 | // ShenZhen pizza factory 16 | class ShenZhenPizzaStore: public PizzaStore { 17 | public: 18 | ShenZhenPizzaStore(){} 19 | ~ShenZhenPizzaStore(){} 20 | 21 | Pizza *createPizza(string type) 22 | { 23 | if (type == string("Cheese")) 24 | { 25 | return new ShenZhenCheesePizza(); 26 | } 27 | else if (type == string("Calm")) 28 | { 29 | return new ShenZhenCalmPizza(); 30 | } 31 | else if (type == string("Veggle")) 32 | { 33 | return new ShenZhenVeeglePizza(); 34 | } 35 | else 36 | { 37 | return NULL; 38 | } 39 | } 40 | }; 41 | } 42 | 43 | #endif 44 | 45 | -------------------------------------------------------------------------------- /cp10/State/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "ConcreteStates.h" 5 | #include "Process.h" 6 | 7 | using namespace state; 8 | 9 | typedef enum 10 | { 11 | RESUM = 0, 12 | TIMEOUT, 13 | NEEDIO, 14 | FINISHIO, 15 | } OP; 16 | 17 | int main() 18 | { 19 | Process *p = new Process("samba"); 20 | while (1) 21 | { 22 | try 23 | { 24 | // generate rand from 0-3 25 | int op = rand() & 0x3; 26 | if (op == RESUM) 27 | p->resumedByCpu(); 28 | else if (op == TIMEOUT) 29 | p->timeSlotEnd(); 30 | else if (op == NEEDIO) 31 | p->needIO(); 32 | else if (op == FINISHIO) 33 | p->finishIO(); 34 | 35 | } 36 | catch (ImpossibleException ie) 37 | { 38 | cout << "Not supported in current state" << endl; 39 | } 40 | sleep(1); 41 | } 42 | 43 | delete p; 44 | 45 | return 0; 46 | } 47 | 48 | -------------------------------------------------------------------------------- /cp09/Iterator/Breakfast.h: -------------------------------------------------------------------------------- 1 | #ifndef BREAKFAST_H 2 | #define BREAKFAST_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "Menu.h" 9 | #include "MenuItem.h" 10 | #include "Iterator.h" 11 | 12 | namespace menu 13 | { 14 | using std::cout; 15 | using std::endl; 16 | using std::string; 17 | using std::vector; 18 | 19 | class Breakfast: public Menu 20 | { 21 | public: 22 | Breakfast() 23 | { 24 | MenuItem *m1 = new MenuItem("Egg", 3); 25 | MenuItem *m2 = new MenuItem("Bread", 5); 26 | MenuItem *m3 = new MenuItem("Milk", 1); 27 | 28 | addMenuItem(m1); 29 | addMenuItem(m2); 30 | addMenuItem(m3); 31 | } 32 | ~Breakfast() 33 | { 34 | // need free memory 35 | } 36 | 37 | void addMenuItem(MenuItem *mi) 38 | { 39 | menu.push_back(mi); 40 | } 41 | Iterator *createIterator() 42 | { 43 | return new BreakfastIterator(menu); 44 | } 45 | 46 | private: 47 | vector menu; 48 | }; 49 | } 50 | 51 | #endif 52 | 53 | -------------------------------------------------------------------------------- /cp09/Iterator/Diner.h: -------------------------------------------------------------------------------- 1 | #ifndef DINER_H 2 | #define DINER_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Menu.h" 8 | #include "MenuItem.h" 9 | #include "Iterator.h" 10 | 11 | namespace menu 12 | { 13 | using std::cout; 14 | using std::endl; 15 | using std::string; 16 | 17 | class Diner: public Menu 18 | { 19 | public: 20 | Diner(): menuCount(0) 21 | { 22 | MenuItem *m1 = new MenuItem("Meat", 10); 23 | MenuItem *m2 = new MenuItem("Fish", 20); 24 | MenuItem *m3 = new MenuItem("Steak", 50); 25 | 26 | addMenuItem(m1); 27 | addMenuItem(m2); 28 | addMenuItem(m3); 29 | } 30 | ~Diner() 31 | { 32 | // need free memory 33 | } 34 | 35 | void addMenuItem(MenuItem *mi) 36 | { 37 | menu[menuCount] = mi; 38 | menuCount++; 39 | } 40 | Iterator *createIterator() 41 | { 42 | return new DinerIterator(menu, menuCount); 43 | } 44 | 45 | private: 46 | MenuItem* menu[10]; 47 | int menuCount; 48 | }; 49 | } 50 | 51 | #endif 52 | 53 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/NewYorkPizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWYORK_PIZZA_STORE_H 2 | #define NEWYORK_PIZZA_STORE_H 3 | 4 | #include 5 | 6 | #include "CalmPizza.h" 7 | #include "CheesePizza.h" 8 | #include "VegglePizza.h" 9 | #include "PizzaStore.h" 10 | #include "NewYorkIngredientFactory.h" 11 | 12 | using std::string; 13 | 14 | namespace pizza 15 | { 16 | 17 | // NewYork pizza factory 18 | class NewYorkPizzaStore: public PizzaStore 19 | { 20 | public: 21 | NewYorkPizzaStore(){} 22 | ~NewYorkPizzaStore(){} 23 | 24 | Pizza *createPizza(string type) 25 | { 26 | PizzaIngredientFactory *p = new NewYorkIngredientFactory(); 27 | if (type == string("Cheese")) 28 | { 29 | return new CheesePizza(p); 30 | } 31 | else if (type == string("Calm")) 32 | { 33 | return new CalmPizza(p); 34 | } 35 | else if (type == string("Veggle")) 36 | { 37 | return new VeeglePizza(p); 38 | } 39 | else 40 | { 41 | return NULL; 42 | } 43 | delete p; 44 | } 45 | }; 46 | } 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/ShenZhenPizzaStore.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_PIZZA_STORE_H 2 | #define SHENZHEN_PIZZA_STORE_H 3 | 4 | #include 5 | 6 | #include "CalmPizza.h" 7 | #include "CheesePizza.h" 8 | #include "VegglePizza.h" 9 | #include "PizzaStore.h" 10 | #include "ShenZhenIngredientFactory.h" 11 | 12 | using std::string; 13 | 14 | namespace pizza 15 | { 16 | 17 | // ShenZhen pizza factory 18 | class ShenZhenPizzaStore: public PizzaStore { 19 | public: 20 | ShenZhenPizzaStore(){} 21 | ~ShenZhenPizzaStore(){} 22 | 23 | Pizza *createPizza(string type) 24 | { 25 | PizzaIngredientFactory *p = new ShenZhenIngredientFactory(); 26 | if (type == string("Cheese")) 27 | { 28 | return new CheesePizza(p); 29 | } 30 | else if (type == string("Calm")) 31 | { 32 | return new CalmPizza(p); 33 | } 34 | else if (type == string("Veggle")) 35 | { 36 | return new VeeglePizza(p); 37 | } 38 | else 39 | { 40 | return NULL; 41 | } 42 | delete p; 43 | } 44 | }; 45 | } 46 | 47 | #endif 48 | 49 | -------------------------------------------------------------------------------- /cp02/WeatherData/CurrentConditionsDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef __CURRENTCONDITIONSDISPLAY_H__ 2 | #define __CURRENTCONDITIONSDISPLAY_H__ 3 | 4 | #include "WeatherData.h" 5 | #include "Observer.h" 6 | #include "DisplayElement.h" 7 | 8 | namespace weather 9 | { 10 | using std::cout; 11 | using std::endl; 12 | 13 | class CurrentConditionsDisplay: public Observer, public DisplayElement 14 | { 15 | public: 16 | CurrentConditionsDisplay(WeatherData *wd) 17 | { 18 | weatherData = wd; 19 | wd->registerObserver(this); 20 | } 21 | 22 | void update() 23 | { 24 | temp = weatherData->getTemp(); 25 | humidity = weatherData->getHumidity(); 26 | pressure = weatherData->getPressure(); 27 | display(); 28 | } 29 | 30 | void display() 31 | { 32 | cout << "CurrentConditionsDisplay displaying:" << endl; 33 | cout << "Temperuture: " << temp << endl; 34 | cout << "Humidity: " << humidity << endl; 35 | cout << "Pressure: " << pressure << endl; 36 | cout << endl; 37 | } 38 | private: 39 | WeatherData *weatherData; 40 | int temp; 41 | int humidity; 42 | int pressure; 43 | }; 44 | } 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /cp09/Composite/SubMenu.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBMENU_H 2 | #define SUBMENU_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "Menu.h" 9 | 10 | namespace menu 11 | { 12 | using std::string; 13 | using std::cout; 14 | using std::endl; 15 | using std::vector; 16 | 17 | // not a good class name, should be composite menu 18 | class SubMenu: public Menu 19 | { 20 | public: 21 | SubMenu(string n): name(n) 22 | {} 23 | ~SubMenu() 24 | { 25 | // should free vector memory 26 | } 27 | 28 | void addMenuItem(Menu *m) 29 | { 30 | menuVector.push_back(m); 31 | } 32 | void print(int indent) 33 | { 34 | for (int i = 0; i < indent; i++) 35 | { 36 | cout << " "; 37 | } 38 | cout << name << "(Sub):" << endl; 39 | vector::const_iterator it = menuVector.begin(); 40 | while(it != menuVector.end()) 41 | { 42 | (*it)->print(indent + 1); 43 | it++; 44 | } 45 | } 46 | 47 | 48 | private: 49 | vector menuVector; 50 | string name; 51 | }; 52 | } 53 | 54 | #endif 55 | 56 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/NewYorkIngredientFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef NEWYORK_INGRIDENT_FACTORY_H 2 | #define NEWYORK_INGRIDENT_FACTORY_H 3 | 4 | #include 5 | 6 | #include "PizzaIngredientFactory.h" 7 | #include "NewYorkCalm.h" 8 | #include "NewYorkCheese.h" 9 | #include "NewYorkSauce.h" 10 | #include "NewYorkVeggle.h" 11 | 12 | using std::cout; 13 | using std::endl; 14 | 15 | namespace pizza 16 | { 17 | 18 | class NewYorkIngredientFactory: public PizzaIngredientFactory 19 | { 20 | public: 21 | NewYorkIngredientFactory(){} 22 | ~NewYorkIngredientFactory(){} 23 | 24 | Calm *createCalm() 25 | { 26 | cout << "Making NewYork calm" << endl; 27 | return new NewYorkCalm(); 28 | } 29 | Cheese *createCheese() 30 | { 31 | cout << "Making NewYork cheese" << endl; 32 | return new NewYorkCheese(); 33 | } 34 | Sauce *createSauce() 35 | { 36 | cout << "Making NewYork sauce" << endl; 37 | return new NewYorkSauce(); 38 | } 39 | Veggle *createVeggle() 40 | { 41 | cout << "Making NewYork veggle" << endl; 42 | return new NewYorkVeggle(); 43 | } 44 | }; 45 | } 46 | 47 | #endif 48 | 49 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/ShenZhenIngredientFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef SHENZHEN_INGRIDENT_FACTORY_H 2 | #define SHENZHEN_INGRIDENT_FACTORY_H 3 | 4 | #include 5 | 6 | #include "PizzaIngredientFactory.h" 7 | #include "ShenZhenCalm.h" 8 | #include "ShenZhenCheese.h" 9 | #include "ShenZhenSauce.h" 10 | #include "ShenZhenVeggle.h" 11 | 12 | using std::cout; 13 | using std::endl; 14 | 15 | namespace pizza 16 | { 17 | 18 | class ShenZhenIngredientFactory: public PizzaIngredientFactory 19 | { 20 | public: 21 | ShenZhenIngredientFactory(){} 22 | ~ShenZhenIngredientFactory(){} 23 | 24 | Calm *createCalm() 25 | { 26 | cout << "Making ShenZhen calm" << endl; 27 | return new ShenZhenCalm(); 28 | } 29 | Cheese *createCheese() 30 | { 31 | cout << "Making ShenZhen cheese" << endl; 32 | return new ShenZhenCheese(); 33 | } 34 | Sauce *createSauce() 35 | { 36 | cout << "Making ShenZhen sauce" << endl; 37 | return new ShenZhenSauce(); 38 | } 39 | Veggle *createVeggle() 40 | { 41 | cout << "Making ShenZhen veggle" << endl; 42 | return new ShenZhenVeggle(); 43 | } 44 | }; 45 | } 46 | 47 | #endif 48 | 49 | 50 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/CalmPizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CALM_PIZZA_H 2 | #define CALM_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Pizza.h" 8 | #include "Calm.h" 9 | #include "Cheese.h" 10 | #include "Sauce.h" 11 | #include "Veggle.h" 12 | #include "PizzaIngredientFactory.h" 13 | 14 | using std::string; 15 | using std::cout; 16 | using std::endl; 17 | 18 | namespace pizza 19 | { 20 | 21 | class CalmPizza: public Pizza 22 | { 23 | public: 24 | CalmPizza(PizzaIngredientFactory *p): name("Calm Pizza") 25 | { 26 | this->pif = p; 27 | }; 28 | ~CalmPizza(){}; 29 | 30 | void prepare() 31 | { 32 | Calm *calm = pif->createCalm(); 33 | Cheese *cheese = pif->createCheese(); 34 | Sauce *sauce = pif->createSauce(); 35 | Veggle *veggle = pif->createVeggle(); 36 | delete calm; 37 | delete cheese; 38 | delete sauce; 39 | delete veggle; 40 | cout << "Preparing " << this->name << endl; 41 | } 42 | void bake() 43 | { 44 | cout << "Baking " << this->name << endl; 45 | } 46 | void cut() 47 | { 48 | cout << "Cutting " << this->name << endl; 49 | } 50 | void box() 51 | { 52 | cout << "Boxing " << this->name << endl; 53 | } 54 | private: 55 | PizzaIngredientFactory *pif; 56 | string name; 57 | }; 58 | } 59 | 60 | #endif 61 | 62 | 63 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/CheesePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_PIZZA_H 2 | #define CHEESE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Pizza.h" 8 | #include "Calm.h" 9 | #include "Cheese.h" 10 | #include "Sauce.h" 11 | #include "Veggle.h" 12 | #include "PizzaIngredientFactory.h" 13 | 14 | using std::string; 15 | using std::cout; 16 | using std::endl; 17 | 18 | namespace pizza 19 | { 20 | 21 | class CheesePizza: public Pizza 22 | { 23 | public: 24 | CheesePizza(PizzaIngredientFactory *p): name("Cheese Pizza") 25 | { 26 | this->pif = p; 27 | }; 28 | ~CheesePizza(){}; 29 | 30 | void prepare() 31 | { 32 | Calm *calm = pif->createCalm(); 33 | Cheese *cheese = pif->createCheese(); 34 | Sauce *sauce = pif->createSauce(); 35 | Veggle *veggle = pif->createVeggle(); 36 | delete calm; 37 | delete cheese; 38 | delete sauce; 39 | delete veggle; 40 | cout << "Preparing " << this->name << endl; 41 | } 42 | void bake() 43 | { 44 | cout << "Baking " << this->name << endl; 45 | } 46 | void cut() 47 | { 48 | cout << "Cutting " << this->name << endl; 49 | } 50 | void box() 51 | { 52 | cout << "Boxing " << this->name << endl; 53 | } 54 | private: 55 | PizzaIngredientFactory *pif; 56 | string name; 57 | }; 58 | } 59 | 60 | #endif 61 | 62 | -------------------------------------------------------------------------------- /cp04/AbstractFactory/VegglePizza.h: -------------------------------------------------------------------------------- 1 | #ifndef VEGGLE_PIZZA_H 2 | #define VEGGLE_PIZZA_H 3 | 4 | #include 5 | #include 6 | 7 | #include "Pizza.h" 8 | #include "Calm.h" 9 | #include "Cheese.h" 10 | #include "Sauce.h" 11 | #include "Veggle.h" 12 | #include "PizzaIngredientFactory.h" 13 | 14 | using std::string; 15 | using std::cout; 16 | using std::endl; 17 | 18 | namespace pizza 19 | { 20 | 21 | class VeeglePizza: public Pizza 22 | { 23 | public: 24 | VeeglePizza(PizzaIngredientFactory *p): name("NewYork Veggle Pizza") 25 | { 26 | this->pif = p; 27 | }; 28 | ~VeeglePizza(){}; void prepare() 29 | { 30 | Calm *calm = pif->createCalm(); 31 | Cheese *cheese = pif->createCheese(); 32 | Sauce *sauce = pif->createSauce(); 33 | Veggle *veggle = pif->createVeggle(); 34 | delete calm; 35 | delete cheese; 36 | delete sauce; 37 | delete veggle; 38 | cout << "Preparing " << this->name << endl; 39 | } 40 | void bake() 41 | { 42 | cout << "Baking " << this->name << endl; 43 | } 44 | void cut() 45 | { 46 | cout << "Cutting " << this->name << endl; 47 | } 48 | void box() 49 | { 50 | cout << "Boxing " << this->name << endl; 51 | } 52 | private: 53 | PizzaIngredientFactory *pif; 54 | string name; 55 | }; 56 | } 57 | 58 | #endif 59 | 60 | 61 | -------------------------------------------------------------------------------- /cp09/Iterator/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ITERATOR_H 2 | #define ITERATOR_H 3 | 4 | #include 5 | #include "MenuItem.h" 6 | 7 | namespace menu 8 | { 9 | using std::vector; 10 | 11 | class Iterator 12 | { 13 | public: 14 | Iterator(){} 15 | virtual ~Iterator(){} 16 | virtual int hasNext() = 0; 17 | virtual MenuItem* next() = 0; 18 | }; 19 | 20 | class DinerIterator: public Iterator 21 | { 22 | public: 23 | DinerIterator(MenuItem *d[], int c): menuList(d), count(c), index(0) 24 | {} 25 | ~DinerIterator(){} 26 | 27 | int hasNext() 28 | { 29 | return index < count; 30 | } 31 | MenuItem* next() 32 | { 33 | MenuItem *m = menuList[index]; 34 | index++; 35 | 36 | return m; 37 | } 38 | private: 39 | MenuItem **menuList; 40 | int count; 41 | int index; 42 | }; 43 | 44 | class BreakfastIterator: public Iterator 45 | { 46 | public: 47 | BreakfastIterator(vector &mv) 48 | { 49 | menuVectorIterator = mv.begin(); 50 | end = mv.end(); 51 | } 52 | ~BreakfastIterator(){} 53 | 54 | int hasNext() 55 | { 56 | return (int)(menuVectorIterator != end); 57 | } 58 | MenuItem* next() 59 | { 60 | MenuItem *m = *(menuVectorIterator); 61 | menuVectorIterator++; 62 | return m; 63 | } 64 | private: 65 | vector::const_iterator menuVectorIterator; 66 | vector::const_iterator end; 67 | }; 68 | } 69 | 70 | #endif 71 | 72 | -------------------------------------------------------------------------------- /cp02/WeatherData/WeatherData.h: -------------------------------------------------------------------------------- 1 | #ifndef __WEATHERDATA_H__ 2 | #define __WEATHERDATA_H__ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #include "Subject.h" 9 | #include "Observer.h" 10 | 11 | namespace weather 12 | { 13 | 14 | using std::cout; 15 | using std::endl; 16 | class WeatherData: public Subject 17 | { 18 | public: 19 | WeatherData(): temp(0), humidity(0), pressure(0) 20 | { 21 | } 22 | 23 | void registerObserver(Observer *ob) 24 | { 25 | observers.push_back(ob); 26 | } 27 | void removeObserver(Observer *ob) 28 | { 29 | std::vector::iterator found = std::find(observers.begin(), observers.end(), ob); 30 | if (found != observers.end()) 31 | { 32 | observers.erase(found); 33 | } 34 | } 35 | void notifyObservers() 36 | { 37 | for (std::vector::iterator it = observers.begin(); it != observers.end(); it++) 38 | { 39 | (*it)->update(); 40 | } 41 | } 42 | 43 | int getTemp() 44 | { 45 | return temp; 46 | } 47 | 48 | int getHumidity() 49 | { 50 | return humidity; 51 | } 52 | 53 | int getPressure() 54 | { 55 | return pressure; 56 | } 57 | 58 | void measurementChanged() 59 | { 60 | notifyObservers(); 61 | } 62 | 63 | void setMeasurement(int t = 1, int h = 2, int p = 3) 64 | { 65 | temp = t; 66 | humidity = h; 67 | pressure = p; 68 | measurementChanged(); 69 | } 70 | private: 71 | std::vector observers; 72 | 73 | int temp; 74 | int humidity; 75 | int pressure; 76 | }; 77 | } 78 | 79 | #endif 80 | -------------------------------------------------------------------------------- /cp10/State/Process.h: -------------------------------------------------------------------------------- 1 | #ifndef PROCESS_H 2 | #define PROCESS_H 3 | 4 | #include 5 | #include 6 | #include "State.h" 7 | 8 | namespace state 9 | { 10 | using std::string; 11 | using std::cout; 12 | using std::endl; 13 | 14 | class Process 15 | { 16 | public: 17 | Process(string n); 18 | ~Process() 19 | { 20 | // need free memory 21 | } 22 | 23 | string getName() 24 | { 25 | return name; 26 | } 27 | void setState(State *s) 28 | { 29 | curState = s; 30 | } 31 | State *getReadyState() 32 | { 33 | return readyState; 34 | } 35 | State *getRunningState() 36 | { 37 | return runningState; 38 | } 39 | State *getBlockedState() 40 | { 41 | return blockedState; 42 | } 43 | void resumedByCpu() 44 | { 45 | State *oldState = curState; 46 | cout << "Resume by CPU event" << endl; 47 | State *s = this->curState; 48 | s->resumedByCpu(); 49 | cout << oldState->getName() << "->" << curState->getName() << endl << endl; 50 | } 51 | void timeSlotEnd() 52 | { 53 | State *oldState = curState; 54 | cout << "Time slot end event" << endl; 55 | State *s = this->curState; 56 | s->timeSlotEnd(); 57 | cout << oldState->getName() << "->" << curState->getName() << endl << endl; 58 | } 59 | void needIO() 60 | { 61 | State *oldState = curState; 62 | cout << "Need IO event" << endl; 63 | State *s = this->curState; 64 | s->needIO(); 65 | cout << oldState->getName() << "->" << curState->getName() << endl << endl; 66 | } 67 | void finishIO() 68 | { 69 | State *oldState = curState; 70 | cout << "Finish IO event" << endl; 71 | State *s = this->curState; 72 | s->finishIO(); 73 | cout << oldState->getName() << "->" << curState->getName() << endl << endl; 74 | } 75 | private: 76 | string name; 77 | State *readyState; 78 | State *runningState; 79 | State *blockedState; 80 | State *curState; 81 | }; 82 | } 83 | 84 | #endif 85 | 86 | -------------------------------------------------------------------------------- /cp06/RemoteControl/Command.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMAND_H 2 | #define COMMAND_H 3 | 4 | #include "Light.h" 5 | #include "Stereo.h" 6 | 7 | namespace control 8 | { 9 | 10 | class Command 11 | { 12 | public: 13 | Command(){} 14 | ~Command(){} 15 | virtual void execute() = 0; 16 | }; 17 | 18 | 19 | 20 | class LightOnCommand: public Command 21 | { 22 | public: 23 | LightOnCommand(Light *l): light(l) 24 | { 25 | } 26 | ~LightOnCommand(){} 27 | 28 | void execute() 29 | { 30 | light->on(); 31 | } 32 | void undo() 33 | { 34 | light->off(); 35 | } 36 | private: 37 | Light *light; 38 | }; 39 | 40 | class LightOffCommand: public Command 41 | { 42 | public: 43 | LightOffCommand(Light *l): light(l) 44 | { 45 | } 46 | ~LightOffCommand(){} 47 | 48 | void execute() 49 | { 50 | light->off(); 51 | } 52 | void undo() 53 | { 54 | light->on(); 55 | } 56 | private: 57 | Light *light; 58 | }; 59 | 60 | class StereoOnCommand: public Command 61 | { 62 | public: 63 | StereoOnCommand(Stereo *s): stereo(s) 64 | { 65 | } 66 | ~StereoOnCommand(){} 67 | 68 | void execute() 69 | { 70 | stereo->on(); 71 | } 72 | void undo() 73 | { 74 | stereo->off(); 75 | } 76 | private: 77 | Stereo *stereo; 78 | }; 79 | 80 | class StereoOffCommand: public Command 81 | { 82 | public: 83 | StereoOffCommand(Stereo *s): stereo(s) 84 | { 85 | } 86 | ~StereoOffCommand(){} 87 | 88 | void execute() 89 | { 90 | stereo->off(); 91 | } 92 | void undo() 93 | { 94 | stereo->on(); 95 | } 96 | private: 97 | Stereo *stereo; 98 | }; 99 | 100 | 101 | class MacroCommand: public Command 102 | { 103 | public: 104 | MacroCommand(Light *l, Stereo *s): light(l), stereo(s) 105 | { 106 | } 107 | ~MacroCommand(){} 108 | 109 | void execute() 110 | { 111 | light->on(); 112 | stereo->on(); 113 | } 114 | void undo() 115 | { 116 | light->off(); 117 | stereo->off(); 118 | } 119 | private: 120 | Light *light; 121 | Stereo *stereo; 122 | }; 123 | } 124 | 125 | #endif 126 | 127 | -------------------------------------------------------------------------------- /cp10/State/ConcreteStates.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETESTATE_H 2 | #define CONCRETESTATE_H 3 | 4 | #include 5 | #include 6 | #include "Process.h" 7 | 8 | 9 | namespace state 10 | { 11 | using std::string; 12 | using std::cout; 13 | using std::endl; 14 | 15 | class ImpossibleException 16 | { 17 | }; 18 | 19 | class ReadyState: public State 20 | { 21 | public: 22 | ReadyState(Process *p, string n): State(n) 23 | { 24 | process = p; 25 | } 26 | ~ReadyState(){} 27 | 28 | void resumedByCpu() 29 | { 30 | cout << process->getName() << " is now running" << endl; 31 | process->setState(process->getRunningState()); 32 | } 33 | void timeSlotEnd() 34 | { 35 | throw ImpossibleException(); 36 | } 37 | void needIO() 38 | { 39 | throw ImpossibleException(); 40 | } 41 | void finishIO() 42 | { 43 | throw ImpossibleException(); 44 | } 45 | private: 46 | Process *process; 47 | }; 48 | class RunningState: public State 49 | { 50 | public: 51 | RunningState(Process *p, string n): State(n) 52 | { 53 | process = p; 54 | } 55 | ~RunningState(){} 56 | 57 | void resumedByCpu() 58 | { 59 | throw ImpossibleException(); 60 | } 61 | void timeSlotEnd() 62 | { 63 | cout << process->getName() << " is now ready" << endl; 64 | process->setState(process->getReadyState()); 65 | } 66 | void needIO() 67 | { 68 | cout << process->getName() << " is now blocked" << endl; 69 | process->setState(process->getBlockedState()); 70 | } 71 | void finishIO() 72 | { 73 | throw ImpossibleException(); 74 | } 75 | private: 76 | Process *process; 77 | }; 78 | class BlockedState: public State 79 | { 80 | public: 81 | BlockedState(Process *p, string n): State(n) 82 | { 83 | process = p; 84 | } 85 | ~BlockedState(){} 86 | 87 | void resumedByCpu() 88 | { 89 | throw ImpossibleException(); 90 | } 91 | void timeSlotEnd() 92 | { 93 | throw ImpossibleException(); 94 | } 95 | void needIO() 96 | { 97 | throw ImpossibleException(); 98 | } 99 | void finishIO() 100 | { 101 | cout << process->getName() << " is now ready" << endl; 102 | process->setState(process->getReadyState()); 103 | } 104 | private: 105 | Process *process; 106 | }; 107 | } 108 | 109 | #endif 110 | 111 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | --------------------------------------------------------------------------------