├── Adapter └── ducks │ ├── class_adapter │ ├── Duck.hpp │ ├── Makefile │ ├── MallardDuck.hpp │ ├── MallardDuckAdapter.hpp │ ├── SimpleRandom.hpp │ ├── Turkey.hpp │ ├── WildTurkey.hpp │ ├── WildTurkeyAdapter.hpp │ └── classAdapterTestDrive.cpp │ └── object_adapter │ ├── Duck.hpp │ ├── DuckAdapter.hpp │ ├── Makefile │ ├── MallardDuck.hpp │ ├── SimpleRandom.hpp │ ├── Turkey.hpp │ ├── TurkeyAdapter.hpp │ ├── WildTurkey.hpp │ ├── challenge │ ├── Drone.hpp │ ├── DroneAdapter.hpp │ └── SuperDrone.hpp │ ├── duckTestDrive.cpp │ ├── random_test.cpp │ └── turkeyTestDrive.cpp ├── Command ├── dinnerLambda │ ├── Cook.hpp │ ├── Customer.hpp │ ├── Makefile │ ├── Waitress.hpp │ └── dinner.cpp ├── party │ ├── CeilingFan.hpp │ ├── CeilingFanHighCommand.hpp │ ├── CeilingFanLowCommand.hpp │ ├── CeilingFanMediumCommand.hpp │ ├── CeilingFanOffCommand.hpp │ ├── Command.hpp │ ├── Hottub.hpp │ ├── HottubOffCommand.hpp │ ├── HottubOnCommand.hpp │ ├── Light.hpp │ ├── LightOffCommand.hpp │ ├── LightOnCommand.hpp │ ├── LivingRoomLightOnCommand.hpp │ ├── LivingroomLightOffCommand.hpp │ ├── MacroCommand.hpp │ ├── Makefile │ ├── NoCommand.hpp │ ├── RemoteControl.hpp │ ├── Stereo.hpp │ ├── StereoOffCommand.hpp │ ├── StereoOnCommand.hpp │ ├── TV.hpp │ ├── TVOffCommand.hpp │ ├── TVOnCommand.hpp │ ├── remoteLoader.cpp │ ├── util.cpp │ └── util.hpp ├── remote │ ├── CeilingFan.hpp │ ├── CeilingFanOffCommand.hpp │ ├── CeilingFanOnCommand.hpp │ ├── Command.hpp │ ├── GarageDoor.hpp │ ├── GarageDoorDownCommand.hpp │ ├── GarageDoorUpCommand.hpp │ ├── Hottub.hpp │ ├── HottubOffCommand.hpp │ ├── HottubOnCommand.hpp │ ├── Light.hpp │ ├── LightOffCommand.hpp │ ├── LightOnCommand.hpp │ ├── LivingRoomLightOnCommand.hpp │ ├── LivingroomLightOffCommand.hpp │ ├── Makefile │ ├── NoCommand.hpp │ ├── RemoteControl.hpp │ ├── Stereo.hpp │ ├── StereoOffCommand.hpp │ ├── StereoOnWithCDCommand.hpp │ ├── TV.hpp │ ├── TVOffCommand.hpp │ ├── TVOnCommand.hpp │ └── remoteLoader.cpp ├── simpleremote │ ├── Command.hpp │ ├── GarageDoor.hpp │ ├── GarageDoorOpen.hpp │ ├── Light.hpp │ ├── LightOnCommand.hpp │ ├── Makefile │ ├── SimpleRemoteControl.hpp │ └── remoteControlTest.cpp └── undo │ ├── CeilingFan.hpp │ ├── CeilingFanHighCommand.hpp │ ├── CeilingFanLowCommand.hpp │ ├── CeilingFanMediumCommand.hpp │ ├── CeilingFanOffCommand.hpp │ ├── Command.hpp │ ├── DimmerLightOffCommand.hpp │ ├── DimmerLightOnCommand.hpp │ ├── Light.hpp │ ├── LightOffCommand.hpp │ ├── LightOnCommand.hpp │ ├── Makefile │ ├── NoCommand.hpp │ ├── RemoteControlWithUndo.hpp │ └── remoteLoader.cpp ├── Composite ├── menu │ ├── Makefile │ ├── Menu.h │ ├── MenuComponent.h │ ├── MenuItem.h │ ├── Waitress.h │ └── menuTestDrive.cpp └── menuiterator │ ├── CompositeIterator.h │ ├── Iterator.h │ ├── Makefile │ ├── Menu.h │ ├── MenuComponent.h │ ├── MenuItem.h │ ├── MenuIterator.h │ ├── NullIterator.h │ ├── Waitress.h │ └── menuTestDrive.cpp ├── Decorator └── starbuzz │ ├── Beverage.h │ ├── CondimentDecorator.h │ ├── DarkRoast.h │ ├── Decaf.h │ ├── Espresso.h │ ├── HouseBlend.h │ ├── Makefile │ ├── Mocha.h │ ├── Soy.h │ ├── Whip.h │ └── starBuzzCoffee.cpp ├── Facade └── hometheater │ ├── Amplifier.h │ ├── CDPlayer.h │ ├── HomeTheaterFacade.h │ ├── Makefile │ ├── PopcornPopper.h │ ├── Projector.h │ ├── Screen.h │ ├── StreamingPlayer.h │ ├── TheaterLights.h │ ├── Tuner.h │ └── homeTheaterTestDrive.cpp ├── Factory ├── pizzaaf │ ├── BlackOlives.hpp │ ├── Cheese.hpp │ ├── CheesePizza.hpp │ ├── ChicagoPizzaIngredientFactory.hpp │ ├── ChicagoPizzaStore.hpp │ ├── ClamPizza.hpp │ ├── Clams.hpp │ ├── Dough.hpp │ ├── Eggplant.hpp │ ├── FreshClams.hpp │ ├── FrozenClams.hpp │ ├── Garlic.hpp │ ├── Makefile │ ├── MarinaraSauce.hpp │ ├── MozzarellaCheese.hpp │ ├── Mushroom.hpp │ ├── NYPizzaIngredientFactory.hpp │ ├── NYPizzaStore.hpp │ ├── Onion.hpp │ ├── ParmesanCheese.hpp │ ├── Pepperoni.hpp │ ├── PepperoniPizza.hpp │ ├── Pizza.hpp │ ├── PizzaIngredientFactory.hpp │ ├── PizzaStore.hpp │ ├── PlumTomatoSauce.hpp │ ├── RedPepper.hpp │ ├── ReggianoCheese.hpp │ ├── Sauce.hpp │ ├── SlicedPepperoni.hpp │ ├── Spinach.hpp │ ├── ThickCrustDough.hpp │ ├── ThinCrustDough.hpp │ ├── VeggiePizza.hpp │ ├── Veggies.hpp │ ├── pizzaTestDrive.cpp │ └── test.cpp ├── pizzafm │ ├── ChicagoPizzaStore.hpp │ ├── ChicagoStyleCheesePizza.hpp │ ├── ChicagoStyleClamPizza.hpp │ ├── ChicagoStylePepperoniPizza.hpp │ ├── ChicagoStyleVeggiePizza.hpp │ ├── Makefile │ ├── NYPizzaStore.hpp │ ├── NYStyleCheesePizza.hpp │ ├── NYStyleClamPizza.hpp │ ├── NYStylePepperoniPizza.hpp │ ├── NYStyleVeggiePizza.hpp │ ├── Pizza.hpp │ ├── PizzaStore.hpp │ └── pizzaTestDrive.cpp └── pizzas │ ├── CheesePizza.hpp │ ├── ClamPizza.hpp │ ├── Makefile │ ├── PepperoniPizza.hpp │ ├── Pizza.hpp │ ├── PizzaStore.hpp │ ├── SimplePizzaFactory.hpp │ ├── VeggiePizza.hpp │ └── pizzaTestDrive.cpp ├── Iterator ├── dinermerger │ ├── DinerMenu.h │ ├── DinerMenuIterator.h │ ├── Iterator.h │ ├── Makefile │ ├── MenuItem.h │ ├── PancakeHouseMenu.h │ ├── PancakeHouseMenuIterator.h │ ├── Waitress.h │ └── menuTestDrive.cpp ├── dinermergercafe │ ├── AlternatingDinerMenuIterator.h │ ├── CafeMenu.h │ ├── CafeMenuIterator.h │ ├── DinerMenu.h │ ├── DinerMenuIterator.h │ ├── Iterator.h │ ├── Makefile │ ├── Menu.h │ ├── MenuItem.h │ ├── PancakeHouseMenu.h │ ├── PancakeHouseMenuIterator.h │ ├── Waitress.h │ └── menuTestDrive.cpp └── dinermergeri │ ├── AlternatingDinerMenuIterator.h │ ├── DinerMenu.h │ ├── DinerMenuIterator.h │ ├── Iterator.h │ ├── Makefile │ ├── Menu.h │ ├── MenuItem.h │ ├── PancakeHouseMenu.h │ ├── PancakeHouseMenuIterator.h │ ├── Waitress.h │ └── menuTestDrive.cpp ├── Observer ├── weather │ ├── CurrentConditionsDisplay.h │ ├── DisplayElement.h │ ├── ForcastDisplay.h │ ├── HeatIndex.h │ ├── Makefile │ ├── Observer.h │ ├── StatisticsDisplay.h │ ├── Subject.h │ ├── WeatherData.h │ └── weatherStation.cpp └── weatherobservable │ ├── CurrentConditionsDisplay.h │ ├── DataObject.h │ ├── DisplayElement.h │ ├── ForcastDisplay.h │ ├── HeatIndex.h │ ├── Makefile │ ├── Observer.h │ ├── StatisticsDisplay.h │ ├── Subject.h │ ├── WeatherData.h │ └── weatherStation.cpp ├── README.md ├── Singleton ├── chocolate │ ├── ChocolateBoiler.hpp │ ├── Makefile │ └── chocolateController.cpp ├── classic │ └── Singleton.hpp ├── dcl │ ├── Makefile │ ├── Singleton.hpp │ └── singletonClient.cpp ├── stat │ ├── Makefile │ ├── Singleton.hpp │ └── singletonClient.cpp ├── subclass │ ├── CoolerSingleton.hpp │ ├── HotterSingleton.hpp │ ├── Makefile │ ├── Singleton.hpp │ └── singleTestDrive.cpp └── threadsafe │ └── Singleton.hpp ├── State ├── gumball │ ├── GumballMachine.h │ ├── Makefile │ └── gumballMachineTestDrive.cpp ├── gumballstate │ ├── GumballMachine.cpp │ ├── GumballMachine.h │ ├── HasQuarterState.cpp │ ├── HasQuarterState.h │ ├── Makefile │ ├── NoQuarterState.cpp │ ├── NoQuarterState.h │ ├── SoldOutState.cpp │ ├── SoldOutState.h │ ├── SoldState.cpp │ ├── SoldState.h │ ├── State.h │ └── gumballMachineTestDrive.cpp └── gumballstatewinner │ ├── GumballMachine.cpp │ ├── GumballMachine.h │ ├── HasQuarterState.cpp │ ├── HasQuarterState.h │ ├── Makefile │ ├── NoQuarterState.cpp │ ├── NoQuarterState.h │ ├── SimpleRandom.cpp │ ├── SimpleRandom.h │ ├── SoldOutState.cpp │ ├── SoldOutState.h │ ├── SoldState.cpp │ ├── SoldState.h │ ├── State.h │ ├── WinnerState.cpp │ ├── WinnerState.h │ └── gumballMachineTestDrive.cpp ├── Strategy ├── Duck.h ├── DuckCall.h ├── FlyBehavior.h ├── FlyNoWay.h ├── FlyRocketPowered.h ├── FlyWithWings.h ├── Makefile ├── MallardDuck.h ├── ModelDuck.h ├── MuteDuck.h ├── MuteQuack.h ├── Quack.h ├── QuackBehavior.h ├── QuackMute.h ├── Squeak.h └── miniDuckSimulator.cpp └── TemplateMethod ├── barista ├── CaffeineBeverage.h ├── CaffeineBeverageWithHook.h ├── Coffee.h ├── CoffeeWithHook.h ├── Makefile ├── Tea.h ├── TeaWithHook.h └── baristaTestDrive.cpp └── sort ├── Duck.h ├── Makefile └── duckSortTestDrive.cpp /Adapter/ducks/class_adapter/Duck.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DUCK_H 2 | #define DUCK_H 3 | 4 | class Duck { 5 | public: 6 | virtual ~Duck () = default; 7 | virtual void quack() = 0; 8 | virtual void fly() = 0; 9 | }; 10 | 11 | #endif /* DUCK_H */ 12 | -------------------------------------------------------------------------------- /Adapter/ducks/class_adapter/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = Duck.hpp MallardDuck.hpp TurkeyAdapter.hpp Turkey.hpp WildTurkey.hpp 4 | 5 | TARGET = classAdapterTestDrive 6 | OBJ = $(TARGET).o 7 | 8 | %.o: %.cpp $(DEPS) 9 | $(CXX) -c -o $@ $< $(CFLAGS) 10 | 11 | $(TARGET): $(OBJ) 12 | $(CXX) -o $@ $^ $(CFLAGS) 13 | 14 | clean: 15 | rm -f *.o $(TARGET) 16 | -------------------------------------------------------------------------------- /Adapter/ducks/class_adapter/MallardDuck.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MALLARD_DUCK_H 2 | #define MALLARD_DUCK_H 3 | 4 | #include "Duck.hpp" 5 | #include 6 | 7 | class MallardDuck : public Duck { 8 | public: 9 | void quack() override { std::cout << "Quack\n"; } 10 | void fly() override { std::cout << "I'm flying\n"; } 11 | }; 12 | 13 | #endif /* MALLARD_DUCK_H */ 14 | -------------------------------------------------------------------------------- /Adapter/ducks/class_adapter/MallardDuckAdapter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MALLARD_DUCK_ADAPTER_H 2 | #define MALLARD_DUCK_ADAPTER_H 3 | 4 | #include "Turkey.hpp" 5 | #include "MallardDuck.hpp" 6 | #include "SimpleRandom.hpp" 7 | 8 | class MallardDuckAdapter : public Turkey, private MallardDuck { 9 | public: 10 | MallardDuckAdapter() = default; 11 | void gobble() override { MallardDuck::quack(); } 12 | void fly() override; 13 | private: 14 | SimpleRandom rand; 15 | 16 | }; 17 | 18 | inline 19 | void 20 | MallardDuckAdapter::fly() 21 | { 22 | if (rand.nextInt(5) == 0) 23 | MallardDuck::fly(); 24 | } 25 | 26 | #endif /* MALLARD_DUCK_ADAPTER_H */ 27 | -------------------------------------------------------------------------------- /Adapter/ducks/class_adapter/SimpleRandom.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLE_RANDOM_H 2 | #define SIMPLE_RANDOM_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class SimpleRandom { 9 | public: 10 | using rand_type = std::default_random_engine::result_type; 11 | SimpleRandom() = default; 12 | rand_type nextInt(const unsigned i); 13 | private: 14 | std::default_random_engine e = std::default_random_engine(std::time(nullptr)); 15 | std::uniform_int_distribution u; 16 | }; 17 | 18 | 19 | SimpleRandom::rand_type 20 | SimpleRandom::nextInt(const unsigned i) 21 | { 22 | u = std::uniform_int_distribution(0,i); 23 | e.discard(1); 24 | return u(e); 25 | } 26 | 27 | #endif /* SIMPLE_RANDOM_H */ 28 | -------------------------------------------------------------------------------- /Adapter/ducks/class_adapter/Turkey.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TURKEY_H 2 | #define TURKEY_H 3 | 4 | class Turkey { 5 | public: 6 | virtual ~Turkey () = default; 7 | virtual void gobble() = 0; 8 | virtual void fly() = 0; 9 | }; 10 | 11 | #endif /* TURKEY_H */ 12 | -------------------------------------------------------------------------------- /Adapter/ducks/class_adapter/WildTurkey.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WILD_TURKEY_H 2 | #define WILD_TURKEY_H 3 | 4 | #include "Turkey.hpp" 5 | #include 6 | 7 | class WildTurkey : public Turkey { 8 | public: 9 | void gobble() override { std::cout << "Gobble, gooble\n"; } 10 | void fly() override { std::cout << "I'm flying a short distance\n"; } 11 | }; 12 | 13 | #endif /* WILD_TURKEY_H */ 14 | -------------------------------------------------------------------------------- /Adapter/ducks/class_adapter/WildTurkeyAdapter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WILD_TURKEY_ADAPTER_H 2 | #define WILD_TURKEY_ADAPTER_H 3 | 4 | #include "Duck.hpp" 5 | #include "Turkey.hpp" 6 | #include "WildTurkey.hpp" 7 | #include 8 | 9 | /* 10 | * Multiple Inheritance: 11 | * Inherit publicly from target (Duck) and privately from adaptee (WildTurky) thus the adapter would be a subclass of target but not the adaptee (Design Pattern, 144). The former is an interface (abstract) while the later is an implimentation (concrete). 12 | */ 13 | 14 | class WildTurkeyAdapter : public Duck, private WildTurkey { 15 | public: 16 | WildTurkeyAdapter() = default; 17 | void quack() override { WildTurkey::gobble(); } 18 | void fly() override; 19 | }; 20 | 21 | inline 22 | void 23 | WildTurkeyAdapter::fly() 24 | { 25 | for (int i = 0; i < 5; ++i) 26 | WildTurkey::fly(); 27 | } 28 | 29 | #endif /* WILD_TURKEY_ADAPTER_H */ 30 | -------------------------------------------------------------------------------- /Adapter/ducks/class_adapter/classAdapterTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "Duck.hpp" 2 | #include "Turkey.hpp" 3 | #include "WildTurkey.hpp" 4 | #include "WildTurkeyAdapter.hpp" 5 | #include "MallardDuck.hpp" 6 | #include "MallardDuckAdapter.hpp" 7 | #include "WildTurkey.hpp" 8 | 9 | #include 10 | 11 | void testDuck(Duck *duck) { 12 | duck->quack(); 13 | duck->fly(); 14 | } 15 | 16 | void testTurkey(Turkey *turkey) { 17 | turkey->gobble(); 18 | turkey->fly(); 19 | } 20 | 21 | int main() 22 | { 23 | auto turkey = WildTurkey(); 24 | std::cout << "The Turkey says...\n"; 25 | turkey.gobble(); 26 | turkey.fly(); 27 | 28 | auto wildTurkeyAdapter = WildTurkeyAdapter(); // instantiate adapter 29 | std::cout << "\nThe TurkeyAdaptor says...\n"; 30 | testDuck(&wildTurkeyAdapter); // pass it as Duck 31 | 32 | auto mallardDuckAdapter = MallardDuckAdapter(); // instantiate 33 | std::cout << "\nThe DuckAdaptor says...\n"; 34 | for (int i = 0; i < 10; ++i) 35 | testTurkey(&mallardDuckAdapter); // pass it as Turkey 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/Duck.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DUCK_H 2 | #define DUCK_H 3 | 4 | class Duck { 5 | public: 6 | virtual ~Duck () = default; 7 | virtual void quack() = 0; 8 | virtual void fly() = 0; 9 | }; 10 | 11 | #endif /* DUCK_H */ 12 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/DuckAdapter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DUCK_ADAPTER_H 2 | #define DUCK_ADAPTER_H 3 | 4 | #include "Duck.hpp" 5 | #include "Turkey.hpp" 6 | #include "SimpleRandom.hpp" // mimics Java's Random 7 | #include 8 | #include 9 | #include 10 | 11 | class DuckAdapter : public Turkey { 12 | public: 13 | DuckAdapter() = default; 14 | DuckAdapter(Duck *tur) : turkey(tur) { } 15 | void gobble() override { turkey->quack(); } 16 | void fly() override; 17 | private: 18 | Duck *turkey; 19 | SimpleRandom rand; // See header for implementation 20 | }; 21 | 22 | inline 23 | void 24 | DuckAdapter::fly() 25 | { 26 | if (rand.nextInt(5) == 0) 27 | turkey->fly(); 28 | } 29 | 30 | #endif /* DUCK_ADAPTER_H */ 31 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = DuckAdapter.hpp Duck.hpp MallardDuck.hpp SimpleRandom.hpp \ 4 | TurkeyAdapter.hpp Turkey.hpp WildTurkey.hpp challenge/Drone.hpp \ 5 | challenge/DroneAdapter.hpp challenge/SuperDrone.hpp 6 | TARGET = duckTestDrive 7 | TARGET2 = turkeyTestDrive 8 | OBJ = $(TARGET).o 9 | OBJ2 = $(TARGET2).o 10 | 11 | %.o: %.cpp $(DEPS) 12 | $(CXX) -c -o $@ $< $(CFLAGS) 13 | 14 | $(TARGET): $(OBJ) 15 | $(CXX) -o $@ $^ $(CFLAGS) 16 | 17 | $(TARGET2): $(OBJ2) 18 | $(CXX) -o $@ $^ $(CFLAGS) 19 | 20 | clean: 21 | rm -f *.o $(TARGET) $(TARGET2) 22 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/MallardDuck.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MALLARD_DUCK_H 2 | #define MALLARD_DUCK_H 3 | 4 | #include "Duck.hpp" 5 | #include 6 | 7 | class MallardDuck : public Duck { 8 | public: 9 | void quack() override { std::cout << "Quack\n"; } 10 | void fly() override { std::cout << "I'm flying\n"; } 11 | }; 12 | 13 | #endif /* MALLARD_DUCK_H */ 14 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/SimpleRandom.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLE_RANDOM_H 2 | #define SIMPLE_RANDOM_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class SimpleRandom { 9 | public: 10 | using rand_type = std::default_random_engine::result_type; 11 | SimpleRandom() = default; 12 | rand_type nextInt(const unsigned i); 13 | private: 14 | std::default_random_engine e = std::default_random_engine(std::time(nullptr)); 15 | std::uniform_int_distribution u; 16 | }; 17 | 18 | 19 | inline 20 | SimpleRandom::rand_type 21 | SimpleRandom::nextInt(const unsigned i) 22 | { 23 | u = std::uniform_int_distribution(0,i); 24 | e.discard(1); 25 | return u(e); 26 | } 27 | 28 | #endif /* SIMPLE_RANDOM_H */ 29 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/Turkey.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TURKEY_H 2 | #define TURKEY_H 3 | 4 | class Turkey { 5 | public: 6 | virtual ~Turkey () = default; 7 | virtual void gobble() = 0; 8 | virtual void fly() = 0; 9 | }; 10 | 11 | #endif /* TURKEY_H */ 12 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/TurkeyAdapter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TURKEY_ADAPTER_H 2 | #define TURKEY_ADAPTER_H 3 | 4 | #include "Duck.hpp" 5 | #include "Turkey.hpp" 6 | #include 7 | 8 | class TurkeyAdapter : public Duck { 9 | public: 10 | TurkeyAdapter(Turkey *tur) : turkey(tur) { } 11 | void quack() override { turkey->gobble(); } 12 | void fly() override; 13 | private: 14 | Turkey *turkey; 15 | }; 16 | 17 | inline 18 | void 19 | TurkeyAdapter::fly() 20 | { 21 | for (int i = 0; i < 5; ++i) 22 | turkey->fly(); 23 | } 24 | 25 | #endif /* TURKEY_ADAPTER_H */ 26 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/WildTurkey.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WILD_TURKEY_H 2 | #define WILD_TURKEY_H 3 | 4 | #include "Turkey.hpp" 5 | #include 6 | 7 | class WildTurkey : public Turkey { 8 | public: 9 | void gobble() override { std::cout << "Gobble, gobble\n"; } 10 | void fly() override { std::cout << "I'm flying a short distance\n"; } 11 | }; 12 | 13 | #endif /* WILD_TURKEY_H */ 14 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/challenge/Drone.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DRONE_H 2 | #define DRONE_H 3 | 4 | class Drone { 5 | public: 6 | virtual ~Drone () = default; 7 | virtual void beep() = 0; 8 | virtual void spin_rotors() = 0; 9 | virtual void take_off() = 0; 10 | }; 11 | 12 | #endif /* DRONE_H */ 13 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/challenge/DroneAdapter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DRONE_ADAPTER_H 2 | #define DRONE_ADAPTER_H 3 | 4 | #include "../Duck.hpp" 5 | #include "Drone.hpp" 6 | 7 | class DroneAdapter : public Duck { 8 | public: 9 | DroneAdapter(Drone *d) : drone(d) {} 10 | virtual ~DroneAdapter() = default; 11 | void fly() override { drone->spin_rotors(); drone->take_off(); } 12 | void quack() override { drone->beep(); } 13 | private: 14 | Drone *drone; 15 | 16 | }; 17 | 18 | #endif /* DRONE_ADAPTER_H */ 19 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/challenge/SuperDrone.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SUPER_DRONE_H 2 | #define SUPER_DRONE_H 3 | 4 | #include "Drone.hpp" 5 | #include 6 | 7 | class SuperDrone : public Drone { 8 | public: 9 | void beep() override { std::cout << "Beep beep beep\n"; } 10 | void spin_rotors() override { std::cout << "Rotors are spinning\n"; } 11 | void take_off() override { std::cout << "Taking off\n"; } 12 | }; 13 | 14 | #endif /* SUPER_DRONE_H */ 15 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/duckTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "challenge/Drone.hpp" 2 | #include "challenge/DroneAdapter.hpp" 3 | #include "challenge/SuperDrone.hpp" 4 | #include "Duck.hpp" 5 | #include "Turkey.hpp" 6 | #include "MallardDuck.hpp" 7 | #include "TurkeyAdapter.hpp" 8 | #include "WildTurkey.hpp" 9 | #include 10 | 11 | void testDuck(Duck *duck) { 12 | duck->quack(); 13 | duck->fly(); 14 | } 15 | 16 | void testTurkey(Turkey *turkey) { 17 | turkey->gobble(); 18 | turkey->fly(); 19 | } 20 | 21 | int main() 22 | { 23 | auto *duck = new MallardDuck(); 24 | 25 | auto turkey = new WildTurkey(); 26 | auto turkeyAdapter = new TurkeyAdapter(turkey); 27 | 28 | std::cout << "The Turkey says...\n"; 29 | testTurkey(turkey); 30 | 31 | std::cout << "\nThe Duck says...\n"; 32 | testDuck(duck); 33 | 34 | std::cout << "\nThe TurkeyAdaptor says...\n"; 35 | testDuck(turkeyAdapter); 36 | 37 | // Challenge 38 | auto drone = new SuperDrone(); 39 | auto droneAdapter = new DroneAdapter(drone); 40 | std::cout << "\nDroneAdapter says...\n"; 41 | testDuck(droneAdapter); 42 | 43 | delete duck; 44 | delete turkey; 45 | delete turkeyAdapter; 46 | delete drone; 47 | delete droneAdapter; 48 | 49 | return 0; 50 | } 51 | 52 | 53 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/random_test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "SimpleRandom.hpp" 6 | 7 | int main() 8 | { 9 | std::default_random_engine e(std::time(nullptr)); 10 | std::uniform_int_distribution u(0,5); 11 | 12 | e.discard(1); 13 | for (size_t i = 0; i < 10; i++) 14 | std::cout << ' ' << u(e); 15 | std::cout << '\n'; 16 | 17 | auto rand = SimpleRandom(); 18 | for (size_t i = 0; i < 10; i++) 19 | std::cout << rand.nextInt(5); 20 | std::cout << '\n'; 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Adapter/ducks/object_adapter/turkeyTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "Turkey.hpp" 2 | #include "DuckAdapter.hpp" 3 | #include "MallardDuck.hpp" 4 | #include 5 | 6 | int main() 7 | { 8 | auto duck = new MallardDuck(); 9 | Turkey *duckAdapter = new DuckAdapter(duck); 10 | for (int i = 0; i < 10; ++i) { 11 | std::cout << "The DuckAdapter says...\n"; 12 | duckAdapter->gobble(); 13 | duckAdapter->fly(); 14 | } 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /Command/dinnerLambda/Cook.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COOK_H 2 | #define COOK_H 3 | 4 | #include 5 | 6 | // Receiver 7 | 8 | class Cook { 9 | public: 10 | void makeBurger() { std::cout << "Making a burger\n"; } 11 | void makeFries() { std::cout << "Making fries\n"; } 12 | }; 13 | 14 | #endif /* COOK_H */ 15 | -------------------------------------------------------------------------------- /Command/dinnerLambda/Customer.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CUSTOMER_H 2 | #define CUSTOMER_H 3 | 4 | #include "Cook.hpp" 5 | #include "Waitress.hpp" 6 | #include 7 | 8 | // Invoker 9 | class Customer { 10 | public: 11 | Customer(Waitress *w, Cook *c) : waitress(w), cook(c) {} 12 | void createOrder() { o = [&]() { cook->makeBurger(); cook->makeFries(); }; } 13 | void hungry() { waitress->takeOrder(o); } 14 | private: 15 | Waitress *waitress; 16 | Cook *cook; 17 | std::function o; 18 | }; 19 | 20 | #endif /* CUSTOMER_H */ 21 | -------------------------------------------------------------------------------- /Command/dinnerLambda/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = Cook.hpp Customer.hpp Waitress.hpp 4 | TARGET = dinner 5 | OBJ = $(TARGET).o 6 | 7 | %.o: %.cpp $(DEPS) 8 | $(CXX) -c -o $@ $< $(CFLAGS) 9 | 10 | $(TARGET): $(OBJ) 11 | $(CXX) -o $@ $^ $(CFLAGS) 12 | 13 | clean: 14 | rm -f *.o $(TARGET) 15 | -------------------------------------------------------------------------------- /Command/dinnerLambda/Waitress.hpp: -------------------------------------------------------------------------------- 1 | #ifndef WAITRESS_H 2 | #define WAITRESS_H 3 | 4 | #include 5 | 6 | class Waitress { 7 | public: 8 | Waitress () = default; 9 | void takeOrder(std::function order) { order(); } 10 | }; 11 | 12 | #endif /* WAITRESS_H */ 13 | -------------------------------------------------------------------------------- /Command/dinnerLambda/dinner.cpp: -------------------------------------------------------------------------------- 1 | #include "Cook.hpp" 2 | #include "Waitress.hpp" 3 | #include "Customer.hpp" 4 | 5 | int main() 6 | { 7 | auto cook = Cook(); 8 | auto waitress = Waitress(); 9 | auto customer = Customer(&waitress, &cook); 10 | 11 | customer.createOrder(); 12 | customer.hungry(); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Command/party/CeilingFanHighCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_HIGH_COMMAND_H 2 | #define CEILING_FAN_HIGH_COMMAND_H 3 | 4 | #include "CeilingFan.hpp" 5 | #include "Command.hpp" 6 | #include 7 | 8 | class CeilingFanHighCommand : public Command { 9 | public: 10 | CeilingFanHighCommand(CeilingFan *cf) : ceilingFan(cf) { } 11 | void execute() override; 12 | void undo() override; 13 | std::string getClassName() const override { return className; }; 14 | private: 15 | CeilingFan *ceilingFan; 16 | CeilingFan::Speed prevSpeed; 17 | std::string className = "CeilingFanHighCommand"; 18 | }; 19 | 20 | inline 21 | void 22 | CeilingFanHighCommand::execute() 23 | { 24 | prevSpeed = ceilingFan->getSpeed(); 25 | ceilingFan->high(); 26 | } 27 | 28 | inline 29 | void 30 | CeilingFanHighCommand::undo() 31 | { 32 | switch (prevSpeed) { 33 | case CeilingFan::Speed::HIGH: 34 | ceilingFan->high(); 35 | break; 36 | case CeilingFan::Speed::MEDIUM: 37 | ceilingFan->medium(); 38 | break; 39 | case CeilingFan::Speed::LOW: 40 | ceilingFan->low(); 41 | break; 42 | case CeilingFan::Speed::OFF: 43 | ceilingFan->off(); 44 | break; 45 | default: 46 | break; 47 | } 48 | } 49 | 50 | #endif /* CEILING_FAN_HIGH_COMMAND_H */ 51 | -------------------------------------------------------------------------------- /Command/party/CeilingFanLowCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_LOW_COMMAND_H 2 | #define CEILING_FAN_LOW_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | #include 7 | 8 | class CeilingFanLowCommand : public Command 9 | { 10 | public: 11 | CeilingFanLowCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override; 13 | void undo() override; 14 | std::string getClassName() const override { return className; } 15 | private: 16 | CeilingFan *ceilingFan; 17 | CeilingFan::Speed prevSpeed; 18 | std::string className = "CeilingFanLowCommand"; 19 | }; 20 | 21 | inline 22 | void 23 | CeilingFanLowCommand::execute() 24 | { 25 | prevSpeed = ceilingFan->getSpeed(); 26 | ceilingFan->low(); 27 | } 28 | 29 | inline 30 | void 31 | CeilingFanLowCommand::undo() 32 | { 33 | switch (prevSpeed) { 34 | case CeilingFan::Speed::HIGH: 35 | ceilingFan->high(); 36 | break; 37 | case CeilingFan::Speed::MEDIUM: 38 | ceilingFan->medium(); 39 | break; 40 | case CeilingFan::Speed::LOW: 41 | ceilingFan->low(); 42 | break; 43 | case CeilingFan::Speed::OFF: 44 | ceilingFan->off(); 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | 51 | #endif /* CEILING_FAN_LOW_COMMAN_H */ 52 | -------------------------------------------------------------------------------- /Command/party/CeilingFanMediumCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_MEDIUM_COMMAND_H 2 | #define CEILING_FAN_MEDIUM_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | #include 7 | 8 | class CeilingFanMediumCommand : public Command 9 | { 10 | public: 11 | CeilingFanMediumCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override; 13 | void undo() override; 14 | std::string getClassName() const override { return className; } 15 | private: 16 | CeilingFan *ceilingFan; 17 | CeilingFan::Speed prevSpeed; 18 | std::string className = "CeilingFanMediumCommand"; 19 | }; 20 | 21 | inline 22 | void 23 | CeilingFanMediumCommand::execute() 24 | { 25 | prevSpeed = ceilingFan->getSpeed(); 26 | ceilingFan->medium(); 27 | } 28 | 29 | inline 30 | void 31 | CeilingFanMediumCommand::undo() 32 | { 33 | switch (prevSpeed) { 34 | case CeilingFan::Speed::HIGH: 35 | ceilingFan->high(); 36 | break; 37 | case CeilingFan::Speed::MEDIUM: 38 | ceilingFan->medium(); 39 | break; 40 | case CeilingFan::Speed::LOW: 41 | ceilingFan->low(); 42 | break; 43 | case CeilingFan::Speed::OFF: 44 | ceilingFan->off(); 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | 51 | #endif /* CEILING_FAN_MEDIUM_COMMAN_H */ 52 | -------------------------------------------------------------------------------- /Command/party/CeilingFanOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_OFF_COMMAND_H 2 | #define CEILING_FAN_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | #include 7 | 8 | class CeilingFanOffCommand : public Command 9 | { 10 | public: 11 | CeilingFanOffCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override; 13 | void undo() override; 14 | std::string getClassName() const override { return className; } 15 | private: 16 | CeilingFan *ceilingFan; 17 | CeilingFan::Speed prevSpeed; 18 | std::string className = "CeilingFanOffCommand"; 19 | }; 20 | 21 | inline 22 | void 23 | CeilingFanOffCommand::execute() 24 | { 25 | prevSpeed = ceilingFan->getSpeed(); 26 | ceilingFan->off(); 27 | } 28 | 29 | inline 30 | void 31 | CeilingFanOffCommand::undo() 32 | { 33 | switch (prevSpeed) { 34 | case CeilingFan::Speed::HIGH: 35 | ceilingFan->high(); 36 | break; 37 | case CeilingFan::Speed::MEDIUM: 38 | ceilingFan->medium(); 39 | break; 40 | case CeilingFan::Speed::LOW: 41 | ceilingFan->low(); 42 | break; 43 | case CeilingFan::Speed::OFF: 44 | ceilingFan->off(); 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | 51 | #endif /* CEILING_FAN_OFF_COMMAN_H */ 52 | -------------------------------------------------------------------------------- /Command/party/Command.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMMAND_H 2 | #define COMMAND_H 3 | 4 | #include 5 | #include 6 | 7 | class Command { 8 | public: 9 | using command_vec = std::vector; // ptr to Command* vector type 10 | using cvec_size = std::vector::size_type; // ptr to command vector size type 11 | virtual ~Command () = default; 12 | virtual void execute() = 0; 13 | virtual void undo() = 0; 14 | virtual std::string getClassName() const = 0; 15 | }; 16 | 17 | #endif /* COMMAND_H */ 18 | -------------------------------------------------------------------------------- /Command/party/Hottub.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOTTUB_H 2 | #define HOTTUB_H 3 | 4 | #include 5 | 6 | class Hottub { 7 | public: 8 | void on() { on_state = true; } 9 | void off() { on_state = false; } 10 | void circulate() { 11 | if (on_state) std::cout << "Hottub is bubbling\n"; } 12 | void jetsOn() { std::cout << "Hottub jets are On\n"; } 13 | void jetsOff() { std::cout << "Hottub jets are Off\n"; } 14 | void setTemperature(const int &temp); 15 | private: 16 | bool on_state; 17 | int temperature; 18 | }; 19 | 20 | inline 21 | void 22 | Hottub::setTemperature(const int &temp) 23 | { 24 | if (temp > temperature) 25 | std::cout << "Hottub is heating to a steaming " << temp << " degrees\n"; 26 | else 27 | std::cout << "Hottub is cooling to " << temp << " degrees\n"; 28 | temperature = temp; 29 | } 30 | 31 | #endif /* HOTTUB_H */ 32 | -------------------------------------------------------------------------------- /Command/party/HottubOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOTTUB_OFF_COMMAND_H 2 | #define HOTTUB_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Hottub.hpp" 6 | #include "string" 7 | 8 | class HottubOffCommand : public Command { 9 | public: 10 | HottubOffCommand(Hottub *ht) : hottub(ht) { } 11 | void execute() override; 12 | void undo() override { hottub->on(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | Hottub *hottub; 16 | std::string className = "HottubOffCommand"; 17 | }; 18 | 19 | inline 20 | void HottubOffCommand::execute() 21 | { 22 | hottub->off(); 23 | hottub->setTemperature(98); 24 | hottub->circulate(); 25 | } 26 | 27 | #endif /* HOTTUB_OFF_COMMAND_H */ 28 | -------------------------------------------------------------------------------- /Command/party/HottubOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOTTUB_ON_COMMAND_H 2 | #define HOTTUB_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Hottub.hpp" 6 | #include "string" 7 | 8 | class HottubOnCommand : public Command { 9 | public: 10 | HottubOnCommand(Hottub *ht) : hottub(ht) { } 11 | void execute() override; 12 | void undo() override { hottub->off(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | Hottub *hottub; 16 | std::string className = "HottubOnCommand"; 17 | }; 18 | 19 | inline 20 | void 21 | HottubOnCommand::execute() 22 | { 23 | hottub->on(); 24 | hottub->setTemperature(104); 25 | hottub->circulate(); 26 | } 27 | 28 | #endif /* HOTTUB_ON_COMMAND_H */ 29 | -------------------------------------------------------------------------------- /Command/party/Light.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_H 2 | #define LIGHT_H 3 | 4 | #include 5 | #include 6 | 7 | class Light { 8 | public: 9 | Light(const std::string &l) : location(l) { } 10 | void on(); 11 | void off(); 12 | void dim(int l); 13 | int getLevel() const { return level; } 14 | private: 15 | std::string location; 16 | int level; 17 | }; 18 | 19 | inline 20 | void 21 | Light::on() 22 | { 23 | level = 100; 24 | std::cout << location << " Light is On\n"; 25 | } 26 | 27 | inline 28 | void 29 | Light::off() 30 | { 31 | level = 0; 32 | std::cout << location << " Light is Off\n"; 33 | } 34 | 35 | inline 36 | void 37 | Light::dim(int l) 38 | { 39 | level = l; 40 | if (level == 0) 41 | off(); 42 | else 43 | std::cout << "Light is dimmed to " << level << "%\n"; 44 | } 45 | 46 | #endif /* LIGHT_H */ 47 | -------------------------------------------------------------------------------- /Command/party/LightOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_OFF_COMMAND_H 2 | #define LIGHT_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | #include 7 | 8 | class LightOffCommand : public Command { 9 | public: 10 | LightOffCommand(Light *l) : light(l) { } 11 | void execute() override { light->off(); } 12 | void undo() override { light->on(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | Light *light; 16 | std::string className = "LightOffCommand"; 17 | }; 18 | 19 | #endif /* LIGHT_OFF_COMMAND_H */ 20 | -------------------------------------------------------------------------------- /Command/party/LightOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_ON_COMMAND_H 2 | #define LIGHT_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | #include 7 | 8 | class LightOnCommand : public Command { 9 | public: 10 | LightOnCommand(Light *l) : light(l) { } 11 | void execute() override { light->on(); } 12 | void undo() override { light->off(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | Light *light; 16 | std::string className = "LightOnCommand"; 17 | }; 18 | 19 | #endif /* LIGHT_ON_COMMAND_H */ 20 | -------------------------------------------------------------------------------- /Command/party/LivingRoomLightOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIVING_ROOM_LIGHT_ON_COMMAND_H 2 | #define LIVING_ROOM_LIGHT_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | 7 | class LivingroomLightOnCommand : public Command { 8 | public: 9 | LivingroomLightOnCommand(Light *l) : light(l) {} 10 | void execute() override { light->on(); } 11 | void undo() override { light->off(); } 12 | private: 13 | Light *light; 14 | }; 15 | 16 | #endif /* LIVING_ROOM_LIGHT_ON_COMMAND_H */ 17 | -------------------------------------------------------------------------------- /Command/party/LivingroomLightOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIVING_ROOM_LIGHT_OFF_COMMAND_H 2 | #define LIVING_ROOM_LIGHT_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | 7 | class LivingroomLightOffCommand : public Command { 8 | public: 9 | LivingroomLightOffCommand(Light *l) : light(l) {} 10 | void execute() override { light->off(); } 11 | void undo() override { light->on(); } 12 | 13 | private: 14 | Light *light; 15 | }; 16 | 17 | #endif /* LIVING_ROOM_LIGHT_OFF_COMMAND_H */ 18 | -------------------------------------------------------------------------------- /Command/party/MacroCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MACRO_COMMAND_H 2 | #define MACRO_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include 6 | 7 | class MacroCommand : public Command { 8 | public: 9 | MacroCommand(const Command::command_vec &c) : commands(c) { } 10 | void execute() override; 11 | void undo() override; 12 | std::string getClassName() const override { return className; } 13 | private: 14 | Command::command_vec commands; 15 | std::string className = "MacroCommand"; 16 | }; 17 | 18 | inline 19 | void 20 | MacroCommand::execute() 21 | { 22 | for (const auto &command : commands) 23 | command->execute(); 24 | } 25 | 26 | inline 27 | void 28 | MacroCommand::undo() 29 | { 30 | for (auto i = commands.size() - 1; i >= 0; --i) 31 | commands[i]->undo(); 32 | } 33 | 34 | #endif /* MACRO_COMMAND_H */ 35 | -------------------------------------------------------------------------------- /Command/party/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = CeilingFanHighCommand.hpp CeilingFan.hpp CeilingFanLowCommand.hpp \ 4 | CeilingFanMediumCommand.hpp CeilingFanOffCommand.hpp Command.hpp \ 5 | Hottub.hpp HottubOffCommand.hpp HottubOnCommand.hpp Light.hpp \ 6 | LightOffCommand.hpp LightOnCommand.hpp MacroCommand.hpp \ 7 | NoCommand.hpp RemoteControl.hpp Stereo.hpp StereoOffCommand.hpp \ 8 | StereoOnCommand.hpp TV.hpp TVOffCommand.hpp TVOnCommand.hpp 9 | TARGET = remoteLoader 10 | OBJ = $(TARGET).o 11 | 12 | %.o: %.cpp $(DEPS) 13 | $(CXX) -c -o $@ $< $(CFLAGS) 14 | 15 | $(TARGET): $(OBJ) 16 | $(CXX) -o $@ $^ $(CFLAGS) 17 | 18 | clean: 19 | rm -f *.o $(TARGET) 20 | -------------------------------------------------------------------------------- /Command/party/NoCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NO_COMMAND_H 2 | #define NO_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include 6 | 7 | class NoCommand : public Command { 8 | public: 9 | void execute() override { } 10 | void undo() override { } 11 | std::string getClassName() const override { return className; } 12 | private: 13 | std::string className = "NoCommand"; 14 | }; 15 | 16 | #endif /* NO_COMMAND_H */ 17 | -------------------------------------------------------------------------------- /Command/party/Stereo.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STEREO_H 2 | #define STEREO_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Stereo { 9 | public: 10 | Stereo(std::string_view l) : location(l) { } 11 | void on() { std::cout << location << " stereo is On\n"; } 12 | void off() { std::cout << location << " stereo is Off\n"; } 13 | void setCD() { std::cout << location << " stereo is set for CD input\n"; } 14 | void setDVD() { std::cout << location << " stereo is set for DVD input\n"; } 15 | void setRadio() { std::cout << location << " stereo is Radio\n"; } 16 | void setVolume(int volume) { std::cout << location << " stereo volume is set to " << volume << '\n'; } 17 | private: 18 | std::string location; 19 | }; 20 | 21 | #endif /* STEREO_H */ 22 | -------------------------------------------------------------------------------- /Command/party/StereoOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STEREO_OFF_COMMAND_H 2 | #define STEREO_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Stereo.hpp" 6 | #include 7 | 8 | class StereoOffCommand : public Command { 9 | public: 10 | StereoOffCommand(Stereo *s) : stereo(s) { } 11 | void execute() override { stereo->off(); } 12 | void undo() override { stereo->on(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | Stereo *stereo; 16 | std::string className = "StereoOffCommand"; 17 | }; 18 | 19 | #endif /* STEREO_OFF_COMMAND_H */ 20 | -------------------------------------------------------------------------------- /Command/party/StereoOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STEREO_ON_COMMAND_H 2 | #define STEREO_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Stereo.hpp" 6 | #include 7 | 8 | class StereoOnCommand : public Command { 9 | public: 10 | StereoOnCommand(Stereo *s) : stereo(s) { } 11 | void execute() override { stereo->on(); } 12 | void undo() override { stereo->off(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | Stereo *stereo; 16 | std::string className = "StereoOnCommand"; 17 | }; 18 | 19 | #endif /* STEREO_ON_COMMAND_H */ 20 | -------------------------------------------------------------------------------- /Command/party/TV.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TV_H 2 | #define TV_H 3 | 4 | #include 5 | #include 6 | 7 | class TV { 8 | public: 9 | TV(const std::string &l) : location(l) { } 10 | void on() { std::cout << location << " TV is on\n"; } 11 | void off() { std::cout << location << " TV is off\n"; } 12 | void setInputChannel(); 13 | void setVolume(int volume) { 14 | std::cout << location << " TV volume is set to " << volume << '\n'; } 15 | private: 16 | std::string location; 17 | int channel; 18 | }; 19 | 20 | inline 21 | void 22 | TV::setInputChannel() 23 | { 24 | channel = 3; 25 | std::cout << location << " TV channel is set for DVD\n"; 26 | } 27 | 28 | #endif /* TV_H */ 29 | -------------------------------------------------------------------------------- /Command/party/TVOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TV_OFF_COMMAND_H 2 | #define TV_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "TV.hpp" 6 | #include 7 | 8 | class TVOffCommand :public Command { 9 | 10 | public: 11 | TVOffCommand(TV *t) : tv(t) { } 12 | void execute() override { tv->off(); } 13 | void undo() override { tv->on(); } 14 | std::string getClassName() const override { return className; } 15 | private: 16 | TV *tv; 17 | std::string className = "TVOffCommand"; 18 | }; 19 | 20 | #endif /* TV_OFF_COMMAND_H */ 21 | -------------------------------------------------------------------------------- /Command/party/TVOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TV_ON_COMMAND_H 2 | #define TV_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "TV.hpp" 6 | #include 7 | 8 | class TVOnCommand :public Command { 9 | 10 | public: 11 | TVOnCommand(TV *t) : tv(t) { } 12 | void execute() override; 13 | void undo() override { tv->off(); } 14 | std::string getClassName() const override { return className; } 15 | private: 16 | TV *tv; 17 | std::string className = "TVOnCommand"; 18 | }; 19 | 20 | inline 21 | void 22 | TVOnCommand::execute() 23 | { 24 | tv->on(); 25 | tv->setInputChannel(); 26 | } 27 | 28 | #endif /* TV_ON_COMMAND_H */ 29 | -------------------------------------------------------------------------------- /Command/party/util.cpp: -------------------------------------------------------------------------------- 1 | #include "util.hpp" 2 | 3 | void undoCeilingFanCommand(Command &cmd) 4 | { 5 | switch (prevSpeed) { 6 | case CeilingFan::Speed::HIGH: 7 | ceilingFan->high(); 8 | break; 9 | case CeilingFan::Speed::MEDIUM: 10 | ceilingFan->medium(); 11 | break; 12 | case CeilingFan::Speed::LOW: 13 | ceilingFan->low(); 14 | break; 15 | case CeilingFan::Speed::OFF: 16 | ceilingFan->off(); 17 | break; 18 | default: 19 | break; 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Command/party/util.hpp: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_H 2 | #define UTIL_H value 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | 7 | void undoCeilingFanCommand(Command &cmd); 8 | 9 | #endif /* ifndef */ 10 | -------------------------------------------------------------------------------- /Command/remote/CeilingFanOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_OFF_COMMAND_H 2 | #define CEILING_FAN_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | #include 7 | 8 | class CeilingFanOffCommand : public Command 9 | { 10 | public: 11 | CeilingFanOffCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override { ceilingFan->off(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | CeilingFan *ceilingFan; 16 | std::string className = "CeilingFanOffCommand"; 17 | }; 18 | 19 | #endif /* CEILING_FAN_OFF_COMMAN_H */ 20 | -------------------------------------------------------------------------------- /Command/remote/CeilingFanOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_ON_COMMAND_H 2 | #define CEILING_FAN_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | #include 7 | 8 | class CeilingFanOnCommand : public Command 9 | { 10 | public: 11 | CeilingFanOnCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override { ceilingFan->medium(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | CeilingFan *ceilingFan; 16 | std::string className = "CeilingFanOnCommand"; 17 | }; 18 | 19 | #endif /* CEILING_FAN_ON_COMMAND_H */ 20 | -------------------------------------------------------------------------------- /Command/remote/Command.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMMAND_H 2 | #define COMMAND_H 3 | 4 | #include 5 | #include 6 | 7 | class Command { 8 | public: 9 | using command_vec = std::vector; // ptr to Command* vector type 10 | using cvec_size = std::vector::size_type; // ptr to command vector size type 11 | virtual ~Command () = default; 12 | virtual void execute() = 0; 13 | virtual std::string getClassName() const = 0; 14 | }; 15 | 16 | #endif /* COMMAND_H */ 17 | -------------------------------------------------------------------------------- /Command/remote/GarageDoor.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GARAGE_DOOR_H 2 | #define GARAGE_DOOR_H 3 | 4 | #include 5 | 6 | class GarageDoor { 7 | public: 8 | void up() { std::cout << "Garage Door is Up\n"; } 9 | void down() { std::cout << "Garage Door is Down\n"; } 10 | void stop() { std::cout << "Garage Door is stopped\n"; } 11 | void lightOn() { std::cout << "Garage Door Light is On\n"; } 12 | void lightOff() { std::cout << "Garage Door Light is Off\n"; } 13 | }; 14 | 15 | #endif /* GARAGE_DOOR_H */ 16 | -------------------------------------------------------------------------------- /Command/remote/GarageDoorDownCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GARAGE_DOOR_DOWN_COMMAND_H 2 | #define GARAGE_DOOR_DOWN_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "GarageDoor.hpp" 6 | #include 7 | 8 | class GarageDoorDownCommand : public Command { 9 | public: 10 | GarageDoorDownCommand(GarageDoor *gd) : garageDoor(gd) { } 11 | void execute() override { garageDoor->down(); } 12 | std::string getClassName() const override { return className; }; 13 | private: 14 | GarageDoor *garageDoor; 15 | std::string className = "GarageDoorDownCommand"; 16 | }; 17 | 18 | #endif /* GARAGE_DOOR_DOWN_COMMAND_H */ 19 | -------------------------------------------------------------------------------- /Command/remote/GarageDoorUpCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GARAGE_DOOR_UP_COMMAND_H 2 | #define GARAGE_DOOR_UP_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "GarageDoor.hpp" 6 | #include 7 | 8 | class GarageDoorUpCommand : public Command { 9 | public: 10 | GarageDoorUpCommand(GarageDoor *gd) : garageDoor(gd) { } 11 | void execute() override { garageDoor->up(); } 12 | std::string getClassName() const override { return className; } 13 | private: 14 | GarageDoor *garageDoor; 15 | std::string className = "GarageDoorUpCommand"; 16 | }; 17 | 18 | #endif /* GARAGE_DOOR_UP_COMMAND_H */ 19 | -------------------------------------------------------------------------------- /Command/remote/Hottub.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOTTUB_H 2 | #define HOTTUB_H 3 | 4 | #include 5 | 6 | class Hottub { 7 | public: 8 | void on() { on_state = true; } 9 | void off() { on_state = false; } 10 | void bubblesOn() { if (on_state) std::cout << "Hottub is bubbling\n"; } 11 | void bubblesOff() { if (on_state) std::cout << "Hottub is not bubbling\n"; } 12 | void jetsOn() { if (on_state) std::cout << "Hottub jets are On\n"; } 13 | void jetsOff() { if (on_state) std::cout << "Hottub jets are Off\n"; } 14 | void setTemperature(int t) { temperature = t; } 15 | void heat(); 16 | void cool(); 17 | private: 18 | bool on_state; 19 | int temperature; 20 | }; 21 | 22 | inline 23 | void 24 | Hottub::heat() 25 | { 26 | temperature = 105; 27 | std::cout << "Hottub is heating to a steaming " << temperature << " degrees\n"; 28 | } 29 | 30 | inline 31 | void 32 | Hottub::cool() 33 | { 34 | temperature = 98; 35 | std::cout << "Hottub is cooling to " << temperature << " degrees\n"; 36 | } 37 | 38 | #endif /* HOTTUB_H */ 39 | -------------------------------------------------------------------------------- /Command/remote/HottubOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOTTUB_OFF_COMMAND_H 2 | #define HOTTUB_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Hottub.hpp" 6 | #include "string" 7 | 8 | class HottubOffCommand : public Command { 9 | public: 10 | HottubOffCommand(Hottub *ht) : hottub(ht) { } 11 | void execute() override; 12 | std::string getClassName() const override { return className; } 13 | private: 14 | Hottub *hottub; 15 | std::string className = "HottubOffCommand"; 16 | }; 17 | 18 | inline 19 | void HottubOffCommand::execute() 20 | { 21 | hottub->off(); 22 | hottub->cool(); 23 | hottub->bubblesOff(); 24 | } 25 | 26 | #endif /* HOTTUB_OFF_COMMAND_H */ 27 | -------------------------------------------------------------------------------- /Command/remote/HottubOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOTTUB_ON_COMMAND_H 2 | #define HOTTUB_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Hottub.hpp" 6 | #include "string" 7 | 8 | class HottubOnCommand : public Command { 9 | public: 10 | HottubOnCommand(Hottub *ht) : hottub(ht) { } 11 | void execute() override; 12 | std::string getClassName() const override { return className; } 13 | private: 14 | Hottub *hottub; 15 | std::string className = "HottubOnCommand"; 16 | }; 17 | 18 | inline 19 | void 20 | HottubOnCommand::execute() 21 | { 22 | hottub->on(); 23 | hottub->heat(); 24 | hottub->bubblesOn(); 25 | } 26 | 27 | #endif /* HOTTUB_ON_COMMAND_H */ 28 | -------------------------------------------------------------------------------- /Command/remote/Light.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_H 2 | #define LIGHT_H 3 | 4 | #include 5 | #include 6 | 7 | class Light { 8 | public: 9 | Light(const std::string &l) : location(l) { } 10 | void on() { std::cout << location << " Light is On\n"; } 11 | void off() { std::cout << location << " Light is off\n"; } 12 | private: 13 | std::string location; 14 | }; 15 | 16 | #endif /* LIGHT_H */ 17 | -------------------------------------------------------------------------------- /Command/remote/LightOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_OFF_COMMAND_H 2 | #define LIGHT_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | #include 7 | 8 | class LightOffCommand : public Command { 9 | public: 10 | LightOffCommand(Light *l) : light(l) { } 11 | void execute() override { light->off(); } 12 | std::string getClassName() const override { return className; } 13 | private: 14 | Light *light; 15 | std::string className = "LightOffCommand"; 16 | }; 17 | 18 | #endif /* LIGHT_OFF_COMMAND_H */ 19 | -------------------------------------------------------------------------------- /Command/remote/LightOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_ON_COMMAND_H 2 | #define LIGHT_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | #include 7 | 8 | class LightOnCommand : public Command { 9 | public: 10 | LightOnCommand(Light *l) : light(l) { } 11 | void execute() override { light->on(); } 12 | std::string getClassName() const override { return className; } 13 | private: 14 | Light *light; 15 | std::string className = "LightOnCommand"; 16 | }; 17 | 18 | #endif /* LIGHT_ON_COMMAND_H */ 19 | -------------------------------------------------------------------------------- /Command/remote/LivingRoomLightOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIVING_ROOM_LIGHT_ON_COMMAND_H 2 | #define LIVING_ROOM_LIGHT_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | 7 | class LivingroomLightOnCommand : public Command { 8 | public: 9 | LivingroomLightOnCommand(Light *l) : light(l) {} 10 | void execute() override { light->on(); } 11 | private: 12 | Light *light; 13 | }; 14 | 15 | #endif /* LIVING_ROOM_LIGHT_ON_COMMAND_H */ 16 | -------------------------------------------------------------------------------- /Command/remote/LivingroomLightOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIVING_ROOM_LIGHT_OFF_COMMAND_H 2 | #define LIVING_ROOM_LIGHT_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | 7 | class LivingroomLightOffCommand : public Command { 8 | public: 9 | LivingroomLightOffCommand(Light *l) : light(l) {} 10 | void execute() override { light->off(); } 11 | private: 12 | Light *light; 13 | }; 14 | 15 | #endif /* LIVING_ROOM_LIGHT_OFF_COMMAND_H */ 16 | -------------------------------------------------------------------------------- /Command/remote/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = CeilingFan.hpp CeilingFanOffCommand.hpp CeilingFanOnCommand.hpp \ 4 | Command.hpp GarageDoorDownCommand.hpp GarageDoor.hpp \ 5 | GarageDoorUpCommand.hpp Light.hpp LightOffCommand.hpp \ 6 | LightOnCommand.hpp NoCommand.hpp RemoteControl.hpp remoteLoader.cpp \ 7 | Stereo.hpp StereoOffCommand.hpp StereoOnWithCDCommand.hpp 8 | 9 | TARGET = remoteLoader 10 | OBJ = $(TARGET).o 11 | 12 | %.o: %.cpp $(DEPS) 13 | $(CXX) -c -o $@ $< $(CFLAGS) 14 | 15 | $(TARGET): $(OBJ) 16 | $(CXX) -o $@ $^ $(CFLAGS) 17 | 18 | clean: 19 | rm -f *.o $(TARGET) 20 | -------------------------------------------------------------------------------- /Command/remote/NoCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NO_COMMAND_H 2 | #define NO_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include 6 | 7 | class NoCommand : public Command { 8 | public: 9 | void execute() override { } 10 | std::string getClassName() const override { return className; } 11 | private: 12 | std::string className = "NoCommand"; 13 | }; 14 | 15 | #endif /* NO_COMMAND_H */ 16 | -------------------------------------------------------------------------------- /Command/remote/Stereo.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STEREO_H 2 | #define STEREO_H 3 | 4 | #include 5 | #include 6 | 7 | class Stereo { 8 | public: 9 | Stereo(const std::string &l) : location(l) { } 10 | void on() { std::cout << location << " stereo is On\n"; } 11 | void off() { std::cout << location << " stereo is Off\n"; } 12 | void setCD() { std::cout << location << " stereo is set for CD input\n"; } 13 | void setDVD() { std::cout << location << " stereo is set for DVD input\n"; } 14 | void setRadio() { std::cout << location << " stereo is set for Radio\n"; } 15 | void setVolume(int volume) { std::cout << location << " stereo volume is set to " << std::to_string(volume) << '\n'; } 16 | private: 17 | std::string location; 18 | }; 19 | 20 | #endif /* STEREO_H */ 21 | -------------------------------------------------------------------------------- /Command/remote/StereoOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STEREO_OFF_COMMAND_H 2 | #define STEREO_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Stereo.hpp" 6 | #include 7 | 8 | class StereoOffCommand : public Command { 9 | public: 10 | StereoOffCommand(Stereo *s) : stereo(s) { } 11 | void execute() override { stereo->off(); } 12 | std::string getClassName() const override { return className; } 13 | private: 14 | Stereo *stereo; 15 | std::string className = "StereoOffCommand"; 16 | }; 17 | 18 | #endif /* STEREO_OFF_COMMAND_H */ 19 | -------------------------------------------------------------------------------- /Command/remote/StereoOnWithCDCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef STEREO_ON_WITH_CD_COMMAND_H 2 | #define STEREO_ON_WITH_CD_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Stereo.hpp" 6 | #include 7 | 8 | class StereoOnWithCDCommand : public Command { 9 | public: 10 | StereoOnWithCDCommand(Stereo *s) : stereo(s) { } 11 | void execute() override; 12 | std::string getClassName() const override { return className; }; 13 | private: 14 | Stereo *stereo; 15 | std::string className = "StereoOnWithCDCommand"; 16 | 17 | }; 18 | 19 | inline 20 | void 21 | StereoOnWithCDCommand::execute() 22 | { 23 | stereo->on(); 24 | stereo->setCD(); 25 | stereo->setVolume(11); 26 | } 27 | #endif /* STEREO_ON_WITH_CD_COMMAND_H */ 28 | -------------------------------------------------------------------------------- /Command/remote/TV.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TV_H 2 | #define TV_H 3 | 4 | #include 5 | #include 6 | 7 | class TV { 8 | public: 9 | TV(const std::string &l) : location(l) { } 10 | void on() { std::cout << location << " TV is on\n"; } 11 | void off() { std::cout << location << " TV is off\n"; } 12 | void setInputChannel(); 13 | void setVolume(int volume) { 14 | std::cout << location << " TV volume is set to " << volume << '\n'; } 15 | private: 16 | std::string location; 17 | int channel; 18 | }; 19 | 20 | inline 21 | void 22 | TV::setInputChannel() 23 | { 24 | channel = 3; 25 | std::cout << location << " TV channel is set to DVD\n"; 26 | } 27 | 28 | #endif /* TV_H */ 29 | -------------------------------------------------------------------------------- /Command/remote/TVOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TV_OFF_COMMAND_H 2 | #define TV_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "TV.hpp" 6 | #include 7 | 8 | class TVOffCommand :public Command { 9 | 10 | public: 11 | TVOffCommand(TV *t) : tv(t) { } 12 | void execute() override { tv->off(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | TV *tv; 16 | std::string className = "TVOffCommand"; 17 | }; 18 | 19 | #endif /* TV_OFF_COMMAND_H */ 20 | -------------------------------------------------------------------------------- /Command/remote/TVOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef TV_ON_COMMAND_H 2 | #define TV_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "TV.hpp" 6 | #include 7 | 8 | class TVOnCommand :public Command { 9 | 10 | public: 11 | TVOnCommand(TV *t) : tv(t) { } 12 | void execute() override { tv->on(); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | TV *tv; 16 | std::string className = "TVOnCommand"; 17 | }; 18 | 19 | #endif /* TV_ON_COMMAND_H */ 20 | -------------------------------------------------------------------------------- /Command/simpleremote/Command.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMMAND_H 2 | #define COMMAND_H 3 | 4 | class Command { 5 | public: 6 | virtual ~Command () = default; 7 | virtual void execute() const = 0; 8 | }; 9 | 10 | #endif /* COMMAND_H */ 11 | -------------------------------------------------------------------------------- /Command/simpleremote/GarageDoor.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GARAGE_DOOR_H 2 | #define GARAGE_DOOR_H 3 | 4 | #include 5 | 6 | class GarageDoor { 7 | public: 8 | void up() { std::cout << "Garage Door is Up\n"; } 9 | void down() { std::cout << "Garage Door is Down\n"; } 10 | void stop() { std::cout << "Garage Door is Stopped\n"; } 11 | void lightOn() { std::cout << "Garage Door Light is On\n"; } 12 | void lightOff() { std::cout << "Garage Door Light is Off\n"; } 13 | }; 14 | 15 | #endif /* GARAGE_DOOR_H */ 16 | -------------------------------------------------------------------------------- /Command/simpleremote/GarageDoorOpen.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GARAGE_DOOR_OPEN_H 2 | #define GARAGE_DOOR_OPEN_H 3 | 4 | #include "Command.hpp" 5 | #include "GarageDoor.hpp" 6 | 7 | class GarageDoorOpen : public Command { 8 | public: 9 | GarageDoorOpen(GarageDoor *gd) : garageDoor(gd) { } 10 | void execute() const override { garageDoor->up(); } 11 | private: 12 | GarageDoor *garageDoor; 13 | }; 14 | 15 | #endif /* GARAGE_DOOR_OPEN_H */ 16 | -------------------------------------------------------------------------------- /Command/simpleremote/Light.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_H 2 | #define LIGHT_H 3 | 4 | #include 5 | 6 | class Light { 7 | public: 8 | void on() { std::cout << "Light is On\n"; } 9 | void off() { std::cout << "Light is off\n"; } 10 | }; 11 | 12 | #endif /* LIGHT_H */ 13 | -------------------------------------------------------------------------------- /Command/simpleremote/LightOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_ON_COMMAND_H 2 | #define LIGHT_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | 7 | class LightOnCommand : public Command { 8 | public: 9 | LightOnCommand(Light *l) : light(l) { } 10 | void execute() const override { light->on(); } 11 | private: 12 | Light *light; 13 | }; 14 | 15 | #endif /* LIGHT_ON_COMMAND_H */ 16 | -------------------------------------------------------------------------------- /Command/simpleremote/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = Command.hpp Light.hpp LightOnCommand.hpp SimpleRemoteControl.hpp 4 | 5 | TARGET = remoteControlTest 6 | OBJ = $(TARGET).o 7 | 8 | %.o: %.cpp $(DEPS) 9 | $(CXX) -c -o $@ $< $(CFLAGS) 10 | 11 | $(TARGET): $(OBJ) 12 | $(CXX) -o $@ $^ $(CFLAGS) 13 | 14 | clean: 15 | rm -f *.o $(TARGET) 16 | -------------------------------------------------------------------------------- /Command/simpleremote/SimpleRemoteControl.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLE_REMOTE_CONTROL_H 2 | #define SIMPLE_REMOTE_CONTROL_H 3 | 4 | // Invoker 5 | 6 | #include "Command.hpp" 7 | #include 8 | 9 | class SimpleRemoteControl { 10 | public: 11 | SimpleRemoteControl () = default; 12 | void setCommand(Command *command) { slot = command; } 13 | void buttonWasPressed() { slot->execute(); } 14 | private: 15 | Command *slot; 16 | }; 17 | 18 | #endif /* SIMPLE_REMOTE_CONTROL_H */ 19 | -------------------------------------------------------------------------------- /Command/simpleremote/remoteControlTest.cpp: -------------------------------------------------------------------------------- 1 | #include "Command.hpp" 2 | #include "GarageDoor.hpp" 3 | #include "GarageDoorOpen.hpp" 4 | #include "Light.hpp" 5 | #include "LightOnCommand.hpp" 6 | #include "SimpleRemoteControl.hpp" 7 | 8 | // remoteControlTest is the Client 9 | 10 | int main() { 11 | auto remote = SimpleRemoteControl(); // Invoker 12 | auto light = new Light(); // Receiver 13 | auto lightOn = new LightOnCommand(light); // create command and pass the receiver to it 14 | remote.setCommand(lightOn); 15 | remote.buttonWasPressed(); 16 | 17 | auto garageDoor = new GarageDoor(); 18 | auto garageDoorOpen = new GarageDoorOpen(garageDoor); 19 | remote.setCommand(garageDoorOpen); 20 | remote.buttonWasPressed(); 21 | 22 | // clean up 23 | delete light; 24 | delete lightOn; 25 | delete garageDoor; 26 | delete garageDoorOpen; 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /Command/undo/CeilingFanHighCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_HIGH_COMMAND_H 2 | #define CEILING_FAN_HIGH_COMMAND_H 3 | 4 | #include "CeilingFan.hpp" 5 | #include "Command.hpp" 6 | #include 7 | 8 | class CeilingFanHighCommand : public Command { 9 | 10 | public: 11 | CeilingFanHighCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override; 13 | void undo() override; 14 | std::string getClassName() const override { return className; }; 15 | private: 16 | CeilingFan *ceilingFan; 17 | CeilingFan::Speed prevSpeed; 18 | std::string className = "CeilingFanHighCommand"; 19 | }; 20 | 21 | inline 22 | void 23 | CeilingFanHighCommand::execute() 24 | { 25 | prevSpeed = ceilingFan->getSpeed(); 26 | ceilingFan->high(); 27 | } 28 | 29 | inline 30 | void 31 | CeilingFanHighCommand::undo() 32 | { 33 | switch (prevSpeed) { 34 | case CeilingFan::Speed::HIGH: 35 | ceilingFan->high(); 36 | break; 37 | case CeilingFan::Speed::MEDIUM: 38 | ceilingFan->medium(); 39 | break; 40 | case CeilingFan::Speed::LOW: 41 | ceilingFan->low(); 42 | break; 43 | case CeilingFan::Speed::OFF: 44 | ceilingFan->off(); 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | 51 | #endif /* CEILING_FAN_HIGH_COMMAND_H */ 52 | -------------------------------------------------------------------------------- /Command/undo/CeilingFanLowCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_LOW_COMMAND_H 2 | #define CEILING_FAN_LOW_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | #include 7 | 8 | class CeilingFanLowCommand : public Command 9 | { 10 | public: 11 | CeilingFanLowCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override; 13 | void undo() override; 14 | std::string getClassName() const override { return className; } 15 | private: 16 | CeilingFan *ceilingFan; 17 | CeilingFan::Speed prevSpeed; 18 | std::string className = "CeilingFanLowCommand"; 19 | }; 20 | 21 | inline 22 | void 23 | CeilingFanLowCommand::execute() 24 | { 25 | prevSpeed = ceilingFan->getSpeed(); 26 | ceilingFan->low(); 27 | } 28 | 29 | inline 30 | void 31 | CeilingFanLowCommand::undo() 32 | { 33 | switch (prevSpeed) { 34 | case CeilingFan::Speed::HIGH: 35 | ceilingFan->high(); 36 | break; 37 | case CeilingFan::Speed::MEDIUM: 38 | ceilingFan->medium(); 39 | break; 40 | case CeilingFan::Speed::LOW: 41 | ceilingFan->low(); 42 | break; 43 | case CeilingFan::Speed::OFF: 44 | ceilingFan->off(); 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | 51 | #endif /* CEILING_FAN_LOW_COMMAN_H */ 52 | -------------------------------------------------------------------------------- /Command/undo/CeilingFanMediumCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_MEDIUM_COMMAND_H 2 | #define CEILING_FAN_MEDIUM_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | #include 7 | 8 | class CeilingFanMediumCommand : public Command 9 | { 10 | public: 11 | CeilingFanMediumCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override; 13 | void undo() override; 14 | std::string getClassName() const override { return className; } 15 | private: 16 | CeilingFan *ceilingFan; 17 | CeilingFan::Speed prevSpeed; 18 | std::string className = "CeilingFanMediumCommand"; 19 | }; 20 | 21 | inline 22 | void 23 | CeilingFanMediumCommand::execute() 24 | { 25 | prevSpeed = ceilingFan->getSpeed(); 26 | ceilingFan->medium(); 27 | } 28 | 29 | inline 30 | void 31 | CeilingFanMediumCommand::undo() 32 | { 33 | switch (prevSpeed) { 34 | case CeilingFan::Speed::HIGH: 35 | ceilingFan->high(); 36 | break; 37 | case CeilingFan::Speed::MEDIUM: 38 | ceilingFan->medium(); 39 | break; 40 | case CeilingFan::Speed::LOW: 41 | ceilingFan->low(); 42 | break; 43 | case CeilingFan::Speed::OFF: 44 | ceilingFan->off(); 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | 51 | #endif /* CEILING_FAN_MEDIUM_COMMAN_H */ 52 | -------------------------------------------------------------------------------- /Command/undo/CeilingFanOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CEILING_FAN_OFF_COMMAND_H 2 | #define CEILING_FAN_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "CeilingFan.hpp" 6 | #include 7 | 8 | class CeilingFanOffCommand : public Command 9 | { 10 | public: 11 | CeilingFanOffCommand(CeilingFan *cf) : ceilingFan(cf) { } 12 | void execute() override; 13 | void undo() override; 14 | std::string getClassName() const override { return className; } 15 | private: 16 | CeilingFan *ceilingFan; 17 | CeilingFan::Speed prevSpeed; 18 | std::string className = "CeilingFanOffCommand"; 19 | }; 20 | 21 | inline 22 | void 23 | CeilingFanOffCommand::execute() 24 | { 25 | prevSpeed = ceilingFan->getSpeed(); 26 | ceilingFan->off(); 27 | } 28 | 29 | inline 30 | void 31 | CeilingFanOffCommand::undo() 32 | { 33 | switch (prevSpeed) { 34 | case CeilingFan::Speed::HIGH: 35 | ceilingFan->high(); 36 | break; 37 | case CeilingFan::Speed::MEDIUM: 38 | ceilingFan->medium(); 39 | break; 40 | case CeilingFan::Speed::LOW: 41 | ceilingFan->low(); 42 | break; 43 | case CeilingFan::Speed::OFF: 44 | ceilingFan->off(); 45 | break; 46 | default: 47 | break; 48 | } 49 | } 50 | 51 | #endif /* CEILING_FAN_OFF_COMMAN_H */ 52 | -------------------------------------------------------------------------------- /Command/undo/Command.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COMMAND_H 2 | #define COMMAND_H 3 | 4 | #include 5 | #include 6 | 7 | class Command { 8 | public: 9 | using command_vec = std::vector; // ptr to Command* vector type 10 | using cvec_size = std::vector::size_type; // ptr to command vector size type 11 | virtual ~Command () = default; 12 | virtual void execute() = 0; 13 | virtual void undo() = 0; 14 | virtual std::string getClassName() const = 0; 15 | }; 16 | 17 | #endif /* COMMAND_H */ 18 | -------------------------------------------------------------------------------- /Command/undo/DimmerLightOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DIMMER_LIGHT_OFF_COMMAND_H 2 | #define DIMMER_LIGHT_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | 7 | class DimmerLightOffCommand : public Command { 8 | public: 9 | DimmerLightOffCommand(Light *l) : light(l) {} 10 | void execute() override; 11 | void undo() override { light->dim(prevLevel); } 12 | private: 13 | Light *light; 14 | int prevLevel = 100; 15 | }; 16 | 17 | inline 18 | void 19 | DimmerLightOffCommand::execute() 20 | { 21 | prevLevel = light->getLevel(); 22 | light->off(); 23 | } 24 | 25 | #endif /* DIMMER_LIGHT_OFF_COMMAND_H */ 26 | -------------------------------------------------------------------------------- /Command/undo/DimmerLightOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DIMMER_LIGHT_ON_COMMAND_H 2 | #define DIMMER_LIGHT_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | 7 | class DimmerLightOnCommand : public Command { 8 | public: 9 | DimmerLightOnCommand(Light *l) : light(l) {} 10 | void execute() override; 11 | void undo() override { light->dim(prevLevel); } 12 | private: 13 | Light *light; 14 | int prevLevel; 15 | }; 16 | 17 | inline 18 | void 19 | DimmerLightOnCommand::execute() 20 | { 21 | prevLevel = light->getLevel(); 22 | light->dim(75); 23 | } 24 | 25 | #endif /* DIMMER_LIGHT_ON_COMMAND_H */ 26 | -------------------------------------------------------------------------------- /Command/undo/Light.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_H 2 | #define LIGHT_H 3 | 4 | #include 5 | #include 6 | 7 | class Light { 8 | public: 9 | Light(const std::string &l) : location(l) { } 10 | void on(); 11 | void off(); 12 | void dim(int l); 13 | int getLevel() const { return level; } 14 | private: 15 | std::string location; 16 | int level; 17 | }; 18 | 19 | inline 20 | void 21 | Light::on() 22 | { 23 | level = 100; 24 | std::cout << location << " Light is On\n"; 25 | } 26 | 27 | inline 28 | void 29 | Light::off() 30 | { 31 | level = 0; 32 | std::cout << location << " Light is off\n"; 33 | } 34 | 35 | inline 36 | void 37 | Light::dim(int l) 38 | { 39 | level = l; 40 | if (level == 0) 41 | off(); 42 | else 43 | std::cout << "Light is dimmed to " << level << "%\n"; 44 | } 45 | 46 | #endif /* LIGHT_H */ 47 | -------------------------------------------------------------------------------- /Command/undo/LightOffCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_OFF_COMMAND_H 2 | #define LIGHT_OFF_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | #include 7 | 8 | class LightOffCommand : public Command { 9 | public: 10 | LightOffCommand(Light *l) : light(l) { } 11 | void execute() override; 12 | void undo() override { light->dim(level); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | Light *light; 16 | int level = 0; 17 | std::string className = "LightOffCommand"; 18 | }; 19 | 20 | inline 21 | void 22 | LightOffCommand::execute() 23 | { 24 | level = light ->getLevel(); 25 | light->off(); 26 | } 27 | 28 | #endif /* LIGHT_OFF_COMMAND_H */ 29 | -------------------------------------------------------------------------------- /Command/undo/LightOnCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef LIGHT_ON_COMMAND_H 2 | #define LIGHT_ON_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include "Light.hpp" 6 | #include 7 | 8 | class LightOnCommand : public Command { 9 | public: 10 | LightOnCommand(Light *l) : light(l) { } 11 | void execute() override; 12 | void undo() override { light->dim(level); } 13 | std::string getClassName() const override { return className; } 14 | private: 15 | Light *light; 16 | int level = 0; 17 | std::string className = "LightOnCommand"; 18 | }; 19 | 20 | inline 21 | void 22 | LightOnCommand::execute() 23 | { 24 | level = light->getLevel(); 25 | light->on(); 26 | } 27 | 28 | #endif /* LIGHT_ON_COMMAND_H */ 29 | -------------------------------------------------------------------------------- /Command/undo/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = CeilingFanHighCommand.hpp CeilingFan.hpp CeilingFanLowCommand.hpp \ 4 | CeilingFanMediumCommand.hpp CeilingFanOffCommand.hpp Command.hpp \ 5 | Light.hpp LightOffCommand.hpp LightOnCommand.hpp NoCommand.hpp \ 6 | RemoteControlWithUndo.hpp 7 | 8 | TARGET = remoteLoader 9 | OBJ = $(TARGET).o 10 | 11 | %.o: %.cpp $(DEPS) 12 | $(CXX) -c -o $@ $< $(CFLAGS) 13 | 14 | $(TARGET): $(OBJ) 15 | $(CXX) -o $@ $^ $(CFLAGS) 16 | 17 | clean: 18 | rm -f *.o $(TARGET) 19 | -------------------------------------------------------------------------------- /Command/undo/NoCommand.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NO_COMMAND_H 2 | #define NO_COMMAND_H 3 | 4 | #include "Command.hpp" 5 | #include 6 | 7 | class NoCommand : public Command { 8 | public: 9 | void execute() override { } 10 | void undo() override { } 11 | std::string getClassName() const override { return className; } 12 | private: 13 | std::string className = "NoCommand"; 14 | }; 15 | 16 | #endif /* NO_COMMAND_H */ 17 | -------------------------------------------------------------------------------- /Composite/menu/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++2a 3 | DEPS = MenuComponent.h Menu.h MenuItem.h Waitress.h 4 | TARGET = menuTestDrive 5 | 6 | OBJ = $(TARGET).o 7 | 8 | %.o: %.cpp $(DEPS) 9 | $(CXX) -c -o $@ $< $(CFLAGS) 10 | 11 | $(TARGET): $(OBJ) 12 | $(CXX) -o $@ $^ $(CFLAGS) 13 | 14 | clean: 15 | rm -f *.o $(TARGET) 16 | -------------------------------------------------------------------------------- /Composite/menu/Menu.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_H 2 | #define MENU_H 3 | 4 | #include "MenuComponent.h" 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class Menu : public MenuComponent { 11 | public: 12 | Menu(std::string_view n, std::string_view d) : name(n), description(d) { } 13 | void add(menu_component_t menuComponent) override { 14 | menuComponents.push_back(menuComponent); } 15 | void remove(menu_component_t menuComponent) override { 16 | menuComponents.remove(menuComponent); } 17 | std::string getName() const override { return name; } 18 | std::string getDescription() const override { return description; } 19 | void print() const override; 20 | private: 21 | std::list menuComponents; 22 | std::string name; 23 | std::string description; 24 | }; 25 | 26 | inline 27 | void 28 | Menu::print() const 29 | { 30 | std::cout << '\n' << getName(); 31 | std::cout << ", " << getDescription() << '\n'; 32 | std::cout << "----------------------\n"; 33 | for (const auto &menuComponent : menuComponents) 34 | menuComponent->print() ; 35 | } 36 | 37 | #endif /* MENU_H */ 38 | -------------------------------------------------------------------------------- /Composite/menu/MenuComponent.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_COMPONENT_H 2 | #define MENU_COMPONENT_H 3 | 4 | #include 5 | #include 6 | 7 | class MenuComponent { 8 | public: 9 | using menu_component_t = MenuComponent *; 10 | virtual ~MenuComponent() = default; 11 | virtual void add([[maybe_unused]]menu_component_t menuComponent) { 12 | throw std::invalid_argument("add(): Unsupported Operation"); } 13 | virtual void remove([[maybe_unused]]menu_component_t menuComponent) { 14 | throw std::invalid_argument("remove(): Unsupported Operation"); } 15 | virtual MenuComponent* getChid([[maybe_unused]]int i) const { 16 | throw std::invalid_argument("getChid(): Unsupported Operation"); } 17 | virtual std::string getName() const { 18 | throw std::invalid_argument("getName(): Unsupported Operation"); } 19 | virtual std::string getDescription() const { 20 | throw std::invalid_argument("getDescription(): Unsupported Operation"); } 21 | virtual double getPrice() const { 22 | throw std::invalid_argument("getPrice(): Unsupported Operation"); } 23 | virtual bool isVegetarian() const { 24 | throw std::invalid_argument("isVegetarian(): Unsupported Operation"); } 25 | virtual void print() const { 26 | throw std::invalid_argument("print(): Unsupported Operation"); } 27 | }; 28 | 29 | #endif /* MENU_COMPONENT_H */ 30 | -------------------------------------------------------------------------------- /Composite/menu/MenuItem.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_ITEM_H 2 | #define MENU_ITEM_H 3 | 4 | #include "MenuComponent.h" 5 | #include 6 | #include 7 | #include 8 | 9 | class MenuItem : public MenuComponent { 10 | public: 11 | MenuItem(std::string_view n, std::string_view d, bool v, double p) 12 | : name(n), description(d), vegetarian(v), price(p) { } 13 | std::string getName() const override { return name; } 14 | std::string getDescription() const override { return description; } 15 | bool isVegetarian() const override { return vegetarian; } 16 | double getPrice() const override { return price; } 17 | void print() const override; 18 | private: 19 | std::string name; 20 | std::string description; 21 | bool vegetarian = false; 22 | double price = 0.0; 23 | }; 24 | 25 | inline 26 | void 27 | MenuItem::print() const 28 | { 29 | std::cout << " " << getName(); 30 | if (isVegetarian()) 31 | std::cout << "(v)"; 32 | std::cout << ", " << getPrice() << '\n'; 33 | std::cout << " --" << getDescription() << '\n'; 34 | } 35 | 36 | #endif /* MENU_ITEM_H */ 37 | -------------------------------------------------------------------------------- /Composite/menu/Waitress.h: -------------------------------------------------------------------------------- 1 | #ifndef WAITRESS_H 2 | #define WAITRESS_H 3 | 4 | #include "MenuComponent.h" 5 | 6 | class Waitress { 7 | public: 8 | using menu_component_t = MenuComponent::menu_component_t; 9 | Waitress(menu_component_t am) : allMenus(std::move(am)) { } 10 | void printMenu() { allMenus->print(); } 11 | private: 12 | menu_component_t allMenus; 13 | }; 14 | 15 | #endif /* WAITRESS_H */ 16 | -------------------------------------------------------------------------------- /Composite/menuiterator/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ITERATOR_H 2 | #define ITERATOR_H 3 | 4 | #include 5 | 6 | // Java's Iterator interface for C++ 7 | template 8 | class Iterator { 9 | public: 10 | virtual ~Iterator() = default; 11 | virtual T* next() = 0; 12 | virtual bool hasNext() = 0; 13 | virtual void remove() = 0; 14 | }; 15 | 16 | #endif /* ITERATOR_H */ 17 | -------------------------------------------------------------------------------- /Composite/menuiterator/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++2a 3 | DEPS = CompositeIterator.h Iterator.h Makefile MenuComponent.h Menu.h \ 4 | MenuItem.h MenuIterator.h NullIterator.h Waitress.h 5 | TARGET = menuTestDrive 6 | #TARGET = menuIterTest 7 | 8 | OBJ = $(TARGET).o 9 | 10 | %.o: %.cpp $(DEPS) 11 | $(CXX) -c -o $@ $< $(CFLAGS) 12 | 13 | $(TARGET): $(OBJ) 14 | $(CXX) -o $@ $^ $(CFLAGS) 15 | 16 | clean: 17 | rm -f *.o $(TARGET) 18 | -------------------------------------------------------------------------------- /Composite/menuiterator/MenuIterator.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_ITERATOR_H 2 | #define MENU_ITERATOR_H 3 | 4 | #include "Iterator.h" 5 | #include "MenuComponent.h" 6 | #include 7 | #include 8 | 9 | // This is an 'object adaptor' to the Iterator class, which adapts the C++ 10 | // list container's iterator to Java's Iterator interface. 11 | 12 | class MenuIterator : public Iterator { 13 | public: 14 | using menu_component_t = MenuComponent::menu_component_t; 15 | MenuIterator(std::list &mc) 16 | : components(mc), iter(components.begin()) {} 17 | menu_component_t next() override { return *iter++; } 18 | bool hasNext() override { return iter != components.end(); } 19 | void remove() override { 20 | throw std::invalid_argument("MenuIterator::remove(): Unsupported Operation"); } 21 | private: 22 | std::list &components; 23 | std::list::iterator iter; 24 | }; 25 | 26 | #endif /* MENU_ITERATOR_H */ 27 | -------------------------------------------------------------------------------- /Composite/menuiterator/NullIterator.h: -------------------------------------------------------------------------------- 1 | #ifndef NULL_ITERATOR_H 2 | #define NULL_ITERATOR_H 3 | 4 | #include "Iterator.h" 5 | #include "MenuComponent.h" 6 | #include 7 | #include 8 | 9 | class NullIterator : public Iterator { 10 | public: 11 | MenuComponent::menu_component_t next() override { return nullptr; } 12 | bool hasNext() override { return false; } 13 | void remove() override { 14 | throw std::invalid_argument("NullIterator::remove(): Unsupported Operation"); } 15 | }; 16 | 17 | #endif /* NULL_ITERATOR_H */ 18 | -------------------------------------------------------------------------------- /Composite/menuiterator/Waitress.h: -------------------------------------------------------------------------------- 1 | #ifndef WAITRESS_H 2 | #define WAITRESS_H 3 | 4 | #include "CompositeIterator.h" 5 | #include "Iterator.h" 6 | #include "MenuComponent.h" 7 | #include 8 | #include 9 | #include 10 | 11 | class Waitress { 12 | public: 13 | using menu_component_t = MenuComponent::menu_component_t; 14 | using menu_component_iterator = MenuComponent::menu_component_iterator; 15 | Waitress(menu_component_t am) : allMenus(am) {} 16 | void printMenu() { allMenus->print(); } 17 | void printVegitarianMenu(); 18 | private: 19 | menu_component_t allMenus; 20 | }; 21 | 22 | inline 23 | void 24 | Waitress::printVegitarianMenu() 25 | { 26 | menu_component_iterator iterator = allMenus->createIterator(); 27 | std::cout << "\nVEGETARIAN\n----\n"; 28 | while (iterator->hasNext()) { 29 | auto menuComponent = iterator->next(); 30 | try { 31 | if (menuComponent->isVegetarian()) 32 | menuComponent->print(); 33 | } catch (std::invalid_argument e) { } 34 | } 35 | } 36 | 37 | #endif /* WAITRESS_H */ 38 | -------------------------------------------------------------------------------- /Decorator/starbuzz/Beverage.h: -------------------------------------------------------------------------------- 1 | #ifndef BEVERAGE_H 2 | #define BEVERAGE_H 3 | 4 | #include 5 | 6 | class Beverage { 7 | public: 8 | enum class Size { TALL, GRANDE, VENTI}; 9 | Beverage() = default; 10 | virtual ~Beverage() = default; 11 | virtual std::string getDescription() const { return description; } 12 | virtual double cost() = 0; 13 | virtual void setSize(Size s) { size = s;} 14 | virtual Size getSize() const { return size; } 15 | virtual std::string getSizeString() const; 16 | protected: 17 | std::string description = "Unknown Beverage"; 18 | Size size = Size::GRANDE; 19 | }; 20 | 21 | inline 22 | std::string 23 | Beverage::getSizeString() const { 24 | std::string result; 25 | switch (size) { 26 | case Size::TALL: 27 | result = "Tall"; 28 | break; 29 | case Size::GRANDE: 30 | result = "Grande"; 31 | break; 32 | case Size::VENTI: 33 | result = "Venti"; 34 | break; 35 | default: 36 | result = "Unknown size"; 37 | break; 38 | } 39 | return result; 40 | } 41 | 42 | #endif /* ifndef BEVERAGE_H */ 43 | -------------------------------------------------------------------------------- /Decorator/starbuzz/CondimentDecorator.h: -------------------------------------------------------------------------------- 1 | #ifndef CONDIMENT_DECORATOR_H 2 | #define CONDIMENT_DECORATOR_H 3 | 4 | #include "Beverage.h" 5 | #include 6 | #include 7 | 8 | class CondimentDecorator : public Beverage { 9 | public: 10 | CondimentDecorator() = default; 11 | CondimentDecorator(std::unique_ptr b) : beverage(std::move(b)) {} 12 | virtual std::string getDescription() const override 13 | { return "Unknown Condiment"; } 14 | // Overridding setSize, getSize and getSizeStr ensures they are propagated to 15 | // the wrapped beverage 16 | void setSize(Size s) override { beverage->setSize(s); } 17 | Beverage::Size getSize() const override { return beverage->getSize(); } 18 | std::string getSizeString() const override { return beverage->getSizeString(); } 19 | protected: 20 | std::unique_ptr beverage = nullptr; 21 | }; 22 | 23 | #endif /* ifndef CONDIMENT_DECORATOR_H */ 24 | -------------------------------------------------------------------------------- /Decorator/starbuzz/DarkRoast.h: -------------------------------------------------------------------------------- 1 | #ifndef DARK_ROAST_H 2 | #define DARK_ROAST_H 3 | 4 | #include "Beverage.h" 5 | 6 | class DarkRoast : public Beverage { 7 | public: 8 | DarkRoast() { description = "DarkRoast"; } 9 | double cost() override { return .99; } 10 | }; 11 | 12 | #endif /* ifndef DARK_ROAST_H */ 13 | -------------------------------------------------------------------------------- /Decorator/starbuzz/Decaf.h: -------------------------------------------------------------------------------- 1 | #ifndef DECAF_H 2 | #define DECAF_H 3 | 4 | #include "Beverage.h" 5 | 6 | class Decaf : public Beverage { 7 | public: 8 | Decaf() { description = "Decaf"; } 9 | double cost() override { return 1.05; } 10 | }; 11 | 12 | #endif /* ifndef DECAF_H */ 13 | -------------------------------------------------------------------------------- /Decorator/starbuzz/Espresso.h: -------------------------------------------------------------------------------- 1 | #ifndef ESPRESSO_H 2 | #define ESPRESSO_H 3 | 4 | #include "Beverage.h" 5 | 6 | class Espresso : public Beverage { 7 | public: 8 | Espresso() { description = "Espresso"; } 9 | double cost() override { return 1.99; }; 10 | }; 11 | 12 | #endif /* ifndef ESPRESSO_H */ 13 | -------------------------------------------------------------------------------- /Decorator/starbuzz/HouseBlend.h: -------------------------------------------------------------------------------- 1 | #ifndef HOUSE_BLEND_H 2 | #define HOUSE_BLEND_H 3 | 4 | #include "Beverage.h" 5 | 6 | class HouseBlend : public Beverage { 7 | public: 8 | HouseBlend() { description = "HouseBlend"; } 9 | double cost() override { return .89; } 10 | }; 11 | 12 | #endif /* ifndef HOUSE_BLEND_H */ 13 | -------------------------------------------------------------------------------- /Decorator/starbuzz/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++20 3 | DEPS = Beverage.h CondimentDecorator.h DarkRoast.h Decaf.h Espresso.h \ 4 | HouseBlend.h Mocha.h Soy.h Whip.h 5 | 6 | TARGET = starBuzzCoffee 7 | OBJ = $(TARGET).o 8 | 9 | %.o: %.cpp $(DEPS) 10 | $(CXX) -c -o $@ $< $(CFLAGS) 11 | 12 | $(TARGET): $(OBJ) 13 | $(CXX) -o $@ $^ $(CFLAGS) 14 | 15 | clean: 16 | rm -f *.o $(TARGET) 17 | -------------------------------------------------------------------------------- /Decorator/starbuzz/Mocha.h: -------------------------------------------------------------------------------- 1 | #ifndef MOCHA_H 2 | #define MOCHA_H 3 | 4 | #include "Beverage.h" 5 | #include "CondimentDecorator.h" 6 | #include 7 | #include 8 | 9 | class Mocha : public CondimentDecorator { 10 | public: 11 | Mocha() = default; 12 | Mocha(std::unique_ptr b) : CondimentDecorator(std::move(b)) {} 13 | std::string getDescription() const override { 14 | return beverage->getDescription() + ", Mocha"; } 15 | double cost() override; 16 | }; 17 | 18 | inline 19 | double 20 | Mocha::cost() { 21 | double cost = beverage->cost(); 22 | switch (beverage->getSize()) { 23 | case Size::TALL: 24 | cost += .15; 25 | break; 26 | case Size::GRANDE: 27 | cost += .20; 28 | break; 29 | case Size::VENTI: 30 | cost += .25; 31 | break; 32 | default: 33 | break; 34 | } 35 | return cost; 36 | } 37 | 38 | #endif /* ifndef MOCHA_H */ 39 | -------------------------------------------------------------------------------- /Decorator/starbuzz/Soy.h: -------------------------------------------------------------------------------- 1 | #ifndef SOY_H 2 | #define SOY_H 3 | 4 | #include "Beverage.h" 5 | #include "CondimentDecorator.h" 6 | #include 7 | #include 8 | 9 | class Soy : public CondimentDecorator { 10 | public: 11 | Soy() = default; 12 | Soy(std::unique_ptr b) : CondimentDecorator(std::move(b)) {} 13 | std::string getDescription() const override { 14 | return beverage->getDescription() + ", Soy"; } 15 | double cost() override; 16 | }; 17 | 18 | inline 19 | double 20 | Soy::cost() { 21 | double cost = beverage->cost(); 22 | switch (beverage->getSize()) { 23 | case Size::TALL: 24 | cost += .10; 25 | break; 26 | case Size::GRANDE: 27 | cost += .15; 28 | break; 29 | case Size::VENTI: 30 | cost += .20; 31 | break; 32 | default: 33 | break; 34 | } 35 | return cost; 36 | } 37 | 38 | #endif /* ifndef SOY_H */ 39 | -------------------------------------------------------------------------------- /Decorator/starbuzz/Whip.h: -------------------------------------------------------------------------------- 1 | #ifndef WHIP_H 2 | #define WHIP_H 3 | 4 | #include "Beverage.h" 5 | #include "CondimentDecorator.h" 6 | #include 7 | #include 8 | 9 | class Whip : public CondimentDecorator { 10 | public: 11 | Whip() = default; 12 | Whip(std::unique_ptr b) : CondimentDecorator(std::move(b)) { } 13 | std::string getDescription() const override { 14 | return beverage->getDescription() + ", Whip"; } 15 | double cost() override; 16 | }; 17 | 18 | inline 19 | double 20 | Whip::cost() { 21 | double cost = beverage->cost(); 22 | switch (beverage->getSize()) { 23 | case Size::TALL: 24 | cost += .05; 25 | break; 26 | case Size::GRANDE: 27 | cost += .10; 28 | break; 29 | case Size::VENTI: 30 | cost += .15; 31 | break; 32 | default: 33 | break; 34 | } 35 | return cost; 36 | } 37 | 38 | #endif /* ifndef WHIP_H */ 39 | -------------------------------------------------------------------------------- /Facade/hometheater/Amplifier.h: -------------------------------------------------------------------------------- 1 | #ifndef AMPLIFIER_H 2 | #define AMPLIFIER_H 3 | 4 | #include "Tuner.h" 5 | #include "StreamingPlayer.h" 6 | #include 7 | #include 8 | 9 | class Amplifier { 10 | public: 11 | Amplifier(const std::string &d) : description(d) {} 12 | void on() { std::cout << description << " on\n"; } 13 | void off() { std::cout << description << " off\n"; } 14 | void setStereoSound() { std::cout << description << " stereo mode on\n"; } 15 | void setSurroundSound() { std::cout << description << " surround sound on (5 speakers, 1 subwoofer)\n"; } 16 | void setVolume(int level) { std::cout << description << " setting volume to " << level << '\n'; } 17 | void setTuner(Tuner *t); 18 | void setStreamingPlayer(StreamingPlayer *p); 19 | std::string toString() { return description; } 20 | private: 21 | std::string description; 22 | Tuner *tuner = nullptr; 23 | StreamingPlayer *player = nullptr; 24 | }; 25 | 26 | inline 27 | void 28 | Amplifier::setTuner(Tuner *t) 29 | { 30 | std::cout << description << " setting tuner to " << player << '\n'; 31 | tuner = t; 32 | } 33 | 34 | inline 35 | void 36 | Amplifier::setStreamingPlayer(StreamingPlayer *p) 37 | { 38 | std::cout << description << " setting Streaming player to " << player << '\n'; 39 | player = p; 40 | } 41 | 42 | #endif /* AMPLIFIER_H */ 43 | -------------------------------------------------------------------------------- /Facade/hometheater/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = Amplifier.h CDPlayer.h HomeTheaterFacade.h PopcornPopper.h Projector.h \ 4 | Screen.h StreamingPlayer.h TheaterLights.h Tuner.h 5 | TARGET = homeTheaterTestDrive 6 | OBJ = $(TARGET).o 7 | 8 | %.o: %.cpp $(DEPS) 9 | $(CXX) -c -o $@ $< $(CFLAGS) 10 | 11 | $(TARGET): $(OBJ) 12 | $(CXX) -o $@ $^ $(CFLAGS) 13 | 14 | clean: 15 | rm -f *.o $(TARGET) 16 | -------------------------------------------------------------------------------- /Facade/hometheater/PopcornPopper.h: -------------------------------------------------------------------------------- 1 | #ifndef POPCORN_POPPER_H 2 | #define POPCORN_POPPER_H 3 | 4 | #include 5 | #include 6 | 7 | class PopcornPopper { 8 | public: 9 | PopcornPopper(const std::string &d) : description(d) {} 10 | void on() { std::cout << description << " on\n"; } 11 | void off() { std::cout << description << " off\n"; } 12 | void pop() { std::cout << description << " popping popcorn\n"; } 13 | std::string toString() { return description; } 14 | private: 15 | std::string description; 16 | }; 17 | 18 | #endif /* POPCORN_POPPER_H */ 19 | -------------------------------------------------------------------------------- /Facade/hometheater/Projector.h: -------------------------------------------------------------------------------- 1 | #ifndef PROJECTOR_H 2 | #define PROJECTOR_H 3 | 4 | #include "StreamingPlayer.h" 5 | #include 6 | #include 7 | 8 | class Projector { 9 | public: 10 | Projector(const std::string d, StreamingPlayer *p) : description(d), player(p) {} 11 | void on() { std::cout << description << " on\n"; } 12 | void off() { std::cout << description << " off\n"; } 13 | void wideScreenMode() { std::cout << description << " in widescreen mode (16x9 aspect ratio)\n"; } 14 | void tvMode() { std::cout << description << " in tv mode (4x3 aspect ratio)\n"; } 15 | std::string toString() { return description; } 16 | private: 17 | std::string description; 18 | [[maybe_unused]] StreamingPlayer *player; 19 | }; 20 | 21 | #endif /* PROJECTOR_H */ 22 | -------------------------------------------------------------------------------- /Facade/hometheater/Screen.h: -------------------------------------------------------------------------------- 1 | #ifndef SCREEN_H 2 | #define SCREEN_H 3 | 4 | #include 5 | #include 6 | 7 | class Screen { 8 | public: 9 | Screen(const std::string & d) : description(d) {} 10 | void up() { std::cout << description << " going up\n"; } 11 | void down() { std::cout << description << " going down\n"; } 12 | std::string toString() { return description; } 13 | private: 14 | std::string description; 15 | }; 16 | 17 | #endif /* SCREEN_H */ 18 | -------------------------------------------------------------------------------- /Facade/hometheater/TheaterLights.h: -------------------------------------------------------------------------------- 1 | #ifndef THEATER_LIGHTS_H 2 | #define THEATER_LIGHTS_H 3 | 4 | #include 5 | #include 6 | 7 | class TheaterLights { 8 | public: 9 | TheaterLights(const std::string &d) : description(d) {} 10 | void on() { std::cout << description << " on\n"; } 11 | void off() { std::cout << description << " off\n"; } 12 | void dim(int level) { std::cout << description << " dimming to " << level << "%\n"; } 13 | private: 14 | std::string description; 15 | }; 16 | 17 | #endif /* THEATER_LIGHTS_H */ 18 | -------------------------------------------------------------------------------- /Facade/hometheater/Tuner.h: -------------------------------------------------------------------------------- 1 | #ifndef TUNER_H 2 | #define TUNER_H 3 | 4 | #include 5 | #include 6 | 7 | class Amplifier; // forward declaration 8 | class Tuner { 9 | public: 10 | Tuner(const std::string &d, Amplifier *a) : description(d), amplifier(a) {} 11 | void on() { std::cout << description << " on\n"; } 12 | void off() { std::cout << description << " off\n"; } 13 | void setFrequency(double f); 14 | void setAm() { std::cout << description << " setting AM mode\n"; } 15 | void setFm() { std::cout << description << " setting FM mode\n"; } 16 | std::string toString() { return description; } 17 | private: 18 | std::string description; 19 | [[maybe_unused]] Amplifier *amplifier; 20 | double frequency; 21 | }; 22 | 23 | inline 24 | void 25 | Tuner::setFrequency(double f) 26 | { 27 | std::cout << description << " setting frequency to " << f << '\n'; 28 | frequency = f; 29 | } 30 | 31 | #endif /* TUNER_H */ 32 | -------------------------------------------------------------------------------- /Facade/hometheater/homeTheaterTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "Amplifier.h" 2 | #include "Tuner.h" 3 | #include "StreamingPlayer.h" 4 | #include "CDPlayer.h" 5 | #include "Projector.h" 6 | #include "TheaterLights.h" 7 | #include "Screen.h" 8 | #include "PopcornPopper.h" 9 | #include "HomeTheaterFacade.h" 10 | 11 | int main() 12 | { 13 | 14 | auto amp = new Amplifier("Amplifier"); 15 | auto tuner = new Tuner("AM/FM Tuner", amp); 16 | auto player = new StreamingPlayer("Streaming Player", amp); 17 | [[maybe_unused]] auto cd = new CDPlayer("CD Player", amp); 18 | auto projector = new Projector("Projector", player); 19 | auto lights = new TheaterLights("Theater Ceiling Lights"); 20 | auto screen = new Screen("Theater Screen"); 21 | auto popper = new PopcornPopper("Popcorn Popper"); 22 | 23 | auto homeTheater = 24 | new HomeTheaterFacade(amp, tuner, player, projector, screen, lights, popper); 25 | 26 | homeTheater->watchMovie("Children of Men"); 27 | homeTheater->endMovie(); 28 | 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /Factory/pizzaaf/BlackOlives.hpp: -------------------------------------------------------------------------------- 1 | #ifndef BLACK_OLIVES_H 2 | #define BLACK_OLIVES_H 3 | 4 | #include "Veggies.hpp" 5 | #include 6 | 7 | class BlackOlives : public Veggies { 8 | public: 9 | std::string toString() const override { return "BlackOlives"; } 10 | }; 11 | 12 | #endif /* BLACK_OLIVES_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Cheese.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_H 2 | #define CHEESE_H 3 | 4 | #include 5 | 6 | class Cheese { 7 | public: 8 | virtual ~Cheese() = default; 9 | virtual std::string toString() const = 0; 10 | }; 11 | 12 | #endif /* CHEESE_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/CheesePizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_PIZZA_H 2 | #define CHEESE_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | #include "PizzaIngredientFactory.hpp" 6 | #include 7 | #include 8 | 9 | class CheesePizza : public Pizza { 10 | public: 11 | CheesePizza () = default; 12 | CheesePizza(std::unique_ptringfac) : ingredientFactory(std::move(ingfac)) {} 13 | void prepare() override; 14 | private: 15 | std::unique_ptr ingredientFactory = nullptr; 16 | }; 17 | 18 | inline 19 | void 20 | CheesePizza::prepare() 21 | { 22 | std::cout << "Preparing " + name << '\n'; 23 | dough = ingredientFactory->createDough(); 24 | sauce = ingredientFactory->createSauce(); 25 | cheese = ingredientFactory->createCheese(); 26 | } 27 | 28 | #endif /* CHEESE_PIZZA_H */ 29 | -------------------------------------------------------------------------------- /Factory/pizzaaf/ClamPizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLAM_PIZZA_H 2 | #define CLAM_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | #include "PizzaIngredientFactory.hpp" 6 | #include 7 | #include 8 | 9 | class ClamPizza : public Pizza { 10 | public: 11 | ClamPizza() = default; 12 | ClamPizza(std::unique_ptr infac) : ingredientFactory(std::move(infac)) {} 13 | void prepare() override; 14 | private: 15 | std::unique_ptr ingredientFactory{nullptr}; 16 | }; 17 | 18 | inline 19 | void 20 | ClamPizza::prepare() 21 | { 22 | std::cout << "Preparing " << name << '\n'; 23 | dough = ingredientFactory->createDough(); 24 | sauce = ingredientFactory->createSauce(); 25 | cheese = ingredientFactory->createCheese(); 26 | clam = ingredientFactory->createClams(); 27 | } 28 | 29 | #endif /* CLAM_PIZZA_H */ 30 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Clams.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLAMS_H 2 | #define CLAMS_H 3 | 4 | #include 5 | 6 | class Clams { 7 | public: 8 | virtual ~Clams() = default; 9 | virtual std::string toString() const = 0; 10 | }; 11 | 12 | #endif /* CLAMS_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Dough.hpp: -------------------------------------------------------------------------------- 1 | #ifndef DOUGH_H 2 | #define DOUGH_H 3 | 4 | #include 5 | 6 | class Dough { 7 | public: 8 | virtual ~Dough() = default; 9 | virtual std::string toString() const = 0; 10 | }; 11 | 12 | #endif /* DOUGH_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Eggplant.hpp: -------------------------------------------------------------------------------- 1 | #ifndef EGGPLANT_H 2 | #define EGGPLANT_H 3 | 4 | #include "Veggies.hpp" 5 | #include 6 | 7 | class Eggplant : public Veggies { 8 | public: 9 | std::string toString() const override { return "Eggplant"; } 10 | }; 11 | 12 | #endif /* EGGPLANT_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/FreshClams.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FRESH_CLAMS_H 2 | #define FRESH_CLAMS_H 3 | 4 | #include "Clams.hpp" 5 | #include 6 | 7 | class FreshClams : public Clams { 8 | public: 9 | std::string toString() const override { return "Fresh Clams from Long Island Sound"; } 10 | }; 11 | 12 | #endif /* FRESH_CLAMS_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/FrozenClams.hpp: -------------------------------------------------------------------------------- 1 | #ifndef FROZEN_CLAMS_H 2 | #define FROZEN_CLAMS_H 3 | 4 | #include "Clams.hpp" 5 | #include 6 | 7 | class FrozenClams : public Clams { 8 | public: 9 | std::string toString() const override { return "Frozen Clams from Chespeake Bay"; } 10 | }; 11 | 12 | #endif /* FROZEN_CLAMS_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Garlic.hpp: -------------------------------------------------------------------------------- 1 | #ifndef GARLIC_H 2 | #define GARLIC_H 3 | 4 | #include "Veggies.hpp" 5 | #include 6 | 7 | class Garlic : public Veggies { 8 | public: 9 | std::string toString() const override { return "Garlic"; } 10 | }; 11 | 12 | #endif /* GARLIC_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = BlackOlives.hpp Cheese.hpp CheesePizza.hpp ChicagoPizzaIngredientFactory.hpp \ 4 | ChicagoPizzaStore.hpp ClamPizza.hpp Clams.hpp Dough.hpp Eggplant.hpp \ 5 | FreshClams.hpp FrozenClams.hpp Garlic.hpp Makefile MarinaraSauce.hpp \ 6 | MozzarellaCheese.hpp Mushroom.hpp NYPizzaIngredientFactory.hpp NYPizzaStore.hpp \ 7 | Onion.hpp ParmesanCheese.hpp Pepperoni.hpp PepperoniPizza.hpp Pizza.hpp \ 8 | PizzaIngredientFactory.hpp PizzaStore.hpp PlumTomatoSauce.hpp RedPepper.hpp \ 9 | ReggianoCheese.hpp Sauce.hpp SlicedPepperoni.hpp Spinach.hpp ThickCrustDough.hpp \ 10 | ThinCrustDough.hpp VeggiePizza.hpp Veggies.hpp 11 | TARGET = pizzaTestDrive 12 | OBJ = $(TARGET).o 13 | 14 | %.o: %.cpp $(DEPS) 15 | $(CXX) -c -o $@ $< $(CFLAGS) 16 | 17 | $(TARGET): $(OBJ) 18 | $(CXX) -o $@ $^ $(CFLAGS) 19 | 20 | clean: 21 | rm -f *.o $(TARGET) 22 | -------------------------------------------------------------------------------- /Factory/pizzaaf/MarinaraSauce.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MARINARA_SAUCE_H 2 | #define MARINARA_SAUCE_H 3 | 4 | #include "Sauce.hpp" 5 | #include 6 | 7 | class MarinaraSauce : public Sauce { 8 | public: 9 | std::string toString() const override { return "Marinara Sauce"; } 10 | }; 11 | 12 | #endif /* MARINARA_SAUCE_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/MozzarellaCheese.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MOZZARELLA_CHEESE_H 2 | #define MOZZARELLA_CHEESE_H 3 | 4 | #include "Cheese.hpp" 5 | #include 6 | 7 | class MozzarellaCheese : public Cheese { 8 | public: 9 | std::string toString() const override { return "Shredded Mozzarella"; } 10 | 11 | }; 12 | 13 | #endif /* MOZZARELLA_CHEESE_H */ 14 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Mushroom.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MUSHROOM_H 2 | #define MUSHROOM_H 3 | 4 | #include "Veggies.hpp" 5 | #include 6 | 7 | class Mushroom : public Veggies { 8 | public: 9 | std::string toString() const override { return "Mushroom"; } 10 | }; 11 | 12 | #endif /* MUSHROOM_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Onion.hpp: -------------------------------------------------------------------------------- 1 | #ifndef ONION_H 2 | #define ONION_H 3 | 4 | #include "Veggies.hpp" 5 | #include 6 | 7 | class Onion : public Veggies { 8 | public: 9 | std::string toString() const override { return "Onion"; } 10 | }; 11 | 12 | #endif /* ONION_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/ParmesanCheese.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PARMESAN_CHEESE_H 2 | #define PARMESAN_CHEESE_H 3 | 4 | #include "Cheese.hpp" 5 | #include 6 | 7 | class ParmesanCheese : public Cheese { 8 | public: 9 | std::string toString() const override { return "Shredded Parmesan"; } 10 | }; 11 | 12 | #endif /* PARMESAN_CHEESE_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Pepperoni.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PEPPERONI_H 2 | #define PEPPERONI_H 3 | 4 | #include 5 | 6 | class Pepperoni { 7 | public: 8 | virtual ~Pepperoni () = default; 9 | virtual std::string toString() const = 0; 10 | }; 11 | 12 | #endif /* PEPPERONI_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/PepperoniPizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PEPPERONI_PIZZA_H 2 | #define PEPPERONI_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | #include "PizzaIngredientFactory.hpp" 6 | #include 7 | #include 8 | 9 | class PepperoniPizza : public Pizza { 10 | public: 11 | PepperoniPizza() = default; 12 | PepperoniPizza(std::unique_ptr infac) : ingredientFactory(std::move(infac)) {} 13 | void prepare() override; 14 | private: 15 | std::unique_ptr ingredientFactory{nullptr}; 16 | }; 17 | 18 | inline 19 | void 20 | PepperoniPizza::prepare() 21 | { 22 | std::cout << "Preparing " << name << '\n'; 23 | dough = ingredientFactory->createDough(); 24 | sauce = ingredientFactory->createSauce(); 25 | cheese = ingredientFactory->createCheese(); 26 | pepperoni = ingredientFactory->createPepperoni(); 27 | } 28 | 29 | #endif /* PEPPERONI_PIZZA_H */ 30 | -------------------------------------------------------------------------------- /Factory/pizzaaf/PizzaIngredientFactory.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_INGREDIENT_FACTORY_H 2 | #define PIZZA_INGREDIENT_FACTORY_H 3 | 4 | #include "Cheese.hpp" 5 | #include "Clams.hpp" 6 | #include "Dough.hpp" 7 | #include "Pepperoni.hpp" 8 | #include "Sauce.hpp" 9 | #include "Veggies.hpp" 10 | #include "memory" 11 | #include 12 | #include 13 | 14 | class PizzaIngredientFactory { 15 | public: 16 | virtual ~PizzaIngredientFactory() = default; 17 | virtual std::unique_ptr createDough() = 0; 18 | virtual std::unique_ptr createSauce() = 0; 19 | virtual std::unique_ptr createCheese() = 0; 20 | virtual std::vector> createVeggies() = 0; 21 | virtual std::unique_ptr createPepperoni() = 0; 22 | virtual std::unique_ptr createClams() = 0; 23 | }; 24 | 25 | #endif /* PIZZA_INGREDIENT_FACTORY_H */ 26 | -------------------------------------------------------------------------------- /Factory/pizzaaf/PizzaStore.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_STORE_H 2 | #define PIZZA_STORE_H 3 | 4 | #include "Pizza.hpp" 5 | #include 6 | #include 7 | 8 | class PizzaStore { 9 | public: 10 | virtual ~PizzaStore() = default; 11 | std::unique_ptr orderPizza(std::string_view item) { 12 | auto pizza = createPizza(item); 13 | std::cout << "--- Making a " << pizza->getName() << " ---\n"; 14 | pizza->prepare(); 15 | pizza->bake(); 16 | pizza->cut(); 17 | pizza->box(); 18 | return pizza; 19 | } 20 | protected: 21 | virtual std::unique_ptr createPizza(std::string_view item) = 0; 22 | }; 23 | 24 | #endif /* ifndef PIZZA_STORE_H */ 25 | -------------------------------------------------------------------------------- /Factory/pizzaaf/PlumTomatoSauce.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PLUM_TOMATO_SAUCE_H 2 | #define PLUM_TOMATO_SAUCE_H 3 | 4 | #include "Sauce.hpp" 5 | #include 6 | 7 | class PlumTomatoSauce : public Sauce { 8 | public: 9 | std::string toString() const override { return "Tomato sauce with plum tomatoes"; } 10 | }; 11 | 12 | #endif /* PLUM_TOMATO_SAUCE_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/RedPepper.hpp: -------------------------------------------------------------------------------- 1 | #ifndef RED_PEPPER_H_H 2 | #define RED_PEPPER_H_H 3 | 4 | #include "Veggies.hpp" 5 | #include 6 | 7 | class RedPepper : public Veggies { 8 | public: 9 | std::string toString() const override { return "RedPepper"; } 10 | }; 11 | 12 | #endif /* RED_PEPPER_H_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/ReggianoCheese.hpp: -------------------------------------------------------------------------------- 1 | #ifndef REGGIANO_CHEESE_H 2 | #define REGGIANO_CHEESE_H 3 | 4 | #include "Cheese.hpp" 5 | #include 6 | 7 | class ReggianoCheese : public Cheese { 8 | public: 9 | std::string toString() const override { return "Reggiano Cheese"; } 10 | }; 11 | 12 | #endif /* REGGIANO_CHEESE_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Sauce.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SAUCE_H 2 | #define SAUCE_H 3 | 4 | #include 5 | 6 | class Sauce { 7 | public: 8 | virtual ~Sauce() = default; 9 | virtual std::string toString() const = 0; 10 | }; 11 | 12 | #endif /* SAUCE_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/SlicedPepperoni.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SLICED_PEPPERONI_H 2 | #define SLICED_PEPPERONI_H 3 | 4 | #include "Pepperoni.hpp" 5 | #include 6 | 7 | class SlicedPepperoni : public Pepperoni { 8 | public: 9 | std::string toString() const override { return "Pepperoni"; } 10 | }; 11 | 12 | #endif /* SLICED_PEPPERONI_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Spinach.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SPINACH_H 2 | #define SPINACH_H 3 | 4 | #include "Veggies.hpp" 5 | #include 6 | 7 | class Spinach : public Veggies { 8 | public: 9 | std::string toString() const override { return "Spinach"; } 10 | }; 11 | 12 | #endif /* SPINACH_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/ThickCrustDough.hpp: -------------------------------------------------------------------------------- 1 | #ifndef THICK_CRUST_DOUGH_H 2 | #define THICK_CRUST_DOUGH_H 3 | 4 | #include "Dough.hpp" 5 | #include 6 | 7 | class ThickCrustDough : public Dough { 8 | public: 9 | std::string toString() const override { return "Thick Crust dough"; } 10 | }; 11 | 12 | #endif /* THICK_CRUST_DOUGH_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/ThinCrustDough.hpp: -------------------------------------------------------------------------------- 1 | #ifndef THIN_CRUST_DOUGH_H 2 | #define THIN_CRUST_DOUGH_H 3 | 4 | #include "Dough.hpp" 5 | #include 6 | 7 | class ThinCrustDough : public Dough { 8 | public: 9 | std::string toString() const override { return "Thin Crust dough"; } 10 | }; 11 | 12 | #endif /* THIN_CRUST_DOUGH_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/VeggiePizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VEGGIE_PIZZA_H 2 | #define VEGGIE_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | #include "PizzaIngredientFactory.hpp" 6 | #include 7 | 8 | class VeggiePizza : public Pizza { 9 | public: 10 | VeggiePizza() = default; 11 | VeggiePizza(std::unique_ptr ingFact) : 12 | ingredientFactory(std::move(ingFact)) {} 13 | void prepare() override; 14 | private: 15 | std::unique_ptr ingredientFactory; 16 | }; 17 | 18 | 19 | inline 20 | void 21 | VeggiePizza::prepare() 22 | { 23 | std::cout << "Preparing " << name << '\n'; 24 | dough = ingredientFactory->createDough(); 25 | sauce = ingredientFactory->createSauce(); 26 | cheese = ingredientFactory->createCheese(); 27 | veggies = ingredientFactory->createVeggies(); 28 | } 29 | 30 | #endif /* VEGGIE_PIZZA_H */ 31 | -------------------------------------------------------------------------------- /Factory/pizzaaf/Veggies.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VEGGIES_H 2 | #define VEGGIES_H 3 | 4 | #include 5 | 6 | class Veggies { 7 | public: 8 | virtual ~Veggies() = default; 9 | virtual std::string toString() const = 0; 10 | }; 11 | 12 | #endif /* VEGGIES_H */ 13 | -------------------------------------------------------------------------------- /Factory/pizzaaf/pizzaTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "Pizza.hpp" 2 | #include "PizzaStore.hpp" 3 | #include "NYPizzaStore.hpp" 4 | #include "ChicagoPizzaStore.hpp" 5 | #include "Veggies.hpp" 6 | #include 7 | #include 8 | #include 9 | 10 | int main() { 11 | // create stores 12 | std::unique_ptr nyStore = std::make_unique(); 13 | std::unique_ptr chicagoStore = std::make_unique(); 14 | 15 | // order different pizzas from different stores 16 | std::unique_ptr pizza = nyStore->orderPizza("cheese"); 17 | std::cout << "Ethan ordered a " << *pizza << "\n\n"; 18 | 19 | pizza = chicagoStore->orderPizza("cheese"); 20 | std::cout << "Joel ordered a " << *pizza << "\n\n"; 21 | 22 | pizza = nyStore->orderPizza("clam"); 23 | std::cout << "Ethan ordered a " << *pizza << "\n\n"; 24 | 25 | pizza = chicagoStore->orderPizza("clam"); 26 | std::cout << "Joel ordered a " << *pizza << "\n\n"; 27 | 28 | pizza = nyStore->orderPizza("pepperoni"); 29 | std::cout << "Ethan ordered a " << *pizza << "\n\n"; 30 | 31 | pizza = chicagoStore->orderPizza("pepperoni"); 32 | std::cout << "Joel ordered a " << *pizza << "\n\n"; 33 | 34 | pizza = nyStore->orderPizza("veggie"); 35 | std::cout << "Ethan ordered a " << *pizza << "\n\n"; 36 | 37 | pizza = chicagoStore->orderPizza("veggie"); 38 | std::cout << "Joel ordered a " << *pizza << "\n\n"; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /Factory/pizzaaf/test.cpp: -------------------------------------------------------------------------------- 1 | #include "Veggies.hpp" 2 | #include "Garlic.hpp" 3 | #include 4 | #include 5 | #include 6 | 7 | int main() 8 | { 9 | std::vector> vVec; 10 | 11 | // auto garlic = std::make_unique(); 12 | vVec.push_back(std::unique_ptr(new Garlic())); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Factory/pizzafm/ChicagoPizzaStore.hpp: -------------------------------------------------------------------------------- 1 | #ifndef Chicago_PIZZA_STORE_H 2 | #define Chicago_PIZZA_STORE_H 3 | 4 | #include "ChicagoStyleCheesePizza.hpp" 5 | #include "ChicagoStyleClamPizza.hpp" 6 | #include "ChicagoStylePepperoniPizza.hpp" 7 | #include "ChicagoStyleVeggiePizza.hpp" 8 | #include "PizzaStore.hpp" 9 | #include "Pizza.hpp" 10 | #include 11 | #include 12 | 13 | class ChicagoPizzaStore : public PizzaStore { 14 | protected: 15 | std::unique_ptr createPizza(std::string_view item) override; 16 | }; 17 | 18 | inline 19 | std::unique_ptr 20 | ChicagoPizzaStore::createPizza(std::string_view item) { 21 | if (item == "cheese") 22 | return std::make_unique(); 23 | if (item == "veggie") 24 | return std::make_unique(); 25 | if (item == "clam") 26 | return std::make_unique(); 27 | if (item == "pepperoni") 28 | return std::make_unique(); 29 | return nullptr; 30 | } 31 | 32 | #endif /* ifndef Chicago_PIZZA_STORE_H */ 33 | -------------------------------------------------------------------------------- /Factory/pizzafm/ChicagoStyleCheesePizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGO_STYLE_CHEESE_PIZZA_H 2 | #define CHICAGO_STYLE_CHEESE_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | #include 6 | 7 | class ChicagoStyleCheesePizza : public Pizza { 8 | public: 9 | ChicagoStyleCheesePizza(); 10 | void cut() const override; 11 | }; 12 | 13 | inline 14 | ChicagoStyleCheesePizza::ChicagoStyleCheesePizza() { 15 | name = "Chicago Style Deep Dish Cheese Pizza"; 16 | dough = "Extra Thick Crust Dough"; 17 | sauce = "Plum Tomato Sauce"; 18 | toppings.push_back("Shredded Mozzarella Cheese"); 19 | } 20 | 21 | inline 22 | void 23 | ChicagoStyleCheesePizza::cut() const 24 | { 25 | std::cout << "cutting the pizza into square slices\n"; 26 | } 27 | 28 | #endif /* ifndef CHICAGO_STYLE_CHEESE_PIZZA_H */ 29 | -------------------------------------------------------------------------------- /Factory/pizzafm/ChicagoStyleClamPizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGO_STYLE_CLAM_PIZZA_H 2 | #define CHICAGO_STYLE_CLAM_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | #include 6 | 7 | class ChicagoStyleClamPizza : public Pizza { 8 | public: 9 | ChicagoStyleClamPizza (); 10 | void cut() const override; 11 | }; 12 | 13 | inline 14 | ChicagoStyleClamPizza::ChicagoStyleClamPizza() 15 | { 16 | name = "Chicago Style Clam Pizza"; 17 | dough = "Extra Thick Crust Dough"; 18 | sauce = "Plum Tomato Sauce"; 19 | toppings.push_back("Shredded Mozzarella Cheese"); 20 | toppings.push_back("Frozen Clams from Chesapeake Bay"); 21 | } 22 | 23 | inline 24 | void 25 | ChicagoStyleClamPizza::cut() const { 26 | std::cout << "Cutting the pizza into square slices"; 27 | } 28 | 29 | #endif /* CHICAGO_STYLE_CLAM_PIZZA_H */ 30 | -------------------------------------------------------------------------------- /Factory/pizzafm/ChicagoStylePepperoniPizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGO_STYLE_PEPPERONI_PIZZA_H 2 | #define CHICAGO_STYLE_PEPPERONI_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | #include 6 | 7 | class ChicagoStylePepperoniPizza : public Pizza { 8 | public: 9 | ChicagoStylePepperoniPizza (); 10 | void cut() const override; 11 | }; 12 | 13 | inline 14 | ChicagoStylePepperoniPizza::ChicagoStylePepperoniPizza() 15 | { 16 | name = "Chicago Style Pepperoni Pizza"; 17 | dough = "Extra Thick Crust Dough"; 18 | sauce = "Plum Tomato Sauce"; 19 | toppings.push_back("Shredded Mozzarella Cheese"); 20 | toppings.push_back("Black Olives"); 21 | toppings.push_back("Spinach"); 22 | toppings.push_back("Eggplant"); 23 | toppings.push_back("Sliced Pepperoni"); 24 | } 25 | 26 | inline 27 | void 28 | ChicagoStylePepperoniPizza::cut() const 29 | { 30 | std::cout << "Cutting the pizza into square slices\n"; 31 | } 32 | 33 | #endif /* CHICAGO_STYLE_PEPPERONI_PIZZA_H */ 34 | -------------------------------------------------------------------------------- /Factory/pizzafm/ChicagoStyleVeggiePizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CHICAGO_STYLE_VEGGIE_PIZZA_H 2 | #define CHICAGO_STYLE_VEGGIE_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | 6 | class ChicagoStyleVeggiePizza : public Pizza { 7 | public: 8 | ChicagoStyleVeggiePizza (); 9 | }; 10 | 11 | inline 12 | ChicagoStyleVeggiePizza::ChicagoStyleVeggiePizza() 13 | { 14 | name = "Chicago Deep Dish Veggie Pizza"; 15 | dough = "Extra Thick Crust Dough"; 16 | sauce = "Plum Tomato Sauce"; 17 | toppings.push_back("Shredded Mozzarella Cheese"); 18 | toppings.push_back("Black Olives"); 19 | toppings.push_back("Spinach"); 20 | toppings.push_back("Eggplant"); 21 | } 22 | 23 | #endif /* CHICAGO_STYLE_VEGGIE_PIZZA_H */ 24 | -------------------------------------------------------------------------------- /Factory/pizzafm/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = ChicagoPizzaStore.hpp ChicagoStyleCheesePizza.hpp \ 4 | ChicagoStyleClamPizza.hpp ChicagoStylePepperoniPizza.hpp \ 5 | ChicagoStyleVeggiePizza.hpp NYPizzaStore.hpp NYStyleCheesePizza.hpp \ 6 | NYStyleClamPizza.hpp NYStylePepperoniPizza.hpp NYStyleVeggiePizza.hpp \ 7 | Pizza.hpp PizzaStore.hpp 8 | TARGET = pizzaTestDrive 9 | OBJ = $(TARGET).o 10 | 11 | %.o: %.cpp $(DEPS) 12 | $(CXX) -c -o $@ $< $(CFLAGS) 13 | 14 | $(TARGET): $(OBJ) 15 | $(CXX) -o $@ $^ $(CFLAGS) 16 | 17 | clean: 18 | rm -f *.o $(TARGET) 19 | -------------------------------------------------------------------------------- /Factory/pizzafm/NYPizzaStore.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NY_PIZZA_STORE_H 2 | #define NY_PIZZA_STORE_H 3 | 4 | #include "NYStyleCheesePizza.hpp" 5 | #include "NYStyleClamPizza.hpp" 6 | #include "NYStylePepperoniPizza.hpp" 7 | #include "NYStyleVeggiePizza.hpp" 8 | #include "NYStyleCheesePizza.hpp" 9 | #include "Pizza.hpp" 10 | #include "PizzaStore.hpp" 11 | #include 12 | #include 13 | 14 | class NYPizzaStore : public PizzaStore { 15 | protected: 16 | std::unique_ptr createPizza(std::string_view item) override; 17 | }; 18 | 19 | inline 20 | std::unique_ptr 21 | NYPizzaStore::createPizza(std::string_view item) { 22 | if (item == "cheese") 23 | return std::make_unique(); 24 | if (item == "veggie") 25 | return std::make_unique(); 26 | if (item == "clam") 27 | return std::make_unique(); 28 | if (item == "pepperoni") 29 | return std::make_unique(); 30 | return nullptr; 31 | } 32 | 33 | #endif /* ifndef NY_PIZZA_STORE_H */ 34 | -------------------------------------------------------------------------------- /Factory/pizzafm/NYStyleCheesePizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NY_STYLE_CHEESE_PIZZA_H 2 | #define NY_STYLE_CHEESE_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | 6 | class NYStyleCheesePizza : public Pizza { 7 | public: 8 | NYStyleCheesePizza(); 9 | }; 10 | 11 | inline 12 | NYStyleCheesePizza::NYStyleCheesePizza() { 13 | name = "NY Style Sauce and Cheese Pizza"; 14 | dough = "Thin Crust Dough"; 15 | sauce = "Marinara Sauce"; 16 | toppings.push_back("Grated Reggiano Chesse"); 17 | } 18 | 19 | #endif /* ifndef NY_STYLE_CHEESE_PIZZA_H */ 20 | -------------------------------------------------------------------------------- /Factory/pizzafm/NYStyleClamPizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NY_STYLE_CLAM_PIZZA_H 2 | #define NY_STYLE_CLAM_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | 6 | class NYStyleClamPizza : public Pizza { 7 | public: 8 | NYStyleClamPizza(); 9 | }; 10 | 11 | inline 12 | NYStyleClamPizza::NYStyleClamPizza() 13 | { 14 | name = "NY Style Clam Pizza"; 15 | dough = "Thin Crust Dough"; 16 | sauce = "Marinara Sauce"; 17 | toppings.push_back("Grated Reggiano Cheese"); 18 | toppings.push_back("Fresh Clams from Long Island Sound"); 19 | } 20 | 21 | #endif /* NY_STYLE_CLAM_PIZZA_H */ 22 | -------------------------------------------------------------------------------- /Factory/pizzafm/NYStylePepperoniPizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NY_STYLE_PEPPERONI_PIZZA_H 2 | #define NY_STYLE_PEPPERONI_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | 6 | class NYStylePepperoniPizza : public Pizza 7 | { 8 | public: 9 | NYStylePepperoniPizza(); 10 | }; 11 | 12 | inline 13 | NYStylePepperoniPizza::NYStylePepperoniPizza() 14 | { 15 | name = "NY Style Pepperoni Pizza"; 16 | dough = "Thin Crust Dough"; 17 | sauce = "Marinara Sauce"; 18 | 19 | toppings.push_back("Grated Reggiano Cheese"); 20 | toppings.push_back("Sliced Pepperoni"); 21 | toppings.push_back("Garlic"); 22 | toppings.push_back("Onion"); 23 | toppings.push_back("Mushrooms"); 24 | toppings.push_back("Red Pepper"); 25 | } 26 | 27 | #endif /* NY_STYLE_PEPPERONI_PIZZA_H */ 28 | -------------------------------------------------------------------------------- /Factory/pizzafm/NYStyleVeggiePizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef NY_STYLE_VEGGIE_PIZZA_H 2 | #define NY_STYLE_VEGGIE_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | 6 | class NYStyleVeggiePizza : public Pizza { 7 | private: 8 | 9 | 10 | public: 11 | NYStyleVeggiePizza(); 12 | }; 13 | 14 | inline 15 | NYStyleVeggiePizza::NYStyleVeggiePizza() 16 | { 17 | name = "NY Style Veggie Pizza"; 18 | dough = "Thin Crust Dough"; 19 | sauce = "Marinara Sauce"; 20 | toppings.push_back("Grated Reggiano Cheese"); 21 | toppings.push_back("Garlic"); 22 | toppings.push_back("Onion"); 23 | toppings.push_back("Mushrooms"); 24 | toppings.push_back("Red Pepper"); 25 | } 26 | 27 | #endif /* NY_STYLE_VEGGIE_PIZZA_H */ 28 | -------------------------------------------------------------------------------- /Factory/pizzafm/Pizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_H 2 | #define PIZZA_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Pizza { 9 | public: 10 | virtual ~Pizza() = default; 11 | virtual void prepare(); 12 | virtual void bake() const { std::cout << "Bake for 25 minutes at 350\n"; } 13 | virtual void cut() const { std::cout << "Cutting the pizza into diagonal slices\n"; } 14 | virtual void box() const { std::cout << "place pizza in offical PizzaStore box\n"; } 15 | void setName(std::string_view n) { name = n; } 16 | std::string getName() const {return name;} 17 | //std::string toString() { /* code to print pizza here */ } 18 | protected: 19 | std::string name = {}; 20 | std::string dough = {}; 21 | std::string sauce = {}; 22 | std::vector toppings = {}; 23 | }; 24 | 25 | inline 26 | void 27 | Pizza::prepare() 28 | { 29 | std::cout << "Preparing " << name << '\n'; 30 | std::cout << "Tossing dough...\n"; 31 | std::cout << "Adding sauce...\n"; 32 | std::cout << "Adding toppings: \n"; 33 | for (const auto &topping : toppings) 34 | std::cout << " " + topping << '\n'; 35 | } 36 | 37 | #endif /* ifndef PIZZA_H */ 38 | -------------------------------------------------------------------------------- /Factory/pizzafm/PizzaStore.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_STORE_H 2 | #define PIZZA_STORE_H 3 | 4 | #include "Pizza.hpp" 5 | #include 6 | #include 7 | 8 | class PizzaStore { 9 | public: 10 | virtual ~PizzaStore() = default; 11 | std::unique_ptr pizzaOrder(std::string_view type); 12 | protected: 13 | virtual std::unique_ptr createPizza(std::string_view item) = 0; // factory method 14 | }; 15 | 16 | inline 17 | std::unique_ptr 18 | PizzaStore::pizzaOrder(std::string_view type) 19 | { 20 | auto pizza = createPizza(type); 21 | pizza->prepare(); 22 | pizza->bake(); 23 | pizza->cut(); 24 | pizza->box(); 25 | return pizza; 26 | } 27 | 28 | #endif /* ifndef PIZZA_STORE_H */ 29 | -------------------------------------------------------------------------------- /Factory/pizzafm/pizzaTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "PizzaStore.hpp" 2 | #include "NYPizzaStore.hpp" 3 | #include "ChicagoPizzaStore.hpp" 4 | #include 5 | #include 6 | 7 | int main() { 8 | std::unique_ptr nyStore = std::make_unique(); 9 | std::unique_ptr chicagoStore = std::make_unique(); 10 | 11 | std::unique_ptr pizza = nyStore->pizzaOrder("cheese"); 12 | std::cout << "Ethan orderd a " << pizza->getName() << "\n\n"; 13 | 14 | pizza = chicagoStore->pizzaOrder("cheese"); 15 | std::cout << "Joel orderd a " << pizza->getName() << "\n\n"; 16 | 17 | pizza = nyStore->pizzaOrder("clam"); 18 | std::cout << "Ethan orderd a " << pizza->getName() << "\n\n"; 19 | 20 | pizza = chicagoStore->pizzaOrder("clam"); 21 | std::cout << "Joel orderd a " << pizza->getName() << "\n\n"; 22 | 23 | pizza = chicagoStore->pizzaOrder("pepperoni"); 24 | std::cout << "Ethan orderd a " << pizza->getName() << "\n\n"; 25 | 26 | pizza = chicagoStore->pizzaOrder("pepperoni"); 27 | std::cout << "Joel orderd a " << pizza->getName() << "\n\n"; 28 | 29 | pizza = chicagoStore->pizzaOrder("veggie"); 30 | std::cout << "Ethan orderd a " << pizza->getName() << "\n\n"; 31 | 32 | pizza = chicagoStore->pizzaOrder("veggie"); 33 | std::cout << "Joel orderd a " << pizza->getName() << "\n\n"; 34 | return 0; 35 | } 36 | -------------------------------------------------------------------------------- /Factory/pizzas/CheesePizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CHEESE_PIZZA_H 2 | #define CHEESE_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | 6 | class CheesePizza : public Pizza { 7 | public: 8 | CheesePizza(); 9 | }; 10 | 11 | inline 12 | CheesePizza::CheesePizza() 13 | { 14 | name = "Cheese Pizza"; 15 | dough = "Thin Crust Dough"; 16 | sauce = "Marinara sauce"; 17 | toppings.push_back("Grated Regiano Cheese"); 18 | } 19 | 20 | #endif /* CHEESE_PIZZA_H */ 21 | -------------------------------------------------------------------------------- /Factory/pizzas/ClamPizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CLAM_PIZZA_H 2 | #define CLAM_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | 6 | class ClamPizza : public Pizza { 7 | public: 8 | ClamPizza(); 9 | }; 10 | 11 | inline 12 | ClamPizza::ClamPizza() 13 | { 14 | name = "Clam Pizza"; 15 | dough = "Thin Crust Dough"; 16 | sauce = "Marinara sauce"; 17 | toppings.push_back("Grated Regiano Clam"); 18 | toppings.push_back("Fresh Clams"); 19 | } 20 | #endif /* CLAM_PIZZA_H */ 21 | -------------------------------------------------------------------------------- /Factory/pizzas/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = CheesePizza.hpp ClamPizza.hpp PepperoniPizza.hpp Pizza.hpp PizzaStore.hpp \ 4 | SimplePizzaFactory.hpp VeggiePizza.hpp 5 | TARGET = pizzaTestDrive 6 | OBJ = $(TARGET).o 7 | 8 | %.o: %.cpp $(DEPS) 9 | $(CXX) -c -o $@ $< $(CFLAGS) 10 | 11 | $(TARGET): $(OBJ) 12 | $(CXX) -o $@ $^ $(CFLAGS) 13 | 14 | clean: 15 | rm -f *.o $(TARGET) 16 | -------------------------------------------------------------------------------- /Factory/pizzas/PepperoniPizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PEPPERONI_PIZZA_H 2 | #define PEPPERONI_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | #include "Pizza.hpp" 6 | 7 | class PepperoniPizza : Pizza { 8 | public: 9 | PepperoniPizza() = default; 10 | PepperoniPizza(std::unique_ptr infac) : ingredientFactory(std::move(infac)) {} 11 | void prepare() override; 12 | private: 13 | std::unique_ptr ingredientFactory{nullptr}; 14 | }; 15 | 16 | inline 17 | void 18 | PepperoniPizza::prepare() 19 | { 20 | std::cout << "Preparing " << name << '\n'; 21 | dough = ingredientFactory->createDough(); 22 | sauce = ingredientFactory->createSauce(); 23 | cheese = ingredientFactory->createCheese(); 24 | veggies = ingredientFactory->createPepperonis(); 25 | pepperoni = ingredientFactory->createPepperoni(); 26 | } 27 | 28 | 29 | #endif /* PEPPERONI_PIZZA_H */ 30 | -------------------------------------------------------------------------------- /Factory/pizzas/Pizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_H 2 | #define PIZZA_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class Pizza { 9 | public: 10 | Pizza () = default; 11 | virtual ~Pizza () = default; // necessay if class includes a virtual function 12 | virtual void prepare() const; 13 | virtual void bake() const { std::cout << "Bake for 25 minutes at 350\n"; } 14 | virtual void cut() const { std::cout << "Cutting the pizza into diagonal slices\n"; } 15 | virtual void box() const { std::cout << "Place pizza in offical PizzaStore box\n"; } 16 | std::string getName() const { return name; } 17 | protected: 18 | // using in-class initialization 19 | std::string name = {}; 20 | std::string dough = {}; 21 | std::string sauce = {}; 22 | std::vector toppings = {}; 23 | }; 24 | 25 | inline 26 | void 27 | Pizza::prepare() const 28 | { 29 | std::cout << "Preparing " + name; 30 | std::cout << "Tossing dough...\n"; 31 | std::cout << "Adding sauce...\n"; 32 | std::cout << "Adding toppings: \n"; 33 | for (const auto &topping : toppings) 34 | std::cout << " " + topping << '\n'; 35 | } 36 | 37 | #endif /* PIZZA_H */ 38 | -------------------------------------------------------------------------------- /Factory/pizzas/PizzaStore.hpp: -------------------------------------------------------------------------------- 1 | #ifndef PIZZA_STORE_H 2 | #define PIZZA_STORE_H 3 | 4 | #include "SimplePizzaFactory.hpp" 5 | #include "Pizza.hpp" 6 | #include 7 | #include 8 | #include 9 | 10 | class PizzaStore 11 | { 12 | public: 13 | PizzaStore() = default; 14 | PizzaStore(std::unique_ptr f) : factory(std::move(f)) {} 15 | std::unique_ptr orderPizza(std::string_view type); 16 | private: 17 | std::unique_ptr factory = nullptr; 18 | }; 19 | 20 | inline 21 | std::unique_ptr 22 | PizzaStore::orderPizza(std::string_view type) 23 | { 24 | auto pizza = factory->createPizza(type); 25 | pizza->prepare(); 26 | pizza->bake(); 27 | pizza->cut(); 28 | pizza->box(); 29 | return pizza; // local object is about to be destoryed, thus std:move is not needed 30 | } 31 | #endif /* PIZZA_STORE_H */ 32 | -------------------------------------------------------------------------------- /Factory/pizzas/SimplePizzaFactory.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLE_PIZZA_FACTORY_H 2 | #define SIMPLE_PIZZA_FACTORY_H 3 | 4 | #include "CheesePizza.hpp" 5 | #include "ClamPizza.hpp" 6 | #include "PepperoniPizza.hpp" 7 | #include "Pizza.hpp" 8 | #include "VeggiePizza.hpp" 9 | #include 10 | #include 11 | 12 | class SimplePizzaFactory { 13 | public: 14 | std::unique_ptr createPizza(std::string_view); 15 | }; 16 | 17 | inline 18 | std::unique_ptr 19 | SimplePizzaFactory::createPizza(std::string_view type) 20 | { 21 | if (type == "cheese") 22 | return std::make_unique(); 23 | if (type == "pepperoni") 24 | return std::make_unique(); 25 | if (type == "clam") 26 | return std::make_unique(); 27 | if (type == "veggie") 28 | return std::make_unique(); 29 | return nullptr; // no match 30 | } 31 | 32 | #endif /* SIMPLE_PIZZA_FACTORY_H */ 33 | -------------------------------------------------------------------------------- /Factory/pizzas/VeggiePizza.hpp: -------------------------------------------------------------------------------- 1 | #ifndef VEGGIE_PIZZA_H 2 | #define VEGGIE_PIZZA_H 3 | 4 | #include "Pizza.hpp" 5 | 6 | class VeggiePizza : public Pizza { 7 | public: 8 | VeggiePizza(); 9 | }; 10 | 11 | inline 12 | VeggiePizza::VeggiePizza() 13 | { 14 | name = "Veggie Pizza"; 15 | dough = "Thin Crust Dough"; 16 | sauce = "Marinara sauce"; 17 | toppings.push_back("Grated Regiano Veggie"); 18 | toppings.push_back("Fresh Veggies"); 19 | } 20 | #endif /* VEGGIE_PIZZA_H */ 21 | -------------------------------------------------------------------------------- /Factory/pizzas/pizzaTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "Pizza.hpp" 2 | #include "PizzaStore.hpp" 3 | #include "SimplePizzaFactory.hpp" 4 | #include 5 | 6 | int main() 7 | { 8 | auto factory = std::make_unique(); // create factory 9 | auto pizzaStore = PizzaStore(std::move(factory)); // create store and pass it factory 10 | auto pizza = pizzaStore.orderPizza("clam"); // order pizza from store 11 | // one-liner: 12 | // auto pizza = PizzaStore(std::make_unique()).orderPizza("clam"); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Iterator/dinermerger/DinerMenuIterator.h: -------------------------------------------------------------------------------- 1 | #ifndef DINER_MENU_ITERATOR_H 2 | #define DINER_MENU_ITERATOR_H 3 | 4 | #include "Iterator.h" 5 | #include "MenuItem.h" 6 | #include 7 | 8 | class DinerMenuIterator : public Iterator { 9 | public: 10 | static constexpr size_t MAX_ITEMS = 6; 11 | DinerMenuIterator(std::array &i) : items(i) {} 12 | MenuItem* next() override { return &items[position++]; }; 13 | bool hasNext() const override { return items.size() > position; } 14 | private: 15 | std::array &items; 16 | std::array::size_type position = 0; 17 | }; 18 | 19 | #endif /* DINER_MENU_ITERATOR_H */ 20 | -------------------------------------------------------------------------------- /Iterator/dinermerger/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ITERATOR_H 2 | #define ITERATOR_H 3 | 4 | template 5 | class Iterator { 6 | public: 7 | virtual ~Iterator() = default; 8 | virtual T* next() = 0; 9 | virtual bool hasNext() const = 0; 10 | }; 11 | 12 | #endif /* ITERATOR_H */ 13 | -------------------------------------------------------------------------------- /Iterator/dinermerger/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = DinerMenu.h DinerMenuIterator.h Iterator.h MenuItem.h \ 4 | PancakeHouseMenu.h PancakeHouseMenuIterator.h Waitress.h 5 | TARGET = menuTestDrive 6 | 7 | OBJ = $(TARGET).o 8 | 9 | %.o: %.cpp $(DEPS) 10 | $(CXX) -c -o $@ $< $(CFLAGS) 11 | 12 | $(TARGET): $(OBJ) 13 | $(CXX) -o $@ $^ $(CFLAGS) 14 | 15 | clean: 16 | rm -f *.o $(TARGET) 17 | -------------------------------------------------------------------------------- /Iterator/dinermerger/MenuItem.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_ITEM_H 2 | #define MENU_ITEM_H 3 | 4 | #include 5 | #include 6 | 7 | class MenuItem { 8 | public: 9 | MenuItem() = default; 10 | MenuItem(std::string_view n, std::string_view d, bool v, double p) : 11 | name(n), desciption(d), vegitarian(v), price(p) {} 12 | std::string getName() const { return name; } 13 | std::string getDescription() const { return desciption; } 14 | bool isVegitarian() const { return vegitarian; } 15 | double getPrice() const { return price; } 16 | private: 17 | std::string name = {}; 18 | std::string desciption = {}; 19 | bool vegitarian = false; 20 | double price = 0.0; 21 | }; 22 | 23 | inline 24 | std::ostream& 25 | operator<<(std::ostream &os, const MenuItem &item) 26 | { 27 | os << item.getName() << ", $" << item.getPrice() << "\n " 28 | < 7 | #include 8 | 9 | class PancakeHouseMenuIterator : public Iterator { 10 | public: 11 | PancakeHouseMenuIterator(std::list &i) : 12 | items(i), iter(items.begin()) { } 13 | MenuItem* next() override { return &*iter++; } 14 | bool hasNext() const override { return iter != items.cend(); } 15 | private: 16 | std::list &items; 17 | std::list::iterator iter; 18 | }; 19 | 20 | #endif /* PANCAKE_HOUSE_MENU_ITERATOR_H */ 21 | -------------------------------------------------------------------------------- /Iterator/dinermerger/Waitress.h: -------------------------------------------------------------------------------- 1 | #ifndef WAITRESS_H 2 | #define WAITRESS_H 3 | 4 | #include "DinerMenu.h" 5 | #include "DinerMenuIterator.h" 6 | #include "Iterator.h" 7 | #include "PancakeHouseMenu.h" 8 | #include "PancakeHouseMenuIterator.h" 9 | #include 10 | #include 11 | 12 | class Waitress { 13 | public: 14 | Waitress(PancakeHouseMenu *phm, DinerMenu *dm) : 15 | pancakeHouseMenu(phm), dinerMenu(dm) {} 16 | void printMenu() const; 17 | void printMenu(Iterator *iteator) const; 18 | private: 19 | PancakeHouseMenu *pancakeHouseMenu; 20 | DinerMenu *dinerMenu; 21 | }; 22 | 23 | inline 24 | void 25 | Waitress::printMenu() const 26 | { 27 | auto pancakeIterator = pancakeHouseMenu->createIterator(); 28 | auto dinerIterator = dinerMenu->createIterator(); 29 | 30 | std::cout << "MENU\n----\nBREAKFAST\n"; 31 | printMenu(pancakeIterator.get()); 32 | std::cout << "\nLUNCH\n"; 33 | printMenu(dinerIterator.get()); 34 | } 35 | 36 | inline 37 | void 38 | Waitress::printMenu(Iterator *iteator) const 39 | { 40 | while (iteator->hasNext()) { 41 | auto menuItem = iteator->next(); 42 | std::cout << menuItem->getName() << ", "; 43 | std::cout << menuItem->getPrice() << " -- "; 44 | std::cout << menuItem->getDescription() << '\n'; 45 | } 46 | } 47 | 48 | #endif /* WAITRESS_H */ 49 | -------------------------------------------------------------------------------- /Iterator/dinermerger/menuTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "DinerMenu.h" 2 | #include "PancakeHouseMenu.h" 3 | #include "Waitress.h" 4 | 5 | int main() 6 | { 7 | auto pancakeHouseMenu = PancakeHouseMenu(); 8 | auto dinerMenu = DinerMenu(); 9 | 10 | auto waitress = Waitress(&pancakeHouseMenu, &dinerMenu); 11 | 12 | waitress.printMenu(); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/CafeMenu.h: -------------------------------------------------------------------------------- 1 | #ifndef CAFE_MENU_H 2 | #define CAFE_MENU_H 3 | 4 | #include "CafeMenuIterator.h" 5 | #include "Menu.h" 6 | #include "MenuItem.h" 7 | #include "Iterator.h" 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | class CafeMenu : public Menu { 14 | public: 15 | CafeMenu(); 16 | void addItem(std::string_view, std::string_view, bool, double); 17 | std::unique_ptr> createIterator() { 18 | return std::make_unique(menuItems); } 19 | private: 20 | std::map menuItems; 21 | }; 22 | 23 | inline 24 | CafeMenu::CafeMenu() 25 | { 26 | addItem("Veggie Burger and Air Fries", 27 | "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", 28 | true, 3.99); 29 | addItem("Soup of the day", 30 | "A cup of the soup of the day, with a side salad", 31 | false, 3.69); 32 | addItem("Burrito", 33 | "A large burrito, with whole pinto beans, salsa, guacamole", 34 | true, 4.29); 35 | } 36 | 37 | inline 38 | void 39 | CafeMenu::addItem(std::string_view name, std::string_view description, 40 | bool vegitarian, double price) 41 | { 42 | auto menuItem = MenuItem(name, description, vegitarian, price); 43 | menuItems[std::string(name)] = menuItem; 44 | } 45 | 46 | #endif /* CAFE_MENU_H */ 47 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/CafeMenuIterator.h: -------------------------------------------------------------------------------- 1 | #ifndef CAFE_MENU_ITERATOR_H 2 | #define CAFE_MENU_ITERATOR_H 3 | 4 | #include "Iterator.h" 5 | #include "MenuItem.h" 6 | #include 7 | #include 8 | 9 | class CafeMenuIterator : public Iterator { 10 | public: 11 | CafeMenuIterator(std::map &i) : 12 | items(i), iter(items.begin()) {} 13 | MenuItem* next() override { return &iter++->second; } 14 | bool hasNext() const override { return iter != items.end(); } 15 | void remove() override; 16 | private: 17 | std::map &items; 18 | std::map::iterator iter; 19 | }; 20 | 21 | inline 22 | void 23 | CafeMenuIterator::remove() 24 | { 25 | if (items.empty()) 26 | throw std::invalid_argument( 27 | "You can't remove an item until you've done at least one next()"); 28 | items.erase(iter); 29 | } 30 | 31 | #endif /* CAFE_MENU_ITERATOR_H */ 32 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/DinerMenuIterator.h: -------------------------------------------------------------------------------- 1 | #ifndef DINER_MENU_ITERATOR_H 2 | #define DINER_MENU_ITERATOR_H 3 | 4 | #include "Iterator.h" 5 | #include "MenuItem.h" 6 | #include 7 | #include 8 | #include 9 | 10 | class DinerMenuIterator : public Iterator { 11 | public: 12 | static constexpr size_t MAX_ITEMS = 6; 13 | DinerMenuIterator(std::array &i) : items(i) {} 14 | MenuItem* next() override { return &items[position++]; }; 15 | bool hasNext() const override { return items.size() > position; } 16 | void remove() override; 17 | private: 18 | std::array &items; 19 | std::array::size_type position = 0; 20 | }; 21 | 22 | inline 23 | void 24 | DinerMenuIterator::remove() 25 | { 26 | if (items.empty()) 27 | throw std::invalid_argument( 28 | "You can't remove an item until you've done at least one next()"); 29 | if (!items.empty()) { 30 | for (auto i = position - 1; i < items.size() - 1; ++i) 31 | items[i] = items[i + 1]; // move items 1 position to the right 32 | items[items.size() - 1] = {}; // clear the last item 33 | } 34 | } 35 | 36 | #endif /* DINER_MENU_ITERATOR_H */ 37 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ITERATOR_H 2 | #define ITERATOR_H 3 | 4 | // Java's Iterator interface for C++ 5 | template 6 | class Iterator { 7 | public: 8 | virtual ~Iterator() = default; 9 | virtual T* next() = 0; 10 | virtual bool hasNext() const = 0; 11 | virtual void remove() = 0; 12 | }; 13 | 14 | #endif /* ITERATOR_H */ 15 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++2a 3 | DEPS = AlternatingDinerMenuIterator.h CafeMenu.h CafeMenuIterator.h \ 4 | DinerMenu.h DinerMenuIterator.h Iterator.h Makefile Menu.h MenuItem.h \ 5 | PancakeHouseMenu.h PancakeHouseMenuIterator.h Waitress.h 6 | TARGET = menuTestDrive 7 | 8 | OBJ = $(TARGET).o 9 | 10 | %.o: %.cpp $(DEPS) 11 | $(CXX) -c -o $@ $< $(CFLAGS) 12 | 13 | $(TARGET): $(OBJ) 14 | $(CXX) -o $@ $^ $(CFLAGS) 15 | 16 | clean: 17 | rm -f *.o $(TARGET) 18 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/Menu.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_H 2 | #define MENU_H 3 | 4 | #include "Iterator.h" 5 | #include "MenuItem.h" 6 | #include 7 | #include 8 | #include 9 | 10 | class Menu { 11 | public: 12 | using menus_map = std::map>; 13 | virtual ~Menu () = default; 14 | virtual std::unique_ptr> createIterator() = 0; 15 | }; 16 | 17 | #endif /* MENU_H */ 18 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/MenuItem.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_ITEM_H 2 | #define MENU_ITEM_H 3 | 4 | #include 5 | #include 6 | 7 | class MenuItem { 8 | public: 9 | MenuItem() = default; 10 | MenuItem(std::string_view n, std::string_view d, bool v, double p) : 11 | name(n), desciption(d), vegitarian(v), price(p) {} 12 | std::string getName() const { return name; } 13 | std::string getDescription() const { return desciption; } 14 | bool isVegitarian() const { return vegitarian; } 15 | double getPrice() const { return price; } 16 | private: 17 | std::string name = {}; 18 | std::string desciption = {}; 19 | bool vegitarian = false; 20 | double price = 0.0; 21 | }; 22 | 23 | inline 24 | std::ostream& 25 | operator<<(std::ostream &os, const MenuItem &item) 26 | { 27 | os << item.getName() << ", $" << item.getPrice() << "\n " 28 | < 7 | #include 8 | #include 9 | #include 10 | 11 | class PancakeHouseMenuIterator : public Iterator { 12 | public: 13 | PancakeHouseMenuIterator(std::list &i) : 14 | items(i), iter(items.begin()) {} 15 | MenuItem* next() override { return &*iter++; } 16 | bool hasNext() const override { return iter != items.cend(); } 17 | void remove() override; 18 | private: 19 | std::list &items; 20 | std::list::iterator iter; 21 | }; 22 | 23 | inline 24 | void 25 | PancakeHouseMenuIterator::remove() 26 | { 27 | if (items.empty()) 28 | throw std::invalid_argument( 29 | "You can't remove an item until you've done at least one next()"); 30 | items.erase(iter); 31 | } 32 | 33 | #endif /* PANCAKE_HOUSE_MENU_ITERATOR_H */ 34 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/Waitress.h: -------------------------------------------------------------------------------- 1 | #ifndef WAITRESS_H 2 | #define WAITRESS_H 3 | 4 | #include "Menu.h" 5 | #include "Iterator.h" 6 | #include 7 | #include 8 | 9 | class Waitress { 10 | public: 11 | Waitress(Menu::menus_map &m) : menus(m) { } 12 | void printMenu(); 13 | void printMenu(Iterator *iteator) const; 14 | private: 15 | Menu::menus_map &menus; 16 | }; 17 | 18 | inline 19 | void 20 | Waitress::printMenu() 21 | { 22 | std::cout << "MENU\n----"; 23 | for (const auto &[name, menu] : menus) { 24 | std::cout << '\n' << name << '\n'; 25 | auto iter = menu->createIterator(); 26 | printMenu(iter.get()); 27 | } 28 | } 29 | 30 | inline 31 | void 32 | Waitress::printMenu(Iterator *iteator) const 33 | { 34 | while (iteator->hasNext()) { 35 | auto menuItem = iteator->next(); 36 | std::cout << menuItem->getName() << ", "; 37 | std::cout << menuItem->getPrice() << " -- "; 38 | std::cout << menuItem->getDescription() << '\n'; 39 | } 40 | } 41 | 42 | #endif /* WAITRESS_H */ 43 | -------------------------------------------------------------------------------- /Iterator/dinermergercafe/menuTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "CafeMenu.h" 2 | #include "DinerMenu.h" 3 | #include "Menu.h" 4 | #include "PancakeHouseMenu.h" 5 | #include "Waitress.h" 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | std::map> menus; 12 | 13 | menus["BREAKFEAST"] = std::make_unique(); 14 | menus["LUNCH"] = std::make_unique(); 15 | menus["DINNER"] = std::make_unique(); 16 | 17 | auto waitress = Waitress(menus); 18 | 19 | waitress.printMenu(); 20 | 21 | return 0; 22 | } 23 | -------------------------------------------------------------------------------- /Iterator/dinermergeri/DinerMenuIterator.h: -------------------------------------------------------------------------------- 1 | #ifndef DINER_MENU_ITERATOR_H 2 | #define DINER_MENU_ITERATOR_H 3 | 4 | #include "Iterator.h" 5 | #include "MenuItem.h" 6 | #include 7 | #include 8 | #include 9 | 10 | class DinerMenuIterator : public Iterator { 11 | public: 12 | static constexpr size_t MAX_ITEMS = 6; 13 | DinerMenuIterator(std::array &i) : items(i) {} 14 | MenuItem* next() override { return &items[position++]; }; 15 | bool hasNext() const override { return items.size() > position; } 16 | void remove() override; 17 | private: 18 | std::array &items; 19 | std::array::size_type position = 0; 20 | }; 21 | 22 | inline 23 | void 24 | DinerMenuIterator::remove() 25 | { 26 | if (items.empty()) 27 | throw std::invalid_argument( 28 | "You can't remove an item until you've done at least one next()"); 29 | if (!items.empty()) { 30 | for (auto i = position - 1; i < items.size() - 1; ++i) 31 | items[i] = items[i + 1]; 32 | items[items.size() - 1] = {}; // clear the last time 33 | } 34 | } 35 | 36 | #endif /* DINER_MENU_ITERATOR_H */ 37 | -------------------------------------------------------------------------------- /Iterator/dinermergeri/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef ITERATOR_H 2 | #define ITERATOR_H 3 | 4 | template 5 | class Iterator { 6 | public: 7 | virtual ~Iterator() = default; 8 | virtual T* next() = 0; 9 | virtual bool hasNext() const = 0; 10 | virtual void remove() = 0; 11 | }; 12 | 13 | #endif /* ITERATOR_H */ 14 | -------------------------------------------------------------------------------- /Iterator/dinermergeri/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = AlternatingDinerMenuIterator.h DinerMenu.h DinerMenuIterator.h \ 4 | Iterator.h Menu.h MenuItem.h PancakeHouseMenu.h \ 5 | PancakeHouseMenuIterator.h Waitress.h 6 | TARGET = menuTestDrive 7 | 8 | OBJ = $(TARGET).o 9 | 10 | %.o: %.cpp $(DEPS) 11 | $(CXX) -c -o $@ $< $(CFLAGS) 12 | 13 | $(TARGET): $(OBJ) 14 | $(CXX) -o $@ $^ $(CFLAGS) 15 | 16 | clean: 17 | rm -f *.o $(TARGET) 18 | -------------------------------------------------------------------------------- /Iterator/dinermergeri/Menu.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_H 2 | #define MENU_H 3 | 4 | #include "Iterator.h" 5 | #include "MenuItem.h" 6 | #include 7 | 8 | class Menu { 9 | public: 10 | virtual ~Menu () = default; 11 | virtual std::unique_ptr> createIterator() = 0; 12 | }; 13 | 14 | #endif /* MENU_H */ 15 | -------------------------------------------------------------------------------- /Iterator/dinermergeri/MenuItem.h: -------------------------------------------------------------------------------- 1 | #ifndef MENU_ITEM_H 2 | #define MENU_ITEM_H 3 | 4 | #include 5 | #include 6 | 7 | class MenuItem { 8 | public: 9 | MenuItem() = default; 10 | MenuItem(std::string_view n, std::string_view d, bool v, double p) : 11 | name(n), desciption(d), vegitarian(v), price(p) {} 12 | std::string getName() const { return name; } 13 | std::string getDescription() const { return desciption; } 14 | bool isVegitarian() const { return vegitarian; } 15 | double getPrice() const { return price; } 16 | private: 17 | std::string name = {}; 18 | std::string desciption = {}; 19 | bool vegitarian = false; 20 | double price = 0.0; 21 | }; 22 | 23 | inline 24 | std::ostream& 25 | operator<<(std::ostream &os, const MenuItem &item) 26 | { 27 | os << item.getName() << ", $" << item.getPrice() << "\n " 28 | < 7 | #include 8 | #include 9 | #include 10 | 11 | class PancakeHouseMenuIterator : public Iterator { 12 | public: 13 | PancakeHouseMenuIterator(std::list &i) : 14 | items(i), position(items.begin()) {} 15 | MenuItem* next() override { return &*position++; } 16 | bool hasNext() const override { return position != items.cend(); } 17 | void remove() override; 18 | private: 19 | std::list &items; 20 | std::list::iterator position; 21 | }; 22 | 23 | inline 24 | void 25 | PancakeHouseMenuIterator::remove() 26 | { 27 | if (items.empty()) 28 | throw std::invalid_argument( 29 | "You can't remove an item until you've done at least one next()"); 30 | items.erase(position); 31 | } 32 | 33 | #endif /* PANCAKE_HOUSE_MENU_ITERATOR_H */ 34 | -------------------------------------------------------------------------------- /Iterator/dinermergeri/Waitress.h: -------------------------------------------------------------------------------- 1 | #ifndef WAITRESS_H 2 | #define WAITRESS_H 3 | 4 | #include "Menu.h" 5 | #include "Iterator.h" 6 | #include 7 | #include 8 | 9 | class Waitress { 10 | public: 11 | Waitress(Menu *phm, Menu *dm) : 12 | pancakeHouseMenu(phm), dinerMenu(dm) {} 13 | void printMenu(); 14 | void printMenu(Iterator *iteator) const; 15 | private: 16 | Menu *pancakeHouseMenu; 17 | Menu *dinerMenu; 18 | }; 19 | 20 | inline 21 | void 22 | Waitress::printMenu() 23 | { 24 | auto pancakeIterator = pancakeHouseMenu->createIterator(); 25 | auto dinerIterator = dinerMenu->createIterator(); 26 | 27 | std::cout << "MENU\n----\nBREAKFAST\n"; 28 | printMenu(pancakeIterator.get()); 29 | std::cout << "\nLUNCH\n"; 30 | printMenu(dinerIterator.get()); 31 | } 32 | 33 | inline 34 | void 35 | Waitress::printMenu(Iterator *iteator) const 36 | { 37 | while (iteator->hasNext()) { 38 | auto menuItem = iteator->next(); 39 | std::cout << menuItem->getName() << ", "; 40 | std::cout << menuItem->getPrice() << " -- "; 41 | std::cout << menuItem->getDescription() << '\n'; 42 | } 43 | } 44 | 45 | #endif /* WAITRESS_H */ 46 | -------------------------------------------------------------------------------- /Iterator/dinermergeri/menuTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "DinerMenu.h" 2 | #include "PancakeHouseMenu.h" 3 | #include "Waitress.h" 4 | 5 | int main() 6 | { 7 | auto pancakeHouseMenu = PancakeHouseMenu(); 8 | auto dinerMenu = DinerMenu(); 9 | 10 | auto waitress = Waitress(&pancakeHouseMenu, &dinerMenu); 11 | 12 | waitress.printMenu(); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /Observer/weather/CurrentConditionsDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef CURRENT_CONDITIONS_DISPLAY 2 | #define CURRENT_CONDITIONS_DISPLAY 3 | 4 | #include "Observer.h" 5 | #include "DisplayElement.h" 6 | #include "Subject.h" 7 | #include 8 | #include 9 | 10 | class CurrentConditionsDisplay : public Observer, public DisplayElement { 11 | public: 12 | CurrentConditionsDisplay() = default; 13 | CurrentConditionsDisplay(Subject *wd) : weatherData(wd) 14 | { weatherData->registerObserver(this); } 15 | void update(double t, double h, double p) override; 16 | void display() const override; 17 | private: 18 | double temperature = 0.0f; 19 | double humidity = 0.0f; 20 | Subject *weatherData = nullptr; 21 | 22 | }; 23 | 24 | inline 25 | void 26 | CurrentConditionsDisplay::update(double t, double h, [[maybe_unused]] double p) { 27 | temperature = t; 28 | humidity = h; 29 | display(); 30 | } 31 | 32 | inline 33 | void 34 | CurrentConditionsDisplay::display() const { 35 | std::cout << "Current Conditions: " << temperature 36 | << "F degree and " << humidity << "% humidity\n"; 37 | 38 | } 39 | 40 | #endif /* ifndef CURRENT_CONDITIONS_DISPLAY */ 41 | -------------------------------------------------------------------------------- /Observer/weather/DisplayElement.h: -------------------------------------------------------------------------------- 1 | #ifndef DISPLAY_ELEMENT 2 | #define DISPLAY_ELEMENT 3 | 4 | class DisplayElement { 5 | public: 6 | virtual ~DisplayElement() = default; 7 | virtual void display() const = 0; 8 | }; 9 | 10 | #endif /* ifndef DISPLAY_ELEMENT */ 11 | -------------------------------------------------------------------------------- /Observer/weather/ForcastDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef FORCAST_DISPLAY_H 2 | #define FORCAST_DISPLAY_H 3 | 4 | #include "DisplayElement.h" 5 | #include "Observer.h" 6 | #include "Subject.h" 7 | #include 8 | 9 | class ForcastDisplay : public Observer, public DisplayElement { 10 | public: 11 | ForcastDisplay() = default; 12 | ForcastDisplay(Subject *wd) : weatherData(wd) { weatherData->registerObserver(this); } 13 | void update(double t, double h, double p) override; 14 | void display() const override; 15 | private: 16 | double currentPressure = 29.92f; 17 | double prevPressure = 0.0f; 18 | Subject *weatherData = nullptr; 19 | }; 20 | 21 | inline 22 | void 23 | ForcastDisplay::update([[maybe_unused]] double t, [[maybe_unused]] double h, double p) { 24 | prevPressure = currentPressure; 25 | currentPressure = p; 26 | display(); 27 | } 28 | 29 | inline 30 | void 31 | ForcastDisplay::display() const { 32 | std::cout << "Forcast: "; 33 | if (currentPressure > prevPressure) 34 | std::cout << "Improving weather on the way!\n"; 35 | else if (currentPressure == prevPressure) 36 | std::cout << "Forcast: More of the same\n"; 37 | else if (currentPressure < prevPressure) 38 | std::cout << "Forcast: Watch out for cooler, rainy weather\n"; 39 | } 40 | 41 | #endif /* ifndef FORCAST_DISPLAY_H */ 42 | -------------------------------------------------------------------------------- /Observer/weather/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++20 3 | DEPS = CurrentConditionsDisplay.h DisplayElement.h ForcastDisplay.h \ 4 | HeatIndex.h Observer.h StatisticsDisplay.h Subject.h WeatherData.h 5 | TARGET = weatherStation 6 | OBJ = $(TARGET).o 7 | 8 | %.o: %.cpp $(DEPS) 9 | $(CXX) -c -o $@ $< $(CFLAGS) 10 | 11 | $(TARGET): $(OBJ) 12 | $(CXX) -o $@ $^ $(CFLAGS) 13 | 14 | clean: 15 | rm -f *.o $(TARGET) 16 | -------------------------------------------------------------------------------- /Observer/weather/Observer.h: -------------------------------------------------------------------------------- 1 | #ifndef OBSERVER_H 2 | #define OBSERVER_H 3 | 4 | class Observer { 5 | public: 6 | virtual ~Observer() = default; 7 | virtual void update(double t, double h, double p) = 0; 8 | }; 9 | 10 | #endif /* ifndef OBSERVER_H */ 11 | -------------------------------------------------------------------------------- /Observer/weather/StatisticsDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef STATISTICS_DISPLAY_H 2 | #define STATISTICS_DISPLAY_H 3 | 4 | #include "Subject.h" 5 | #include "Observer.h" 6 | #include "DisplayElement.h" 7 | #include "WeatherData.h" 8 | #include 9 | 10 | class StatisticsDisplay : public Observer, public DisplayElement { 11 | public: 12 | StatisticsDisplay() = default; 13 | StatisticsDisplay(Subject *wd) : weatherData(wd) 14 | { weatherData->registerObserver(this); } 15 | void update(double t, double h, double p) override; 16 | void display() const override; 17 | private: 18 | double minTemp = 200.0f; 19 | double maxTemp = 0.0f; 20 | double tempSum = 0.0f; 21 | int numberOfReadings = 0; 22 | Subject *weatherData = nullptr; 23 | }; 24 | 25 | inline 26 | void 27 | StatisticsDisplay::update(double t, [[maybe_unused]] double h, [[maybe_unused]] double p) { 28 | tempSum += t; 29 | ++numberOfReadings; 30 | if (t < minTemp) 31 | minTemp = t; 32 | if (t > maxTemp) 33 | maxTemp = t; 34 | display(); 35 | } 36 | 37 | inline 38 | void 39 | StatisticsDisplay::display() const { 40 | std::cout << "Avg/Max/Min Temperature = " << 41 | tempSum / numberOfReadings << "/" << maxTemp << "/" << minTemp << '\n'; 42 | } 43 | 44 | #endif /* ifndef STATISTICS_DISPLAY_H */ 45 | -------------------------------------------------------------------------------- /Observer/weather/Subject.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBJECT_H 2 | #define SUBJECT_H 3 | 4 | #include "Observer.h" 5 | 6 | class Subject { 7 | public: 8 | virtual ~Subject() = default; 9 | virtual void registerObserver(Observer *o) = 0; 10 | virtual void removeObserver(Observer *o) = 0; 11 | virtual void notifyObservers() = 0; 12 | }; 13 | 14 | #endif /* ifndef SUBJECT_H */ 15 | -------------------------------------------------------------------------------- /Observer/weather/WeatherData.h: -------------------------------------------------------------------------------- 1 | #ifndef WEATHER_DATA_H 2 | #define WEATHER_DATA_H 3 | 4 | #include "Subject.h" 5 | #include "Observer.h" 6 | #include 7 | 8 | class WeatherData : public Subject { 9 | public: 10 | WeatherData() = default; 11 | void registerObserver(Observer *o) override { observers.push_back(o); } 12 | void removeObserver(Observer *o) override; 13 | void notifyObservers() override; 14 | void measurementChanged() { notifyObservers(); } 15 | void setMeasurements(const double &t, const double &h, const double &p); 16 | private: 17 | double temperature = 0.0f; 18 | double humidity = 0.0f; 19 | double pressure = 0.0f; 20 | std::list observers = {}; 21 | }; 22 | 23 | inline 24 | void 25 | WeatherData::removeObserver(Observer *o) { 26 | if (!observers.empty()) 27 | std::erase(observers, o); // std::erase is C++20 addition 28 | } 29 | 30 | inline 31 | void 32 | WeatherData::notifyObservers() { 33 | for (auto o : observers) 34 | o->update(temperature, humidity, pressure); 35 | } 36 | 37 | inline 38 | void 39 | WeatherData::setMeasurements(const double &t, const double &h, const double &p) { 40 | temperature = t; 41 | humidity = h; 42 | pressure = p; 43 | measurementChanged(); 44 | } 45 | 46 | #endif /* ifndef WEATHER_DATA_H */ 47 | -------------------------------------------------------------------------------- /Observer/weather/weatherStation.cpp: -------------------------------------------------------------------------------- 1 | #include "WeatherData.h" 2 | #include "CurrentConditionsDisplay.h" 3 | #include "StatisticsDisplay.h" 4 | #include "ForcastDisplay.h" 5 | #include "HeatIndex.h" 6 | #include 7 | #include 8 | 9 | int main() 10 | { 11 | auto weatherData = WeatherData(); 12 | auto currentDisplay = CurrentConditionsDisplay(&weatherData); 13 | auto statsDisplay = StatisticsDisplay(&weatherData); 14 | auto forcastDisplay = ForcastDisplay(&weatherData); 15 | auto heatIndex = HeatIndex(&weatherData); 16 | weatherData.setMeasurements(80, 65, 30.4f); 17 | weatherData.setMeasurements(82, 70, 29.2f); 18 | weatherData.setMeasurements(78, 90, 29.2f); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /Observer/weatherobservable/CurrentConditionsDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef CURRENT_CONDITIONS_DISPLAY 2 | #define CURRENT_CONDITIONS_DISPLAY 3 | 4 | #include "DataObject.h" 5 | #include "DisplayElement.h" 6 | #include "Observer.h" 7 | #include "Subject.h" 8 | #include "WeatherData.h" 9 | #include 10 | #include 11 | 12 | class CurrentConditionsDisplay : public Observer, public DisplayElement { 13 | public: 14 | CurrentConditionsDisplay() = default; 15 | CurrentConditionsDisplay(Subject *sbj) : observable(sbj) 16 | { observable->addObserver(this); } 17 | void update(Subject *sbj, DataObject *arg) override; 18 | void display() const override; 19 | private: 20 | double temperature = 0.0f; 21 | double humidity = 0.0f; 22 | Subject *observable; 23 | 24 | }; 25 | 26 | inline 27 | void 28 | CurrentConditionsDisplay::update(Subject *sbj, [[maybe_unused]] DataObject *arg) { 29 | if (auto *wd = dynamic_cast(sbj)) { // sbj is not nullptr 30 | temperature = wd->getTemperature(); 31 | humidity = wd->getHumidity(); 32 | display(); 33 | } 34 | } 35 | 36 | inline 37 | void 38 | CurrentConditionsDisplay::display() const { 39 | std::cout << "Current Conditions: " << temperature 40 | << "F degree and " << humidity << "% humidity\n"; 41 | 42 | } 43 | 44 | #endif /* ifndef CURRENT_CONDITIONS_DISPLAY */ 45 | -------------------------------------------------------------------------------- /Observer/weatherobservable/DataObject.h: -------------------------------------------------------------------------------- 1 | #ifndef DATA_OBJECT_H 2 | #define DATA_OBJECT_H 3 | 4 | struct DataObject { 5 | double temperature = 0.0; 6 | double humidity = 0.0; 7 | double pressure = 0.0; 8 | }; 9 | 10 | #endif /* DATA_OBJECT_H */ 11 | -------------------------------------------------------------------------------- /Observer/weatherobservable/DisplayElement.h: -------------------------------------------------------------------------------- 1 | #ifndef DISPLAY_ELEMENT 2 | #define DISPLAY_ELEMENT 3 | 4 | class DisplayElement { 5 | public: 6 | virtual ~DisplayElement() = default; 7 | virtual void display() const = 0; 8 | }; 9 | 10 | #endif /* ifndef DISPLAY_ELEMENT */ 11 | -------------------------------------------------------------------------------- /Observer/weatherobservable/ForcastDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef FORCAST_DISPLAY_H 2 | #define FORCAST_DISPLAY_H 3 | 4 | #include "DataObject.h" 5 | #include "DisplayElement.h" 6 | #include "Observer.h" 7 | #include "Subject.h" 8 | #include "WeatherData.h" 9 | #include 10 | 11 | class ForcastDisplay : public Observer, DisplayElement { 12 | public: 13 | ForcastDisplay() = default; 14 | ForcastDisplay(Subject *sbj) : observable(sbj) { observable->addObserver(this); } 15 | void update(Subject *sbj, DataObject *arg = nullptr) override; 16 | void display() const override; 17 | private: 18 | double currentPressure = 29.92f; 19 | double prevPressure = 0.0f; 20 | Subject *observable = nullptr; 21 | }; 22 | 23 | inline 24 | void 25 | ForcastDisplay::update(Subject *sbj, [[maybe_unused]] DataObject *arg) 26 | { 27 | if (auto *wd = dynamic_cast(sbj)) { // sbj is not nullptr 28 | prevPressure = currentPressure; 29 | currentPressure = wd->getPressure(); 30 | display(); 31 | } 32 | } 33 | 34 | inline 35 | void 36 | ForcastDisplay::display() const 37 | { 38 | std::cout << "Forcast: "; 39 | if (currentPressure > prevPressure) 40 | std::cout << "Improving weather on the way!\n"; 41 | else if (currentPressure == prevPressure) 42 | std::cout << "Forcast: More of the same\n"; 43 | else if (currentPressure < prevPressure) 44 | std::cout << "Forcast: Watch out for cooler, rainy weather\n"; 45 | } 46 | 47 | #endif /* ifndef FORCAST_DISPLAY_H */ 48 | -------------------------------------------------------------------------------- /Observer/weatherobservable/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++20 3 | DEPS = CurrentConditionsDisplay.h DataObject.h DisplayElement.h \ 4 | ForcastDisplay.h HeatIndex.h Observer.h StatisticsDisplay.h \ 5 | Subject.h WeatherData.h 6 | 7 | TARGET = weatherStation 8 | OBJ = $(TARGET).o 9 | 10 | %.o: %.cpp $(DEPS) 11 | $(CXX) -c -o $@ $< $(CFLAGS) 12 | 13 | $(TARGET): $(OBJ) 14 | $(CXX) -o $@ $^ $(CFLAGS) 15 | 16 | clean: 17 | rm -f *.o $(TARGET) 18 | -------------------------------------------------------------------------------- /Observer/weatherobservable/Observer.h: -------------------------------------------------------------------------------- 1 | #ifndef OBSERVER_H 2 | #define OBSERVER_H 3 | 4 | #include "DataObject.h" 5 | 6 | class Subject; // forward declaration 7 | class Observer { 8 | public: 9 | virtual ~Observer() = default; 10 | virtual void update(Subject *sbj, DataObject *arg) = 0; 11 | }; 12 | 13 | #endif /* ifndef OBSERVER_H */ 14 | -------------------------------------------------------------------------------- /Observer/weatherobservable/StatisticsDisplay.h: -------------------------------------------------------------------------------- 1 | #ifndef STATISTICS_DISPLAY_H 2 | #define STATISTICS_DISPLAY_H 3 | 4 | #include "DataObject.h" 5 | #include "DisplayElement.h" 6 | #include "Subject.h" 7 | #include "Observer.h" 8 | #include "WeatherData.h" 9 | #include 10 | 11 | class StatisticsDisplay : public Observer, public DisplayElement { 12 | public: 13 | StatisticsDisplay() = default; 14 | StatisticsDisplay(Subject *wd) : weatherData(wd) 15 | { weatherData->addObserver(this); } 16 | void update(Subject *sbj, DataObject *arg) override; 17 | void display() const override; 18 | private: 19 | double minTemp = 200.0f; 20 | double maxTemp = 0.0f; 21 | double tempSum = 0.0f; 22 | int numberOfReadings = 0; 23 | Subject *weatherData = nullptr; 24 | }; 25 | 26 | inline 27 | void 28 | StatisticsDisplay::update(Subject *sbj, [[maybe_unused]] DataObject *arg) 29 | { 30 | if (auto *wd = dynamic_cast(sbj)) { // sbj is not nullptr 31 | auto temp = wd->getTemperature(); 32 | tempSum += temp; 33 | ++numberOfReadings; 34 | if (temp < minTemp) 35 | minTemp = temp; 36 | if (temp > maxTemp) 37 | maxTemp = temp; 38 | display(); 39 | } 40 | } 41 | 42 | inline 43 | void 44 | StatisticsDisplay::display() const 45 | { 46 | std::cout << "Avg/Max/Min Temperature = " << 47 | tempSum / numberOfReadings << "/" << maxTemp << "/" << minTemp << '\n'; 48 | } 49 | 50 | #endif /* ifndef STATISTICS_DISPLAY_H */ 51 | -------------------------------------------------------------------------------- /Observer/weatherobservable/Subject.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBJECT_H 2 | #define SUBJECT_H 3 | 4 | #include "DataObject.h" 5 | #include "Observer.h" 6 | #include 7 | 8 | class Subject { 9 | public: 10 | Subject() = default; 11 | virtual ~Subject() = default; 12 | void addObserver(Observer *o) { observers.push_back(o); } 13 | void deleteObserver(Observer *o); 14 | void notifyObservers(DataObject *arg = nullptr); // using default arg instead of overloading 15 | protected: 16 | void setChanged() { changed = true; } 17 | void clearChanged() { changed = false; } 18 | bool hasChanged() { return changed; } 19 | private: 20 | std::list observers; 21 | bool changed = false; 22 | }; 23 | 24 | inline 25 | void 26 | Subject::deleteObserver(Observer *o) 27 | { 28 | if (!observers.empty()) 29 | std::erase(observers, o); // std::erase is C++20 addition 30 | } 31 | 32 | inline 33 | void 34 | Subject::notifyObservers(DataObject *arg) 35 | { 36 | if (changed) { 37 | for (const auto &observer : observers) 38 | observer->update(this, arg); 39 | changed = false; 40 | } 41 | } 42 | 43 | #endif /* ifndef SUBJECT_H */ 44 | -------------------------------------------------------------------------------- /Observer/weatherobservable/WeatherData.h: -------------------------------------------------------------------------------- 1 | #ifndef WEATHER_DATA_H 2 | #define WEATHER_DATA_H 3 | 4 | #include "Subject.h" 5 | #include "Observer.h" 6 | #include 7 | 8 | class WeatherData : public Subject { 9 | public: 10 | WeatherData() = default; 11 | void measurementsChanged(); 12 | void setMeasurements(const double &t, const double &h, const double &p); 13 | double getTemperature() const { return temperature; } 14 | double getHumidity() const { return humidity; } 15 | double getPressure() const { return pressure; } 16 | private: 17 | double temperature = 0.0f; 18 | double humidity = 0.0f; 19 | double pressure = 0.0f; 20 | }; 21 | 22 | inline 23 | void 24 | WeatherData::measurementsChanged() { 25 | setChanged(); 26 | notifyObservers(); // pull model 27 | } 28 | 29 | inline 30 | void 31 | WeatherData::setMeasurements(const double &t, const double &h, const double &p) { 32 | temperature = t; 33 | humidity = h; 34 | pressure = p; 35 | measurementsChanged(); 36 | } 37 | 38 | #endif /* ifndef WEATHER_DATA_H */ 39 | -------------------------------------------------------------------------------- /Observer/weatherobservable/weatherStation.cpp: -------------------------------------------------------------------------------- 1 | #include "WeatherData.h" 2 | #include "CurrentConditionsDisplay.h" 3 | #include "StatisticsDisplay.h" 4 | #include "ForcastDisplay.h" 5 | #include "HeatIndex.h" 6 | #include 7 | 8 | int main() 9 | { 10 | auto weatherData = WeatherData(); 11 | auto currentDisplay = CurrentConditionsDisplay(&weatherData); 12 | auto statsDisplay = StatisticsDisplay(&weatherData); 13 | auto forcastDisplay = ForcastDisplay(&weatherData); 14 | auto heatIndex = HeatIndex(&weatherData); 15 | weatherData.setMeasurements(80, 65, 30.4f); 16 | weatherData.setMeasurements(82, 70, 29.2f); 17 | weatherData.setMeasurements(78, 90, 29.2f); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /Singleton/chocolate/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++20 3 | DEPS = ChocolateBoiler.hpp 4 | TARGET = chocolateController 5 | OBJ = $(TARGET).o 6 | 7 | %.o: %.cpp $(DEPS) 8 | $(CXX) -c -o $@ $< $(CFLAGS) 9 | 10 | $(TARGET): $(OBJ) 11 | $(CXX) -o $@ $^ $(CFLAGS) 12 | 13 | clean: 14 | rm -f *.o $(TARGET) 15 | -------------------------------------------------------------------------------- /Singleton/chocolate/chocolateController.cpp: -------------------------------------------------------------------------------- 1 | #include "ChocolateBoiler.hpp" 2 | #include 3 | #include 4 | 5 | int main() 6 | { 7 | ChocolateBoiler *boiler = ChocolateBoiler::getInstance(); 8 | boiler->fill(); 9 | boiler->boil(); 10 | boiler->drain(); 11 | [[maybe_unused]] ChocolateBoiler *boiler2 = ChocolateBoiler::getInstance(); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /Singleton/classic/Singleton.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SINGLETON_H 2 | #define SINGLETON_H 3 | 4 | // NOTE: This is not thread safe! 5 | 6 | class Singleton { 7 | public: 8 | static Singleton* getInstance(); 9 | private: 10 | Singleton() = default; 11 | static Singleton *uniqeInstance; 12 | }; 13 | 14 | inline 15 | Singleton* 16 | Singleton::getInstance() 17 | { 18 | if (uniqeInstance == nullptr) 19 | uniqeInstance = new Singleton(); 20 | return uniqeInstance; 21 | } 22 | 23 | #endif /* SINGLETON_H */ 24 | -------------------------------------------------------------------------------- /Singleton/dcl/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++20 3 | DEPS = Singleton.hpp 4 | TARGET = singletonClient 5 | OBJ = $(TARGET).o 6 | 7 | %.o: %.cpp $(DEPS) 8 | $(CXX) -c -o $@ $< $(CFLAGS) 9 | 10 | $(TARGET): $(OBJ) 11 | $(CXX) -o $@ $^ $(CFLAGS) 12 | 13 | clean: 14 | rm -f *.o $(TARGET) 15 | -------------------------------------------------------------------------------- /Singleton/dcl/Singleton.hpp: -------------------------------------------------------------------------------- 1 | // Double-checked locking 2 | 3 | #ifndef SINGLETON_H 4 | #define SINGLETON_H 5 | 6 | #include 7 | 8 | class Singleton { 9 | public: 10 | volatile static Singleton* getInstance(); 11 | private: 12 | Singleton () = default; 13 | volatile static Singleton *uniqueInstance; 14 | static std::mutex uniqueInstance_mtx; // protect uniqueInstance 15 | }; 16 | 17 | inline 18 | volatile 19 | Singleton* Singleton::uniqueInstance = nullptr; 20 | 21 | inline 22 | std::mutex Singleton::uniqueInstance_mtx; // protect uniqueInstance 23 | 24 | inline 25 | volatile Singleton* 26 | Singleton::getInstance() 27 | { 28 | if (uniqueInstance == nullptr) { 29 | std::lock_guard gaurd(uniqueInstance_mtx); // lock 30 | if (uniqueInstance == nullptr) 31 | uniqueInstance = new Singleton(); 32 | } 33 | return uniqueInstance; 34 | } 35 | 36 | #endif /* SINGLETON_H */ 37 | -------------------------------------------------------------------------------- /Singleton/dcl/singletonClient.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.hpp" 2 | 3 | int main() 4 | { 5 | [[maybe_unused]] volatile Singleton *singleton = Singleton::getInstance(); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /Singleton/stat/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++20 3 | DEPS = Singleton.hpp 4 | TARGET = singletonClient 5 | OBJ = $(TARGET).o 6 | 7 | %.o: %.cpp $(DEPS) 8 | $(CXX) -c -o $@ $< $(CFLAGS) 9 | 10 | $(TARGET): $(OBJ) 11 | $(CXX) -o $@ $^ $(CFLAGS) 12 | 13 | clean: 14 | rm -f *.o $(TARGET) 15 | -------------------------------------------------------------------------------- /Singleton/stat/Singleton.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SINGLETON_H 2 | #define SINGLETON_H 3 | 4 | class Singleton { 5 | public: 6 | static Singleton* getInstance() { return uniqueInstance; } 7 | private: 8 | Singleton () = default; 9 | static Singleton *uniqueInstance; 10 | }; 11 | 12 | inline 13 | Singleton* Singleton::uniqueInstance = new Singleton(); 14 | 15 | #endif /* SINGLETON_H */ 16 | -------------------------------------------------------------------------------- /Singleton/stat/singletonClient.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.hpp" 2 | 3 | int main() 4 | { 5 | [[maybe_unused]] Singleton *singleton = Singleton::getInstance(); 6 | return 0; 7 | } 8 | -------------------------------------------------------------------------------- /Singleton/subclass/CoolerSingleton.hpp: -------------------------------------------------------------------------------- 1 | #ifndef COOLER_SINGLETON_H 2 | #define COOLER_SINGLETON_H 3 | 4 | #include "Singleton.hpp" 5 | 6 | class CoolerSingleton : public Singleton { 7 | protected: 8 | static Singleton *uniqueInstance; 9 | private: 10 | CoolerSingleton() : super() {} 11 | }; 12 | 13 | #endif /* COOLER_SINGLETON_H */ 14 | -------------------------------------------------------------------------------- /Singleton/subclass/HotterSingleton.hpp: -------------------------------------------------------------------------------- 1 | #ifndef HOTTER_SINGLETON_H 2 | #define HOTTER_SINGLETON_H 3 | 4 | #include "Singleton.hpp" 5 | 6 | class HotterSingleton : public Singleton { 7 | private: 8 | HotterSingleton() : super() { } 9 | }; 10 | 11 | #endif /* HOTTER_SINGLETON_H */ 12 | -------------------------------------------------------------------------------- /Singleton/subclass/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++20 3 | DEPS = CoolerSingleton.hpp HotterSingleton.hpp Singleton.hpp 4 | TARGET = singleTestDrive 5 | OBJ = $(TARGET).o 6 | 7 | %.o: %.cpp $(DEPS) 8 | $(CXX) -c -o $@ $< $(CFLAGS) 9 | 10 | $(TARGET): $(OBJ) 11 | $(CXX) -o $@ $^ $(CFLAGS) 12 | 13 | clean: 14 | rm -f *.o $(TARGET) 15 | -------------------------------------------------------------------------------- /Singleton/subclass/Singleton.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SINGLETON_H 2 | #define SINGLETON_H 3 | 4 | #include 5 | 6 | class Singleton { 7 | public: 8 | using super = Singleton; 9 | static Singleton* getInstance(); 10 | protected: 11 | Singleton() = default; 12 | static Singleton *uniqueInstance; 13 | static std::mutex uniqueInstance_mtx; 14 | }; 15 | 16 | inline 17 | Singleton* Singleton::uniqueInstance = nullptr; 18 | 19 | inline 20 | std::mutex Singleton::uniqueInstance_mtx; 21 | 22 | inline 23 | Singleton* 24 | Singleton::getInstance() 25 | { 26 | std::lock_guard guard(uniqueInstance_mtx); 27 | if (uniqueInstance == nullptr) 28 | uniqueInstance = new Singleton(); 29 | return uniqueInstance; 30 | } 31 | 32 | #endif /* SINGLETON_H */ 33 | -------------------------------------------------------------------------------- /Singleton/subclass/singleTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.hpp" 2 | #include "CoolerSingleton.hpp" 3 | #include "HotterSingleton.hpp" 4 | #include 5 | 6 | int main() 7 | { 8 | Singleton *foo = CoolerSingleton::getInstance(); 9 | Singleton *bar = HotterSingleton::getInstance(); 10 | std::cout << foo << '\n'; 11 | std::cout << bar << '\n'; 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Singleton/threadsafe/Singleton.hpp: -------------------------------------------------------------------------------- 1 | #ifndef SINGLETON_H 2 | #define SINGLETON_H 3 | 4 | #include 5 | 6 | class Singleton { 7 | public: 8 | static Singleton* getInstance(); 9 | private: 10 | Singleton() = default; 11 | static Singleton *uniqueInstance; 12 | static std::mutex uniqueInstance_mtx; // protect uniqueInstance 13 | }; 14 | 15 | inline 16 | Singleton Singleton::*uniqueInstance = nullptr; 17 | 18 | inline 19 | Singleton* 20 | Singleton::getInstance() 21 | { 22 | std::lock_guard gaurd(uniqueInstance_mtx); 23 | if (uniqueInstance == nullptr) 24 | uniqueInstance = new Singleton(); 25 | return uniqueInstance; 26 | } 27 | 28 | #endif /* SINGLETON_H */ 29 | -------------------------------------------------------------------------------- /State/gumball/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = GumballMachine.h 4 | TARGET = gumballMachineTestDrive 5 | OBJ = $(TARGET).o 6 | 7 | %.o: %.cpp $(DEPS) 8 | $(CXX) -c -o $@ $< $(CFLAGS) 9 | 10 | $(TARGET): $(OBJ) 11 | $(CXX) -o $@ $^ $(CFLAGS) 12 | 13 | clean: 14 | rm -f *.o $(TARGET) $(TARGET2) 15 | -------------------------------------------------------------------------------- /State/gumball/gumballMachineTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "GumballMachine.h" 2 | #include 3 | 4 | int main() 5 | { 6 | auto gumballMachine = GumballMachine(5); 7 | 8 | std::cout << gumballMachine << '\n'; 9 | 10 | gumballMachine.insertQuarter(); 11 | gumballMachine.turnCrank(); 12 | 13 | std::cout << gumballMachine << '\n'; 14 | 15 | 16 | gumballMachine.insertQuarter(); 17 | gumballMachine.ejectQuarter(); 18 | gumballMachine.turnCrank(); 19 | std::cout << gumballMachine << '\n'; 20 | 21 | gumballMachine.insertQuarter(); 22 | gumballMachine.turnCrank(); 23 | gumballMachine.insertQuarter(); 24 | gumballMachine.turnCrank(); 25 | gumballMachine.ejectQuarter(); 26 | 27 | std::cout << gumballMachine << '\n'; 28 | 29 | gumballMachine.insertQuarter(); 30 | gumballMachine.insertQuarter(); 31 | gumballMachine.turnCrank(); 32 | gumballMachine.insertQuarter(); 33 | gumballMachine.turnCrank(); 34 | gumballMachine.insertQuarter(); 35 | gumballMachine.turnCrank(); 36 | 37 | std::cout << gumballMachine << '\n'; 38 | 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /State/gumballstate/GumballMachine.h: -------------------------------------------------------------------------------- 1 | #ifndef GUMBALL_MACHINE_H 2 | #define GUMBALL_MACHINE_H 3 | 4 | #include "State.h" 5 | #include 6 | #include 7 | 8 | class NoQuarterState; 9 | class HasQuarterState; 10 | class SoldOutState; 11 | class SoldState; 12 | 13 | class GumballMachine { 14 | friend std::ostream& operator<<(std::ostream &, const GumballMachine &); 15 | public: 16 | GumballMachine(); 17 | GumballMachine(int); 18 | void insertQuarter(); 19 | void ejectQuarter(); 20 | void turnCrank(); 21 | void releaseBall(); 22 | int getCount() const; 23 | void refill(int); 24 | void setState(State *); 25 | State* getState() const; 26 | State* getNoQuarterState() const; 27 | State* getHasQuarterState() const; 28 | State* getSoldOutState() const; 29 | State* getSoldState() const; 30 | private: 31 | std::unique_ptr soldOutState; 32 | std::unique_ptr noQuarterState; 33 | std::unique_ptr hasQuarterState; 34 | std::unique_ptr soldState; 35 | State *state; 36 | int count = 0; 37 | }; 38 | 39 | #endif /* GUMBALL_MACHINE_H */ 40 | -------------------------------------------------------------------------------- /State/gumballstate/HasQuarterState.cpp: -------------------------------------------------------------------------------- 1 | #include "GumballMachine.h" 2 | #include "HasQuarterState.h" 3 | #include 4 | 5 | HasQuarterState::HasQuarterState(GumballMachine *gbm) 6 | : gumballMachine(gbm) { } 7 | 8 | void 9 | HasQuarterState::insertQuarter() 10 | { 11 | std::cout << "You can't insert another quarter\n"; 12 | } 13 | 14 | void 15 | HasQuarterState::ejectQuarter() 16 | { 17 | std::cout << "Quarter return\n"; 18 | gumballMachine->setState(gumballMachine->getNoQuarterState()); 19 | } 20 | 21 | void 22 | HasQuarterState::turnCrank() 23 | { 24 | std::cout << "You turned...\n"; 25 | gumballMachine->setState(gumballMachine->getSoldState()); 26 | } 27 | 28 | void 29 | HasQuarterState::dispense() 30 | { 31 | std::cout << "No gumball dispensed\n"; 32 | } 33 | 34 | void 35 | HasQuarterState::refill() { } 36 | 37 | void 38 | HasQuarterState::toString(std::ostream &os) const 39 | { 40 | os << "waiting for turn of crank"; 41 | } 42 | -------------------------------------------------------------------------------- /State/gumballstate/HasQuarterState.h: -------------------------------------------------------------------------------- 1 | #ifndef HAS_QUARTER_STATE_H 2 | #define HAS_QUARTER_STATE_H 3 | 4 | #include "State.h" 5 | #include 6 | 7 | class GumballMachine; 8 | 9 | class HasQuarterState : public State { 10 | public: 11 | HasQuarterState() = default; 12 | HasQuarterState(GumballMachine *gbm); 13 | void insertQuarter() override; 14 | void ejectQuarter() override; 15 | void turnCrank() override; 16 | void dispense() override; 17 | void refill() override; 18 | protected: 19 | void toString(std::ostream &) const override; 20 | private: 21 | GumballMachine *gumballMachine; 22 | }; 23 | 24 | #endif /* HAS_QUARTER_STATE_H */ 25 | -------------------------------------------------------------------------------- /State/gumballstate/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = GumballMachine.h HasQuarterState.h NoQuarterState.h SoldOutState.h \ 4 | SoldState.h State.h 5 | TARGET = gumballMachineTestDrive 6 | OBJ = $(TARGET).o GumballMachine.o HasQuarterState.o NoQuarterState.o \ 7 | SoldOutState.o SoldState.o 8 | 9 | %.o: %.cpp $(DEPS) 10 | $(CXX) -c -o $@ $< $(CFLAGS) 11 | 12 | $(TARGET): $(OBJ) 13 | $(CXX) -o $@ $^ $(CFLAGS) 14 | 15 | clean: 16 | rm -f *.o $(TARGET) $(TARGET2) 17 | -------------------------------------------------------------------------------- /State/gumballstate/NoQuarterState.cpp: -------------------------------------------------------------------------------- 1 | #include "NoQuarterState.h" 2 | #include "GumballMachine.h" 3 | #include 4 | #include 5 | 6 | NoQuarterState::NoQuarterState(GumballMachine *gbm) : gumballMachine(gbm) { } 7 | 8 | void 9 | NoQuarterState::insertQuarter() 10 | { 11 | std::cout << "You inseted a quarter\n"; 12 | gumballMachine->setState(gumballMachine->getHasQuarterState()); 13 | } 14 | 15 | void 16 | NoQuarterState::ejectQuarter() 17 | { 18 | std::cout << "You haven't inseted a quarter\n"; 19 | } 20 | 21 | void 22 | NoQuarterState::turnCrank() 23 | { 24 | std::cout << "You turned, but there's no quarter\n"; 25 | } 26 | 27 | void 28 | NoQuarterState::dispense() 29 | { 30 | std::cout << "You need to pay first\n"; 31 | } 32 | 33 | void 34 | NoQuarterState::refill() { } 35 | 36 | void 37 | NoQuarterState::toString(std::ostream &os) const 38 | { 39 | os << "waiting for turn of crank"; 40 | } 41 | -------------------------------------------------------------------------------- /State/gumballstate/NoQuarterState.h: -------------------------------------------------------------------------------- 1 | #ifndef NO_QUARTER_STATE_H 2 | #define NO_QUARTER_STATE_H 3 | 4 | #include "State.h" 5 | #include 6 | 7 | class GumballMachine; 8 | 9 | class NoQuarterState : public State { 10 | public: 11 | NoQuarterState() = default; 12 | NoQuarterState(GumballMachine *gbm); 13 | void insertQuarter() override; 14 | void ejectQuarter() override; 15 | void turnCrank() override; 16 | void dispense() override; 17 | void refill() override; 18 | protected: 19 | void toString(std::ostream &) const override; 20 | private: 21 | GumballMachine *gumballMachine; 22 | }; 23 | 24 | #endif /* NO_QUARTER_STATE_H */ 25 | -------------------------------------------------------------------------------- /State/gumballstate/SoldOutState.cpp: -------------------------------------------------------------------------------- 1 | #include "SoldOutState.h" 2 | #include "GumballMachine.h" 3 | #include 4 | 5 | SoldOutState::SoldOutState(GumballMachine *gbm) : gumballMachine(gbm) { } 6 | 7 | void 8 | SoldOutState::insertQuarter() { 9 | std::cout << "You can't insert a quarter, the machine is sold out\n"; 10 | } 11 | 12 | void 13 | SoldOutState::ejectQuarter() { 14 | std::cout << "You can't eject, you haven't inserted a quarter yet\n"; 15 | } 16 | 17 | void 18 | SoldOutState::turnCrank() { 19 | std::cout << "You turned, but there are no gumballs\n"; 20 | } 21 | 22 | void 23 | SoldOutState::dispense() { 24 | std::cout << "No gumball dispensed\n"; 25 | } 26 | 27 | void 28 | SoldOutState::refill() 29 | { 30 | gumballMachine->setState(gumballMachine->getNoQuarterState()); 31 | } 32 | 33 | void 34 | SoldOutState::toString(std::ostream &os) const 35 | { 36 | os << "dispensing a gumball"; 37 | } 38 | -------------------------------------------------------------------------------- /State/gumballstate/SoldOutState.h: -------------------------------------------------------------------------------- 1 | #ifndef SOLD_OUT_STATE_H 2 | #define SOLD_OUT_STATE_H 3 | 4 | #include "State.h" 5 | #include 6 | 7 | class GumballMachine; 8 | 9 | class SoldOutState : public State { 10 | public: 11 | SoldOutState() = default; 12 | SoldOutState(GumballMachine *); 13 | void insertQuarter() override; 14 | void ejectQuarter() override; 15 | void turnCrank() override; 16 | void dispense() override; 17 | void refill() override; 18 | protected: 19 | void toString(std::ostream &) const override; 20 | private: 21 | GumballMachine *gumballMachine; 22 | }; 23 | 24 | #endif /* SOLD_OUT_STATE_H */ 25 | -------------------------------------------------------------------------------- /State/gumballstate/SoldState.cpp: -------------------------------------------------------------------------------- 1 | #include "GumballMachine.h" 2 | #include "SoldState.h" 3 | #include 4 | 5 | SoldState::SoldState(GumballMachine *gbm) : gumballMachine(gbm) { } 6 | 7 | void 8 | SoldState::insertQuarter() 9 | { 10 | std::cout << "Please wait, we're already giving you a gumball\n"; 11 | } 12 | 13 | void 14 | SoldState::ejectQuarter() 15 | { 16 | std::cout << "Sorry, you already turned the crank\n"; 17 | } 18 | 19 | void 20 | SoldState::turnCrank() 21 | { 22 | std::cout << "Turning twice doesn't get you another gumball!\n"; 23 | } 24 | 25 | void 26 | SoldState::dispense() 27 | { 28 | gumballMachine->releaseBall(); 29 | if (gumballMachine->getCount() > 0) 30 | gumballMachine->setState(gumballMachine->getNoQuarterState()); 31 | else { 32 | std::cout << "Oops, out of gumballs\n"; 33 | gumballMachine->setState(gumballMachine->getSoldOutState()); 34 | } 35 | 36 | } 37 | 38 | void 39 | SoldState::refill() { } 40 | 41 | void 42 | SoldState::toString(std::ostream &os) const 43 | { 44 | os << "waiting for quarter"; 45 | } 46 | -------------------------------------------------------------------------------- /State/gumballstate/SoldState.h: -------------------------------------------------------------------------------- 1 | #ifndef SOLD_STATE_H 2 | #define SOLD_STATE_H 3 | 4 | #include "State.h" 5 | #include 6 | 7 | class GumballMachine; 8 | 9 | class SoldState : public State { 10 | public: 11 | SoldState() = default; 12 | SoldState(GumballMachine *gbm); 13 | void insertQuarter() override; 14 | void ejectQuarter() override; 15 | void turnCrank() override; 16 | void dispense() override; 17 | void refill() override; 18 | protected: 19 | void toString(std::ostream &) const override; 20 | private: 21 | GumballMachine *gumballMachine; 22 | }; 23 | 24 | #endif /* SOLD_STATE_H */ 25 | -------------------------------------------------------------------------------- /State/gumballstate/State.h: -------------------------------------------------------------------------------- 1 | #ifndef STATE_H 2 | #define STATE_H 3 | 4 | #include 5 | class State { 6 | friend std::ostream& operator<<(std::ostream&, const State &); 7 | public: 8 | virtual ~State () = default; 9 | virtual void insertQuarter() = 0; 10 | virtual void ejectQuarter() = 0; 11 | virtual void turnCrank() = 0; 12 | virtual void dispense() = 0; 13 | virtual void refill() = 0; 14 | protected: 15 | virtual void toString(std::ostream &) const = 0; 16 | }; 17 | 18 | inline 19 | std::ostream& operator<<(std::ostream& os, const State &state) 20 | { 21 | state.toString(os); 22 | return os; 23 | } 24 | 25 | #endif /* STATE_H */ 26 | -------------------------------------------------------------------------------- /State/gumballstate/gumballMachineTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "GumballMachine.h" 2 | #include 3 | 4 | int main() 5 | { 6 | auto gumballMachine = GumballMachine(2); 7 | 8 | std::cout << gumballMachine << '\n'; 9 | 10 | gumballMachine.insertQuarter(); 11 | gumballMachine.turnCrank(); 12 | 13 | std::cout << gumballMachine << '\n'; 14 | 15 | gumballMachine.insertQuarter(); 16 | gumballMachine.turnCrank(); 17 | gumballMachine.insertQuarter(); 18 | gumballMachine.turnCrank(); 19 | 20 | gumballMachine.refill(5); 21 | gumballMachine.insertQuarter(); 22 | gumballMachine.turnCrank(); 23 | 24 | std::cout << gumballMachine; 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /State/gumballstatewinner/GumballMachine.h: -------------------------------------------------------------------------------- 1 | #ifndef GUMBALL_MACHINE_H 2 | #define GUMBALL_MACHINE_H 3 | 4 | #include "State.h" 5 | #include 6 | #include 7 | 8 | class NoQuarterState; 9 | class HasQuarterState; 10 | class SoldOutState; 11 | class SoldState; 12 | 13 | class GumballMachine { 14 | friend std::ostream& operator<<(std::ostream &, const GumballMachine &); 15 | public: 16 | GumballMachine(); 17 | GumballMachine(int); 18 | void insertQuarter(); 19 | void ejectQuarter(); 20 | void turnCrank(); 21 | void releaseBall(); 22 | int getCount() const; 23 | void refill(int); 24 | void setState(State *); 25 | State* getState() const; 26 | State* getNoQuarterState() const; 27 | State* getHasQuarterState() const; 28 | State* getSoldOutState() const; 29 | State* getSoldState() const; 30 | State* getWinnerState() const; 31 | private: 32 | std::unique_ptr soldOutState; 33 | std::unique_ptr noQuarterState; 34 | std::unique_ptr hasQuarterState; 35 | std::unique_ptr soldState; 36 | std::unique_ptr winnerState; 37 | State *state; 38 | int count = 0; 39 | }; 40 | 41 | #endif /* GUMBALL_MACHINE_H */ 42 | -------------------------------------------------------------------------------- /State/gumballstatewinner/HasQuarterState.cpp: -------------------------------------------------------------------------------- 1 | #include "GumballMachine.h" 2 | #include "HasQuarterState.h" 3 | #include "SimpleRandom.h" 4 | #include 5 | 6 | HasQuarterState::HasQuarterState(GumballMachine *gbm) 7 | : gumballMachine(gbm) { } 8 | 9 | void 10 | HasQuarterState::insertQuarter() 11 | { 12 | std::cout << "You can't insert another quarter\n"; 13 | } 14 | 15 | void 16 | HasQuarterState::ejectQuarter() 17 | { 18 | std::cout << "Quarter return\n"; 19 | gumballMachine->setState(gumballMachine->getNoQuarterState()); 20 | } 21 | 22 | void 23 | HasQuarterState::turnCrank() 24 | { 25 | std::cout << "You turned...\n"; 26 | if (auto winner = randomWinner.nextInt(10); 27 | (winner == 0) && (gumballMachine->getCount() > 1)) 28 | gumballMachine->setState(gumballMachine->getWinnerState()); 29 | else 30 | gumballMachine->setState(gumballMachine->getSoldState()); 31 | } 32 | 33 | void 34 | HasQuarterState::dispense() 35 | { 36 | std::cout << "No gumball dispensed\n"; 37 | } 38 | 39 | void 40 | HasQuarterState::refill() { } 41 | 42 | void 43 | HasQuarterState::toString(std::ostream &os) const 44 | { 45 | os << "waiting for turn of crank"; 46 | } 47 | 48 | 49 | -------------------------------------------------------------------------------- /State/gumballstatewinner/HasQuarterState.h: -------------------------------------------------------------------------------- 1 | #ifndef HAS_QUARTER_STATE_H 2 | #define HAS_QUARTER_STATE_H 3 | 4 | #include "State.h" 5 | #include "SimpleRandom.h" 6 | #include 7 | 8 | class GumballMachine; 9 | 10 | class HasQuarterState : public State { 11 | public: 12 | HasQuarterState() = default; 13 | HasQuarterState(GumballMachine *gbm); 14 | void insertQuarter() override; 15 | void ejectQuarter() override; 16 | void turnCrank() override; 17 | void dispense() override; 18 | void refill() override; 19 | protected: 20 | void toString(std::ostream &) const override; 21 | private: 22 | GumballMachine *gumballMachine; 23 | SimpleRandom randomWinner = SimpleRandom(std::time(nullptr)); 24 | }; 25 | 26 | #endif /* HAS_QUARTER_STATE_H */ 27 | -------------------------------------------------------------------------------- /State/gumballstatewinner/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = GumballMachine.h HasQuarterState.h NoQuarterState.h SoldOutState.h \ 4 | SimpleRandom.h SoldState.h State.h WinnerState.h 5 | TARGET = gumballMachineTestDrive 6 | OBJ = $(TARGET).o GumballMachine.o HasQuarterState.o NoQuarterState.o \ 7 | SimpleRandom.o SoldOutState.o SoldState.o WinnerState.o 8 | 9 | %.o: %.cpp $(DEPS) 10 | $(CXX) -c -o $@ $< $(CFLAGS) 11 | 12 | $(TARGET): $(OBJ) 13 | $(CXX) -o $@ $^ $(CFLAGS) 14 | 15 | clean: 16 | rm -f *.o $(TARGET) $(TARGET2) 17 | -------------------------------------------------------------------------------- /State/gumballstatewinner/NoQuarterState.cpp: -------------------------------------------------------------------------------- 1 | #include "NoQuarterState.h" 2 | #include "GumballMachine.h" 3 | #include 4 | #include 5 | 6 | NoQuarterState::NoQuarterState(GumballMachine *gbm) : gumballMachine(gbm) { } 7 | 8 | void 9 | NoQuarterState::insertQuarter() 10 | { 11 | std::cout << "You inseted a quarter\n"; 12 | gumballMachine->setState(gumballMachine->getHasQuarterState()); 13 | } 14 | 15 | void 16 | NoQuarterState::ejectQuarter() 17 | { 18 | std::cout << "You haven't inseted a quarter\n"; 19 | } 20 | 21 | void 22 | NoQuarterState::turnCrank() 23 | { 24 | std::cout << "You turned, but there's no quarter\n"; 25 | } 26 | 27 | void 28 | NoQuarterState::dispense() 29 | { 30 | std::cout << "You need to pay first\n"; 31 | } 32 | 33 | void 34 | NoQuarterState::refill() { } 35 | 36 | void 37 | NoQuarterState::toString(std::ostream &os) const 38 | { 39 | os << "waiting for turn of crank"; 40 | } 41 | -------------------------------------------------------------------------------- /State/gumballstatewinner/NoQuarterState.h: -------------------------------------------------------------------------------- 1 | #ifndef NO_QUARTER_STATE_H 2 | #define NO_QUARTER_STATE_H 3 | 4 | #include "State.h" 5 | #include 6 | 7 | class GumballMachine; 8 | 9 | class NoQuarterState : public State { 10 | public: 11 | NoQuarterState() = default; 12 | NoQuarterState(GumballMachine *gbm); 13 | void insertQuarter() override; 14 | void ejectQuarter() override; 15 | void turnCrank() override; 16 | void dispense() override; 17 | void refill() override; 18 | protected: 19 | void toString(std::ostream &) const override; 20 | private: 21 | GumballMachine *gumballMachine; 22 | }; 23 | 24 | #endif /* NO_QUARTER_STATE_H */ 25 | -------------------------------------------------------------------------------- /State/gumballstatewinner/SimpleRandom.cpp: -------------------------------------------------------------------------------- 1 | #include "SimpleRandom.h" 2 | #include 3 | #include 4 | 5 | SimpleRandom::SimpleRandom(std::time_t time) : e(time) { } 6 | 7 | SimpleRandom::rand_type 8 | SimpleRandom::nextInt(const unsigned i) 9 | { 10 | u = std::uniform_int_distribution(0, i); 11 | e.discard(1); 12 | return u(e); 13 | } 14 | -------------------------------------------------------------------------------- /State/gumballstatewinner/SimpleRandom.h: -------------------------------------------------------------------------------- 1 | #ifndef SIMPLE_RANDOM_H 2 | #define SIMPLE_RANDOM_H 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | class SimpleRandom { 9 | public: 10 | using rand_type = std::default_random_engine::result_type; 11 | SimpleRandom() = default; 12 | SimpleRandom(std::time_t); 13 | rand_type nextInt(const unsigned i); 14 | private: 15 | std::default_random_engine e = std::default_random_engine(std::time(nullptr)); 16 | std::uniform_int_distribution u; 17 | }; 18 | 19 | #endif /* SIMPLE_RANDOM_H */ 20 | -------------------------------------------------------------------------------- /State/gumballstatewinner/SoldOutState.cpp: -------------------------------------------------------------------------------- 1 | #include "SoldOutState.h" 2 | #include "GumballMachine.h" 3 | #include 4 | 5 | SoldOutState::SoldOutState(GumballMachine *gbm) : gumballMachine(gbm) { } 6 | 7 | void 8 | SoldOutState::insertQuarter() { 9 | std::cout << "You can't insert a quarter, the machine is sold out\n"; 10 | } 11 | 12 | void 13 | SoldOutState::ejectQuarter() { 14 | std::cout << "You can't eject, you haven't inserted a quarter yet\n"; 15 | } 16 | 17 | void 18 | SoldOutState::turnCrank() { 19 | std::cout << "You turned, but there are no gumballs\n"; 20 | } 21 | 22 | void 23 | SoldOutState::dispense() { 24 | std::cout << "No gumball dispensed\n"; 25 | } 26 | 27 | void 28 | SoldOutState::refill() 29 | { 30 | gumballMachine->setState(gumballMachine->getNoQuarterState()); 31 | } 32 | 33 | void 34 | SoldOutState::toString(std::ostream &os) const 35 | { 36 | os << "dispensing a gumball"; 37 | } 38 | -------------------------------------------------------------------------------- /State/gumballstatewinner/SoldOutState.h: -------------------------------------------------------------------------------- 1 | #ifndef SOLD_OUT_STATE_H 2 | #define SOLD_OUT_STATE_H 3 | 4 | #include "State.h" 5 | #include 6 | 7 | class GumballMachine; 8 | 9 | class SoldOutState : public State { 10 | public: 11 | SoldOutState() = default; 12 | SoldOutState(GumballMachine *); 13 | void insertQuarter() override; 14 | void ejectQuarter() override; 15 | void turnCrank() override; 16 | void dispense() override; 17 | void refill() override; 18 | protected: 19 | void toString(std::ostream &) const override; 20 | private: 21 | GumballMachine *gumballMachine; 22 | }; 23 | 24 | #endif /* SOLD_OUT_STATE_H */ 25 | -------------------------------------------------------------------------------- /State/gumballstatewinner/SoldState.cpp: -------------------------------------------------------------------------------- 1 | #include "GumballMachine.h" 2 | #include "SoldState.h" 3 | #include 4 | 5 | SoldState::SoldState(GumballMachine *gbm) : gumballMachine(gbm) { } 6 | 7 | void 8 | SoldState::insertQuarter() 9 | { 10 | std::cout << "Please wait, we're already giving you a gumball\n"; 11 | } 12 | 13 | void 14 | SoldState::ejectQuarter() 15 | { 16 | std::cout << "Sorry, you already turned the crank\n"; 17 | } 18 | 19 | void 20 | SoldState::turnCrank() 21 | { 22 | std::cout << "Turning twice doesn't get you another gumball!\n"; 23 | } 24 | 25 | void 26 | SoldState::dispense() 27 | { 28 | gumballMachine->releaseBall(); 29 | if (gumballMachine->getCount() > 0) 30 | gumballMachine->setState(gumballMachine->getNoQuarterState()); 31 | else { 32 | std::cout << "Oops, out of gumballs\n"; 33 | gumballMachine->setState(gumballMachine->getSoldOutState()); 34 | } 35 | } 36 | 37 | void 38 | SoldState::refill() { } 39 | 40 | void 41 | SoldState::toString(std::ostream &os) const 42 | { 43 | os << "waiting for quarter"; 44 | } 45 | -------------------------------------------------------------------------------- /State/gumballstatewinner/SoldState.h: -------------------------------------------------------------------------------- 1 | #ifndef SOLD_STATE_H 2 | #define SOLD_STATE_H 3 | 4 | #include "State.h" 5 | #include 6 | 7 | class GumballMachine; 8 | 9 | class SoldState : public State { 10 | public: 11 | SoldState() = default; 12 | SoldState(GumballMachine *gbm); 13 | void insertQuarter() override; 14 | void ejectQuarter() override; 15 | void turnCrank() override; 16 | void dispense() override; 17 | void refill() override; 18 | protected: 19 | void toString(std::ostream &) const override; 20 | private: 21 | GumballMachine *gumballMachine; 22 | }; 23 | 24 | #endif /* SOLD_STATE_H */ 25 | -------------------------------------------------------------------------------- /State/gumballstatewinner/State.h: -------------------------------------------------------------------------------- 1 | #ifndef STATE_H 2 | #define STATE_H 3 | 4 | #include 5 | class State { 6 | friend std::ostream& operator<<(std::ostream&, const State &); 7 | public: 8 | virtual ~State () = default; 9 | virtual void insertQuarter() = 0; 10 | virtual void ejectQuarter() = 0; 11 | virtual void turnCrank() = 0; 12 | virtual void dispense() = 0; 13 | virtual void refill() = 0; 14 | protected: 15 | virtual void toString(std::ostream &) const = 0; 16 | }; 17 | 18 | inline 19 | std::ostream& operator<<(std::ostream& os, const State &state) 20 | { 21 | state.toString(os); 22 | return os; 23 | } 24 | 25 | #endif /* STATE_H */ 26 | -------------------------------------------------------------------------------- /State/gumballstatewinner/WinnerState.cpp: -------------------------------------------------------------------------------- 1 | #include "GumballMachine.h" 2 | #include "WinnerState.h" 3 | #include 4 | 5 | WinnerState::WinnerState(GumballMachine *gbm) : gumballMachine(gbm) { } 6 | 7 | void 8 | WinnerState::insertQuarter() 9 | { 10 | std::cout << "Please wait, we're already giving you a Gumball\n"; 11 | } 12 | 13 | void 14 | WinnerState::ejectQuarter() 15 | { 16 | std::cout << "Please wait, we're already giving you a Gumball\n"; 17 | } 18 | 19 | void 20 | WinnerState::turnCrank() { 21 | std::cout << "Turning again doesn't get you another gumball!\n"; 22 | } 23 | 24 | void 25 | WinnerState::dispense() 26 | { 27 | gumballMachine->releaseBall(); 28 | if (gumballMachine->getCount() == 0) 29 | gumballMachine->setState(gumballMachine->getSoldOutState()); 30 | else { 31 | gumballMachine->releaseBall(); 32 | std::cout << "YOU'RE A WINNER! You got two gumballs for your quarter\n"; 33 | if (gumballMachine->getCount() > 0) 34 | gumballMachine->setState(gumballMachine->getNoQuarterState()); 35 | else { 36 | std::cout << "Oops, out of gumballs!\n"; 37 | gumballMachine->setState(gumballMachine->getSoldOutState()); 38 | } 39 | } 40 | } 41 | 42 | void 43 | WinnerState::refill() { } 44 | 45 | 46 | void 47 | WinnerState::toString(std::ostream &os) const 48 | { 49 | os << "despensing two gumballs for your quarter, because YOU'RE A WINNER!"; 50 | } 51 | 52 | -------------------------------------------------------------------------------- /State/gumballstatewinner/WinnerState.h: -------------------------------------------------------------------------------- 1 | #ifndef WINNER_STATE_H 2 | #define WINNER_STATE_H 3 | 4 | #include "State.h" 5 | 6 | class GumballMachine; 7 | 8 | class WinnerState : public State { 9 | public: 10 | WinnerState(GumballMachine *); 11 | void insertQuarter() override; 12 | void ejectQuarter() override; 13 | void turnCrank() override; 14 | void dispense() override; 15 | void refill() override; 16 | protected: 17 | virtual void toString(std::ostream &) const override; 18 | private: 19 | GumballMachine *gumballMachine; 20 | }; 21 | 22 | #endif /* WINNER_STATE_H */ 23 | -------------------------------------------------------------------------------- /State/gumballstatewinner/gumballMachineTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "GumballMachine.h" 2 | #include 3 | 4 | int main() 5 | { 6 | auto gumballMachine = GumballMachine(10); 7 | 8 | std::cout << gumballMachine << '\n'; 9 | 10 | gumballMachine.insertQuarter(); 11 | gumballMachine.turnCrank(); 12 | gumballMachine.insertQuarter(); 13 | gumballMachine.turnCrank(); 14 | 15 | std::cout << gumballMachine << '\n'; 16 | 17 | gumballMachine.insertQuarter(); 18 | gumballMachine.turnCrank(); 19 | gumballMachine.insertQuarter(); 20 | gumballMachine.turnCrank(); 21 | 22 | std::cout << gumballMachine << '\n'; 23 | 24 | gumballMachine.insertQuarter(); 25 | gumballMachine.turnCrank(); 26 | gumballMachine.insertQuarter(); 27 | gumballMachine.turnCrank(); 28 | 29 | std::cout << gumballMachine << '\n'; 30 | 31 | gumballMachine.insertQuarter(); 32 | gumballMachine.turnCrank(); 33 | gumballMachine.insertQuarter(); 34 | gumballMachine.turnCrank(); 35 | 36 | std::cout << gumballMachine << '\n'; 37 | 38 | gumballMachine.insertQuarter(); 39 | gumballMachine.turnCrank(); 40 | gumballMachine.insertQuarter(); 41 | gumballMachine.turnCrank(); 42 | 43 | std::cout << gumballMachine << '\n'; 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /Strategy/Duck.h: -------------------------------------------------------------------------------- 1 | #ifndef DUCK_H 2 | #define DUCK_H 3 | 4 | #include "FlyBehavior.h" 5 | #include "QuackBehavior.h" 6 | #include 7 | #include 8 | 9 | // abstract class 10 | class Duck { 11 | public: 12 | virtual ~Duck() = default; 13 | virtual void display() const = 0; // pure virtual function 14 | void performQuack() const { quackBehavior->quack(); } 15 | void performFly() const { flyBehavior->fly(); } 16 | void swim() const { std::cout << "all ducks float, even decoys!\n"; } 17 | void setFlyBehavior(std::unique_ptr fb) { flyBehavior = std::move(fb); } // passes ownership; C++ Core Guideline, F.26 18 | void setQuackBehavior(std::unique_ptr qb) { quackBehavior = std::move(qb); } 19 | protected: 20 | std::unique_ptr flyBehavior = nullptr; 21 | std::unique_ptr quackBehavior = nullptr; 22 | }; 23 | 24 | #endif /* ifndef DUCK_H */ 25 | -------------------------------------------------------------------------------- /Strategy/DuckCall.h: -------------------------------------------------------------------------------- 1 | #ifndef DUCK_CALL_H 2 | #define DUCK_CALL_H 3 | 4 | #include "QuackBehavior.h" 5 | #include "Quack.h" 6 | #include 7 | #include 8 | 9 | class DuckCall { 10 | public: 11 | DuckCall() { quackBehavior = std::make_unique(); } 12 | void display() const { std::cout << "I'm not a duck!\n"; } 13 | void performQuack() { quackBehavior->quack(); } 14 | private: 15 | std::unique_ptr quackBehavior; 16 | }; 17 | 18 | #endif /* ifndef DUCK_CALL_H */ 19 | -------------------------------------------------------------------------------- /Strategy/FlyBehavior.h: -------------------------------------------------------------------------------- 1 | #ifndef FLY_BEHAVIOR_H 2 | #define FLY_BEHAVIOR_H 3 | 4 | class FlyBehavior { 5 | public: 6 | virtual ~FlyBehavior() = default; 7 | virtual void fly() = 0; 8 | }; 9 | 10 | #endif /* ifndef FLY_BEHAVIOR_H */ 11 | -------------------------------------------------------------------------------- /Strategy/FlyNoWay.h: -------------------------------------------------------------------------------- 1 | #ifndef FLY_NOWAY_H 2 | #define FLY_NOWAY_H 3 | 4 | #include "FlyBehavior.h" 5 | #include 6 | 7 | class FlyNoWay : public FlyBehavior { 8 | public: 9 | void fly() override { std::cout << "I can't fly\n"; } 10 | }; 11 | 12 | #endif /* ifndef FLY_NOWAY_H */ 13 | -------------------------------------------------------------------------------- /Strategy/FlyRocketPowered.h: -------------------------------------------------------------------------------- 1 | #ifndef FLY_ROCKET_POWERED_H 2 | #define FLY_ROCKET_POWERED_H 3 | 4 | #include "FlyBehavior.h" 5 | #include 6 | 7 | class FlyRocketPowered : public FlyBehavior { 8 | public: 9 | void fly() { std::cout << "I'm flying with a rocket!\n"; } 10 | }; 11 | 12 | #endif /* ifndef FLY_ROCKET_POWERED_H */ 13 | -------------------------------------------------------------------------------- /Strategy/FlyWithWings.h: -------------------------------------------------------------------------------- 1 | #ifndef FLY_WITH_WINGS_H 2 | #define FLY_WITH_WINGS_H 3 | 4 | #include "FlyBehavior.h" 5 | #include 6 | 7 | class FlyWithWings : public FlyBehavior { 8 | public: 9 | void fly() override { std::cout << "I'm flying!!\n";} 10 | }; 11 | 12 | #endif /* ifndef FLY_WITH_WINGS_H */ 13 | -------------------------------------------------------------------------------- /Strategy/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = DuckCall.h Duck.h FlyBehavior.h FlyNoWay.h FlyRocketPowered.h \ 4 | FlyWithWings.h MallardDuck.h ModelDuck.h MuteDuck.h MuteQuack.h \ 5 | QuackBehavior.h Quack.h QuackMute.h Squeak.h 6 | 7 | TARGET = miniDuckSimulator 8 | OBJ = $(TARGET).o 9 | 10 | %.o: %.cpp $(DEPS) 11 | $(CXX) -c -o $@ $< $(CFLAGS) 12 | 13 | $(TARGET): $(OBJ) 14 | $(CXX) -o $@ $^ $(CFLAGS) 15 | 16 | clean: 17 | rm -f *.o $(TARGET) 18 | -------------------------------------------------------------------------------- /Strategy/MallardDuck.h: -------------------------------------------------------------------------------- 1 | #ifndef MALLARD_DUCK_H 2 | #define MALLARD_DUCK_H 3 | 4 | #include "Duck.h" 5 | #include "Quack.h" 6 | #include "FlyWithWings.h" 7 | #include 8 | #include 9 | 10 | class MallardDuck : public Duck { 11 | public: 12 | MallardDuck(); 13 | void display() const override { std::cout << "I'm a real Mallard Duck\n"; } 14 | }; 15 | 16 | inline 17 | MallardDuck::MallardDuck() { 18 | quackBehavior = std::make_unique(); 19 | flyBehavior = std::make_unique(); 20 | } 21 | 22 | #endif /* ifndef MALLARD_DUCK_H */ 23 | -------------------------------------------------------------------------------- /Strategy/ModelDuck.h: -------------------------------------------------------------------------------- 1 | #ifndef MODEL_DUCK_H 2 | #define MODEL_DUCK_H 3 | 4 | #include "Duck.h" 5 | #include "FlyNoWay.h" 6 | #include "Quack.h" 7 | #include 8 | 9 | class ModelDuck : public Duck { 10 | public: 11 | ModelDuck(); 12 | void display() const override { std::cout << "I'm a model duck"; } 13 | }; 14 | 15 | inline ModelDuck::ModelDuck() { 16 | flyBehavior = std::make_unique(); 17 | quackBehavior = std::make_unique(); 18 | } 19 | 20 | #endif /* ifndef MODEL_DUCK_H */ 21 | -------------------------------------------------------------------------------- /Strategy/MuteDuck.h: -------------------------------------------------------------------------------- 1 | #ifndef Mute_Duck_H 2 | #define Mute_Duck_H 3 | 4 | #include "Duck.h" 5 | #include "FlyWithWings.h" 6 | #include "MuteQuack.h" 7 | #include 8 | #include 9 | 10 | class MuteDuck : public Duck { 11 | public: 12 | MuteDuck(); 13 | void display() const override { std::cout << "I am a mute duck\n"; } 14 | }; 15 | 16 | inline 17 | MuteDuck::MuteDuck() { 18 | flyBehavior = std::make_unique(); 19 | quackBehavior = std::make_unique(); 20 | } 21 | 22 | #endif /* ifndef Mute_Duck */ 23 | -------------------------------------------------------------------------------- /Strategy/MuteQuack.h: -------------------------------------------------------------------------------- 1 | #ifndef MUTEQUACK_H 2 | #define MUTEQUACK_H 3 | 4 | #include "QuackBehavior.h" 5 | #include 6 | 7 | class MuteQuack : public QuackBehavior { 8 | public: 9 | void quack() override { std::cout << "<< Silence >>\n"; } 10 | }; 11 | 12 | #endif /* ifndef MUTEQUACK_H */ 13 | -------------------------------------------------------------------------------- /Strategy/Quack.h: -------------------------------------------------------------------------------- 1 | #ifndef QUACK_H 2 | #define QUACK_H 3 | 4 | #include "QuackBehavior.h" 5 | #include 6 | 7 | class Quack : public QuackBehavior { 8 | public: 9 | void quack() override { std::cout << "Quack\n"; } 10 | }; 11 | 12 | #endif /* ifndef QUACK_H */ 13 | -------------------------------------------------------------------------------- /Strategy/QuackBehavior.h: -------------------------------------------------------------------------------- 1 | #ifndef QUACKBEHAVIOR_H 2 | #define QUACKBEHAVIOR_H 3 | 4 | class QuackBehavior { 5 | public: 6 | virtual ~QuackBehavior() = default; 7 | virtual void quack() = 0; 8 | }; 9 | 10 | #endif /* ifndef QUACKBEHAVIOR_H */ 11 | -------------------------------------------------------------------------------- /Strategy/QuackMute.h: -------------------------------------------------------------------------------- 1 | #ifndef QUAK_MUTE_H 2 | #define QUAK_MUTE_H 3 | 4 | #include "QuackBehavior.h" 5 | #include 6 | 7 | class QuackMute : public QuackBehavior { 8 | void quack() override { std::cout << "<< Silence >>\n"; } 9 | }; 10 | 11 | #endif /* ifndef QUAK_MUTE_H */ 12 | -------------------------------------------------------------------------------- /Strategy/Squeak.h: -------------------------------------------------------------------------------- 1 | #ifndef SQUEAK_H 2 | #define SQUEAK_H 3 | 4 | #include "QuackBehavior.h" 5 | #include 6 | 7 | class Squeak : public QuackBehavior { 8 | public: 9 | void quack() override { std::cout << "Squeak"; } 10 | }; 11 | 12 | #endif /* ifndef SQUEAK_H */ 13 | -------------------------------------------------------------------------------- /Strategy/miniDuckSimulator.cpp: -------------------------------------------------------------------------------- 1 | #include "Duck.h" 2 | #include "MallardDuck.h" 3 | #include "ModelDuck.h" 4 | #include "FlyRocketPowered.h" 5 | #include "DuckCall.h" 6 | #include "MuteDuck.h" 7 | #include 8 | 9 | int main() { 10 | using DuckPtr = std::unique_ptr; // type alias 11 | DuckPtr mallard = std::make_unique(); 12 | mallard->performQuack(); 13 | mallard->performFly(); 14 | 15 | DuckPtr model = std::make_unique(); 16 | model->performFly(); 17 | model->setFlyBehavior(std::make_unique()); 18 | model->performFly(); 19 | 20 | auto duckCall = DuckCall(); 21 | duckCall.performQuack(); 22 | duckCall.display(); 23 | 24 | DuckPtr muteduck = std::make_unique(); 25 | muteduck->performFly(); 26 | muteduck->performQuack(); 27 | muteduck->display(); 28 | 29 | return 0; 30 | }; 31 | -------------------------------------------------------------------------------- /TemplateMethod/barista/CaffeineBeverage.h: -------------------------------------------------------------------------------- 1 | #ifndef CAFFEINE_BEVERAGE_H 2 | #define CAFFEINE_BEVERAGE_H 3 | 4 | #include 5 | 6 | class CaffeineBeverage { 7 | public: 8 | virtual ~CaffeineBeverage() = default; 9 | virtual void prepareRecipe() final; 10 | protected: 11 | virtual void brew() = 0; 12 | virtual void addCondiments() = 0; 13 | void boilWater() { std::cout << "Boiling water\n"; } 14 | void pourInCup() { std::cout << "Pouring into cup\n"; } 15 | }; 16 | 17 | inline 18 | void 19 | CaffeineBeverage::prepareRecipe() 20 | { 21 | boilWater(); 22 | brew(); 23 | pourInCup(); 24 | addCondiments(); 25 | } 26 | 27 | #endif /* CAFFEINE_BEVERAGE_H */ 28 | -------------------------------------------------------------------------------- /TemplateMethod/barista/CaffeineBeverageWithHook.h: -------------------------------------------------------------------------------- 1 | #ifndef CAFFEINE_BEVERAGEWITH_HOOK_H 2 | #define CAFFEINE_BEVERAGEWITH_HOOK_H 3 | 4 | #include 5 | 6 | class CaffeineBeverageWithHook { 7 | public: 8 | virtual ~CaffeineBeverageWithHook() = default; 9 | virtual void prepareRecipe() final; 10 | protected: 11 | virtual void brew() = 0; 12 | virtual void addCondiments() = 0; 13 | virtual bool customerWantCondiment() { return true; } 14 | void boilWater() { std::cout << "Boiling water\n"; } 15 | void pourInCup() { std::cout << "Pouring into cup\n"; } 16 | }; 17 | 18 | inline 19 | void 20 | CaffeineBeverageWithHook::prepareRecipe() 21 | { 22 | boilWater(); 23 | brew(); 24 | pourInCup(); 25 | if (customerWantCondiment()) 26 | addCondiments(); 27 | } 28 | 29 | 30 | 31 | #endif /* CAFFEINE_BEVERAGEWITH_HOOK_H */ 32 | -------------------------------------------------------------------------------- /TemplateMethod/barista/Coffee.h: -------------------------------------------------------------------------------- 1 | #ifndef COFFE_H 2 | #define COFFE_H 3 | 4 | #include "CaffeineBeverage.h" 5 | #include 6 | 7 | class Coffe : public CaffeineBeverage { 8 | public: 9 | void brew() override { std::cout << "Dripping Coffe through filter\n"; } 10 | void addCondiments() override { std::cout << "Adding Sugar and Milk\n"; } 11 | }; 12 | 13 | #endif /* COFFE_H */ 14 | -------------------------------------------------------------------------------- /TemplateMethod/barista/CoffeeWithHook.h: -------------------------------------------------------------------------------- 1 | #ifndef COFFEE_WITH_HOOK_H 2 | #define COFFEE_WITH_HOOK_H 3 | 4 | #include "CaffeineBeverageWithHook.h" 5 | #include 6 | #include "istream" 7 | #include 8 | #include 9 | 10 | class CoffeeWithHook : public CaffeineBeverageWithHook { 11 | public: 12 | void brew() override { std::cout << "Dripping Coffee through filter\n"; } 13 | void addCondiments() override { std::cout << "Adding Sugar and Milk\n"; } 14 | bool customerWantCondiment() override; 15 | std::string getUSerInput(); 16 | }; 17 | 18 | inline 19 | bool 20 | CoffeeWithHook::customerWantCondiment() 21 | { 22 | std::string answer = getUSerInput(); 23 | if (std::tolower(answer[0]) == 'y') 24 | return true; 25 | return false; 26 | } 27 | 28 | inline 29 | std::string 30 | CoffeeWithHook::getUSerInput() 31 | { 32 | std::string answer = {}; 33 | std::cout << "Would you like milk and sugar with your coffee (y/n)? "; 34 | if (!std::getline(std::cin, answer)) 35 | std::cerr << "IO error trying to read your answer\n"; 36 | if (answer.empty()) 37 | return "no"; 38 | return answer; 39 | } 40 | 41 | #endif /* COFFEE_WITH_HOOK_H */ 42 | -------------------------------------------------------------------------------- /TemplateMethod/barista/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = CaffeineBeverage.h CaffeineBeverageWithHook.h Coffee.h \ 4 | CoffeeWithHook.h Tea.h TeaWithHook.h 5 | TARGET = baristaTestDrive 6 | OBJ = $(TARGET).o 7 | 8 | %.o: %.cpp $(DEPS) 9 | $(CXX) -c -o $@ $< $(CFLAGS) 10 | 11 | $(TARGET): $(OBJ) 12 | $(CXX) -o $@ $^ $(CFLAGS) 13 | 14 | clean: 15 | rm -f *.o $(TARGET) 16 | -------------------------------------------------------------------------------- /TemplateMethod/barista/Tea.h: -------------------------------------------------------------------------------- 1 | #ifndef TEA_H 2 | #define TEA_H 3 | 4 | #include "CaffeineBeverage.h" 5 | #include 6 | 7 | class Tea : public CaffeineBeverage { 8 | public: 9 | void brew() override { std::cout << "Steeping the tea\n"; } 10 | void addCondiments() override { std::cout << "Adding Lemon\n"; } 11 | }; 12 | 13 | #endif /* TEA_H */ 14 | -------------------------------------------------------------------------------- /TemplateMethod/barista/TeaWithHook.h: -------------------------------------------------------------------------------- 1 | #ifndef TEA_WITH_HOOK_H 2 | #define TEA_WITH_HOOK_H 3 | 4 | #include "CaffeineBeverageWithHook.h" 5 | #include 6 | #include 7 | #include 8 | 9 | class TeaWithHook : public CaffeineBeverageWithHook { 10 | public: 11 | void brew() override { std::cout << "Steeping the tea\n"; } 12 | void addCondiments() override { std::cout << "Adding Lemon\n"; } 13 | bool customerWantCondiment() override; 14 | std::string getUSerInput(); 15 | }; 16 | 17 | inline 18 | bool 19 | TeaWithHook::customerWantCondiment() 20 | { 21 | std::string answer = getUSerInput(); 22 | if (std::tolower(answer[0]) == 'y') 23 | return true; 24 | return false; 25 | } 26 | 27 | inline 28 | std::string 29 | TeaWithHook::getUSerInput() 30 | { 31 | std::string answer = {}; 32 | std::cout << "Would you like lemon with your tea (y/n)? "; 33 | if (!std::getline(std::cin, answer)) 34 | std::cerr << "IO error trying to read your answer\n"; 35 | if (answer.empty()) 36 | return "no"; 37 | return answer; 38 | } 39 | 40 | #endif /* TEA_WITH_HOOK_H */ 41 | -------------------------------------------------------------------------------- /TemplateMethod/barista/baristaTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "CaffeineBeverage.h" 2 | #include "Coffee.h" 3 | #include "CoffeeWithHook.h" 4 | #include "Tea.h" 5 | #include "TeaWithHook.h" 6 | 7 | int main() 8 | { 9 | auto tea = Tea(); 10 | auto coffee = Coffe(); 11 | 12 | std::cout << "\nMaking tea...\n"; 13 | tea.prepareRecipe(); 14 | 15 | std::cout << "\nMaking coffee...\n"; 16 | coffee.prepareRecipe(); 17 | 18 | auto teaHook = TeaWithHook(); 19 | auto coffeeHook = CoffeeWithHook(); 20 | 21 | std::cout << "\nMaking tea...\n"; 22 | teaHook.prepareRecipe(); 23 | 24 | std::cout << "\nMaking coffee...\n"; 25 | coffeeHook.prepareRecipe(); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /TemplateMethod/sort/Duck.h: -------------------------------------------------------------------------------- 1 | #ifndef DUCK_H 2 | #define DUCK_H 3 | 4 | #include 5 | #include 6 | 7 | /* 8 | * C++ Does not have an equivalent to Java's Comparable class (interface). The standard 9 | * library sort(), however, does employ the 'template' model, albeit not through 10 | * class inheritance. By default sort uses the element's less-than (<) operator 11 | * to compare elements. To take advantage of this one only need to provide an 12 | * overload to the class' operator <, as seen below. 13 | */ 14 | 15 | class Duck { 16 | public: 17 | Duck(const std::string &n, int w) : name(n), weight(w) {} 18 | int getWeight() const { return weight; } 19 | std::string getName() const { return name; } 20 | private: 21 | std::string name; 22 | int weight; 23 | }; 24 | 25 | // overloaded output operator 26 | inline 27 | std::ostream& 28 | operator<<(std::ostream &os, const Duck &duck) 29 | { 30 | os << duck.getName() << " weighs " << duck.getWeight(); 31 | return os; 32 | } 33 | 34 | // overloaded less-than operator 35 | inline 36 | bool 37 | operator<(const Duck &lhs, const Duck &rhs) 38 | { 39 | return lhs.getWeight() < rhs.getWeight(); 40 | } 41 | 42 | #endif /* DUCK_H */ 43 | -------------------------------------------------------------------------------- /TemplateMethod/sort/Makefile: -------------------------------------------------------------------------------- 1 | CXX = clang++ 2 | CFLAGS = -g -Wall -Wextra -Werror -Weffc++ -pedantic-errors -std=c++17 3 | DEPS = Duck.h 4 | TARGET = duckSortTestDrive 5 | OBJ = $(TARGET).o 6 | 7 | %.o: %.cpp $(DEPS) 8 | $(CXX) -c -o $@ $< $(CFLAGS) 9 | 10 | $(TARGET): $(OBJ) 11 | $(CXX) -o $@ $^ $(CFLAGS) 12 | 13 | clean: 14 | rm -f *.o $(TARGET) 15 | -------------------------------------------------------------------------------- /TemplateMethod/sort/duckSortTestDrive.cpp: -------------------------------------------------------------------------------- 1 | #include "Duck.h" 2 | #include 3 | #include 4 | #include 5 | 6 | void display(const std::vector &ducks) 7 | { 8 | for (const auto &duck : ducks) 9 | std::cout << duck << '\n'; 10 | } 11 | 12 | int main() 13 | { 14 | std::vector ducks = { Duck("Daffy", 8), 15 | Duck("Dewey", 2), 16 | Duck("Howard", 7), 17 | Duck("Louie", 2), 18 | Duck("Donald", 10), 19 | Duck("Huey", 2) }; 20 | 21 | std::cout << "Before sorting:\n"; 22 | display(ducks); 23 | 24 | std::sort(ducks.begin(), ducks.end()); 25 | 26 | std::cout << "\nAfter sorting:\n"; 27 | std::cout << "descending order\n"; 28 | display(ducks); 29 | 30 | // Template pattern can be achieved through functional style programming 31 | // through the use of function pointers and lambda's 32 | std::sort(ducks.begin(), ducks.end(), 33 | [] (const Duck &a, const Duck &b) { return a.getWeight() > b.getWeight(); }); 34 | std::cout << "\nAscending order:\n"; 35 | display(ducks); 36 | 37 | return 0; 38 | } 39 | --------------------------------------------------------------------------------