From 41c04d0fd534ff0074f510835ae7f712a39118bb Mon Sep 17 00:00:00 2001 From: d0pare <4062518+d0pare@users.noreply.github.com> Date: Tue, 19 May 2020 22:34:52 -0400 Subject: [PATCH] Initial commit --- .gitignore | 212 ++++++++++++ .vscode/launch.json | 280 ++++++++++++++++ .vscode/tasks.json | 307 ++++++++++++++++++ DesignPatterns.sln | 279 ++++++++++++++++ NuGet.Config | 7 + README.md | 35 ++ global.json | 6 + src/adapter/Ducks/Adapters/DuckAdapter.cs | 32 ++ src/adapter/Ducks/Adapters/TurkeyAdapter.cs | 29 ++ src/adapter/Ducks/Ducks.csproj | 8 + src/adapter/Ducks/Models/Ducks/IDuck.cs | 8 + src/adapter/Ducks/Models/Ducks/MallardDuck.cs | 17 + src/adapter/Ducks/Models/Turkeys/ITurkey.cs | 8 + .../Ducks/Models/Turkeys/WildTurkey.cs | 17 + src/adapter/Ducks/Program.cs | 44 +++ src/adapter/README.md | 2 + .../DuckSimulator/AbstractDuckFactory.cs | 10 + .../DuckSimulator/CountingDuckFactory.cs | 13 + src/combined/DuckSimulator/DecoyDuck.cs | 26 ++ src/combined/DuckSimulator/DuckCall.cs | 26 ++ src/combined/DuckSimulator/DuckFactory.cs | 13 + .../DuckSimulator/DuckSimulator.csproj | 8 + src/combined/DuckSimulator/Flock.cs | 31 ++ src/combined/DuckSimulator/Goose.cs | 11 + src/combined/DuckSimulator/GooseAdapter.cs | 25 ++ src/combined/DuckSimulator/IObserver.cs | 7 + .../DuckSimulator/IQuackObservable.cs | 8 + src/combined/DuckSimulator/IQuackable.cs | 7 + src/combined/DuckSimulator/MallardDuck.cs | 26 ++ src/combined/DuckSimulator/Observable.cs | 27 ++ src/combined/DuckSimulator/Program.cs | 55 ++++ src/combined/DuckSimulator/QuackCounter.cs | 26 ++ src/combined/DuckSimulator/Quackologist.cs | 11 + src/combined/DuckSimulator/RedheadDuck.cs | 26 ++ src/combined/DuckSimulator/RubberDuck.cs | 26 ++ .../Party/Commands/Abstractions/ICommand.cs | 8 + .../Party/Commands/CeilingFanHighCommand.cs | 40 +++ .../Party/Commands/CeilingFanMediumCommand.cs | 40 +++ .../Party/Commands/CeilingFanOffCommand.cs | 40 +++ .../Party/Commands/HottubOffCommand.cs | 22 ++ src/command/Party/Commands/HottubOnCommand.cs | 23 ++ src/command/Party/Commands/LightOffCommand.cs | 18 + src/command/Party/Commands/LightOnCommand.cs | 18 + src/command/Party/Commands/MacroCommand.cs | 28 ++ src/command/Party/Commands/NoCommand.cs | 8 + .../Party/Commands/StereoOffCommand.cs | 18 + src/command/Party/Commands/StereoOnCommand.cs | 18 + .../Party/Commands/StereoOnWithCDCommand.cs | 23 ++ src/command/Party/Commands/TVOffCommand.cs | 18 + src/command/Party/Commands/TVOnCommand.cs | 22 ++ src/command/Party/Invokers/RemoteControl.cs | 63 ++++ src/command/Party/Party.csproj | 8 + src/command/Party/Program.cs | 43 +++ src/command/Party/Receivers/CeilingFan.cs | 50 +++ src/command/Party/Receivers/Hottub.cs | 51 +++ src/command/Party/Receivers/Light.cs | 42 +++ src/command/Party/Receivers/Stereo.cs | 28 ++ src/command/Party/Receivers/TV.cs | 25 ++ src/command/README.md | 2 + .../Commands/Abstractions/ICommand.cs | 7 + .../Commands/CeilingFanOffCommand.cs | 17 + .../Commands/CeilingFanOnCommand.cs | 17 + .../Commands/GarageDoorDownCommand.cs | 17 + .../Commands/GarageDoorUpCommand.cs | 17 + .../Commands/HottubOffCommand.cs | 21 ++ .../RemoteControl/Commands/HottubOnCommand.cs | 22 ++ .../RemoteControl/Commands/LightOffCommand.cs | 17 + .../RemoteControl/Commands/LightOnCommand.cs | 17 + .../Commands/LivingroomLightOffCommand.cs | 17 + .../Commands/LivingroomLightOnCommand.cs | 17 + .../RemoteControl/Commands/NoCommand.cs | 10 + .../Commands/StereoOffCommand.cs | 17 + .../Commands/StereoOnWithCDCommand.cs | 22 ++ .../RemoteControl/Invokers/RemoteControl.cs | 56 ++++ src/command/RemoteControl/Program.cs | 50 +++ .../RemoteControl/Receivers/CeilingFan.cs | 49 +++ .../RemoteControl/Receivers/GarageDoor.cs | 24 ++ src/command/RemoteControl/Receivers/Hottub.cs | 60 ++++ src/command/RemoteControl/Receivers/Light.cs | 18 + src/command/RemoteControl/Receivers/Stereo.cs | 26 ++ src/command/RemoteControl/Receivers/TV.cs | 25 ++ .../RemoteControl/RemoteControl.csproj | 8 + .../Commands/Abstractions/ICommand.cs | 8 + .../Commands/CeilingFanHighCommand.cs | 41 +++ .../Commands/CeilingFanLowCommand.cs | 41 +++ .../Commands/CeilingFanMediumCommand.cs | 41 +++ .../Commands/CeilingFanOffCommand.cs | 41 +++ .../Commands/DimmerLightOffCommand.cs | 25 ++ .../Commands/DimmerLightOnCommand.cs | 24 ++ .../Commands/LightOffCommand.cs | 24 ++ .../Commands/LightOnCommand.cs | 24 ++ .../Commands/NoCommand.cs | 10 + .../Invokers/RemoteControlWithUndo.cs | 63 ++++ src/command/RemoteControlWithUndo/Program.cs | 48 +++ .../Receivers/CeilingFan.cs | 50 +++ .../RemoteControlWithUndo/Receivers/Light.cs | 42 +++ .../RemoteControlWithUndo.csproj | 8 + .../Commands/Abstractions/ICommand.cs | 7 + .../Commands/GarageDoorOpenCommand.cs | 16 + .../Commands/LightOffCommand.cs | 16 + .../Commands/LightOnCommand.cs | 16 + .../Invokers/SimpleRemoteControl.cs | 14 + src/command/SimpleRemoteControl/Program.cs | 26 ++ .../Receivers/GarageDoor.cs | 17 + .../SimpleRemoteControl/Receivers/Light.cs | 11 + .../SimpleRemoteControl.csproj | 8 + src/composite/Menu/Menu.cs | 49 +++ src/composite/Menu/Menu.csproj | 8 + src/composite/Menu/MenuComponent.cs | 58 ++++ src/composite/Menu/MenuItem.cs | 32 ++ src/composite/Menu/Program.cs | 57 ++++ src/composite/Menu/Waitress.cs | 17 + .../MenuIterator/CompositeIterator.cs | 59 ++++ src/composite/MenuIterator/Menu.cs | 55 ++++ src/composite/MenuIterator/MenuComponent.cs | 61 ++++ src/composite/MenuIterator/MenuItem.cs | 38 +++ .../MenuIterator/MenuIterator.csproj | 8 + src/composite/MenuIterator/NullIterator.cs | 18 + src/composite/MenuIterator/Program.cs | 51 +++ src/composite/MenuIterator/Waitress.cs | 38 +++ src/composite/README.md | 2 + src/decorator/README.md | 2 + .../Components/Abstractions/Beverage.cs | 9 + .../StarbuzzCoffee/Components/DarkRoast.cs | 17 + .../StarbuzzCoffee/Components/Espresso.cs | 17 + .../StarbuzzCoffee/Components/HouseBlend.cs | 17 + .../Abstractions/CondimentDecorator.cs | 9 + .../StarbuzzCoffee/Decorators/Mocha.cs | 22 ++ .../StarbuzzCoffee/Decorators/Soy.cs | 22 ++ .../StarbuzzCoffee/Decorators/Whip.cs | 22 ++ src/decorator/StarbuzzCoffee/Program.cs | 27 ++ .../StarbuzzCoffee/StarbuzzCoffee.csproj | 8 + .../HomeTheater/Facade/HomeTheaterFacade.cs | 103 ++++++ src/facade/HomeTheater/HomeTheater.csproj | 8 + src/facade/HomeTheater/Program.cs | 25 ++ .../HomeTheater/Subsystems/Amplifier.cs | 66 ++++ src/facade/HomeTheater/Subsystems/CdPlayer.cs | 71 ++++ .../HomeTheater/Subsystems/DvdPlayer.cs | 81 +++++ .../HomeTheater/Subsystems/PopcornPopper.cs | 34 ++ .../HomeTheater/Subsystems/Projector.cs | 41 +++ src/facade/HomeTheater/Subsystems/Screen.cs | 29 ++ .../HomeTheater/Subsystems/TheaterLights.cs | 34 ++ src/facade/HomeTheater/Subsystems/Tuner.cs | 49 +++ src/facade/README.md | 2 + .../Abstractions/IPizzaIngredientFactory.cs | 14 + .../ChicagoPizzaIngredientFactory.cs | 38 +++ .../NYPizzaIngredientFactory.cs | 38 +++ .../FactoryMethods/Abstractions/PizzaStore.cs | 21 ++ .../FactoryMethods/ChicagoPizzaStore.cs | 32 ++ .../FactoryMethods/NYPizzaStore.cs | 32 ++ .../PizzaStoreAbstractFactory.csproj | 8 + .../Ingredients/Abstractions/ICheese.cs | 7 + .../Ingredients/Abstractions/IClams.cs | 7 + .../Ingredients/Abstractions/IDough.cs | 7 + .../Ingredients/Abstractions/IPepperoni.cs | 7 + .../Ingredients/Abstractions/ISauce.cs | 7 + .../Ingredients/Abstractions/IVeggies.cs | 7 + .../Products/Ingredients/BlackOlives.cs | 10 + .../Products/Ingredients/Eggplant.cs | 10 + .../Products/Ingredients/FreshClams.cs | 10 + .../Products/Ingredients/FrozenClams.cs | 10 + .../Products/Ingredients/Garlic.cs | 10 + .../Products/Ingredients/MarinaraSauce.cs | 10 + .../Products/Ingredients/MozzarellaCheese.cs | 10 + .../Products/Ingredients/Mushroom.cs | 10 + .../Products/Ingredients/Onion.cs | 10 + .../Products/Ingredients/ParmesanCheese.cs | 10 + .../Products/Ingredients/PlumTomatoSauce.cs | 10 + .../Products/Ingredients/RedPepper.cs | 10 + .../Products/Ingredients/ReggianoCheese.cs | 10 + .../Products/Ingredients/SlicedPepperoni.cs | 10 + .../Products/Ingredients/Spinach.cs | 10 + .../Products/Ingredients/ThickCrustDough.cs | 10 + .../Products/Ingredients/ThinCrustDough.cs | 10 + .../Products/Pizzas/Abstractions/Pizza.cs | 75 +++++ .../Products/Pizzas/CheesePizza.cs | 23 ++ .../Products/Pizzas/ClamPizza.cs | 24 ++ .../Products/Pizzas/PepperoniPizza.cs | 25 ++ .../Products/Pizzas/VeggiePizza.cs | 24 ++ .../PizzaStoreAbstractFactory/Program.cs | 39 +++ .../FactoryMethods/Abstractions/PizzaStore.cs | 21 ++ .../FactoryMethods/ChicagoPizzaStore.cs | 31 ++ .../FactoryMethods/NYPizzaStore.cs | 30 ++ .../PizzaStoreFactoryMethod.csproj | 8 + .../Products/Pizzas/Abstractions/Pizza.cs | 56 ++++ .../Pizzas/ChicagoStyleCheesePizza.cs | 22 ++ .../Products/Pizzas/ChicagoStyleClamPizza.cs | 23 ++ .../Pizzas/ChicagoStylePepperoniPizza.cs | 26 ++ .../Pizzas/ChicagoStyleVeggiePizza.cs | 25 ++ .../Products/Pizzas/NYStyleCheesePizza.cs | 16 + .../Products/Pizzas/NYStyleClamPizza.cs | 17 + .../Products/Pizzas/NYStylePepperoniPizza.cs | 21 ++ .../Products/Pizzas/NYStyleVeggiePizza.cs | 26 ++ .../PizzaStoreFactoryMethod/Program.cs | 39 +++ .../PizzaStoreSimpleFactory/PizzaStore.cs | 27 ++ .../PizzaStoreSimpleFactory.csproj | 8 + .../Pizzas/Abstractions/Pizza.cs | 49 +++ .../Pizzas/CheesePizza.cs | 17 + .../Pizzas/ClamPizza.cs | 17 + .../Pizzas/PepperoniPizza.cs | 18 + .../Pizzas/VeggiePizza.cs | 21 ++ .../PizzaStoreSimpleFactory/Program.cs | 21 ++ .../SimpleFactories/SimplePizzaFactory.cs | 29 ++ src/factory/README.md | 6 + .../AlternatingDinerMenuIterator.cs | 26 ++ src/iterator/DinerMerger/DinerMenu.cs | 50 +++ src/iterator/DinerMerger/DinerMenuIterator.cs | 17 + src/iterator/DinerMerger/DinerMerger.csproj | 8 + src/iterator/DinerMerger/IIterator.cs | 8 + src/iterator/DinerMerger/IMenu.cs | 7 + src/iterator/DinerMerger/MenuItem.cs | 25 ++ src/iterator/DinerMerger/PancakeHouseMenu.cs | 41 +++ .../DinerMerger/PancakeHouseMenuIterator.cs | 25 ++ src/iterator/DinerMerger/Program.cs | 24 ++ src/iterator/DinerMerger/Waitress.cs | 70 ++++ src/iterator/DinerMergerCafe/CafeMenu.cs | 33 ++ src/iterator/DinerMergerCafe/DinerMenu.cs | 48 +++ .../DinerMergerCafe/DinerMenuIterator.cs | 34 ++ .../DinerMergerCafe/DinerMergerCafe.csproj | 8 + src/iterator/DinerMergerCafe/IMenu.cs | 9 + src/iterator/DinerMergerCafe/MenuItem.cs | 25 ++ .../DinerMergerCafe/PancakeHouseMenu.cs | 41 +++ src/iterator/DinerMergerCafe/Program.cs | 29 ++ src/iterator/DinerMergerCafe/Waitress.cs | 98 ++++++ src/iterator/README.md | 2 + src/observer/README.md | 2 + .../Observers/Abstractions/IDisplayElement.cs | 7 + .../Observers/CurrentConditionsDisplay.cs | 41 +++ .../Observers/ForecastDisplay.cs | 54 +++ .../Observers/HeatIndexDisplay.cs | 52 +++ .../Observers/StatisticsDisplay.cs | 55 ++++ .../WeatherStation.Observable/Program.cs | 23 ++ .../Subjects/WeatherData.cs | 66 ++++ .../WeatherStation.Observable.csproj | 8 + .../Observers/Abstractions/IDisplayElement.cs | 7 + .../Observers/Abstractions/IObserver.cs | 7 + .../Observers/CurrentConditionsDisplay.cs | 30 ++ .../Observers/ForecastDisplay.cs | 43 +++ .../Observers/HeatIndexDisplay.cs | 41 +++ .../Observers/StatisticsDisplay.cs | 44 +++ src/observer/WeatherStation/Program.cs | 23 ++ .../Subjects/Abstractions/ISubject.cs | 11 + .../WeatherStation/Subjects/WeatherData.cs | 49 +++ .../WeatherStation/WeatherStation.csproj | 8 + src/proxy/README.md | 1 + .../ChocolateBoiler/ChocolateBoiler.cs | 61 ++++ .../ChocolateBoiler/ChocolateBoiler.csproj | 8 + src/singleton/ChocolateBoiler/Program.cs | 18 + .../MultipleSingleton.csproj | 8 + src/singleton/MultipleSingleton/Program.cs | 18 + .../Singletons/ClassicSingleton.cs | 12 + .../Singletons/DclSingleton.cs | 28 ++ .../Singletons/StaticSingleton.cs | 9 + .../Singletons/Subclasses/BaseSingleton.cs | 26 ++ .../Singletons/Subclasses/CoolerSingleton.cs | 9 + .../Singletons/Subclasses/HotterSingleton.cs | 7 + .../Singletons/ThreadSafeSingleton.cs | 26 ++ src/singleton/README.md | 1 + src/state/Gumball/Gumball.csproj | 8 + src/state/Gumball/GumballMachine.cs | 155 +++++++++ src/state/Gumball/Program.cs | 46 +++ src/state/GumballState/GumballMachine.cs | 84 +++++ src/state/GumballState/GumballState.csproj | 8 + src/state/GumballState/Program.cs | 33 ++ .../States/Abstractions/IState.cs | 12 + .../GumballState/States/HasQuarterState.cs | 43 +++ .../GumballState/States/NoQuarterState.cs | 42 +++ src/state/GumballState/States/SoldOutState.cs | 44 +++ src/state/GumballState/States/SoldState.cs | 50 +++ .../GumballStateWinner/GumballMachine.cs | 86 +++++ .../GumballStateWinner.csproj | 8 + src/state/GumballStateWinner/Program.cs | 52 +++ .../States/Abstractions/IState.cs | 12 + .../States/HasQuarterState.cs | 52 +++ .../States/NoQuarterState.cs | 42 +++ .../GumballStateWinner/States/SoldOutState.cs | 44 +++ .../GumballStateWinner/States/SoldState.cs | 50 +++ .../GumballStateWinner/States/WinnerState.cs | 59 ++++ src/state/README.md | 2 + .../Behaviors/Abstractions/IFlyBehavior.cs | 7 + .../Behaviors/Abstractions/IQuackBehavior.cs | 7 + .../MiniDuckSimulator/Behaviors/FlyNoWay.cs | 12 + .../Behaviors/FlyRocketPowered.cs | 12 + .../Behaviors/FlyWithWings.cs | 12 + .../MiniDuckSimulator/Behaviors/MuteQuack.cs | 12 + .../MiniDuckSimulator/Behaviors/Quack.cs | 12 + .../MiniDuckSimulator/Behaviors/Squeak.cs | 12 + .../Ducks/Abstractions/Duck.cs | 32 ++ .../MiniDuckSimulator/Ducks/DecoyDuck.cs | 19 ++ .../MiniDuckSimulator/Ducks/MallardDuck.cs | 19 ++ .../MiniDuckSimulator/Ducks/ModelDuck.cs | 19 ++ .../MiniDuckSimulator/Ducks/RedHeadDuck.cs | 19 ++ .../MiniDuckSimulator/Ducks/RubberDuck.cs | 19 ++ .../MiniDuckSimulator.csproj | 8 + src/strategy/MiniDuckSimulator/Program.cs | 26 ++ src/strategy/README.md | 2 + src/templatemethod/Barista/Barista.csproj | 8 + .../Barista/CaffeineBeverage.cs | 28 ++ .../Barista/CaffeineBeverageWithHook.cs | 33 ++ src/templatemethod/Barista/Coffee.cs | 17 + src/templatemethod/Barista/CoffeeWithHook.cs | 28 ++ src/templatemethod/Barista/Program.cs | 29 ++ src/templatemethod/Barista/Tea.cs | 17 + src/templatemethod/Barista/TeaWithHook.cs | 28 ++ src/templatemethod/DuckSort/Duck.cs | 28 ++ src/templatemethod/DuckSort/DuckSort.csproj | 7 + src/templatemethod/DuckSort/Program.cs | 37 +++ src/templatemethod/README.md | 2 + 308 files changed, 8820 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 DesignPatterns.sln create mode 100644 NuGet.Config create mode 100644 README.md create mode 100644 global.json create mode 100644 src/adapter/Ducks/Adapters/DuckAdapter.cs create mode 100644 src/adapter/Ducks/Adapters/TurkeyAdapter.cs create mode 100644 src/adapter/Ducks/Ducks.csproj create mode 100644 src/adapter/Ducks/Models/Ducks/IDuck.cs create mode 100644 src/adapter/Ducks/Models/Ducks/MallardDuck.cs create mode 100644 src/adapter/Ducks/Models/Turkeys/ITurkey.cs create mode 100644 src/adapter/Ducks/Models/Turkeys/WildTurkey.cs create mode 100644 src/adapter/Ducks/Program.cs create mode 100644 src/adapter/README.md create mode 100644 src/combined/DuckSimulator/AbstractDuckFactory.cs create mode 100644 src/combined/DuckSimulator/CountingDuckFactory.cs create mode 100644 src/combined/DuckSimulator/DecoyDuck.cs create mode 100644 src/combined/DuckSimulator/DuckCall.cs create mode 100644 src/combined/DuckSimulator/DuckFactory.cs create mode 100644 src/combined/DuckSimulator/DuckSimulator.csproj create mode 100644 src/combined/DuckSimulator/Flock.cs create mode 100644 src/combined/DuckSimulator/Goose.cs create mode 100644 src/combined/DuckSimulator/GooseAdapter.cs create mode 100644 src/combined/DuckSimulator/IObserver.cs create mode 100644 src/combined/DuckSimulator/IQuackObservable.cs create mode 100644 src/combined/DuckSimulator/IQuackable.cs create mode 100644 src/combined/DuckSimulator/MallardDuck.cs create mode 100644 src/combined/DuckSimulator/Observable.cs create mode 100644 src/combined/DuckSimulator/Program.cs create mode 100644 src/combined/DuckSimulator/QuackCounter.cs create mode 100644 src/combined/DuckSimulator/Quackologist.cs create mode 100644 src/combined/DuckSimulator/RedheadDuck.cs create mode 100644 src/combined/DuckSimulator/RubberDuck.cs create mode 100644 src/command/Party/Commands/Abstractions/ICommand.cs create mode 100644 src/command/Party/Commands/CeilingFanHighCommand.cs create mode 100644 src/command/Party/Commands/CeilingFanMediumCommand.cs create mode 100644 src/command/Party/Commands/CeilingFanOffCommand.cs create mode 100644 src/command/Party/Commands/HottubOffCommand.cs create mode 100644 src/command/Party/Commands/HottubOnCommand.cs create mode 100644 src/command/Party/Commands/LightOffCommand.cs create mode 100644 src/command/Party/Commands/LightOnCommand.cs create mode 100644 src/command/Party/Commands/MacroCommand.cs create mode 100644 src/command/Party/Commands/NoCommand.cs create mode 100644 src/command/Party/Commands/StereoOffCommand.cs create mode 100644 src/command/Party/Commands/StereoOnCommand.cs create mode 100644 src/command/Party/Commands/StereoOnWithCDCommand.cs create mode 100644 src/command/Party/Commands/TVOffCommand.cs create mode 100644 src/command/Party/Commands/TVOnCommand.cs create mode 100644 src/command/Party/Invokers/RemoteControl.cs create mode 100644 src/command/Party/Party.csproj create mode 100644 src/command/Party/Program.cs create mode 100644 src/command/Party/Receivers/CeilingFan.cs create mode 100644 src/command/Party/Receivers/Hottub.cs create mode 100644 src/command/Party/Receivers/Light.cs create mode 100644 src/command/Party/Receivers/Stereo.cs create mode 100644 src/command/Party/Receivers/TV.cs create mode 100644 src/command/README.md create mode 100644 src/command/RemoteControl/Commands/Abstractions/ICommand.cs create mode 100644 src/command/RemoteControl/Commands/CeilingFanOffCommand.cs create mode 100644 src/command/RemoteControl/Commands/CeilingFanOnCommand.cs create mode 100644 src/command/RemoteControl/Commands/GarageDoorDownCommand.cs create mode 100644 src/command/RemoteControl/Commands/GarageDoorUpCommand.cs create mode 100644 src/command/RemoteControl/Commands/HottubOffCommand.cs create mode 100644 src/command/RemoteControl/Commands/HottubOnCommand.cs create mode 100644 src/command/RemoteControl/Commands/LightOffCommand.cs create mode 100644 src/command/RemoteControl/Commands/LightOnCommand.cs create mode 100644 src/command/RemoteControl/Commands/LivingroomLightOffCommand.cs create mode 100644 src/command/RemoteControl/Commands/LivingroomLightOnCommand.cs create mode 100644 src/command/RemoteControl/Commands/NoCommand.cs create mode 100644 src/command/RemoteControl/Commands/StereoOffCommand.cs create mode 100644 src/command/RemoteControl/Commands/StereoOnWithCDCommand.cs create mode 100644 src/command/RemoteControl/Invokers/RemoteControl.cs create mode 100644 src/command/RemoteControl/Program.cs create mode 100644 src/command/RemoteControl/Receivers/CeilingFan.cs create mode 100644 src/command/RemoteControl/Receivers/GarageDoor.cs create mode 100644 src/command/RemoteControl/Receivers/Hottub.cs create mode 100644 src/command/RemoteControl/Receivers/Light.cs create mode 100644 src/command/RemoteControl/Receivers/Stereo.cs create mode 100644 src/command/RemoteControl/Receivers/TV.cs create mode 100644 src/command/RemoteControl/RemoteControl.csproj create mode 100644 src/command/RemoteControlWithUndo/Commands/Abstractions/ICommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/CeilingFanHighCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/CeilingFanLowCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/CeilingFanMediumCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/CeilingFanOffCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/DimmerLightOffCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/DimmerLightOnCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/LightOffCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/LightOnCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Commands/NoCommand.cs create mode 100644 src/command/RemoteControlWithUndo/Invokers/RemoteControlWithUndo.cs create mode 100644 src/command/RemoteControlWithUndo/Program.cs create mode 100644 src/command/RemoteControlWithUndo/Receivers/CeilingFan.cs create mode 100644 src/command/RemoteControlWithUndo/Receivers/Light.cs create mode 100644 src/command/RemoteControlWithUndo/RemoteControlWithUndo.csproj create mode 100644 src/command/SimpleRemoteControl/Commands/Abstractions/ICommand.cs create mode 100644 src/command/SimpleRemoteControl/Commands/GarageDoorOpenCommand.cs create mode 100644 src/command/SimpleRemoteControl/Commands/LightOffCommand.cs create mode 100644 src/command/SimpleRemoteControl/Commands/LightOnCommand.cs create mode 100644 src/command/SimpleRemoteControl/Invokers/SimpleRemoteControl.cs create mode 100644 src/command/SimpleRemoteControl/Program.cs create mode 100644 src/command/SimpleRemoteControl/Receivers/GarageDoor.cs create mode 100644 src/command/SimpleRemoteControl/Receivers/Light.cs create mode 100644 src/command/SimpleRemoteControl/SimpleRemoteControl.csproj create mode 100644 src/composite/Menu/Menu.cs create mode 100644 src/composite/Menu/Menu.csproj create mode 100644 src/composite/Menu/MenuComponent.cs create mode 100644 src/composite/Menu/MenuItem.cs create mode 100644 src/composite/Menu/Program.cs create mode 100644 src/composite/Menu/Waitress.cs create mode 100644 src/composite/MenuIterator/CompositeIterator.cs create mode 100644 src/composite/MenuIterator/Menu.cs create mode 100644 src/composite/MenuIterator/MenuComponent.cs create mode 100644 src/composite/MenuIterator/MenuItem.cs create mode 100644 src/composite/MenuIterator/MenuIterator.csproj create mode 100644 src/composite/MenuIterator/NullIterator.cs create mode 100644 src/composite/MenuIterator/Program.cs create mode 100644 src/composite/MenuIterator/Waitress.cs create mode 100644 src/composite/README.md create mode 100644 src/decorator/README.md create mode 100644 src/decorator/StarbuzzCoffee/Components/Abstractions/Beverage.cs create mode 100644 src/decorator/StarbuzzCoffee/Components/DarkRoast.cs create mode 100644 src/decorator/StarbuzzCoffee/Components/Espresso.cs create mode 100644 src/decorator/StarbuzzCoffee/Components/HouseBlend.cs create mode 100644 src/decorator/StarbuzzCoffee/Decorators/Abstractions/CondimentDecorator.cs create mode 100644 src/decorator/StarbuzzCoffee/Decorators/Mocha.cs create mode 100644 src/decorator/StarbuzzCoffee/Decorators/Soy.cs create mode 100644 src/decorator/StarbuzzCoffee/Decorators/Whip.cs create mode 100644 src/decorator/StarbuzzCoffee/Program.cs create mode 100644 src/decorator/StarbuzzCoffee/StarbuzzCoffee.csproj create mode 100644 src/facade/HomeTheater/Facade/HomeTheaterFacade.cs create mode 100644 src/facade/HomeTheater/HomeTheater.csproj create mode 100644 src/facade/HomeTheater/Program.cs create mode 100644 src/facade/HomeTheater/Subsystems/Amplifier.cs create mode 100644 src/facade/HomeTheater/Subsystems/CdPlayer.cs create mode 100644 src/facade/HomeTheater/Subsystems/DvdPlayer.cs create mode 100644 src/facade/HomeTheater/Subsystems/PopcornPopper.cs create mode 100644 src/facade/HomeTheater/Subsystems/Projector.cs create mode 100644 src/facade/HomeTheater/Subsystems/Screen.cs create mode 100644 src/facade/HomeTheater/Subsystems/TheaterLights.cs create mode 100644 src/facade/HomeTheater/Subsystems/Tuner.cs create mode 100644 src/facade/README.md create mode 100644 src/factory/PizzaStoreAbstractFactory/AbstractFactories/Abstractions/IPizzaIngredientFactory.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/AbstractFactories/ChicagoPizzaIngredientFactory.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/AbstractFactories/NYPizzaIngredientFactory.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/FactoryMethods/Abstractions/PizzaStore.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/FactoryMethods/ChicagoPizzaStore.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/FactoryMethods/NYPizzaStore.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/PizzaStoreAbstractFactory.csproj create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/ICheese.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IClams.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IDough.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IPepperoni.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/ISauce.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IVeggies.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/BlackOlives.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Eggplant.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/FreshClams.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/FrozenClams.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Garlic.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/MarinaraSauce.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/MozzarellaCheese.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Mushroom.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Onion.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ParmesanCheese.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/PlumTomatoSauce.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/RedPepper.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ReggianoCheese.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/SlicedPepperoni.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Spinach.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ThickCrustDough.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ThinCrustDough.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Pizzas/Abstractions/Pizza.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Pizzas/CheesePizza.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Pizzas/ClamPizza.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Pizzas/PepperoniPizza.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Products/Pizzas/VeggiePizza.cs create mode 100644 src/factory/PizzaStoreAbstractFactory/Program.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/FactoryMethods/Abstractions/PizzaStore.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/FactoryMethods/ChicagoPizzaStore.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/FactoryMethods/NYPizzaStore.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/PizzaStoreFactoryMethod.csproj create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/Abstractions/Pizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleCheesePizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleClamPizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStylePepperoniPizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleVeggiePizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleCheesePizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleClamPizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStylePepperoniPizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleVeggiePizza.cs create mode 100644 src/factory/PizzaStoreFactoryMethod/Program.cs create mode 100644 src/factory/PizzaStoreSimpleFactory/PizzaStore.cs create mode 100644 src/factory/PizzaStoreSimpleFactory/PizzaStoreSimpleFactory.csproj create mode 100644 src/factory/PizzaStoreSimpleFactory/Pizzas/Abstractions/Pizza.cs create mode 100644 src/factory/PizzaStoreSimpleFactory/Pizzas/CheesePizza.cs create mode 100644 src/factory/PizzaStoreSimpleFactory/Pizzas/ClamPizza.cs create mode 100644 src/factory/PizzaStoreSimpleFactory/Pizzas/PepperoniPizza.cs create mode 100644 src/factory/PizzaStoreSimpleFactory/Pizzas/VeggiePizza.cs create mode 100644 src/factory/PizzaStoreSimpleFactory/Program.cs create mode 100644 src/factory/PizzaStoreSimpleFactory/SimpleFactories/SimplePizzaFactory.cs create mode 100644 src/factory/README.md create mode 100644 src/iterator/DinerMerger/AlternatingDinerMenuIterator.cs create mode 100644 src/iterator/DinerMerger/DinerMenu.cs create mode 100644 src/iterator/DinerMerger/DinerMenuIterator.cs create mode 100644 src/iterator/DinerMerger/DinerMerger.csproj create mode 100644 src/iterator/DinerMerger/IIterator.cs create mode 100644 src/iterator/DinerMerger/IMenu.cs create mode 100644 src/iterator/DinerMerger/MenuItem.cs create mode 100644 src/iterator/DinerMerger/PancakeHouseMenu.cs create mode 100644 src/iterator/DinerMerger/PancakeHouseMenuIterator.cs create mode 100644 src/iterator/DinerMerger/Program.cs create mode 100644 src/iterator/DinerMerger/Waitress.cs create mode 100644 src/iterator/DinerMergerCafe/CafeMenu.cs create mode 100644 src/iterator/DinerMergerCafe/DinerMenu.cs create mode 100644 src/iterator/DinerMergerCafe/DinerMenuIterator.cs create mode 100644 src/iterator/DinerMergerCafe/DinerMergerCafe.csproj create mode 100644 src/iterator/DinerMergerCafe/IMenu.cs create mode 100644 src/iterator/DinerMergerCafe/MenuItem.cs create mode 100644 src/iterator/DinerMergerCafe/PancakeHouseMenu.cs create mode 100644 src/iterator/DinerMergerCafe/Program.cs create mode 100644 src/iterator/DinerMergerCafe/Waitress.cs create mode 100644 src/iterator/README.md create mode 100644 src/observer/README.md create mode 100644 src/observer/WeatherStation.Observable/Observers/Abstractions/IDisplayElement.cs create mode 100644 src/observer/WeatherStation.Observable/Observers/CurrentConditionsDisplay.cs create mode 100644 src/observer/WeatherStation.Observable/Observers/ForecastDisplay.cs create mode 100644 src/observer/WeatherStation.Observable/Observers/HeatIndexDisplay.cs create mode 100644 src/observer/WeatherStation.Observable/Observers/StatisticsDisplay.cs create mode 100644 src/observer/WeatherStation.Observable/Program.cs create mode 100644 src/observer/WeatherStation.Observable/Subjects/WeatherData.cs create mode 100644 src/observer/WeatherStation.Observable/WeatherStation.Observable.csproj create mode 100644 src/observer/WeatherStation/Observers/Abstractions/IDisplayElement.cs create mode 100644 src/observer/WeatherStation/Observers/Abstractions/IObserver.cs create mode 100644 src/observer/WeatherStation/Observers/CurrentConditionsDisplay.cs create mode 100644 src/observer/WeatherStation/Observers/ForecastDisplay.cs create mode 100644 src/observer/WeatherStation/Observers/HeatIndexDisplay.cs create mode 100644 src/observer/WeatherStation/Observers/StatisticsDisplay.cs create mode 100644 src/observer/WeatherStation/Program.cs create mode 100644 src/observer/WeatherStation/Subjects/Abstractions/ISubject.cs create mode 100644 src/observer/WeatherStation/Subjects/WeatherData.cs create mode 100644 src/observer/WeatherStation/WeatherStation.csproj create mode 100644 src/proxy/README.md create mode 100644 src/singleton/ChocolateBoiler/ChocolateBoiler.cs create mode 100644 src/singleton/ChocolateBoiler/ChocolateBoiler.csproj create mode 100644 src/singleton/ChocolateBoiler/Program.cs create mode 100644 src/singleton/MultipleSingleton/MultipleSingleton.csproj create mode 100644 src/singleton/MultipleSingleton/Program.cs create mode 100644 src/singleton/MultipleSingleton/Singletons/ClassicSingleton.cs create mode 100644 src/singleton/MultipleSingleton/Singletons/DclSingleton.cs create mode 100644 src/singleton/MultipleSingleton/Singletons/StaticSingleton.cs create mode 100644 src/singleton/MultipleSingleton/Singletons/Subclasses/BaseSingleton.cs create mode 100644 src/singleton/MultipleSingleton/Singletons/Subclasses/CoolerSingleton.cs create mode 100644 src/singleton/MultipleSingleton/Singletons/Subclasses/HotterSingleton.cs create mode 100644 src/singleton/MultipleSingleton/Singletons/ThreadSafeSingleton.cs create mode 100644 src/singleton/README.md create mode 100644 src/state/Gumball/Gumball.csproj create mode 100644 src/state/Gumball/GumballMachine.cs create mode 100644 src/state/Gumball/Program.cs create mode 100644 src/state/GumballState/GumballMachine.cs create mode 100644 src/state/GumballState/GumballState.csproj create mode 100644 src/state/GumballState/Program.cs create mode 100644 src/state/GumballState/States/Abstractions/IState.cs create mode 100644 src/state/GumballState/States/HasQuarterState.cs create mode 100644 src/state/GumballState/States/NoQuarterState.cs create mode 100644 src/state/GumballState/States/SoldOutState.cs create mode 100644 src/state/GumballState/States/SoldState.cs create mode 100644 src/state/GumballStateWinner/GumballMachine.cs create mode 100644 src/state/GumballStateWinner/GumballStateWinner.csproj create mode 100644 src/state/GumballStateWinner/Program.cs create mode 100644 src/state/GumballStateWinner/States/Abstractions/IState.cs create mode 100644 src/state/GumballStateWinner/States/HasQuarterState.cs create mode 100644 src/state/GumballStateWinner/States/NoQuarterState.cs create mode 100644 src/state/GumballStateWinner/States/SoldOutState.cs create mode 100644 src/state/GumballStateWinner/States/SoldState.cs create mode 100644 src/state/GumballStateWinner/States/WinnerState.cs create mode 100644 src/state/README.md create mode 100644 src/strategy/MiniDuckSimulator/Behaviors/Abstractions/IFlyBehavior.cs create mode 100644 src/strategy/MiniDuckSimulator/Behaviors/Abstractions/IQuackBehavior.cs create mode 100644 src/strategy/MiniDuckSimulator/Behaviors/FlyNoWay.cs create mode 100644 src/strategy/MiniDuckSimulator/Behaviors/FlyRocketPowered.cs create mode 100644 src/strategy/MiniDuckSimulator/Behaviors/FlyWithWings.cs create mode 100644 src/strategy/MiniDuckSimulator/Behaviors/MuteQuack.cs create mode 100644 src/strategy/MiniDuckSimulator/Behaviors/Quack.cs create mode 100644 src/strategy/MiniDuckSimulator/Behaviors/Squeak.cs create mode 100644 src/strategy/MiniDuckSimulator/Ducks/Abstractions/Duck.cs create mode 100644 src/strategy/MiniDuckSimulator/Ducks/DecoyDuck.cs create mode 100644 src/strategy/MiniDuckSimulator/Ducks/MallardDuck.cs create mode 100644 src/strategy/MiniDuckSimulator/Ducks/ModelDuck.cs create mode 100644 src/strategy/MiniDuckSimulator/Ducks/RedHeadDuck.cs create mode 100644 src/strategy/MiniDuckSimulator/Ducks/RubberDuck.cs create mode 100644 src/strategy/MiniDuckSimulator/MiniDuckSimulator.csproj create mode 100644 src/strategy/MiniDuckSimulator/Program.cs create mode 100644 src/strategy/README.md create mode 100644 src/templatemethod/Barista/Barista.csproj create mode 100644 src/templatemethod/Barista/CaffeineBeverage.cs create mode 100644 src/templatemethod/Barista/CaffeineBeverageWithHook.cs create mode 100644 src/templatemethod/Barista/Coffee.cs create mode 100644 src/templatemethod/Barista/CoffeeWithHook.cs create mode 100644 src/templatemethod/Barista/Program.cs create mode 100644 src/templatemethod/Barista/Tea.cs create mode 100644 src/templatemethod/Barista/TeaWithHook.cs create mode 100644 src/templatemethod/DuckSort/Duck.cs create mode 100644 src/templatemethod/DuckSort/DuckSort.csproj create mode 100644 src/templatemethod/DuckSort/Program.cs create mode 100644 src/templatemethod/README.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b06e864 --- /dev/null +++ b/.gitignore @@ -0,0 +1,212 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +build/ +bld/ +[Bb]in/ +[Oo]bj/ + +# Visual Studio 2015 cache/options directory +.vs/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +## TODO: Comment the next line if you want to checkin your +## web deploy settings but do note that will include unencrypted +## passwords +#*.pubxml + +*.publishproj + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config + +# Windows Azure Build Output +csx/ +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# LightSwitch generated files +GeneratedArtifacts/ +_Pvt_Extensions/ +ModelManifest.xml diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..6e84eb2 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,280 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Adapter - Ducks", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-adapter-ducks", + "program": "${workspaceRoot}/src/adapter/Ducks/bin/Debug/netcoreapp3.1/Ducks.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Combined - DuckSimulator", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-combined-ducksimulator", + "program": "${workspaceRoot}/src/combined/DuckSimulator/bin/Debug/netcoreapp3.1/DuckSimulator.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Command - Party", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-command-party", + "program": "${workspaceRoot}/src/command/Party/bin/Debug/netcoreapp3.1/Party.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Command - RemoteControl", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-command-remotecontrol", + "program": "${workspaceRoot}/src/command/RemoteControl/bin/Debug/netcoreapp3.1/RemoteControl.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Command - RemoteControlWithUndo", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-command-remotecontrolwithundo", + "program": "${workspaceRoot}/src/command/RemoteControlWithUndo/bin/Debug/netcoreapp3.1/RemoteControlWithUndo.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Command - SimpleRemoteControl", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-command-simpleremotecontrol", + "program": "${workspaceRoot}/src/command/SimpleRemoteControl/bin/Debug/netcoreapp3.1/SimpleRemoteControl.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Composite - Menu", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-composite-menu", + "program": "${workspaceRoot}/src/composite/Menu/bin/Debug/netcoreapp3.1/Menu.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Composite - MenuIterator", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-composite-menuiterator", + "program": "${workspaceRoot}/src/composite/MenuIterator/bin/Debug/netcoreapp3.1/MenuIterator.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Decorator - StarbuzzCoffee", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-decorator-starbuzzcoffee", + "program": "${workspaceRoot}/src/decorator/StarbuzzCoffee/bin/Debug/netcoreapp3.1/StarbuzzCoffee.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Facade - HomeTheater", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-facade-hometheater", + "program": "${workspaceRoot}/src/facade/HomeTheater/bin/Debug/netcoreapp3.1/HomeTheater.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Factory - PizzaStoreAbstractFactory", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-factory-pizzastoreabstractfactory", + "program": "${workspaceRoot}/src/factory/PizzaStoreAbstractFactory/bin/Debug/netcoreapp3.1/PizzaStoreAbstractFactory.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Factory - PizzaStoreFactoryMethod", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-factory-pizzastorefactorymethod", + "program": "${workspaceRoot}/src/factory/PizzaStoreFactoryMethod/bin/Debug/netcoreapp3.1/PizzaStoreFactoryMethod.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Factory - PizzaStoreSimpleFactory", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-factory-pizzastoresimplefactory", + "program": "${workspaceRoot}/src/factory/PizzaStoreSimpleFactory/bin/Debug/netcoreapp3.1/PizzaStoreSimpleFactory.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Iterator - DinerMerger", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-iterator-dinermerger", + "program": "${workspaceRoot}/src/iterator/DinerMerger/bin/Debug/netcoreapp3.1/DinerMerger.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Iterator - DinerMergerCafe", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-iterator-dinermergercafe", + "program": "${workspaceRoot}/src/iterator/DinerMergerCafe/bin/Debug/netcoreapp3.1/DinerMergerCafe.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Observer - WeatherStation", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-observer-weatherstation", + "program": "${workspaceRoot}/src/observer/WeatherStation/bin/Debug/netcoreapp3.1/WeatherStation.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Observer - WeatherStation.Observable", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-observer-weatherstation.observable", + "program": "${workspaceRoot}/src/observer/WeatherStation.Observable/bin/Debug/netcoreapp3.1/WeatherStation.Observable.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Singleton - ChocolateBoiler", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-singleton-chocolateboiler", + "program": "${workspaceRoot}/src/singleton/ChocolateBoiler/bin/Debug/netcoreapp3.1/ChocolateBoiler.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Singleton - MultipleSingleton", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-singleton-multiplesingleton", + "program": "${workspaceRoot}/src/singleton/MultipleSingleton/bin/Debug/netcoreapp3.1/MultipleSingleton.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "State - Gumball", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-state-gumball", + "program": "${workspaceRoot}/src/state/Gumball/bin/Debug/netcoreapp3.1/Gumball.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "State - GumballState", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-state-gumballstate", + "program": "${workspaceRoot}/src/state/GumballState/bin/Debug/netcoreapp3.1/GumballState.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "State - GumballStateWinner", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-state-gumballstatewinner", + "program": "${workspaceRoot}/src/state/GumballStateWinner/bin/Debug/netcoreapp3.1/GumballStateWinner.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Strategy - MiniDuckSimulator", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-strategy-miniducksimulator", + "program": "${workspaceRoot}/src/strategy/MiniDuckSimulator/bin/Debug/netcoreapp3.1/MiniDuckSimulator.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Template Method - Barista", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-templatemethod-barista", + "program": "${workspaceRoot}/src/templatemethod/Barista/bin/Debug/netcoreapp3.1/Barista.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + }, + { + "name": "Template Method - DuckSort", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build-templatemethod-ducksort", + "program": "${workspaceRoot}/src/templatemethod/DuckSort/bin/Debug/netcoreapp3.1/DuckSort.dll", + "args": [], + "cwd": "${workspaceRoot}", + "stopAtEntry": false, + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..87807c4 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,307 @@ +{ + "version": "2.0.0", + "command": "dotnet", + "args": [], + "tasks": [ + { + "label": "build-adapter-ducks", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/adapter/Ducks/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-combined-ducksimulator", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/combined/DuckSimulator/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-command-party", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/command/Party/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-command-remotecontrol", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/command/RemoteControl/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-command-remotecontrolwithundo", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/command/RemoteControlWithUndo/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-command-simpleremotecontrol", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/command/SimpleRemoteControl/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-composite-menu", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/composite/Menu/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-composite-menuiterator", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/composite/MenuIterator/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-decorator-starbuzzcoffee", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/decorator/StarbuzzCofee/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-facade-hometheater", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/facade/HomeTheater/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-factory-pizzastoreabstractfactory", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/factory/PizzaStoreAbstractFactory/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-factory-pizzastorefactorymethod", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/factory/PizzaStoreFactoryMethod/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-factory-pizzastoresimplefactory", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/factory/PizzaStoreSimpleFactory/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-iterator-dinermerger", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/iterator/DinerMerger/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-iterator-dinermergercafe", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/iterator/DinerMergerCafe/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-observer-weatherstation", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/observer/WeatherStation/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-observer-weatherstation.observable", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/observer/WeatherStation.Observable/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-singleton-chocolateboiler", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/singleton/ChocolateBoiler/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-singleton-multiplesingleton", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/singleton/MultipleSingleton/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-state-gumball", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/state/Gumball/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-state-gumballstate", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/state/GumballState/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-state-gumballstatewinner", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/state/GumballStateWinner/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-strategy-miniducksimulator", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/strategy/MiniDuckSimulator/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-templatemethod-barista", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/templatemethod/Barista/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "build-templatemethod-ducksort", + "type": "process", + "command": "dotnet", + "args": [ + "build", + "./src/templatemethod/DuckSort/", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/DesignPatterns.sln b/DesignPatterns.sln new file mode 100644 index 0000000..025d4ac --- /dev/null +++ b/DesignPatterns.sln @@ -0,0 +1,279 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27705.2000 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{65062D4B-4A78-482E-938E-F1E2E2F1BECE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ducks", "src\adapter\Ducks\Ducks.csproj", "{F6540DE3-EB82-49B1-B203-AF931FB55232}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "adapter", "adapter", "{5738C963-92E0-4276-8987-A4E20408112A}" + ProjectSection(SolutionItems) = preProject + src\adapter\README.md = src\adapter\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "combined", "combined", "{509A32F9-B946-4DBF-B82C-E2E8F86B6E11}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "command", "command", "{C8C88EC8-657D-4FDA-8CBC-4ED5CE77706B}" + ProjectSection(SolutionItems) = preProject + src\command\README.md = src\command\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "composite", "composite", "{25CF7B6C-F643-477A-A905-D53E6E0E40DA}" + ProjectSection(SolutionItems) = preProject + src\composite\README.md = src\composite\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "decorator", "decorator", "{FBDDC616-8140-4B07-8C0A-BF30577FB605}" + ProjectSection(SolutionItems) = preProject + src\decorator\README.md = src\decorator\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "facade", "facade", "{829AAE4D-2504-4C96-A64E-613A6F1BB061}" + ProjectSection(SolutionItems) = preProject + src\facade\README.md = src\facade\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "factory", "factory", "{096572AF-69B2-4784-91D3-A4714A3D2982}" + ProjectSection(SolutionItems) = preProject + src\factory\README.md = src\factory\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "iterator", "iterator", "{10450979-D8EC-4CAE-88C3-215BDB7D379B}" + ProjectSection(SolutionItems) = preProject + src\iterator\README.md = src\iterator\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "observer", "observer", "{575C6C3C-8E6F-4869-BA9B-C1F2E29DE4EB}" + ProjectSection(SolutionItems) = preProject + src\observer\README.md = src\observer\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "proxy", "proxy", "{8F06ADF3-A68C-4E58-BC21-3CF7AFD0E116}" + ProjectSection(SolutionItems) = preProject + src\proxy\README.md = src\proxy\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "singleton", "singleton", "{AC7FC415-3583-40E4-B34A-10E3A74503E3}" + ProjectSection(SolutionItems) = preProject + src\singleton\README.md = src\singleton\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "state", "state", "{AF390EAF-182F-4801-8E38-7B549E1B3C9C}" + ProjectSection(SolutionItems) = preProject + src\state\README.md = src\state\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "strategy", "strategy", "{218C089F-D1AA-4773-AA1F-CC65640CFE90}" + ProjectSection(SolutionItems) = preProject + src\strategy\README.md = src\strategy\README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "templatemethod", "templatemethod", "{1281B167-C550-442C-8D64-1A3C225BD53C}" + ProjectSection(SolutionItems) = preProject + src\templatemethod\README.md = src\templatemethod\README.md + EndProjectSection +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DuckSimulator", "src\combined\DuckSimulator\DuckSimulator.csproj", "{E06DF4BE-C600-47C1-9FD6-8EA6E22D574F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Party", "src\command\Party\Party.csproj", "{F61F6444-1C7A-4AC0-B897-EE3DF3BA470D}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RemoteControl", "src\command\RemoteControl\RemoteControl.csproj", "{E7200A68-221E-46F7-8D10-95E4D45A120B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RemoteControlWithUndo", "src\command\RemoteControlWithUndo\RemoteControlWithUndo.csproj", "{F1A47B58-0DC6-4465-9781-E046FCF720A0}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SimpleRemoteControl", "src\command\SimpleRemoteControl\SimpleRemoteControl.csproj", "{7C1207C7-EC00-4FA4-A796-79125C1163C7}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Menu", "src\composite\Menu\Menu.csproj", "{C2187BAB-4FF2-4918-9996-38D04534D443}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MenuIterator", "src\composite\MenuIterator\MenuIterator.csproj", "{2A7691A4-C548-499F-8891-9E648F5AA498}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StarbuzzCoffee", "src\decorator\StarbuzzCoffee\StarbuzzCoffee.csproj", "{BD337891-E563-4B48-8F16-658BED78710E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HomeTheater", "src\facade\HomeTheater\HomeTheater.csproj", "{B4792F59-0CDB-4ED3-A062-7DF93ED9C4C4}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzaStoreAbstractFactory", "src\factory\PizzaStoreAbstractFactory\PizzaStoreAbstractFactory.csproj", "{6D01DEF2-749F-4BF4-B50B-70CCB81CE7E2}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzaStoreFactoryMethod", "src\factory\PizzaStoreFactoryMethod\PizzaStoreFactoryMethod.csproj", "{4299E4CA-21ED-4783-8734-B8DBB8ED3230}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PizzaStoreSimpleFactory", "src\factory\PizzaStoreSimpleFactory\PizzaStoreSimpleFactory.csproj", "{5A46FE1F-A957-4C9F-A9F7-6718ADB9115A}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerMergerCafe", "src\iterator\DinerMergerCafe\DinerMergerCafe.csproj", "{9C48DFA7-88AF-4796-B195-4BE0BD88144E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DinerMerger", "src\iterator\DinerMerger\DinerMerger.csproj", "{3FCF64FD-C8A7-45D0-978B-8F71FA9DD7F2}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WeatherStation", "src\observer\WeatherStation\WeatherStation.csproj", "{ED3FA3B9-B51A-4894-BA75-60E6F8C9446E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WeatherStation.Observable", "src\observer\WeatherStation.Observable\WeatherStation.Observable.csproj", "{5395E547-70DD-4A95-AF78-907E8D665E60}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChocolateBoiler", "src\singleton\ChocolateBoiler\ChocolateBoiler.csproj", "{3D9CC0F4-1EAE-4FBF-AC38-9A6A91002DCC}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MultipleSingleton", "src\singleton\MultipleSingleton\MultipleSingleton.csproj", "{1D415773-E5E4-4272-B5BA-7BE8896AA30B}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Gumball", "src\state\Gumball\Gumball.csproj", "{4B390C4C-F9F4-40FC-839E-450650644E90}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GumballState", "src\state\GumballState\GumballState.csproj", "{89ABCF8C-9DCC-41B4-9098-495B592C467F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GumballStateWinner", "src\state\GumballStateWinner\GumballStateWinner.csproj", "{73B04201-2729-45A4-B280-C682F905D59F}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MiniDuckSimulator", "src\strategy\MiniDuckSimulator\MiniDuckSimulator.csproj", "{31FF343A-D429-4BAC-A405-BAA91E12EAC8}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Barista", "src\templatemethod\Barista\Barista.csproj", "{86BC7597-7E02-4CD7-B9FC-5C2D62D3F105}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DuckSort", "src\templatemethod\DuckSort\DuckSort.csproj", "{F407757D-F442-409C-A1FC-0DBBC51823BB}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F6540DE3-EB82-49B1-B203-AF931FB55232}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F6540DE3-EB82-49B1-B203-AF931FB55232}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F6540DE3-EB82-49B1-B203-AF931FB55232}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F6540DE3-EB82-49B1-B203-AF931FB55232}.Release|Any CPU.Build.0 = Release|Any CPU + {E06DF4BE-C600-47C1-9FD6-8EA6E22D574F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E06DF4BE-C600-47C1-9FD6-8EA6E22D574F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E06DF4BE-C600-47C1-9FD6-8EA6E22D574F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E06DF4BE-C600-47C1-9FD6-8EA6E22D574F}.Release|Any CPU.Build.0 = Release|Any CPU + {F61F6444-1C7A-4AC0-B897-EE3DF3BA470D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F61F6444-1C7A-4AC0-B897-EE3DF3BA470D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F61F6444-1C7A-4AC0-B897-EE3DF3BA470D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F61F6444-1C7A-4AC0-B897-EE3DF3BA470D}.Release|Any CPU.Build.0 = Release|Any CPU + {E7200A68-221E-46F7-8D10-95E4D45A120B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E7200A68-221E-46F7-8D10-95E4D45A120B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E7200A68-221E-46F7-8D10-95E4D45A120B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E7200A68-221E-46F7-8D10-95E4D45A120B}.Release|Any CPU.Build.0 = Release|Any CPU + {F1A47B58-0DC6-4465-9781-E046FCF720A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F1A47B58-0DC6-4465-9781-E046FCF720A0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F1A47B58-0DC6-4465-9781-E046FCF720A0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F1A47B58-0DC6-4465-9781-E046FCF720A0}.Release|Any CPU.Build.0 = Release|Any CPU + {7C1207C7-EC00-4FA4-A796-79125C1163C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7C1207C7-EC00-4FA4-A796-79125C1163C7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7C1207C7-EC00-4FA4-A796-79125C1163C7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7C1207C7-EC00-4FA4-A796-79125C1163C7}.Release|Any CPU.Build.0 = Release|Any CPU + {C2187BAB-4FF2-4918-9996-38D04534D443}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C2187BAB-4FF2-4918-9996-38D04534D443}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C2187BAB-4FF2-4918-9996-38D04534D443}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C2187BAB-4FF2-4918-9996-38D04534D443}.Release|Any CPU.Build.0 = Release|Any CPU + {2A7691A4-C548-499F-8891-9E648F5AA498}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2A7691A4-C548-499F-8891-9E648F5AA498}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2A7691A4-C548-499F-8891-9E648F5AA498}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2A7691A4-C548-499F-8891-9E648F5AA498}.Release|Any CPU.Build.0 = Release|Any CPU + {BD337891-E563-4B48-8F16-658BED78710E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {BD337891-E563-4B48-8F16-658BED78710E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {BD337891-E563-4B48-8F16-658BED78710E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {BD337891-E563-4B48-8F16-658BED78710E}.Release|Any CPU.Build.0 = Release|Any CPU + {B4792F59-0CDB-4ED3-A062-7DF93ED9C4C4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B4792F59-0CDB-4ED3-A062-7DF93ED9C4C4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B4792F59-0CDB-4ED3-A062-7DF93ED9C4C4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B4792F59-0CDB-4ED3-A062-7DF93ED9C4C4}.Release|Any CPU.Build.0 = Release|Any CPU + {6D01DEF2-749F-4BF4-B50B-70CCB81CE7E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6D01DEF2-749F-4BF4-B50B-70CCB81CE7E2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6D01DEF2-749F-4BF4-B50B-70CCB81CE7E2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6D01DEF2-749F-4BF4-B50B-70CCB81CE7E2}.Release|Any CPU.Build.0 = Release|Any CPU + {4299E4CA-21ED-4783-8734-B8DBB8ED3230}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4299E4CA-21ED-4783-8734-B8DBB8ED3230}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4299E4CA-21ED-4783-8734-B8DBB8ED3230}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4299E4CA-21ED-4783-8734-B8DBB8ED3230}.Release|Any CPU.Build.0 = Release|Any CPU + {5A46FE1F-A957-4C9F-A9F7-6718ADB9115A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5A46FE1F-A957-4C9F-A9F7-6718ADB9115A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5A46FE1F-A957-4C9F-A9F7-6718ADB9115A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5A46FE1F-A957-4C9F-A9F7-6718ADB9115A}.Release|Any CPU.Build.0 = Release|Any CPU + {9C48DFA7-88AF-4796-B195-4BE0BD88144E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9C48DFA7-88AF-4796-B195-4BE0BD88144E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9C48DFA7-88AF-4796-B195-4BE0BD88144E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9C48DFA7-88AF-4796-B195-4BE0BD88144E}.Release|Any CPU.Build.0 = Release|Any CPU + {3FCF64FD-C8A7-45D0-978B-8F71FA9DD7F2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3FCF64FD-C8A7-45D0-978B-8F71FA9DD7F2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3FCF64FD-C8A7-45D0-978B-8F71FA9DD7F2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3FCF64FD-C8A7-45D0-978B-8F71FA9DD7F2}.Release|Any CPU.Build.0 = Release|Any CPU + {ED3FA3B9-B51A-4894-BA75-60E6F8C9446E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {ED3FA3B9-B51A-4894-BA75-60E6F8C9446E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {ED3FA3B9-B51A-4894-BA75-60E6F8C9446E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {ED3FA3B9-B51A-4894-BA75-60E6F8C9446E}.Release|Any CPU.Build.0 = Release|Any CPU + {5395E547-70DD-4A95-AF78-907E8D665E60}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5395E547-70DD-4A95-AF78-907E8D665E60}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5395E547-70DD-4A95-AF78-907E8D665E60}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5395E547-70DD-4A95-AF78-907E8D665E60}.Release|Any CPU.Build.0 = Release|Any CPU + {3D9CC0F4-1EAE-4FBF-AC38-9A6A91002DCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3D9CC0F4-1EAE-4FBF-AC38-9A6A91002DCC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3D9CC0F4-1EAE-4FBF-AC38-9A6A91002DCC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3D9CC0F4-1EAE-4FBF-AC38-9A6A91002DCC}.Release|Any CPU.Build.0 = Release|Any CPU + {1D415773-E5E4-4272-B5BA-7BE8896AA30B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1D415773-E5E4-4272-B5BA-7BE8896AA30B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1D415773-E5E4-4272-B5BA-7BE8896AA30B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1D415773-E5E4-4272-B5BA-7BE8896AA30B}.Release|Any CPU.Build.0 = Release|Any CPU + {4B390C4C-F9F4-40FC-839E-450650644E90}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B390C4C-F9F4-40FC-839E-450650644E90}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B390C4C-F9F4-40FC-839E-450650644E90}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B390C4C-F9F4-40FC-839E-450650644E90}.Release|Any CPU.Build.0 = Release|Any CPU + {89ABCF8C-9DCC-41B4-9098-495B592C467F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {89ABCF8C-9DCC-41B4-9098-495B592C467F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {89ABCF8C-9DCC-41B4-9098-495B592C467F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {89ABCF8C-9DCC-41B4-9098-495B592C467F}.Release|Any CPU.Build.0 = Release|Any CPU + {73B04201-2729-45A4-B280-C682F905D59F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {73B04201-2729-45A4-B280-C682F905D59F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {73B04201-2729-45A4-B280-C682F905D59F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {73B04201-2729-45A4-B280-C682F905D59F}.Release|Any CPU.Build.0 = Release|Any CPU + {31FF343A-D429-4BAC-A405-BAA91E12EAC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {31FF343A-D429-4BAC-A405-BAA91E12EAC8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {31FF343A-D429-4BAC-A405-BAA91E12EAC8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {31FF343A-D429-4BAC-A405-BAA91E12EAC8}.Release|Any CPU.Build.0 = Release|Any CPU + {86BC7597-7E02-4CD7-B9FC-5C2D62D3F105}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {86BC7597-7E02-4CD7-B9FC-5C2D62D3F105}.Debug|Any CPU.Build.0 = Debug|Any CPU + {86BC7597-7E02-4CD7-B9FC-5C2D62D3F105}.Release|Any CPU.ActiveCfg = Release|Any CPU + {86BC7597-7E02-4CD7-B9FC-5C2D62D3F105}.Release|Any CPU.Build.0 = Release|Any CPU + {F407757D-F442-409C-A1FC-0DBBC51823BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F407757D-F442-409C-A1FC-0DBBC51823BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F407757D-F442-409C-A1FC-0DBBC51823BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F407757D-F442-409C-A1FC-0DBBC51823BB}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {F6540DE3-EB82-49B1-B203-AF931FB55232} = {5738C963-92E0-4276-8987-A4E20408112A} + {5738C963-92E0-4276-8987-A4E20408112A} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {509A32F9-B946-4DBF-B82C-E2E8F86B6E11} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {C8C88EC8-657D-4FDA-8CBC-4ED5CE77706B} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {25CF7B6C-F643-477A-A905-D53E6E0E40DA} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {FBDDC616-8140-4B07-8C0A-BF30577FB605} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {829AAE4D-2504-4C96-A64E-613A6F1BB061} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {096572AF-69B2-4784-91D3-A4714A3D2982} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {10450979-D8EC-4CAE-88C3-215BDB7D379B} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {575C6C3C-8E6F-4869-BA9B-C1F2E29DE4EB} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {8F06ADF3-A68C-4E58-BC21-3CF7AFD0E116} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {AC7FC415-3583-40E4-B34A-10E3A74503E3} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {AF390EAF-182F-4801-8E38-7B549E1B3C9C} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {218C089F-D1AA-4773-AA1F-CC65640CFE90} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {1281B167-C550-442C-8D64-1A3C225BD53C} = {65062D4B-4A78-482E-938E-F1E2E2F1BECE} + {E06DF4BE-C600-47C1-9FD6-8EA6E22D574F} = {509A32F9-B946-4DBF-B82C-E2E8F86B6E11} + {F61F6444-1C7A-4AC0-B897-EE3DF3BA470D} = {C8C88EC8-657D-4FDA-8CBC-4ED5CE77706B} + {E7200A68-221E-46F7-8D10-95E4D45A120B} = {C8C88EC8-657D-4FDA-8CBC-4ED5CE77706B} + {F1A47B58-0DC6-4465-9781-E046FCF720A0} = {C8C88EC8-657D-4FDA-8CBC-4ED5CE77706B} + {7C1207C7-EC00-4FA4-A796-79125C1163C7} = {C8C88EC8-657D-4FDA-8CBC-4ED5CE77706B} + {C2187BAB-4FF2-4918-9996-38D04534D443} = {25CF7B6C-F643-477A-A905-D53E6E0E40DA} + {2A7691A4-C548-499F-8891-9E648F5AA498} = {25CF7B6C-F643-477A-A905-D53E6E0E40DA} + {BD337891-E563-4B48-8F16-658BED78710E} = {FBDDC616-8140-4B07-8C0A-BF30577FB605} + {B4792F59-0CDB-4ED3-A062-7DF93ED9C4C4} = {829AAE4D-2504-4C96-A64E-613A6F1BB061} + {6D01DEF2-749F-4BF4-B50B-70CCB81CE7E2} = {096572AF-69B2-4784-91D3-A4714A3D2982} + {4299E4CA-21ED-4783-8734-B8DBB8ED3230} = {096572AF-69B2-4784-91D3-A4714A3D2982} + {5A46FE1F-A957-4C9F-A9F7-6718ADB9115A} = {096572AF-69B2-4784-91D3-A4714A3D2982} + {9C48DFA7-88AF-4796-B195-4BE0BD88144E} = {10450979-D8EC-4CAE-88C3-215BDB7D379B} + {3FCF64FD-C8A7-45D0-978B-8F71FA9DD7F2} = {10450979-D8EC-4CAE-88C3-215BDB7D379B} + {ED3FA3B9-B51A-4894-BA75-60E6F8C9446E} = {575C6C3C-8E6F-4869-BA9B-C1F2E29DE4EB} + {5395E547-70DD-4A95-AF78-907E8D665E60} = {575C6C3C-8E6F-4869-BA9B-C1F2E29DE4EB} + {3D9CC0F4-1EAE-4FBF-AC38-9A6A91002DCC} = {AC7FC415-3583-40E4-B34A-10E3A74503E3} + {1D415773-E5E4-4272-B5BA-7BE8896AA30B} = {AC7FC415-3583-40E4-B34A-10E3A74503E3} + {4B390C4C-F9F4-40FC-839E-450650644E90} = {AF390EAF-182F-4801-8E38-7B549E1B3C9C} + {89ABCF8C-9DCC-41B4-9098-495B592C467F} = {AF390EAF-182F-4801-8E38-7B549E1B3C9C} + {73B04201-2729-45A4-B280-C682F905D59F} = {AF390EAF-182F-4801-8E38-7B549E1B3C9C} + {31FF343A-D429-4BAC-A405-BAA91E12EAC8} = {218C089F-D1AA-4773-AA1F-CC65640CFE90} + {86BC7597-7E02-4CD7-B9FC-5C2D62D3F105} = {1281B167-C550-442C-8D64-1A3C225BD53C} + {F407757D-F442-409C-A1FC-0DBBC51823BB} = {1281B167-C550-442C-8D64-1A3C225BD53C} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {DA16AFB8-AFE4-4072-83B0-BE20C97C72D8} + EndGlobalSection +EndGlobal diff --git a/NuGet.Config b/NuGet.Config new file mode 100644 index 0000000..d80bb29 --- /dev/null +++ b/NuGet.Config @@ -0,0 +1,7 @@ + + + + + + + diff --git a/README.md b/README.md new file mode 100644 index 0000000..890ead2 --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +#Design Patterns + + +## OO Patterns +* [Strategy](/src/strategy) +* [Observer](/src/observer) +* [Decorator](/src/decorator) +* [Factory Method](/src/factory) +* [Abstract Factory](/src/factory) +* [Singleton](/src/singleton) +* [Command](/src/command) +* [Adapter](/src/adapter) +* [Facade](/src/facade) +* [Template Method](/src/templatemethod) +* [Iterator](/src/iterator) +* [Composite](/src/composite) +* [State](/src/state) +* [Proxy](/src/proxy) + +## OO Principles + +* Encapsulates what varies. +* Favor composition over inheritance. +* Program to interfaces, not implementations. +* Strive for loosely coupled designs between objects that interact +* Classes should be open for extension but closed for modification. +* Depend on abstractions. Do not depend on concrete classes. +* Principle of Least Knowledge: talk only to your immediate friends. +* The Hollywood Principle: Don't call us, we'll call you. +* A class should have only one reason to change. + + + + +**You need [.NET Core 3.1](https://www.dot.net) to run these examples.** diff --git a/global.json b/global.json new file mode 100644 index 0000000..faeec19 --- /dev/null +++ b/global.json @@ -0,0 +1,6 @@ +{ + "sdk": { + "version": "3.1.202", + "rollForward": "latestFeature" + } + } \ No newline at end of file diff --git a/src/adapter/Ducks/Adapters/DuckAdapter.cs b/src/adapter/Ducks/Adapters/DuckAdapter.cs new file mode 100644 index 0000000..170c83a --- /dev/null +++ b/src/adapter/Ducks/Adapters/DuckAdapter.cs @@ -0,0 +1,32 @@ +using System; +using Ducks.Models.Ducks; +using Ducks.Models.Turkeys; + +namespace Ducks.Adapters +{ + public class DuckAdapter : ITurkey //target interface + { + //adaptee interface + private readonly IDuck _duck; + private readonly Random _random; + + public DuckAdapter(IDuck duck) + { + _duck = duck; + _random = new Random(); + } + + public void Gobble() + { + _duck.Quack(); + } + + public void Fly() + { + if (_random.Next(5) == 0) + { + _duck.Fly(); + } + } + } +} \ No newline at end of file diff --git a/src/adapter/Ducks/Adapters/TurkeyAdapter.cs b/src/adapter/Ducks/Adapters/TurkeyAdapter.cs new file mode 100644 index 0000000..9432a40 --- /dev/null +++ b/src/adapter/Ducks/Adapters/TurkeyAdapter.cs @@ -0,0 +1,29 @@ +using Ducks.Models.Ducks; +using Ducks.Models.Turkeys; + +namespace Ducks.Adapters +{ + public class TurkeyAdapter : IDuck //target interface + { + //adaptee interface + private readonly ITurkey _turkey; + + public TurkeyAdapter(ITurkey turkey) + { + _turkey = turkey; + } + + public void Quack() + { + _turkey.Gobble(); + } + + public void Fly() + { + for (int i = 0; i < 5; i++) + { + _turkey.Fly(); + } + } + } +} \ No newline at end of file diff --git a/src/adapter/Ducks/Ducks.csproj b/src/adapter/Ducks/Ducks.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/adapter/Ducks/Ducks.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/adapter/Ducks/Models/Ducks/IDuck.cs b/src/adapter/Ducks/Models/Ducks/IDuck.cs new file mode 100644 index 0000000..5ae165b --- /dev/null +++ b/src/adapter/Ducks/Models/Ducks/IDuck.cs @@ -0,0 +1,8 @@ +namespace Ducks.Models.Ducks +{ + public interface IDuck + { + void Quack(); + void Fly(); + } +} \ No newline at end of file diff --git a/src/adapter/Ducks/Models/Ducks/MallardDuck.cs b/src/adapter/Ducks/Models/Ducks/MallardDuck.cs new file mode 100644 index 0000000..e26632c --- /dev/null +++ b/src/adapter/Ducks/Models/Ducks/MallardDuck.cs @@ -0,0 +1,17 @@ +using System; + +namespace Ducks.Models.Ducks +{ + public class MallardDuck : IDuck + { + public void Quack() + { + Console.WriteLine("Quack"); + } + + public void Fly() + { + Console.WriteLine("I'm flying"); + } + } +} \ No newline at end of file diff --git a/src/adapter/Ducks/Models/Turkeys/ITurkey.cs b/src/adapter/Ducks/Models/Turkeys/ITurkey.cs new file mode 100644 index 0000000..769a705 --- /dev/null +++ b/src/adapter/Ducks/Models/Turkeys/ITurkey.cs @@ -0,0 +1,8 @@ +namespace Ducks.Models.Turkeys +{ + public interface ITurkey + { + void Gobble(); + void Fly(); + } +} \ No newline at end of file diff --git a/src/adapter/Ducks/Models/Turkeys/WildTurkey.cs b/src/adapter/Ducks/Models/Turkeys/WildTurkey.cs new file mode 100644 index 0000000..e78b397 --- /dev/null +++ b/src/adapter/Ducks/Models/Turkeys/WildTurkey.cs @@ -0,0 +1,17 @@ +using System; + +namespace Ducks.Models.Turkeys +{ + public class WildTurkey : ITurkey + { + public void Gobble() + { + Console.WriteLine("Gobble gobble"); + } + + public void Fly() + { + Console.WriteLine("I'm flying a short distance"); + } + } +} \ No newline at end of file diff --git a/src/adapter/Ducks/Program.cs b/src/adapter/Ducks/Program.cs new file mode 100644 index 0000000..c1f34ec --- /dev/null +++ b/src/adapter/Ducks/Program.cs @@ -0,0 +1,44 @@ +using System; +using Ducks.Adapters; +using Ducks.Models.Ducks; +using Ducks.Models.Turkeys; + +namespace Ducks +{ + public class Program + { + public static void Main(string[] args) + { + MallardDuck duck = new MallardDuck(); + + WildTurkey turkey = new WildTurkey(); + IDuck turkeyAdapter = new TurkeyAdapter(turkey); + + Console.WriteLine("The Turkey says..."); + turkey.Gobble(); + turkey.Fly(); + + Console.WriteLine("\nThe Duck says..."); + TestDuck(duck); + + Console.WriteLine("\nThe TurkeyAdapter says..."); + TestDuck(turkeyAdapter); + + ITurkey duckAdapter = new DuckAdapter(duck); + + Console.WriteLine(""); + for (int i = 0; i < 10; i++) + { + Console.WriteLine("The DuckAdapter says..."); + duckAdapter.Gobble(); + duckAdapter.Fly(); + } + } + + public static void TestDuck(IDuck duck) + { + duck.Quack(); + duck.Fly(); + } + } +} diff --git a/src/adapter/README.md b/src/adapter/README.md new file mode 100644 index 0000000..61e1c21 --- /dev/null +++ b/src/adapter/README.md @@ -0,0 +1,2 @@ +**The Adapter Pattern** converts the interface of a class into another interface the clients expect. +Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. \ No newline at end of file diff --git a/src/combined/DuckSimulator/AbstractDuckFactory.cs b/src/combined/DuckSimulator/AbstractDuckFactory.cs new file mode 100644 index 0000000..87e9939 --- /dev/null +++ b/src/combined/DuckSimulator/AbstractDuckFactory.cs @@ -0,0 +1,10 @@ +namespace DuckSimulator +{ + public abstract class AbstractDuckFactory + { + public abstract IQuackable CreateMallardDuck(); + public abstract IQuackable CreateRedheadDuck(); + public abstract IQuackable CreateDuckCall(); + public abstract IQuackable CreateRubberDuck(); + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/CountingDuckFactory.cs b/src/combined/DuckSimulator/CountingDuckFactory.cs new file mode 100644 index 0000000..653f712 --- /dev/null +++ b/src/combined/DuckSimulator/CountingDuckFactory.cs @@ -0,0 +1,13 @@ +namespace DuckSimulator +{ + public class CountingDuckFactory : AbstractDuckFactory + { + public override IQuackable CreateDuckCall() => new QuackCounter(new DuckCall()); + + public override IQuackable CreateMallardDuck() => new QuackCounter(new MallardDuck()); + + public override IQuackable CreateRedheadDuck() => new QuackCounter(new RedheadDuck()); + + public override IQuackable CreateRubberDuck() => new QuackCounter(new RubberDuck()); + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/DecoyDuck.cs b/src/combined/DuckSimulator/DecoyDuck.cs new file mode 100644 index 0000000..432450f --- /dev/null +++ b/src/combined/DuckSimulator/DecoyDuck.cs @@ -0,0 +1,26 @@ +using System; + +namespace DuckSimulator +{ + public class DecoyDuck : IQuackable + { + private Observable _observable; + + public DecoyDuck() + { + _observable = new Observable(this); + } + + public void Quack() + { + Console.WriteLine("<< Silence >>"); + NotifyObservers(); + } + + public void RegisterObserver(IObserver observer) => _observable.RegisterObserver(observer); + + public void NotifyObservers() => _observable.NotifyObservers(); + + public override string ToString() => "Decoy Duck"; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/DuckCall.cs b/src/combined/DuckSimulator/DuckCall.cs new file mode 100644 index 0000000..7c15aec --- /dev/null +++ b/src/combined/DuckSimulator/DuckCall.cs @@ -0,0 +1,26 @@ +using System; + +namespace DuckSimulator +{ + public class DuckCall : IQuackable + { + private Observable _observable; + + public DuckCall() + { + _observable = new Observable(this); + } + + public void Quack() + { + Console.WriteLine("Kwak"); + NotifyObservers(); + } + + public void RegisterObserver(IObserver observer) => _observable.RegisterObserver(observer); + + public void NotifyObservers() => _observable.NotifyObservers(); + + public override string ToString() => "Duck Call"; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/DuckFactory.cs b/src/combined/DuckSimulator/DuckFactory.cs new file mode 100644 index 0000000..e83e543 --- /dev/null +++ b/src/combined/DuckSimulator/DuckFactory.cs @@ -0,0 +1,13 @@ +namespace DuckSimulator +{ + public class DuckFactory : AbstractDuckFactory + { + public override IQuackable CreateDuckCall() => new DuckCall(); + + public override IQuackable CreateMallardDuck() => new MallardDuck(); + + public override IQuackable CreateRedheadDuck() => new RedheadDuck(); + + public override IQuackable CreateRubberDuck() => new RubberDuck(); + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/DuckSimulator.csproj b/src/combined/DuckSimulator/DuckSimulator.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/combined/DuckSimulator/DuckSimulator.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/combined/DuckSimulator/Flock.cs b/src/combined/DuckSimulator/Flock.cs new file mode 100644 index 0000000..e88e92c --- /dev/null +++ b/src/combined/DuckSimulator/Flock.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; + +namespace DuckSimulator +{ + public class Flock : IQuackable + { + private List _ducks = new List(); + + public void Add(IQuackable duck) => _ducks.Add(duck); + + public void Quack() + { + foreach (var duck in _ducks) + { + duck.Quack(); + } + } + + public void RegisterObserver(IObserver observer) + { + foreach (var duck in _ducks) + { + duck.RegisterObserver(observer); + } + } + + public void NotifyObservers() {} + + public override string ToString() => "Flock of Ducks"; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/Goose.cs b/src/combined/DuckSimulator/Goose.cs new file mode 100644 index 0000000..cd1cad2 --- /dev/null +++ b/src/combined/DuckSimulator/Goose.cs @@ -0,0 +1,11 @@ +using System; + +namespace DuckSimulator +{ + public class Goose + { + public void Honk() => Console.WriteLine("Honk"); + + public override string ToString() => "Goose"; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/GooseAdapter.cs b/src/combined/DuckSimulator/GooseAdapter.cs new file mode 100644 index 0000000..56e1a84 --- /dev/null +++ b/src/combined/DuckSimulator/GooseAdapter.cs @@ -0,0 +1,25 @@ +namespace DuckSimulator +{ + public class GooseAdapter : IQuackable + { + private Goose _goose; + private Observable _observable; + + public GooseAdapter(Goose goose) + { + _goose = goose; + _observable = new Observable(this); + } + + public void Quack() + { + _goose.Honk(); + } + + public void RegisterObserver(IObserver observer) => _observable.RegisterObserver(observer); + + public void NotifyObservers() => _observable.NotifyObservers(); + + public override string ToString() => "Goose pretending to be a Duck"; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/IObserver.cs b/src/combined/DuckSimulator/IObserver.cs new file mode 100644 index 0000000..69572c1 --- /dev/null +++ b/src/combined/DuckSimulator/IObserver.cs @@ -0,0 +1,7 @@ +namespace DuckSimulator +{ + public interface IObserver + { + void Update(IQuackObservable duck); + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/IQuackObservable.cs b/src/combined/DuckSimulator/IQuackObservable.cs new file mode 100644 index 0000000..b2e8ef9 --- /dev/null +++ b/src/combined/DuckSimulator/IQuackObservable.cs @@ -0,0 +1,8 @@ +namespace DuckSimulator +{ + public interface IQuackObservable + { + void RegisterObserver(IObserver observer); + void NotifyObservers(); + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/IQuackable.cs b/src/combined/DuckSimulator/IQuackable.cs new file mode 100644 index 0000000..fb62835 --- /dev/null +++ b/src/combined/DuckSimulator/IQuackable.cs @@ -0,0 +1,7 @@ +namespace DuckSimulator +{ + public interface IQuackable : IQuackObservable + { + void Quack(); + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/MallardDuck.cs b/src/combined/DuckSimulator/MallardDuck.cs new file mode 100644 index 0000000..24b63ce --- /dev/null +++ b/src/combined/DuckSimulator/MallardDuck.cs @@ -0,0 +1,26 @@ +using System; + +namespace DuckSimulator +{ + public class MallardDuck : IQuackable + { + private Observable _observable; + + public MallardDuck() + { + _observable = new Observable(this); + } + + public void Quack() + { + Console.WriteLine("Quack"); + NotifyObservers(); + } + + public void RegisterObserver(IObserver observer) => _observable.RegisterObserver(observer); + + public void NotifyObservers() => _observable.NotifyObservers(); + + public override string ToString() => "Mallard Duck"; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/Observable.cs b/src/combined/DuckSimulator/Observable.cs new file mode 100644 index 0000000..620211a --- /dev/null +++ b/src/combined/DuckSimulator/Observable.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; + +namespace DuckSimulator +{ + public class Observable : IQuackObservable + { + private List _observers = new List(); + private IQuackObservable _duck; + + public Observable(IQuackObservable duck) + { + _duck = duck; + } + + public void RegisterObserver(IObserver observer) => _observers.Add(observer); + + public void NotifyObservers() + { + foreach (var observer in _observers) + { + observer.Update(_duck); + } + } + + public IEnumerable GetOberservers => _observers; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/Program.cs b/src/combined/DuckSimulator/Program.cs new file mode 100644 index 0000000..230cc26 --- /dev/null +++ b/src/combined/DuckSimulator/Program.cs @@ -0,0 +1,55 @@ +using System; + +namespace DuckSimulator +{ + public class DuckSimulator + { + public static void Main(string[] args) + { + var simulator = new DuckSimulator(); + AbstractDuckFactory duckFactory = new CountingDuckFactory(); + + simulator.Simulate(duckFactory); + } + + public void Simulate(AbstractDuckFactory duckFactory) + { + IQuackable redheadDuck = duckFactory.CreateRedheadDuck(); + IQuackable duckCall = duckFactory.CreateDuckCall(); + IQuackable rubberDuck = duckFactory.CreateRubberDuck(); + IQuackable gooseDuck = new GooseAdapter(new Goose()); + + Flock flockOfDucks = new Flock(); + + flockOfDucks.Add(redheadDuck); + flockOfDucks.Add(duckCall); + flockOfDucks.Add(rubberDuck); + flockOfDucks.Add(gooseDuck); + + Flock flockOfMallards = new Flock(); + + IQuackable mallardOne = duckFactory.CreateMallardDuck(); + IQuackable mallardTwo = duckFactory.CreateMallardDuck(); + IQuackable mallardThree = duckFactory.CreateMallardDuck(); + IQuackable mallardFour = duckFactory.CreateMallardDuck(); + + flockOfMallards.Add(mallardOne); + flockOfMallards.Add(mallardTwo); + flockOfMallards.Add(mallardThree); + flockOfMallards.Add(mallardFour); + + flockOfDucks.Add(flockOfMallards); + + Console.WriteLine("\nDuck Simulator: With Observer"); + + Quackologist quackologist = new Quackologist(); + flockOfDucks.RegisterObserver(quackologist); + + Simulate(flockOfDucks); + + Console.WriteLine($"\nThe ducks quacked {QuackCounter.NumberOfQuacks} times"); + } + + public void Simulate(IQuackable duck) => duck.Quack(); + } +} diff --git a/src/combined/DuckSimulator/QuackCounter.cs b/src/combined/DuckSimulator/QuackCounter.cs new file mode 100644 index 0000000..3a571a9 --- /dev/null +++ b/src/combined/DuckSimulator/QuackCounter.cs @@ -0,0 +1,26 @@ +namespace DuckSimulator +{ + public class QuackCounter : IQuackable + { + public static int NumberOfQuacks { get; private set; } + + private IQuackable _duck; + + public QuackCounter(IQuackable duck) + { + _duck = duck; + } + + public void Quack() + { + _duck.Quack(); + NumberOfQuacks++; + } + + public void RegisterObserver(IObserver observer) => _duck.RegisterObserver(observer); + + public void NotifyObservers() => _duck.NotifyObservers(); + + public override string ToString() => _duck.ToString(); + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/Quackologist.cs b/src/combined/DuckSimulator/Quackologist.cs new file mode 100644 index 0000000..08ebfc1 --- /dev/null +++ b/src/combined/DuckSimulator/Quackologist.cs @@ -0,0 +1,11 @@ +using System; + +namespace DuckSimulator +{ + public class Quackologist : IObserver + { + public void Update(IQuackObservable duck) => Console.WriteLine("Quackologist: " + duck + " just quacked."); + + public override string ToString() => "Quackologist"; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/RedheadDuck.cs b/src/combined/DuckSimulator/RedheadDuck.cs new file mode 100644 index 0000000..7197821 --- /dev/null +++ b/src/combined/DuckSimulator/RedheadDuck.cs @@ -0,0 +1,26 @@ +using System; + +namespace DuckSimulator +{ + public class RedheadDuck : IQuackable + { + private Observable _observable; + + public RedheadDuck() + { + _observable = new Observable(this); + } + + public void Quack() + { + Console.WriteLine("Quack"); + NotifyObservers(); + } + + public void RegisterObserver(IObserver observer) => _observable.RegisterObserver(observer); + + public void NotifyObservers() => _observable.NotifyObservers(); + + public override string ToString() => "Redhead Duck"; + } +} \ No newline at end of file diff --git a/src/combined/DuckSimulator/RubberDuck.cs b/src/combined/DuckSimulator/RubberDuck.cs new file mode 100644 index 0000000..d1ac9f2 --- /dev/null +++ b/src/combined/DuckSimulator/RubberDuck.cs @@ -0,0 +1,26 @@ +using System; + +namespace DuckSimulator +{ + public class RubberDuck : IQuackable + { + private Observable _observable; + + public RubberDuck() + { + _observable = new Observable(this); + } + + public void Quack() + { + Console.WriteLine("Squeak"); + NotifyObservers(); + } + + public void RegisterObserver(IObserver observer) => _observable.RegisterObserver(observer); + + public void NotifyObservers() => _observable.NotifyObservers(); + + public override string ToString() => "Rubber Duck"; + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/Abstractions/ICommand.cs b/src/command/Party/Commands/Abstractions/ICommand.cs new file mode 100644 index 0000000..ad77b61 --- /dev/null +++ b/src/command/Party/Commands/Abstractions/ICommand.cs @@ -0,0 +1,8 @@ +namespace Party.Commands +{ + public interface ICommand + { + void Execute(); + void Undo(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/CeilingFanHighCommand.cs b/src/command/Party/Commands/CeilingFanHighCommand.cs new file mode 100644 index 0000000..cfca144 --- /dev/null +++ b/src/command/Party/Commands/CeilingFanHighCommand.cs @@ -0,0 +1,40 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class CeilingFanHighCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + private int _prevSpeed; + + public CeilingFanHighCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() + { + _prevSpeed = _ceilingFan.GetSpeed(); + _ceilingFan.High(); + } + + public void Undo() + { + switch (_prevSpeed) + { + case CeilingFan.HIGH: + _ceilingFan.High(); + break; + case CeilingFan.MEDIUM: + _ceilingFan.Medium(); + break; + case CeilingFan.LOW: + _ceilingFan.Low(); + break; + case CeilingFan.OFF: + _ceilingFan.Off(); + break; + } + } + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/CeilingFanMediumCommand.cs b/src/command/Party/Commands/CeilingFanMediumCommand.cs new file mode 100644 index 0000000..e22428a --- /dev/null +++ b/src/command/Party/Commands/CeilingFanMediumCommand.cs @@ -0,0 +1,40 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class CeilingFanMediumCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + private int _prevSpeed; + + public CeilingFanMediumCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() + { + _prevSpeed = _ceilingFan.GetSpeed(); + _ceilingFan.Medium(); + } + + public void Undo() + { + switch (_prevSpeed) + { + case CeilingFan.HIGH: + _ceilingFan.High(); + break; + case CeilingFan.MEDIUM: + _ceilingFan.Medium(); + break; + case CeilingFan.LOW: + _ceilingFan.Low(); + break; + case CeilingFan.OFF: + _ceilingFan.Off(); + break; + } + } + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/CeilingFanOffCommand.cs b/src/command/Party/Commands/CeilingFanOffCommand.cs new file mode 100644 index 0000000..ff032be --- /dev/null +++ b/src/command/Party/Commands/CeilingFanOffCommand.cs @@ -0,0 +1,40 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class CeilingFanOffCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + private int _prevSpeed; + + public CeilingFanOffCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() + { + _prevSpeed = _ceilingFan.GetSpeed(); + _ceilingFan.Off(); + } + + public void Undo() + { + switch (_prevSpeed) + { + case CeilingFan.HIGH: + _ceilingFan.High(); + break; + case CeilingFan.MEDIUM: + _ceilingFan.Medium(); + break; + case CeilingFan.LOW: + _ceilingFan.Low(); + break; + case CeilingFan.OFF: + _ceilingFan.Off(); + break; + } + } + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/HottubOffCommand.cs b/src/command/Party/Commands/HottubOffCommand.cs new file mode 100644 index 0000000..7af2057 --- /dev/null +++ b/src/command/Party/Commands/HottubOffCommand.cs @@ -0,0 +1,22 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class HottubOffCommand : ICommand + { + private readonly Hottub _hottub; + + public HottubOffCommand(Hottub hottub) + { + _hottub = hottub; + } + + public void Execute() + { + _hottub.SetTemperature(98); + _hottub.Off(); + } + + public void Undo() => _hottub.On(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/HottubOnCommand.cs b/src/command/Party/Commands/HottubOnCommand.cs new file mode 100644 index 0000000..0f1a3b0 --- /dev/null +++ b/src/command/Party/Commands/HottubOnCommand.cs @@ -0,0 +1,23 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class HottubOnCommand : ICommand + { + private readonly Hottub _hottub; + + public HottubOnCommand(Hottub hottub) + { + _hottub = hottub; + } + + public void Execute() + { + _hottub.On(); + _hottub.SetTemperature(104); + _hottub.Circulate(); + } + + public void Undo() => _hottub.Off(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/LightOffCommand.cs b/src/command/Party/Commands/LightOffCommand.cs new file mode 100644 index 0000000..8b2d18a --- /dev/null +++ b/src/command/Party/Commands/LightOffCommand.cs @@ -0,0 +1,18 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class LightOffCommand : ICommand + { + private readonly Light _light; + + public LightOffCommand(Light light) + { + _light = light; + } + + public void Execute() => _light.Off(); + + public void Undo() => _light.On(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/LightOnCommand.cs b/src/command/Party/Commands/LightOnCommand.cs new file mode 100644 index 0000000..4edf99c --- /dev/null +++ b/src/command/Party/Commands/LightOnCommand.cs @@ -0,0 +1,18 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class LightOnCommand : ICommand + { + private readonly Light _light; + + public LightOnCommand(Light light) + { + _light = light; + } + + public void Execute() => _light.On(); + + public void Undo() => _light.Off(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/MacroCommand.cs b/src/command/Party/Commands/MacroCommand.cs new file mode 100644 index 0000000..60350e9 --- /dev/null +++ b/src/command/Party/Commands/MacroCommand.cs @@ -0,0 +1,28 @@ +namespace Party.Commands +{ + public class MacroCommand : ICommand + { + private readonly ICommand[] _commands; + + public MacroCommand(ICommand[] commands) + { + _commands = commands; + } + + public void Execute() + { + foreach (ICommand command in _commands) + { + command.Execute(); + } + } + + public void Undo() + { + for (int i = _commands.Length - 1; i >= 0; i--) + { + _commands[i].Undo(); + } + } + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/NoCommand.cs b/src/command/Party/Commands/NoCommand.cs new file mode 100644 index 0000000..1b55a7f --- /dev/null +++ b/src/command/Party/Commands/NoCommand.cs @@ -0,0 +1,8 @@ +namespace Party.Commands +{ + public class NoCommand : ICommand + { + public void Execute() {} + public void Undo() {} + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/StereoOffCommand.cs b/src/command/Party/Commands/StereoOffCommand.cs new file mode 100644 index 0000000..cdd68f0 --- /dev/null +++ b/src/command/Party/Commands/StereoOffCommand.cs @@ -0,0 +1,18 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class StereoOffCommand : ICommand + { + private readonly Stereo _stereo; + + public StereoOffCommand(Stereo stereo) + { + _stereo = stereo; + } + + public void Execute() => _stereo.Off(); + + public void Undo() => _stereo.On(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/StereoOnCommand.cs b/src/command/Party/Commands/StereoOnCommand.cs new file mode 100644 index 0000000..46415a3 --- /dev/null +++ b/src/command/Party/Commands/StereoOnCommand.cs @@ -0,0 +1,18 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class StereoOnCommand : ICommand + { + private readonly Stereo _stereo; + + public StereoOnCommand(Stereo stereo) + { + _stereo = stereo; + } + + public void Execute() => _stereo.On(); + + public void Undo() => _stereo.Off(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/StereoOnWithCDCommand.cs b/src/command/Party/Commands/StereoOnWithCDCommand.cs new file mode 100644 index 0000000..02a3d75 --- /dev/null +++ b/src/command/Party/Commands/StereoOnWithCDCommand.cs @@ -0,0 +1,23 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class StereoOnWithCDCommand : ICommand + { + private readonly Stereo _stereo; + + public StereoOnWithCDCommand(Stereo stereo) + { + _stereo = stereo; + } + + public void Execute() + { + _stereo.On(); + _stereo.SetCD(); + _stereo.SetVolume(11); + } + + public void Undo() => _stereo.Off(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/TVOffCommand.cs b/src/command/Party/Commands/TVOffCommand.cs new file mode 100644 index 0000000..ed03ced --- /dev/null +++ b/src/command/Party/Commands/TVOffCommand.cs @@ -0,0 +1,18 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class TVOffCommand : ICommand + { + private readonly TV _tv; + + public TVOffCommand(TV tv) + { + _tv = tv; + } + + public void Execute() => _tv.Off(); + + public void Undo() => _tv.On(); + } +} \ No newline at end of file diff --git a/src/command/Party/Commands/TVOnCommand.cs b/src/command/Party/Commands/TVOnCommand.cs new file mode 100644 index 0000000..5b21c74 --- /dev/null +++ b/src/command/Party/Commands/TVOnCommand.cs @@ -0,0 +1,22 @@ +using Party.Receivers; + +namespace Party.Commands +{ + public class TVOnCommand : ICommand + { + private readonly TV _tv; + + public TVOnCommand(TV tv) + { + _tv = tv; + } + + public void Execute() + { + _tv.On(); + _tv.SetInputChannel(); + } + + public void Undo() => _tv.Off(); + } +} \ No newline at end of file diff --git a/src/command/Party/Invokers/RemoteControl.cs b/src/command/Party/Invokers/RemoteControl.cs new file mode 100644 index 0000000..aa21671 --- /dev/null +++ b/src/command/Party/Invokers/RemoteControl.cs @@ -0,0 +1,63 @@ +using System.Text; +using Party.Commands; + +namespace Party.Invokers +{ + // + // This is the invoker + // + public class RemoteControl + { + private readonly ICommand[] _onCommands; + private readonly ICommand[] _offCommands; + private ICommand _undoCommand; + public RemoteControl() + { + _onCommands = new ICommand[7]; + _offCommands = new ICommand[7]; + + ICommand noCommand = new NoCommand(); + for (int i = 0; i < 7; i++) + { + _onCommands[i] = noCommand; + _offCommands[i] = noCommand; + } + _undoCommand = new NoCommand(); + } + + public void SetCommand(int slot, ICommand onCommand, ICommand offCommand) + { + _onCommands[slot] = onCommand; + _offCommands[slot] = offCommand; + } + + public void OnButtonWasPushed(int slot) + { + _onCommands[slot].Execute(); + _undoCommand = _onCommands[slot]; + } + + public void OffButtonWasPushed(int slot) + { + _offCommands[slot].Execute(); + _undoCommand = _offCommands[slot]; + } + + public void UndoButtonWasPushed() => _undoCommand.Undo(); + + public override string ToString() + { + var stringBuilder= new StringBuilder(); + stringBuilder.Append("\n------ Remote Control -------\n"); + + for (int i = 0; i < _onCommands.Length; i++) + { + stringBuilder.Append("[slot " + i + "] " + _onCommands[i].GetType().Name + " " + _offCommands[i].GetType().Name + "\n"); + } + + stringBuilder.Append("[undo] " + _undoCommand.GetType().Name + "\n"); + + return stringBuilder.ToString(); + } + } +} \ No newline at end of file diff --git a/src/command/Party/Party.csproj b/src/command/Party/Party.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/command/Party/Party.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/command/Party/Program.cs b/src/command/Party/Program.cs new file mode 100644 index 0000000..d7def00 --- /dev/null +++ b/src/command/Party/Program.cs @@ -0,0 +1,43 @@ +using System; +using Party.Commands; +using Party.Invokers; +using Party.Receivers; + +namespace Party +{ + public class Program + { + public static void Main(string[] args) + { + var remoteControl = new RemoteControl(); + + Light light = new Light("Living Room"); + TV tv = new TV("Living Room"); + Stereo stereo = new Stereo("Living Room"); + Hottub hottub = new Hottub(); + + LightOnCommand lightOn = new LightOnCommand(light); + StereoOnCommand stereoOn = new StereoOnCommand(stereo); + TVOnCommand tvOn = new TVOnCommand(tv); + HottubOnCommand hottubOn = new HottubOnCommand(hottub); + LightOffCommand lightOff = new LightOffCommand(light); + StereoOffCommand stereoOff = new StereoOffCommand(stereo); + TVOffCommand tvOff = new TVOffCommand(tv); + HottubOffCommand hottubOff = new HottubOffCommand(hottub); + + ICommand[] partyOn = { lightOn, stereoOn, tvOn, hottubOn }; + ICommand[] partyOff = { lightOff, stereoOff, tvOff, hottubOff }; + + MacroCommand partyOnMacro = new MacroCommand(partyOn); + MacroCommand partyOffMacro = new MacroCommand(partyOff); + + remoteControl.SetCommand(0, partyOnMacro, partyOffMacro); + + Console.WriteLine(remoteControl); + Console.WriteLine("--- Pushing Macro On---"); + remoteControl.OnButtonWasPushed(0); + Console.WriteLine("--- Pushing Macro Off---"); + remoteControl.OffButtonWasPushed(0); + } + } +} diff --git a/src/command/Party/Receivers/CeilingFan.cs b/src/command/Party/Receivers/CeilingFan.cs new file mode 100644 index 0000000..69aea45 --- /dev/null +++ b/src/command/Party/Receivers/CeilingFan.cs @@ -0,0 +1,50 @@ +using System; + +namespace Party.Receivers +{ + public class CeilingFan + { + private readonly string _location; + private int _speed; + + public const int HIGH = 3; + public const int MEDIUM = 2; + public const int LOW = 1; + public const int OFF = 0; + + public CeilingFan(string location) + { + _location = location; + } + + public void High() + { + // turns the ceiling fan on to high + _speed = HIGH; + Console.WriteLine(_location + " ceiling fan is on high"); + } + + public void Medium() + { + // turns the ceiling fan on to medium + _speed = MEDIUM; + Console.WriteLine(_location + " ceiling fan is on medium"); + } + + public void Low() + { + // turns the ceiling fan on to low + _speed = LOW; + Console.WriteLine(_location + " ceiling fan is on low"); + } + + public void Off() + { + // turns the ceiling fan off + _speed = OFF; + Console.WriteLine(_location + " ceiling fan is off"); + } + + public int GetSpeed() => _speed; + } +} \ No newline at end of file diff --git a/src/command/Party/Receivers/Hottub.cs b/src/command/Party/Receivers/Hottub.cs new file mode 100644 index 0000000..1c299e3 --- /dev/null +++ b/src/command/Party/Receivers/Hottub.cs @@ -0,0 +1,51 @@ +using System; + +namespace Party.Receivers +{ + public class Hottub + { + private bool _on; + private int _temperature; + + public void On() => _on = true; + + public void Off() => _on = false; + + public void Circulate() + { + if (_on) + { + Console.WriteLine("Hottub is bubbling!"); + } + } + + public void JetsOn() + { + if (_on) + { + Console.WriteLine("Hottub jets are on"); + } + } + + public void JetsOff() + { + if (_on) + { + Console.WriteLine("Hottub jets are off"); + } + } + + public void SetTemperature(int temperature) + { + if (temperature > _temperature) + { + Console.WriteLine("Hottub is heating to a steaming " + temperature + " degrees"); + } + else + { + Console.WriteLine("Hottub is cooling to " + temperature + " degrees"); + } + _temperature = temperature; + } + } +} \ No newline at end of file diff --git a/src/command/Party/Receivers/Light.cs b/src/command/Party/Receivers/Light.cs new file mode 100644 index 0000000..e5aad96 --- /dev/null +++ b/src/command/Party/Receivers/Light.cs @@ -0,0 +1,42 @@ +using System; + +namespace Party.Receivers +{ + public class Light + { + private readonly string _location; + private int _level; + + public Light(string location) + { + _location = location; + } + + public void On() + { + _level = 100; + Console.WriteLine(_location + " light is on"); + } + + public void Off() + { + _level = 0; + Console.WriteLine(_location + " light is off"); + } + + public void Dim(int level) + { + _level = level; + if (level == 0) + { + Off(); + } + else + { + Console.WriteLine("Light is dimmed to " + level + "%"); + } + } + + public int GetLevel() => _level; + } +} diff --git a/src/command/Party/Receivers/Stereo.cs b/src/command/Party/Receivers/Stereo.cs new file mode 100644 index 0000000..ca85c72 --- /dev/null +++ b/src/command/Party/Receivers/Stereo.cs @@ -0,0 +1,28 @@ +using System; + +namespace Party.Receivers +{ + public class Stereo + { + private readonly string _location; + + public Stereo(string location) + { + _location = location; + } + + public void On() => Console.WriteLine(_location + " stereo is on"); + + public void Off() => Console.WriteLine(_location + " stereo is off"); + + public void SetCD() => Console.WriteLine(_location + " stereo is set for CD input"); + + public void SetDVD() => Console.WriteLine(_location + " stereo is set for DVD input"); + + public void SetRadio() => Console.WriteLine(_location + " stereo is set for Radio"); + + // code to set the volume + // valid range: 1-11 (after all 11 is better than 10, right?) + public void SetVolume(int volume) => Console.WriteLine(_location + " stereo volume set to " + volume); + } +} \ No newline at end of file diff --git a/src/command/Party/Receivers/TV.cs b/src/command/Party/Receivers/TV.cs new file mode 100644 index 0000000..064642c --- /dev/null +++ b/src/command/Party/Receivers/TV.cs @@ -0,0 +1,25 @@ +using System; + +namespace Party.Receivers +{ + public class TV + { + private readonly string _location; + private int _channel; + + public TV(string location) + { + _location = location; + } + + public void On() => Console.WriteLine(_location + " TV is on"); + + public void Off() => Console.WriteLine(_location + " TV is off"); + + public void SetInputChannel() + { + _channel = 3; + Console.WriteLine(_location + " TV channel is set for DVD"); + } + } +} \ No newline at end of file diff --git a/src/command/README.md b/src/command/README.md new file mode 100644 index 0000000..01ffbd8 --- /dev/null +++ b/src/command/README.md @@ -0,0 +1,2 @@ +**The Command Pattern** encapsulates a request as an object, thereby letting you parameterize other objects with different requests, +queue or log requests, and support undoable oparations. \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/Abstractions/ICommand.cs b/src/command/RemoteControl/Commands/Abstractions/ICommand.cs new file mode 100644 index 0000000..98ebf90 --- /dev/null +++ b/src/command/RemoteControl/Commands/Abstractions/ICommand.cs @@ -0,0 +1,7 @@ +namespace RemoteControl.Commands +{ + public interface ICommand + { + void Execute(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/CeilingFanOffCommand.cs b/src/command/RemoteControl/Commands/CeilingFanOffCommand.cs new file mode 100644 index 0000000..a437f38 --- /dev/null +++ b/src/command/RemoteControl/Commands/CeilingFanOffCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class CeilingFanOffCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + + public CeilingFanOffCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() => _ceilingFan.Off(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/CeilingFanOnCommand.cs b/src/command/RemoteControl/Commands/CeilingFanOnCommand.cs new file mode 100644 index 0000000..d23c029 --- /dev/null +++ b/src/command/RemoteControl/Commands/CeilingFanOnCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class CeilingFanOnCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + + public CeilingFanOnCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() => _ceilingFan.High(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/GarageDoorDownCommand.cs b/src/command/RemoteControl/Commands/GarageDoorDownCommand.cs new file mode 100644 index 0000000..060d88d --- /dev/null +++ b/src/command/RemoteControl/Commands/GarageDoorDownCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class GarageDoorDownCommand : ICommand + { + private readonly GarageDoor _garageDoor; + + public GarageDoorDownCommand(GarageDoor garageDoor) + { + _garageDoor = garageDoor; + } + + public void Execute() => _garageDoor.Down(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/GarageDoorUpCommand.cs b/src/command/RemoteControl/Commands/GarageDoorUpCommand.cs new file mode 100644 index 0000000..f8170c6 --- /dev/null +++ b/src/command/RemoteControl/Commands/GarageDoorUpCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class GarageDoorUpCommand : ICommand + { + private readonly GarageDoor _garageDoor; + + public GarageDoorUpCommand(GarageDoor garageDoor) + { + _garageDoor = garageDoor; + } + + public void Execute() => _garageDoor.Up(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/HottubOffCommand.cs b/src/command/RemoteControl/Commands/HottubOffCommand.cs new file mode 100644 index 0000000..05d58a2 --- /dev/null +++ b/src/command/RemoteControl/Commands/HottubOffCommand.cs @@ -0,0 +1,21 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class HottubOffCommand : ICommand + { + private readonly Hottub _hottub; + + public HottubOffCommand(Hottub hottub) + { + _hottub = hottub; + } + + public void Execute() + { + _hottub.Cool(); + _hottub.Off(); + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/HottubOnCommand.cs b/src/command/RemoteControl/Commands/HottubOnCommand.cs new file mode 100644 index 0000000..01037cd --- /dev/null +++ b/src/command/RemoteControl/Commands/HottubOnCommand.cs @@ -0,0 +1,22 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class HottubOnCommand : ICommand + { + private readonly Hottub _hottub; + + public HottubOnCommand(Hottub hottub) + { + _hottub = hottub; + } + + public void Execute() + { + _hottub.On(); + _hottub.Heat(); + _hottub.BubblesOn(); + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/LightOffCommand.cs b/src/command/RemoteControl/Commands/LightOffCommand.cs new file mode 100644 index 0000000..24f2ee9 --- /dev/null +++ b/src/command/RemoteControl/Commands/LightOffCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class LightOffCommand : ICommand + { + private readonly Light _light; + + public LightOffCommand(Light light) + { + _light = light; + } + + public void Execute() => _light.Off(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/LightOnCommand.cs b/src/command/RemoteControl/Commands/LightOnCommand.cs new file mode 100644 index 0000000..6f42c18 --- /dev/null +++ b/src/command/RemoteControl/Commands/LightOnCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class LightOnCommand : ICommand + { + private readonly Light _light; + + public LightOnCommand(Light light) + { + _light = light; + } + + public void Execute() => _light.On(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/LivingroomLightOffCommand.cs b/src/command/RemoteControl/Commands/LivingroomLightOffCommand.cs new file mode 100644 index 0000000..adfd13c --- /dev/null +++ b/src/command/RemoteControl/Commands/LivingroomLightOffCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class LivingroomLightOffCommand : ICommand + { + private readonly Light _light; + + public LivingroomLightOffCommand(Light light) + { + _light = light; + } + + public void Execute() => _light.Off(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/LivingroomLightOnCommand.cs b/src/command/RemoteControl/Commands/LivingroomLightOnCommand.cs new file mode 100644 index 0000000..2fd4f9a --- /dev/null +++ b/src/command/RemoteControl/Commands/LivingroomLightOnCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class LivingroomLightOnCommand : ICommand + { + private readonly Light _light; + + public LivingroomLightOnCommand(Light light) + { + _light = light; + } + + public void Execute() => _light.On(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/NoCommand.cs b/src/command/RemoteControl/Commands/NoCommand.cs new file mode 100644 index 0000000..382fd45 --- /dev/null +++ b/src/command/RemoteControl/Commands/NoCommand.cs @@ -0,0 +1,10 @@ +using System; +using RemoteControl.Commands; + +namespace RemoteControl.Commands +{ + public class NoCommand : ICommand + { + public void Execute() {} + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/StereoOffCommand.cs b/src/command/RemoteControl/Commands/StereoOffCommand.cs new file mode 100644 index 0000000..1177ff6 --- /dev/null +++ b/src/command/RemoteControl/Commands/StereoOffCommand.cs @@ -0,0 +1,17 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class StereoOffCommand : ICommand + { + private readonly Stereo _stereo; + + public StereoOffCommand(Stereo stereo) + { + _stereo = stereo; + } + + public void Execute() => _stereo.Off(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Commands/StereoOnWithCDCommand.cs b/src/command/RemoteControl/Commands/StereoOnWithCDCommand.cs new file mode 100644 index 0000000..7165a52 --- /dev/null +++ b/src/command/RemoteControl/Commands/StereoOnWithCDCommand.cs @@ -0,0 +1,22 @@ +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl.Commands +{ + public class StereoOnWithCDCommand : ICommand + { + private readonly Stereo _stereo; + + public StereoOnWithCDCommand(Stereo stereo) + { + _stereo = stereo; + } + + public void Execute() + { + _stereo.On(); + _stereo.SetCD(); + _stereo.SetVolume(11); + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Invokers/RemoteControl.cs b/src/command/RemoteControl/Invokers/RemoteControl.cs new file mode 100644 index 0000000..b15be29 --- /dev/null +++ b/src/command/RemoteControl/Invokers/RemoteControl.cs @@ -0,0 +1,56 @@ +using System.Text; +using RemoteControl.Commands; + +namespace RemoteControl.Invokers +{ + // + // This is the invoker + // + public class RemoteControl + { + private readonly ICommand[] _onCommands; + private readonly ICommand[] _offCommands; + + public RemoteControl() + { + _onCommands = new ICommand[7]; + _offCommands = new ICommand[7]; + + ICommand noCommand = new NoCommand(); + for (int i = 0; i < 7; i++) + { + _onCommands[i] = noCommand; + _offCommands[i] = noCommand; + } + } + + public void SetCommand(int slot, ICommand onCommand, ICommand offCommand) + { + _onCommands[slot] = onCommand; + _offCommands[slot] = offCommand; + } + + public void OnButtonWasPushed(int slot) + { + _onCommands[slot].Execute(); + } + + public void OffButtonWasPushed(int slot) + { + _offCommands[slot].Execute(); + } + + public override string ToString() + { + var stringBuilder= new StringBuilder(); + stringBuilder.Append("\n------ Remote Control -------\n"); + + for (int i = 0; i < _onCommands.Length; i++) + { + stringBuilder.Append("[slot " + i + "] " + _onCommands[i].GetType().Name + " " + _offCommands[i].GetType().Name + "\n"); + } + + return stringBuilder.ToString(); + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Program.cs b/src/command/RemoteControl/Program.cs new file mode 100644 index 0000000..2079e88 --- /dev/null +++ b/src/command/RemoteControl/Program.cs @@ -0,0 +1,50 @@ +using System; +using RemoteControl.Commands; +using RemoteControl.Receivers; + +namespace RemoteControl +{ + public class Program + { + public static void Main(string[] args) + { + var remoteControl = new Invokers.RemoteControl(); + + Light livingRoomLight = new Light("Living Room"); + Light kitchenLight = new Light("Kitchen"); + CeilingFan ceilingFan = new CeilingFan("Living Room"); + GarageDoor garageDoor = new GarageDoor(""); + Stereo stereo = new Stereo("Living Room"); + + LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); + LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); + LightOnCommand kitchenLightOn = new LightOnCommand(kitchenLight); + LightOffCommand kitchenLightOff = new LightOffCommand(kitchenLight); + + CeilingFanOnCommand ceilingFanOn = new CeilingFanOnCommand(ceilingFan); + CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); + + GarageDoorUpCommand garageDoorUp = new GarageDoorUpCommand(garageDoor); + GarageDoorDownCommand garageDoorDown = new GarageDoorDownCommand(garageDoor); + + StereoOnWithCDCommand stereoOnWithCD = new StereoOnWithCDCommand(stereo); + StereoOffCommand stereoOff = new StereoOffCommand(stereo); + + remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); + remoteControl.SetCommand(1, kitchenLightOn, kitchenLightOff); + remoteControl.SetCommand(2, ceilingFanOn, ceilingFanOff); + remoteControl.SetCommand(3, stereoOnWithCD, stereoOff); + + Console.WriteLine(remoteControl); + + remoteControl.OnButtonWasPushed(0); + remoteControl.OffButtonWasPushed(0); + remoteControl.OnButtonWasPushed(1); + remoteControl.OffButtonWasPushed(1); + remoteControl.OnButtonWasPushed(2); + remoteControl.OffButtonWasPushed(2); + remoteControl.OnButtonWasPushed(3); + remoteControl.OffButtonWasPushed(3); + } + } +} diff --git a/src/command/RemoteControl/Receivers/CeilingFan.cs b/src/command/RemoteControl/Receivers/CeilingFan.cs new file mode 100644 index 0000000..e1711bf --- /dev/null +++ b/src/command/RemoteControl/Receivers/CeilingFan.cs @@ -0,0 +1,49 @@ +using System; + +namespace RemoteControl.Receivers +{ + public class CeilingFan + { + private readonly string _location; + private int _level; + + public const int HIGH = 2; + public const int MEDIUM = 1; + public const int LOW = 0; + + public CeilingFan(string location) + { + _location = location; + } + + public void High() + { + // turns the ceiling fan on to high + _level = HIGH; + Console.WriteLine(_location + " ceiling fan is on high"); + } + + public void Medium() + { + // turns the ceiling fan on to medium + _level = MEDIUM; + Console.WriteLine(_location + " ceiling fan is on medium"); + } + + public void Low() + { + // turns the ceiling fan on to low + _level = LOW; + Console.WriteLine(_location + " ceiling fan is on low"); + } + + public void Off() + { + // turns the ceiling fan off + _level = 0; + Console.WriteLine(_location + " ceiling fan is off"); + } + + public int GetSpeed() => _level; + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Receivers/GarageDoor.cs b/src/command/RemoteControl/Receivers/GarageDoor.cs new file mode 100644 index 0000000..e46cec2 --- /dev/null +++ b/src/command/RemoteControl/Receivers/GarageDoor.cs @@ -0,0 +1,24 @@ +using System; + +namespace RemoteControl.Receivers +{ + public class GarageDoor + { + private readonly string _location; + + public GarageDoor(string location) + { + _location = location; + } + + public void Up() => Console.WriteLine(_location + " garage Door is Open"); + + public void Down() => Console.WriteLine(_location + " garage Door is Closed"); + + public void Stop() => Console.WriteLine(_location + " garage Door is Stopped"); + + public void LightOn() => Console.WriteLine(_location + " garage light is on"); + + public void LightOff() => Console.WriteLine(_location + " garage light is off"); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Receivers/Hottub.cs b/src/command/RemoteControl/Receivers/Hottub.cs new file mode 100644 index 0000000..bb64863 --- /dev/null +++ b/src/command/RemoteControl/Receivers/Hottub.cs @@ -0,0 +1,60 @@ +using System; + +namespace RemoteControl.Receivers +{ + public class Hottub + { + private bool _on; + private int _temperature; + + public void On() => _on = true; + + public void Off() => _on = false; + + public void BubblesOn() + { + if (_on) + { + Console.WriteLine("Hottub is bubbling!"); + } + } + + public void BubblesOff() + { + if (_on) + { + Console.WriteLine("Hottub is not bubbling!"); + } + } + + public void JetsOn() + { + if (_on) + { + Console.WriteLine("Hottub jets are on"); + } + } + + public void JetsOff() + { + if (_on) + { + Console.WriteLine("Hottub jets are off"); + } + } + + public void SetTemperature(int temperature) => _temperature = temperature; + + public void Heat() + { + _temperature = 105; + Console.WriteLine("Hottub is heating to a steaming 105 degrees"); + } + + public void Cool() + { + _temperature = 98; + Console.WriteLine("Hottub is cooling to 98 degrees"); + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Receivers/Light.cs b/src/command/RemoteControl/Receivers/Light.cs new file mode 100644 index 0000000..23d45fc --- /dev/null +++ b/src/command/RemoteControl/Receivers/Light.cs @@ -0,0 +1,18 @@ +using System; + +namespace RemoteControl.Receivers +{ + public class Light + { + private readonly string _location; + + public Light(string location) + { + _location = location; + } + + public void On() => Console.WriteLine(_location + " light is on"); + + public void Off() => Console.WriteLine(_location + " light is off"); + } +} diff --git a/src/command/RemoteControl/Receivers/Stereo.cs b/src/command/RemoteControl/Receivers/Stereo.cs new file mode 100644 index 0000000..3bed747 --- /dev/null +++ b/src/command/RemoteControl/Receivers/Stereo.cs @@ -0,0 +1,26 @@ +using System; + +namespace RemoteControl.Receivers +{ + public class Stereo + { + private readonly string _location; + + public Stereo(string location) + { + _location = location; + } + + public void On() => Console.WriteLine(_location + " stereo is on"); + + public void Off() => Console.WriteLine(_location + " stereo is off"); + + public void SetCD() => Console.WriteLine(_location + " stereo is set for CD input"); + + public void SetDVD() => Console.WriteLine(_location + " stereo is set for DVD input"); + + public void SetRadio() => Console.WriteLine(_location + " stereo is set for Radio"); + + public void SetVolume(int volume) => Console.WriteLine(_location + " stereo volume set to " + volume); + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/Receivers/TV.cs b/src/command/RemoteControl/Receivers/TV.cs new file mode 100644 index 0000000..bb81af1 --- /dev/null +++ b/src/command/RemoteControl/Receivers/TV.cs @@ -0,0 +1,25 @@ +using System; + +namespace RemoteControl.Receivers +{ + public class TV + { + private string _location; + private int _channel; + + public TV(string location) + { + _location = location; + } + + public void On() => Console.WriteLine("TV is on"); + + public void Off() => Console.WriteLine("TV is off"); + + public void SetInputChannel() + { + _channel = 3; + Console.WriteLine("Channel is set for VCR"); + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControl/RemoteControl.csproj b/src/command/RemoteControl/RemoteControl.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/command/RemoteControl/RemoteControl.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/command/RemoteControlWithUndo/Commands/Abstractions/ICommand.cs b/src/command/RemoteControlWithUndo/Commands/Abstractions/ICommand.cs new file mode 100644 index 0000000..8babf0a --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/Abstractions/ICommand.cs @@ -0,0 +1,8 @@ +namespace RemoteControlWithUndo.Commands +{ + public interface ICommand + { + void Execute(); + void Undo(); + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/CeilingFanHighCommand.cs b/src/command/RemoteControlWithUndo/Commands/CeilingFanHighCommand.cs new file mode 100644 index 0000000..5d28173 --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/CeilingFanHighCommand.cs @@ -0,0 +1,41 @@ +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo.Commands +{ + public class CeilingFanHighCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + private int _prevSpeed; + + public CeilingFanHighCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() + { + _prevSpeed = _ceilingFan.GetSpeed(); + _ceilingFan.High(); + } + + public void Undo() + { + switch (_prevSpeed) + { + case CeilingFan.HIGH: + _ceilingFan.High(); + break; + case CeilingFan.MEDIUM: + _ceilingFan.Medium(); + break; + case CeilingFan.LOW: + _ceilingFan.Low(); + break; + case CeilingFan.OFF: + _ceilingFan.Off(); + break; + } + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/CeilingFanLowCommand.cs b/src/command/RemoteControlWithUndo/Commands/CeilingFanLowCommand.cs new file mode 100644 index 0000000..858c522 --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/CeilingFanLowCommand.cs @@ -0,0 +1,41 @@ +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo.Commands +{ + public class CeilingFanLowCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + private int _prevSpeed; + + public CeilingFanLowCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() + { + _prevSpeed = _ceilingFan.GetSpeed(); + _ceilingFan.Low(); + } + + public void Undo() + { + switch (_prevSpeed) + { + case CeilingFan.HIGH: + _ceilingFan.High(); + break; + case CeilingFan.MEDIUM: + _ceilingFan.Medium(); + break; + case CeilingFan.LOW: + _ceilingFan.Low(); + break; + case CeilingFan.OFF: + _ceilingFan.Off(); + break; + } + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/CeilingFanMediumCommand.cs b/src/command/RemoteControlWithUndo/Commands/CeilingFanMediumCommand.cs new file mode 100644 index 0000000..49b651e --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/CeilingFanMediumCommand.cs @@ -0,0 +1,41 @@ +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo.Commands +{ + public class CeilingFanMediumCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + private int _prevSpeed; + + public CeilingFanMediumCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() + { + _prevSpeed = _ceilingFan.GetSpeed(); + _ceilingFan.Medium(); + } + + public void Undo() + { + switch (_prevSpeed) + { + case CeilingFan.HIGH: + _ceilingFan.High(); + break; + case CeilingFan.MEDIUM: + _ceilingFan.Medium(); + break; + case CeilingFan.LOW: + _ceilingFan.Low(); + break; + case CeilingFan.OFF: + _ceilingFan.Off(); + break; + } + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/CeilingFanOffCommand.cs b/src/command/RemoteControlWithUndo/Commands/CeilingFanOffCommand.cs new file mode 100644 index 0000000..ee09e1b --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/CeilingFanOffCommand.cs @@ -0,0 +1,41 @@ +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo.Commands +{ + public class CeilingFanOffCommand : ICommand + { + private readonly CeilingFan _ceilingFan; + private int _prevSpeed; + + public CeilingFanOffCommand(CeilingFan ceilingFan) + { + _ceilingFan = ceilingFan; + } + + public void Execute() + { + _prevSpeed = _ceilingFan.GetSpeed(); + _ceilingFan.Off(); + } + + public void Undo() + { + switch (_prevSpeed) + { + case CeilingFan.HIGH: + _ceilingFan.High(); + break; + case CeilingFan.MEDIUM: + _ceilingFan.Medium(); + break; + case CeilingFan.LOW: + _ceilingFan.Low(); + break; + case CeilingFan.OFF: + _ceilingFan.Off(); + break; + } + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/DimmerLightOffCommand.cs b/src/command/RemoteControlWithUndo/Commands/DimmerLightOffCommand.cs new file mode 100644 index 0000000..68927df --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/DimmerLightOffCommand.cs @@ -0,0 +1,25 @@ +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo.Commands +{ + public class DimmerLightOffCommand : ICommand + { + private readonly Light _light; + private int _prevLevel; + + public DimmerLightOffCommand(Light light) + { + _light = light; + _prevLevel = 100; + } + + public void Execute() + { + _prevLevel = _light.GetLevel(); + _light.Off(); + } + + public void Undo() => _light.Dim(_prevLevel); + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/DimmerLightOnCommand.cs b/src/command/RemoteControlWithUndo/Commands/DimmerLightOnCommand.cs new file mode 100644 index 0000000..c3062bc --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/DimmerLightOnCommand.cs @@ -0,0 +1,24 @@ +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo.Commands +{ + public class DimmerLightOnCommand : ICommand + { + private readonly Light _light; + private int _prevLevel; + + public DimmerLightOnCommand(Light light) + { + _light = light; + } + + public void Execute() + { + _prevLevel = _light.GetLevel(); + _light.Dim(75); + } + + public void Undo() => _light.Dim(_prevLevel); + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/LightOffCommand.cs b/src/command/RemoteControlWithUndo/Commands/LightOffCommand.cs new file mode 100644 index 0000000..89befb0 --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/LightOffCommand.cs @@ -0,0 +1,24 @@ +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo.Commands +{ + public class LightOffCommand : ICommand + { + private readonly Light _light; + private int _level; + + public LightOffCommand(Light light) + { + _light = light; + } + + public void Execute() + { + _level = _light.GetLevel(); + _light.Off(); + } + + public void Undo() => _light.Dim(_level); + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/LightOnCommand.cs b/src/command/RemoteControlWithUndo/Commands/LightOnCommand.cs new file mode 100644 index 0000000..b0bd23a --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/LightOnCommand.cs @@ -0,0 +1,24 @@ +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo.Commands +{ + public class LightOnCommand : ICommand + { + private readonly Light _light; + private int _level; + + public LightOnCommand(Light light) + { + _light = light; + } + + public void Execute() + { + _level = _light.GetLevel(); + _light.On(); + } + + public void Undo() => _light.Dim(_level); + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Commands/NoCommand.cs b/src/command/RemoteControlWithUndo/Commands/NoCommand.cs new file mode 100644 index 0000000..bb1bb70 --- /dev/null +++ b/src/command/RemoteControlWithUndo/Commands/NoCommand.cs @@ -0,0 +1,10 @@ +using RemoteControlWithUndo.Commands; + +namespace RemoteControlWithUndo.Commands +{ + public class NoCommand : ICommand + { + public void Execute() {} + public void Undo() {} + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Invokers/RemoteControlWithUndo.cs b/src/command/RemoteControlWithUndo/Invokers/RemoteControlWithUndo.cs new file mode 100644 index 0000000..d6ba87e --- /dev/null +++ b/src/command/RemoteControlWithUndo/Invokers/RemoteControlWithUndo.cs @@ -0,0 +1,63 @@ +using System.Text; +using RemoteControlWithUndo.Commands; + +namespace RemoteControlWithUndo.Invokers +{ + // + // This is the invoker + // + public class RemoteControlWithUndo + { + private readonly ICommand[] _onCommands; + private readonly ICommand[] _offCommands; + private ICommand _undoCommand; + public RemoteControlWithUndo() + { + _onCommands = new ICommand[7]; + _offCommands = new ICommand[7]; + + ICommand noCommand = new NoCommand(); + for (int i = 0; i < 7; i++) + { + _onCommands[i] = noCommand; + _offCommands[i] = noCommand; + } + _undoCommand = new NoCommand(); + } + + public void SetCommand(int slot, ICommand onCommand, ICommand offCommand) + { + _onCommands[slot] = onCommand; + _offCommands[slot] = offCommand; + } + + public void OnButtonWasPushed(int slot) + { + _onCommands[slot].Execute(); + _undoCommand = _onCommands[slot]; + } + + public void OffButtonWasPushed(int slot) + { + _offCommands[slot].Execute(); + _undoCommand = _offCommands[slot]; + } + + public void UndoButtonWasPushed() => _undoCommand.Undo(); + + public override string ToString() + { + var stringBuilder= new StringBuilder(); + stringBuilder.Append("\n------ Remote Control -------\n"); + + for (int i = 0; i < _onCommands.Length; i++) + { + stringBuilder.Append("[slot " + i + "] " + _onCommands[i].GetType().Name + " " + _offCommands[i].GetType().Name + "\n"); + } + + stringBuilder.Append("[undo] " + _undoCommand.GetType().Name + "\n"); + + return stringBuilder.ToString(); + } + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Program.cs b/src/command/RemoteControlWithUndo/Program.cs new file mode 100644 index 0000000..cb5ce33 --- /dev/null +++ b/src/command/RemoteControlWithUndo/Program.cs @@ -0,0 +1,48 @@ +using System; +using RemoteControlWithUndo.Commands; +using RemoteControlWithUndo.Receivers; + +namespace RemoteControlWithUndo +{ + public class Program + { + public static void Main(string[] args) + { + var remoteControl = new Invokers.RemoteControlWithUndo(); + + Light livingRoomLight = new Light("Living Room"); + + LightOnCommand livingRoomLightOn = new LightOnCommand(livingRoomLight); + LightOffCommand livingRoomLightOff = new LightOffCommand(livingRoomLight); + + remoteControl.SetCommand(0, livingRoomLightOn, livingRoomLightOff); + + remoteControl.OnButtonWasPushed(0); + remoteControl.OffButtonWasPushed(0); + Console.WriteLine(remoteControl); + remoteControl.UndoButtonWasPushed(); + remoteControl.OffButtonWasPushed(0); + remoteControl.OnButtonWasPushed(0); + Console.WriteLine(remoteControl); + remoteControl.UndoButtonWasPushed(); + + CeilingFan ceilingFan = new CeilingFan("Living Room"); + + CeilingFanMediumCommand ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan); + CeilingFanHighCommand ceilingFanHigh = new CeilingFanHighCommand(ceilingFan); + CeilingFanOffCommand ceilingFanOff = new CeilingFanOffCommand(ceilingFan); + + remoteControl.SetCommand(0, ceilingFanMedium, ceilingFanOff); + remoteControl.SetCommand(1, ceilingFanHigh, ceilingFanOff); + + remoteControl.OnButtonWasPushed(0); + remoteControl.OffButtonWasPushed(0); + Console.WriteLine(remoteControl); + remoteControl.UndoButtonWasPushed(); + + remoteControl.OnButtonWasPushed(1); + Console.WriteLine(remoteControl); + remoteControl.UndoButtonWasPushed(); + } + } +} diff --git a/src/command/RemoteControlWithUndo/Receivers/CeilingFan.cs b/src/command/RemoteControlWithUndo/Receivers/CeilingFan.cs new file mode 100644 index 0000000..bd5874f --- /dev/null +++ b/src/command/RemoteControlWithUndo/Receivers/CeilingFan.cs @@ -0,0 +1,50 @@ +using System; + +namespace RemoteControlWithUndo.Receivers +{ + public class CeilingFan + { + private readonly string _location; + private int _speed; + + public const int HIGH = 3; + public const int MEDIUM = 2; + public const int LOW = 1; + public const int OFF = 0; + + public CeilingFan(string location) + { + _location = location; + } + + public void High() + { + // turns the ceiling fan on to high + _speed = HIGH; + Console.WriteLine(_location + " ceiling fan is on high"); + } + + public void Medium() + { + // turns the ceiling fan on to medium + _speed = MEDIUM; + Console.WriteLine(_location + " ceiling fan is on medium"); + } + + public void Low() + { + // turns the ceiling fan on to low + _speed = LOW; + Console.WriteLine(_location + " ceiling fan is on low"); + } + + public void Off() + { + // turns the ceiling fan off + _speed = OFF; + Console.WriteLine(_location + " ceiling fan is off"); + } + + public int GetSpeed() => _speed; + } +} \ No newline at end of file diff --git a/src/command/RemoteControlWithUndo/Receivers/Light.cs b/src/command/RemoteControlWithUndo/Receivers/Light.cs new file mode 100644 index 0000000..78b978b --- /dev/null +++ b/src/command/RemoteControlWithUndo/Receivers/Light.cs @@ -0,0 +1,42 @@ +using System; + +namespace RemoteControlWithUndo.Receivers +{ + public class Light + { + private readonly string _location; + private int _level; + + public Light(string location) + { + _location = location; + } + + public void On() + { + _level = 100; + Console.WriteLine(_location + " light is on"); + } + + public void Off() + { + _level = 0; + Console.WriteLine(_location + " light is off"); + } + + public void Dim(int level) + { + _level = level; + if (level == 0) + { + Off(); + } + else + { + Console.WriteLine("Light is dimmed to " + level + "%"); + } + } + + public int GetLevel() => _level; + } +} diff --git a/src/command/RemoteControlWithUndo/RemoteControlWithUndo.csproj b/src/command/RemoteControlWithUndo/RemoteControlWithUndo.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/command/RemoteControlWithUndo/RemoteControlWithUndo.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/command/SimpleRemoteControl/Commands/Abstractions/ICommand.cs b/src/command/SimpleRemoteControl/Commands/Abstractions/ICommand.cs new file mode 100644 index 0000000..cf50ebc --- /dev/null +++ b/src/command/SimpleRemoteControl/Commands/Abstractions/ICommand.cs @@ -0,0 +1,7 @@ +namespace SimpleRemoteControl.Commands +{ + public interface ICommand + { + void Execute(); + } +} \ No newline at end of file diff --git a/src/command/SimpleRemoteControl/Commands/GarageDoorOpenCommand.cs b/src/command/SimpleRemoteControl/Commands/GarageDoorOpenCommand.cs new file mode 100644 index 0000000..cd953c6 --- /dev/null +++ b/src/command/SimpleRemoteControl/Commands/GarageDoorOpenCommand.cs @@ -0,0 +1,16 @@ +using SimpleRemoteControl.Receivers; + +namespace SimpleRemoteControl.Commands +{ + public class GarageDoorOpenCommand : ICommand + { + private readonly GarageDoor _garageDoor; + + public GarageDoorOpenCommand(GarageDoor garageDoor) + { + _garageDoor = garageDoor; + } + + public void Execute() => _garageDoor.Up(); + } +} \ No newline at end of file diff --git a/src/command/SimpleRemoteControl/Commands/LightOffCommand.cs b/src/command/SimpleRemoteControl/Commands/LightOffCommand.cs new file mode 100644 index 0000000..12f73fe --- /dev/null +++ b/src/command/SimpleRemoteControl/Commands/LightOffCommand.cs @@ -0,0 +1,16 @@ +using SimpleRemoteControl.Receivers; + +namespace SimpleRemoteControl.Commands +{ + public class LightOffCommand : ICommand + { + private readonly Light _light; + + public LightOffCommand(Light light) + { + _light = light; + } + + public void Execute() => _light.Off(); + } +} \ No newline at end of file diff --git a/src/command/SimpleRemoteControl/Commands/LightOnCommand.cs b/src/command/SimpleRemoteControl/Commands/LightOnCommand.cs new file mode 100644 index 0000000..58a5a52 --- /dev/null +++ b/src/command/SimpleRemoteControl/Commands/LightOnCommand.cs @@ -0,0 +1,16 @@ +using SimpleRemoteControl.Receivers; + +namespace SimpleRemoteControl.Commands +{ + public class LightOnCommand : ICommand + { + private readonly Light _light; + + public LightOnCommand(Light light) + { + _light = light; + } + + public void Execute() => _light.On(); + } +} \ No newline at end of file diff --git a/src/command/SimpleRemoteControl/Invokers/SimpleRemoteControl.cs b/src/command/SimpleRemoteControl/Invokers/SimpleRemoteControl.cs new file mode 100644 index 0000000..bb90133 --- /dev/null +++ b/src/command/SimpleRemoteControl/Invokers/SimpleRemoteControl.cs @@ -0,0 +1,14 @@ +using SimpleRemoteControl.Commands; + +namespace SimpleRemoteControl.Invokers +{ + //This is our invoker + public class SimpleRemoteControl + { + private ICommand _slot; + + public void SetCommand(ICommand command) => _slot = command; + + public void ButtonWasPressed() => _slot.Execute(); + } +} \ No newline at end of file diff --git a/src/command/SimpleRemoteControl/Program.cs b/src/command/SimpleRemoteControl/Program.cs new file mode 100644 index 0000000..7dab52b --- /dev/null +++ b/src/command/SimpleRemoteControl/Program.cs @@ -0,0 +1,26 @@ +using System; +using SimpleRemoteControl.Commands; +using SimpleRemoteControl.Receivers; + +namespace SimpleRemoteControl +{ + public class Program + { + public static void Main(string[] args) + { + var remote = new Invokers.SimpleRemoteControl(); + + var light = new Light(); + var garageDoor = new GarageDoor(); + + var lightOn = new LightOnCommand(light); + var garageOpen = new GarageDoorOpenCommand(garageDoor); + + remote.SetCommand(lightOn); + remote.ButtonWasPressed(); + + remote.SetCommand(garageOpen); + remote.ButtonWasPressed(); + } + } +} diff --git a/src/command/SimpleRemoteControl/Receivers/GarageDoor.cs b/src/command/SimpleRemoteControl/Receivers/GarageDoor.cs new file mode 100644 index 0000000..de42c57 --- /dev/null +++ b/src/command/SimpleRemoteControl/Receivers/GarageDoor.cs @@ -0,0 +1,17 @@ +using System; + +namespace SimpleRemoteControl.Receivers +{ + public class GarageDoor + { + public void Up() => Console.WriteLine("Garage Door is Open"); + + public void Down() => Console.WriteLine("Garage Door is Closed"); + + public void Stop() => Console.WriteLine("Garage Door is Stopped"); + + public void LightOn() => Console.WriteLine("Garage light is on"); + + public void LightOff() => Console.WriteLine("Garage light is off"); + } +} \ No newline at end of file diff --git a/src/command/SimpleRemoteControl/Receivers/Light.cs b/src/command/SimpleRemoteControl/Receivers/Light.cs new file mode 100644 index 0000000..378c878 --- /dev/null +++ b/src/command/SimpleRemoteControl/Receivers/Light.cs @@ -0,0 +1,11 @@ +using System; + +namespace SimpleRemoteControl.Receivers +{ + public class Light + { + public void On() => Console.WriteLine("Light is on"); + + public void Off() => Console.WriteLine("Light is off"); + } +} diff --git a/src/command/SimpleRemoteControl/SimpleRemoteControl.csproj b/src/command/SimpleRemoteControl/SimpleRemoteControl.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/command/SimpleRemoteControl/SimpleRemoteControl.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/composite/Menu/Menu.cs b/src/composite/Menu/Menu.cs new file mode 100644 index 0000000..4aee46a --- /dev/null +++ b/src/composite/Menu/Menu.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; + +namespace Menu +{ + public class Menu : MenuComponent + { + private readonly List _menuComponents = new List(); + + public Menu(string name, string description) + { + Name = name; + Description = description; + } + + public override string Name { get; } + + public override string Description { get; } + + public override void Add(MenuComponent menuComponent) + { + _menuComponents.Add(menuComponent); + } + + public override void Remove(MenuComponent menuComponent) + { + _menuComponents.Remove(menuComponent); + } + + public override MenuComponent GetChild(int i) + { + return _menuComponents[i]; + } + + public override void Print() + { + Console.Write("\n" + Name); + Console.WriteLine(", " + Description); + Console.WriteLine("---------------------"); + + var iterator = _menuComponents.GetEnumerator(); + while (iterator.MoveNext()) + { + MenuComponent menuComponent = iterator.Current; + menuComponent?.Print(); + } + } + } +} \ No newline at end of file diff --git a/src/composite/Menu/Menu.csproj b/src/composite/Menu/Menu.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/composite/Menu/Menu.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/composite/Menu/MenuComponent.cs b/src/composite/Menu/MenuComponent.cs new file mode 100644 index 0000000..6155333 --- /dev/null +++ b/src/composite/Menu/MenuComponent.cs @@ -0,0 +1,58 @@ +using System; + +namespace Menu +{ + public abstract class MenuComponent + { + public virtual string Name + { + get + { + throw new NotImplementedException(); + } + } + + public virtual string Description + { + get + { + throw new NotImplementedException(); + } + } + + public virtual decimal Price + { + get + { + throw new NotImplementedException(); + } + } + + public virtual bool IsVegetarian + { + get + { + throw new NotImplementedException(); + } + } + + public virtual void Add(MenuComponent menuComponent) + { + throw new NotImplementedException(); + } + + public virtual void Remove(MenuComponent menuComponent) + { + throw new NotImplementedException(); + } + public virtual MenuComponent GetChild(int i) + { + throw new NotImplementedException(); + } + + public virtual void Print() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/composite/Menu/MenuItem.cs b/src/composite/Menu/MenuItem.cs new file mode 100644 index 0000000..568bc27 --- /dev/null +++ b/src/composite/Menu/MenuItem.cs @@ -0,0 +1,32 @@ +using System; +using System.Globalization; + +namespace Menu +{ + public class MenuItem : MenuComponent + { + public MenuItem(string name, string description, bool vegetarian, decimal price) + { + Name = name; + Description = description; + IsVegetarian = vegetarian; + Price = price; + } + + public override string Name { get; } + public override string Description { get; } + public override bool IsVegetarian { get; } + public override decimal Price { get; } + + public override void Print() + { + Console.Write(" " + Name); + if (IsVegetarian) + { + Console.Write("(v)"); + } + Console.WriteLine(", " + Price.ToString(CultureInfo.InvariantCulture)); + Console.WriteLine(" --" + Description); + } + } +} \ No newline at end of file diff --git a/src/composite/Menu/Program.cs b/src/composite/Menu/Program.cs new file mode 100644 index 0000000..cda6d11 --- /dev/null +++ b/src/composite/Menu/Program.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Menu +{ + public class Program + { + public static void Main(string[] args) + { + MenuComponent pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast"); + MenuComponent dinerMenu = new Menu("DINER MENU", "Lunch"); + MenuComponent cafeMenu = new Menu("CAFE MENU", "Dinner"); + MenuComponent dessertMenu = new Menu("DESSERT MENU", "Dessert of course!"); + MenuComponent coffeeMenu = new Menu("COFFEE MENU", "Stuff to go with your afternoon coffee"); + + MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined"); + + allMenus.Add(pancakeHouseMenu); + allMenus.Add(dinerMenu); + allMenus.Add(cafeMenu); + + pancakeHouseMenu.Add(new MenuItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99m)); + pancakeHouseMenu.Add(new MenuItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99m)); + pancakeHouseMenu.Add(new MenuItem("Blueberry Pancakes", "Pancakes made with fresh blueberries, and blueberry syrup", true, 3.49m)); + pancakeHouseMenu.Add(new MenuItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59m)); + + dinerMenu.Add(new MenuItem("Vegetarian BLT", "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99m)); + dinerMenu.Add(new MenuItem("BLT", "Bacon with lettuce & tomato on whole wheat", false, 2.99m)); + dinerMenu.Add(new MenuItem("Soup of the day", "A bowl of the soup of the day, with a side of potato salad", false, 3.29m)); + dinerMenu.Add(new MenuItem("Hotdog", "A hot dog, with sauerkraut, relish, onions, topped with cheese", false, 3.05m)); + dinerMenu.Add(new MenuItem("Steamed Veggies and Brown Rice", "Steamed vegetables over brown rice", true, 3.99m)); + dinerMenu.Add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89m)); + + dinerMenu.Add(dessertMenu); + + dessertMenu.Add(new MenuItem("Apple Pie", "Apple pie with a flakey crust, topped with vanilla icecream", true, 1.59m)); + dessertMenu.Add(new MenuItem("Cheesecake", "Creamy New York cheesecake, with a chocolate graham crust", true, 1.99m)); + dessertMenu.Add(new MenuItem("Sorbet", "A scoop of raspberry and a scoop of lime", true, 1.89m)); + + cafeMenu.Add(new MenuItem("Veggie Burger and Air Fries", "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", true, 3.99m)); + cafeMenu.Add(new MenuItem("Soup of the day", "A cup of the soup of the day, with a side salad", false, 3.69m)); + cafeMenu.Add(new MenuItem("Burrito", "A large burrito, with whole pinto beans, salsa, guacamole", true, 4.29m)); + + cafeMenu.Add(coffeeMenu); + + coffeeMenu.Add(new MenuItem("Coffee Cake", "Crumbly cake topped with cinnamon and walnuts", true, 1.59m)); + coffeeMenu.Add(new MenuItem("Bagel", "Flavors include sesame, poppyseed, cinnamon raisin, pumpkin", false, 0.69m)); + coffeeMenu.Add(new MenuItem("Biscotti", "Three almond or hazelnut biscotti cookies", true, 0.89m)); + + Waitress waitress = new Waitress(allMenus); + + waitress.PrintMenu(); + } + } +} diff --git a/src/composite/Menu/Waitress.cs b/src/composite/Menu/Waitress.cs new file mode 100644 index 0000000..3160c42 --- /dev/null +++ b/src/composite/Menu/Waitress.cs @@ -0,0 +1,17 @@ +namespace Menu +{ + public class Waitress + { + private readonly MenuComponent _allMenus; + + public Waitress(MenuComponent allMenus) + { + _allMenus = allMenus; + } + + public void PrintMenu() + { + _allMenus.Print(); + } + } +} \ No newline at end of file diff --git a/src/composite/MenuIterator/CompositeIterator.cs b/src/composite/MenuIterator/CompositeIterator.cs new file mode 100644 index 0000000..4788122 --- /dev/null +++ b/src/composite/MenuIterator/CompositeIterator.cs @@ -0,0 +1,59 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace MenuIterator +{ + public class CompositeIterator : IEnumerator + { + private readonly IEnumerator _iterator; + private readonly Stack> _stack = new Stack>(); + private MenuComponent _current; + + public CompositeIterator(IEnumerator iterator) + { + _iterator = iterator; + _stack.Push(iterator); + } + + public void Dispose() + { + } + + public bool MoveNext() + { + if (!_stack.Any()) + { + _current = null; + return false; + } + + var iterator = _stack.Peek(); + if (!iterator.MoveNext()) + { + _stack.Pop(); + return MoveNext(); + } + + _current = iterator.Current; + + if (_current is Menu) + { + _stack.Push(_current.CreateIterator()); + } + + return true; + } + + public void Reset() + { + _current = null; + _stack.Clear(); + _stack.Push(_iterator); + } + + public MenuComponent Current => _current; + + object IEnumerator.Current => Current; + } +} \ No newline at end of file diff --git a/src/composite/MenuIterator/Menu.cs b/src/composite/MenuIterator/Menu.cs new file mode 100644 index 0000000..0c07070 --- /dev/null +++ b/src/composite/MenuIterator/Menu.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; + +namespace MenuIterator +{ + public class Menu : MenuComponent + { + private readonly List _menuComponents = new List(); + private CompositeIterator _iterator; + + public Menu(string name, string description) + { + Name = name; + Description = description; + } + + public override string Name { get; } + + public override string Description { get; } + + public override void Add(MenuComponent menuComponent) + { + _menuComponents.Add(menuComponent); + } + + public override void Remove(MenuComponent menuComponent) + { + _menuComponents.Remove(menuComponent); + } + + public override MenuComponent GetChild(int i) + { + return _menuComponents[i]; + } + + public override IEnumerator CreateIterator() + { + return _iterator ?? (_iterator = new CompositeIterator(_menuComponents.GetEnumerator())); + } + + public override void Print() + { + Console.Write("\n" + Name); + Console.WriteLine(", " + Description); + Console.WriteLine("---------------------"); + + var iterator = _menuComponents.GetEnumerator(); + while (iterator.MoveNext()) + { + MenuComponent menuComponent = iterator.Current; + menuComponent?.Print(); + } + } + } +} \ No newline at end of file diff --git a/src/composite/MenuIterator/MenuComponent.cs b/src/composite/MenuIterator/MenuComponent.cs new file mode 100644 index 0000000..96a8237 --- /dev/null +++ b/src/composite/MenuIterator/MenuComponent.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; + +namespace MenuIterator +{ + public abstract class MenuComponent + { + public virtual string Name + { + get + { + throw new NotImplementedException(); + } + } + + public virtual string Description + { + get + { + throw new NotImplementedException(); + } + } + + public virtual decimal Price + { + get + { + throw new NotImplementedException(); + } + } + + public virtual bool IsVegetarian + { + get + { + throw new NotImplementedException(); + } + } + + public virtual void Add(MenuComponent menuComponent) + { + throw new NotImplementedException(); + } + + public virtual void Remove(MenuComponent menuComponent) + { + throw new NotImplementedException(); + } + public virtual MenuComponent GetChild(int i) + { + throw new NotImplementedException(); + } + + public abstract IEnumerator CreateIterator(); + + public virtual void Print() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/src/composite/MenuIterator/MenuItem.cs b/src/composite/MenuIterator/MenuItem.cs new file mode 100644 index 0000000..9c27dee --- /dev/null +++ b/src/composite/MenuIterator/MenuItem.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace MenuIterator +{ + public class MenuItem : MenuComponent + { + public MenuItem(string name, string description, bool vegetarian, decimal price) + { + Name = name; + Description = description; + IsVegetarian = vegetarian; + Price = price; + } + + public override string Name { get; } + public override string Description { get; } + public override bool IsVegetarian { get; } + public override decimal Price { get; } + + public override IEnumerator CreateIterator() + { + return new NullIterator(); + } + + public override void Print() + { + Console.Write(" " + Name); + if (IsVegetarian) + { + Console.Write("(v)"); + } + Console.WriteLine(", " + Price.ToString(CultureInfo.InvariantCulture)); + Console.WriteLine(" --" + Description); + } + } +} \ No newline at end of file diff --git a/src/composite/MenuIterator/MenuIterator.csproj b/src/composite/MenuIterator/MenuIterator.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/composite/MenuIterator/MenuIterator.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/composite/MenuIterator/NullIterator.cs b/src/composite/MenuIterator/NullIterator.cs new file mode 100644 index 0000000..ed8673e --- /dev/null +++ b/src/composite/MenuIterator/NullIterator.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; + +namespace MenuIterator +{ + public class NullIterator : IEnumerator + { + public void Dispose() { } + + public bool MoveNext() => false; + + public void Reset() { } + + public MenuComponent Current => null; + + object IEnumerator.Current => Current; + } +} \ No newline at end of file diff --git a/src/composite/MenuIterator/Program.cs b/src/composite/MenuIterator/Program.cs new file mode 100644 index 0000000..2017f6f --- /dev/null +++ b/src/composite/MenuIterator/Program.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace MenuIterator +{ + public class Program + { + public static void Main(string[] args) + { + MenuComponent pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast"); + MenuComponent dinerMenu = new Menu("DINER MENU", "Lunch"); + MenuComponent cafeMenu = new Menu("CAFE MENU", "Dinner"); + MenuComponent dessertMenu = new Menu("DESSERT MENU", "Dessert of course!"); + + MenuComponent allMenus = new Menu("ALL MENUS", "All menus combined"); + + allMenus.Add(pancakeHouseMenu); + allMenus.Add(dinerMenu); + allMenus.Add(cafeMenu); + + pancakeHouseMenu.Add(new MenuItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99m)); + pancakeHouseMenu.Add(new MenuItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99m)); + pancakeHouseMenu.Add(new MenuItem("Blueberry Pancakes", "Pancakes made with fresh blueberries, and blueberry syrup", true, 3.49m)); + pancakeHouseMenu.Add(new MenuItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59m)); + + dinerMenu.Add(new MenuItem("Vegetarian BLT", "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99m)); + dinerMenu.Add(new MenuItem("BLT", "Bacon with lettuce & tomato on whole wheat", false, 2.99m)); + dinerMenu.Add(new MenuItem("Soup of the day", "A bowl of the soup of the day, with a side of potato salad", false, 3.29m)); + dinerMenu.Add(new MenuItem("Hotdog", "A hot dog, with sauerkraut, relish, onions, topped with cheese", false, 3.05m)); + dinerMenu.Add(new MenuItem("Steamed Veggies and Brown Rice", "A medly of steamed vegetables over brown rice", true, 3.99m)); + + dinerMenu.Add(new MenuItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89m)); + + dinerMenu.Add(dessertMenu); + + dessertMenu.Add(new MenuItem("Apple Pie", "Apple pie with a flakey crust, topped with vanilla icecream", true, 1.59m)); + dessertMenu.Add(new MenuItem("Cheesecake", "Creamy New York cheesecake, with a chocolate graham crust", true, 1.99m)); + dessertMenu.Add(new MenuItem("Sorbet", "A scoop of raspberry and a scoop of lime", true, 1.89m)); + + cafeMenu.Add(new MenuItem("Veggie Burger and Air Fries", "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", true, 3.99m)); + cafeMenu.Add(new MenuItem("Soup of the day", "A cup of the soup of the day, with a side salad", false, 3.69m)); + cafeMenu.Add(new MenuItem("Burrito", "A large burrito, with whole pinto beans, salsa, guacamole", true, 4.29m)); + + Waitress waitress = new Waitress(allMenus); + + waitress.PrintVegetarianMenu(); + } + } +} diff --git a/src/composite/MenuIterator/Waitress.cs b/src/composite/MenuIterator/Waitress.cs new file mode 100644 index 0000000..c6cb492 --- /dev/null +++ b/src/composite/MenuIterator/Waitress.cs @@ -0,0 +1,38 @@ +using System; + +namespace MenuIterator +{ + public class Waitress + { + private readonly MenuComponent _allMenus; + + public Waitress(MenuComponent allMenus) + { + _allMenus = allMenus; + } + + public void PrintMenu() + { + _allMenus.Print(); + } + + public void PrintVegetarianMenu() + { + var iterator = _allMenus.CreateIterator(); + + Console.WriteLine("\nVEGETARIAN MENU\n----"); + while (iterator.MoveNext()) + { + MenuComponent menuComponent = iterator.Current; + try + { + if (menuComponent.IsVegetarian) + { + menuComponent.Print(); + } + } + catch (NotImplementedException) { } + } + } + } +} \ No newline at end of file diff --git a/src/composite/README.md b/src/composite/README.md new file mode 100644 index 0000000..226163f --- /dev/null +++ b/src/composite/README.md @@ -0,0 +1,2 @@ +**The Composite Pattern** allows you to compose objects into tree structures to represent part-whole hierarchies. +Composite lets clients treat individual objects and compositions of objects uniformly. \ No newline at end of file diff --git a/src/decorator/README.md b/src/decorator/README.md new file mode 100644 index 0000000..832c583 --- /dev/null +++ b/src/decorator/README.md @@ -0,0 +1,2 @@ +**The Decorator Pattern** attaches additional responsibilities to an object dynamically. +Decorators provide a flexible alternative to subclassing for extending functionality. \ No newline at end of file diff --git a/src/decorator/StarbuzzCoffee/Components/Abstractions/Beverage.cs b/src/decorator/StarbuzzCoffee/Components/Abstractions/Beverage.cs new file mode 100644 index 0000000..bd70e3a --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Components/Abstractions/Beverage.cs @@ -0,0 +1,9 @@ +namespace StarbuzzCoffee.Components +{ + public abstract class Beverage + { + public virtual string Description { get; protected set; } = "Unknown Beverage"; + + public abstract double Cost(); + } +} diff --git a/src/decorator/StarbuzzCoffee/Components/DarkRoast.cs b/src/decorator/StarbuzzCoffee/Components/DarkRoast.cs new file mode 100644 index 0000000..a46f1f6 --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Components/DarkRoast.cs @@ -0,0 +1,17 @@ +using StarbuzzCoffee.Components; + +namespace StarbuzzCoffee.Components +{ + public class DarkRoast : Beverage + { + public DarkRoast() + { + Description = "Dark Roast Coffee"; + } + + public override double Cost() + { + return .99; + } + } +} diff --git a/src/decorator/StarbuzzCoffee/Components/Espresso.cs b/src/decorator/StarbuzzCoffee/Components/Espresso.cs new file mode 100644 index 0000000..99faa4e --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Components/Espresso.cs @@ -0,0 +1,17 @@ +using StarbuzzCoffee.Components; + +namespace StarbuzzCoffee.Components +{ + public class Espresso : Beverage + { + public Espresso() + { + Description = "Espresso"; + } + + public override double Cost() + { + return 1.99; + } + } +} diff --git a/src/decorator/StarbuzzCoffee/Components/HouseBlend.cs b/src/decorator/StarbuzzCoffee/Components/HouseBlend.cs new file mode 100644 index 0000000..c04e3ba --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Components/HouseBlend.cs @@ -0,0 +1,17 @@ +using StarbuzzCoffee.Components; + +namespace StarbuzzCoffee.Components +{ + public class HouseBlend : Beverage + { + public HouseBlend() + { + Description = "House Blend Coffee"; + } + + public override double Cost() + { + return .89; + } + } +} diff --git a/src/decorator/StarbuzzCoffee/Decorators/Abstractions/CondimentDecorator.cs b/src/decorator/StarbuzzCoffee/Decorators/Abstractions/CondimentDecorator.cs new file mode 100644 index 0000000..72fae07 --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Decorators/Abstractions/CondimentDecorator.cs @@ -0,0 +1,9 @@ +using StarbuzzCoffee.Components; + +namespace StarbuzzCoffee.Decorators +{ + public abstract class CondimentDecorator : Beverage + { + public abstract override string Description { get; } + } +} diff --git a/src/decorator/StarbuzzCoffee/Decorators/Mocha.cs b/src/decorator/StarbuzzCoffee/Decorators/Mocha.cs new file mode 100644 index 0000000..0d1be74 --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Decorators/Mocha.cs @@ -0,0 +1,22 @@ +using StarbuzzCoffee.Components; +using StarbuzzCoffee.Decorators; + +namespace StarbuzzCoffee.Decorators +{ + public class Mocha : CondimentDecorator + { + private readonly Beverage _beverage; + + public Mocha(Beverage beverage) + { + _beverage = beverage; + } + + public override string Description => _beverage.Description + ", Mocha"; + + public override double Cost() + { + return .20 + _beverage.Cost(); + } + } +} diff --git a/src/decorator/StarbuzzCoffee/Decorators/Soy.cs b/src/decorator/StarbuzzCoffee/Decorators/Soy.cs new file mode 100644 index 0000000..41f1d64 --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Decorators/Soy.cs @@ -0,0 +1,22 @@ +using StarbuzzCoffee.Components; +using StarbuzzCoffee.Decorators; + +namespace StarbuzzCoffee.Decorators +{ + public class Soy : CondimentDecorator + { + private readonly Beverage _beverage; + + public Soy(Beverage beverage) + { + _beverage = beverage; + } + + public override double Cost() + { + return .15 + _beverage.Cost(); + } + + public override string Description => _beverage.Description + ", Soy"; + } +} diff --git a/src/decorator/StarbuzzCoffee/Decorators/Whip.cs b/src/decorator/StarbuzzCoffee/Decorators/Whip.cs new file mode 100644 index 0000000..a900cf1 --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Decorators/Whip.cs @@ -0,0 +1,22 @@ +using StarbuzzCoffee.Components; +using StarbuzzCoffee.Decorators; + +namespace StarbuzzCoffee.Decorators +{ + public class Whip : CondimentDecorator + { + private readonly Beverage _beverage; + + public Whip(Beverage beverage) + { + _beverage = beverage; + } + + public override double Cost() + { + return .10 + _beverage.Cost(); + } + + public override string Description => _beverage.Description + ", Whip"; + } +} diff --git a/src/decorator/StarbuzzCoffee/Program.cs b/src/decorator/StarbuzzCoffee/Program.cs new file mode 100644 index 0000000..a8bc6fe --- /dev/null +++ b/src/decorator/StarbuzzCoffee/Program.cs @@ -0,0 +1,27 @@ +using System; +using StarbuzzCoffee.Components; +using StarbuzzCoffee.Decorators; + +namespace StarbuzzCoffee +{ + public class Program + { + public static void Main(string[] args) + { + Beverage beverage = new Espresso(); + Console.WriteLine(beverage.Description + " $" + beverage.Cost()); + + Beverage beverage2 = new DarkRoast(); + beverage2 = new Mocha(beverage2); + beverage2 = new Mocha(beverage2); + beverage2 = new Whip(beverage2); + Console.WriteLine(beverage2.Description + " $" + beverage2.Cost()); + + Beverage beverage3 = new HouseBlend(); + beverage3 = new Soy(beverage3); + beverage3 = new Mocha(beverage3); + beverage3 = new Whip(beverage3); + Console.WriteLine(beverage3.Description + " $" + beverage3.Cost()); + } + } +} diff --git a/src/decorator/StarbuzzCoffee/StarbuzzCoffee.csproj b/src/decorator/StarbuzzCoffee/StarbuzzCoffee.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/decorator/StarbuzzCoffee/StarbuzzCoffee.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/facade/HomeTheater/Facade/HomeTheaterFacade.cs b/src/facade/HomeTheater/Facade/HomeTheaterFacade.cs new file mode 100644 index 0000000..0a3ed02 --- /dev/null +++ b/src/facade/HomeTheater/Facade/HomeTheaterFacade.cs @@ -0,0 +1,103 @@ +using System; + +namespace HomeTheater +{ + public class HomeTheaterFacade + { + private readonly Amplifier _amplifier; + private readonly Tuner _tuner; + private readonly DvdPlayer _dvdPlayer; + private readonly CdPlayer _cdPlayer; + private readonly Projector _projector; + private readonly Screen _screen; + private readonly TheaterLights _theaterLights; + private readonly PopcornPopper _popcornPopper; + + public HomeTheaterFacade(Amplifier amplifier, + Tuner tuner, + DvdPlayer dvdPlayer, + CdPlayer cdPlayer, + Projector projector, + Screen screen, + TheaterLights theaterLights, + PopcornPopper popcornPopper) + { + _amplifier = amplifier; + _tuner = tuner; + _dvdPlayer = dvdPlayer; + _cdPlayer = cdPlayer; + _projector = projector; + _screen = screen; + _theaterLights = theaterLights; + _popcornPopper = popcornPopper; + } + + public void WatchMovie(string movie) + { + Console.WriteLine("Get ready to watch a movie..."); + _popcornPopper.On(); + _popcornPopper.Pop(); + _theaterLights.Dim(10); + _screen.Down(); + _projector.On(); + _projector.WideScreenMode(); + _amplifier.On(); + _amplifier.SetDvd(_dvdPlayer); + _amplifier.SetSurroundSound(); + _amplifier.SetVolume(5); + _dvdPlayer.On(); + _dvdPlayer.Play(movie); + } + + public void EndMovie() + { + Console.WriteLine("Shutting movie theater down..."); + _popcornPopper.Off(); + _theaterLights.On(); + _screen.Up(); + _projector.Off(); + _amplifier.Off(); + _dvdPlayer.Stop(); + _dvdPlayer.Eject(); + _dvdPlayer.Off(); + } + + public void ListenToCd(string cdTitle) + { + Console.WriteLine("Get ready for an audiopile experience..."); + _theaterLights.On(); + _amplifier.On(); + _amplifier.SetVolume(5); + _amplifier.SetCd(_cdPlayer); + _amplifier.SetStereoSound(); + _cdPlayer.On(); + _cdPlayer.Play(cdTitle); + } + + public void EndCd() + { + Console.WriteLine("Shutting down CD..."); + _amplifier.Off(); + _amplifier.SetCd(_cdPlayer); + _cdPlayer.Eject(); + _cdPlayer.Off(); + } + + public void ListenToRadio(double frequency) + { + Console.WriteLine("Tuning in the airwaves..."); + _tuner.On(); + _tuner.SetFrequency(frequency); + _amplifier.On(); + _amplifier.SetVolume(5); + _amplifier.SetTuner(_tuner); + } + + public void EndRadio() + { + Console.WriteLine("Shutting down the tuner..."); + _tuner.Off(); + _amplifier.Off(); + } + } +} \ No newline at end of file diff --git a/src/facade/HomeTheater/HomeTheater.csproj b/src/facade/HomeTheater/HomeTheater.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/facade/HomeTheater/HomeTheater.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/facade/HomeTheater/Program.cs b/src/facade/HomeTheater/Program.cs new file mode 100644 index 0000000..f1bdfa4 --- /dev/null +++ b/src/facade/HomeTheater/Program.cs @@ -0,0 +1,25 @@ +using System; +using System.Linq; + +namespace HomeTheater +{ + public class Program + { + public static void Main(string[] args) + { + var amp = new Amplifier("Top-O-Line Amplifier"); + var tuner = new Tuner("Top-O-Line AM/FM Tuner", amp); + var dvd = new DvdPlayer("Top-O-Line DVD Player", amp); + var cd = new CdPlayer("Top-O-Line CD Player", amp); + var projector = new Projector("Top-O-Line Projector", dvd); + var lights = new TheaterLights("Theater Ceiling Lights"); + var screen = new Screen("Theater Screen"); + var popper = new PopcornPopper("Popcorn Popper"); + + var homeTheater = new HomeTheaterFacade(amp, tuner, dvd, cd, projector, screen, lights, popper); + + homeTheater.WatchMovie("Raiders of the Lost Ark"); + homeTheater.EndMovie(); + } + } +} diff --git a/src/facade/HomeTheater/Subsystems/Amplifier.cs b/src/facade/HomeTheater/Subsystems/Amplifier.cs new file mode 100644 index 0000000..9e998ae --- /dev/null +++ b/src/facade/HomeTheater/Subsystems/Amplifier.cs @@ -0,0 +1,66 @@ +using System; + +namespace HomeTheater +{ + public class Amplifier + { + private readonly string _description; + + public Amplifier(string description) + { + _description = description; + } + + public Tuner Tuner { get; private set; } + public DvdPlayer Dvd { get; private set; } + public CdPlayer Cd { get; private set; } + + public void On() + { + Console.WriteLine($"{_description} on"); + } + + public void Off() + { + Console.WriteLine($"{_description} off"); + } + + public void SetStereoSound() + { + Console.WriteLine($"{_description} stereo mode on"); + } + + public void SetSurroundSound() + { + Console.WriteLine($"{_description} surround sound on (5 speakers, 1 subwoofer)"); + } + + public void SetVolume(int level) + { + Console.WriteLine($"{_description} setting volume to {level.ToString()}"); + } + + public void SetTuner(Tuner tuner) + { + Console.WriteLine($"{_description} setting tunner to {tuner}"); + Tuner = tuner; + } + + public void SetDvd(DvdPlayer dvd) + { + Console.WriteLine($"{_description} setting DVD player to {dvd}"); + Dvd = dvd; + } + + public void SetCd(CdPlayer cd) + { + Console.WriteLine($"{_description} setting CD player to {cd}"); + Cd = cd; + } + + public override string ToString() + { + return _description; + } + } +} \ No newline at end of file diff --git a/src/facade/HomeTheater/Subsystems/CdPlayer.cs b/src/facade/HomeTheater/Subsystems/CdPlayer.cs new file mode 100644 index 0000000..1eda01a --- /dev/null +++ b/src/facade/HomeTheater/Subsystems/CdPlayer.cs @@ -0,0 +1,71 @@ +using System; + +namespace HomeTheater +{ + public class CdPlayer + { + private readonly string _description; + private readonly Amplifier _amplifier; + private string _title; + private int _currentTrack; + + public CdPlayer(string description, Amplifier amplifier) + { + _description = description; + _amplifier = amplifier; + } + + public void On() + { + Console.WriteLine($"{_description} on"); + } + + public void Off() + { + Console.WriteLine($"{_description} off"); + } + + public void Eject() + { + _title = null; + Console.WriteLine($"{_description} eject"); + } + + public void Play(string title) + { + _title = title; + _currentTrack = 0; + + Console.WriteLine($"{_description} playing \"{_title}\""); + } + + public void Play(int track) + { + if (_title == null) + { + Console.WriteLine($"{_description} can't play track {_currentTrack.ToString()}, no cd inserted"); + } + else + { + _currentTrack = track; + Console.WriteLine($"{_description} playing track {_currentTrack.ToString()}"); + } + } + + public void Stop() + { + _currentTrack = 0; + Console.WriteLine($"{_description} stopped"); + } + + public void Pause() + { + Console.WriteLine($"{_description} paused \"{_title}\""); + } + + public override string ToString() + { + return _description; + } + } +} \ No newline at end of file diff --git a/src/facade/HomeTheater/Subsystems/DvdPlayer.cs b/src/facade/HomeTheater/Subsystems/DvdPlayer.cs new file mode 100644 index 0000000..d7f5129 --- /dev/null +++ b/src/facade/HomeTheater/Subsystems/DvdPlayer.cs @@ -0,0 +1,81 @@ +using System; + +namespace HomeTheater +{ + public class DvdPlayer + { + private readonly string _description; + private readonly Amplifier _amplifier; + private string _movie; + private int _currentTrack; + + public DvdPlayer(string description, Amplifier amplifier) + { + _description = description; + _amplifier = amplifier; + } + + public void On() + { + Console.WriteLine($"{_description} on"); + } + + public void Off() + { + Console.WriteLine($"{_description} off"); + } + + public void Eject() + { + _movie = null; + Console.WriteLine($"{_description} eject"); + } + + public void Play(string movie) + { + _movie = movie; + _currentTrack = 0; + + Console.WriteLine($"{_description} playing \"{_movie}\""); + } + + public void Play(int track) + { + if (_movie == null) + { + Console.WriteLine($"{_description} can't play track {_currentTrack.ToString()}, no dvd inserted"); + } + else + { + _currentTrack = track; + Console.WriteLine($"{_description} playing track {_currentTrack.ToString()} of \"{_movie}\""); + } + } + + public void Stop() + { + _currentTrack = 0; + Console.WriteLine($"{_description} stopped \"{_movie}\""); + } + + public void Pause() + { + Console.WriteLine($"{_description} paused \"{_movie}\""); + } + + public void SetTwoChannelAudio() + { + Console.WriteLine($"{_description} set two channel audio"); + } + + public void SetSurroundAudio() + { + Console.WriteLine($"{_description} set surround audio"); + } + + public override string ToString() + { + return _description; + } + } +} \ No newline at end of file diff --git a/src/facade/HomeTheater/Subsystems/PopcornPopper.cs b/src/facade/HomeTheater/Subsystems/PopcornPopper.cs new file mode 100644 index 0000000..0b1f695 --- /dev/null +++ b/src/facade/HomeTheater/Subsystems/PopcornPopper.cs @@ -0,0 +1,34 @@ +using System; + +namespace HomeTheater +{ + public class PopcornPopper + { + private readonly string _description; + + public PopcornPopper(string description) + { + _description = description; + } + + public void On() + { + Console.WriteLine($"{_description} on"); + } + + public void Off() + { + Console.WriteLine($"{_description} off"); + } + + public void Pop() + { + Console.WriteLine($"{_description} popping popcorn!"); + } + + public override string ToString() + { + return _description; + } + } +} \ No newline at end of file diff --git a/src/facade/HomeTheater/Subsystems/Projector.cs b/src/facade/HomeTheater/Subsystems/Projector.cs new file mode 100644 index 0000000..9b934c7 --- /dev/null +++ b/src/facade/HomeTheater/Subsystems/Projector.cs @@ -0,0 +1,41 @@ +using System; + +namespace HomeTheater +{ + public class Projector + { + private readonly string _description; + private readonly DvdPlayer _dvdPlayer; + + public Projector(string description, DvdPlayer dvdPlayer) + { + _description = description; + _dvdPlayer = dvdPlayer; + } + + public void On() + { + Console.WriteLine($"{_description} on"); + } + + public void Off() + { + Console.WriteLine($"{_description} off"); + } + + public void WideScreenMode() + { + Console.WriteLine($"{_description} in widescreen mode (16x9 aspect ratio)"); + } + + public void TvMode() + { + Console.WriteLine($"{_description} in tv mode (4x3 aspect ratio)"); + } + + public override string ToString() + { + return _description; + } + } +} \ No newline at end of file diff --git a/src/facade/HomeTheater/Subsystems/Screen.cs b/src/facade/HomeTheater/Subsystems/Screen.cs new file mode 100644 index 0000000..fe64127 --- /dev/null +++ b/src/facade/HomeTheater/Subsystems/Screen.cs @@ -0,0 +1,29 @@ +using System; + +namespace HomeTheater +{ + public class Screen + { + private readonly string _description; + + public Screen(string description) + { + _description = description; + } + + public void Up() + { + Console.WriteLine($"{_description} going up"); + } + + public void Down() + { + Console.WriteLine($"{_description} going down"); + } + + public override string ToString() + { + return _description; + } + } +} \ No newline at end of file diff --git a/src/facade/HomeTheater/Subsystems/TheaterLights.cs b/src/facade/HomeTheater/Subsystems/TheaterLights.cs new file mode 100644 index 0000000..14b47df --- /dev/null +++ b/src/facade/HomeTheater/Subsystems/TheaterLights.cs @@ -0,0 +1,34 @@ +using System; + +namespace HomeTheater +{ + public class TheaterLights + { + private readonly string _description; + + public TheaterLights(string description) + { + _description = description; + } + + public void On() + { + Console.WriteLine($"{_description} on"); + } + + public void Off() + { + Console.WriteLine($"{_description} off"); + } + + public void Dim(int level) + { + Console.WriteLine($"{_description} dimming to {level.ToString()}%"); + } + + public override string ToString() + { + return _description; + } + } +} \ No newline at end of file diff --git a/src/facade/HomeTheater/Subsystems/Tuner.cs b/src/facade/HomeTheater/Subsystems/Tuner.cs new file mode 100644 index 0000000..4f0453a --- /dev/null +++ b/src/facade/HomeTheater/Subsystems/Tuner.cs @@ -0,0 +1,49 @@ +using System; +using System.Globalization; + +namespace HomeTheater +{ + public class Tuner + { + private readonly string _description; + private readonly Amplifier _amplifier; + private double _frequency; + + public Tuner(string description, Amplifier amplifier) + { + _description = description; + _amplifier = amplifier; + } + + public void On() + { + Console.WriteLine($"{_description} on"); + } + + public void Off() + { + Console.WriteLine($"{_description} off"); + } + + public void SetFrequency(double frequency) + { + Console.WriteLine($"{_description} setting frequency to {frequency.ToString(CultureInfo.InvariantCulture)}"); + _frequency = frequency; + } + + public void SetAm() + { + Console.WriteLine($"{_description} setting AM mode"); + } + + public void SetFm() + { + Console.WriteLine($"{_description} setting FM mode"); + } + + public override string ToString() + { + return _description; + } + } +} \ No newline at end of file diff --git a/src/facade/README.md b/src/facade/README.md new file mode 100644 index 0000000..8498f37 --- /dev/null +++ b/src/facade/README.md @@ -0,0 +1,2 @@ +**The Facade Pattern** provides a unified interface to a set of interfaces in a subsystem. +Facade defines a higher-level interface that makes the subsystem easier to use. \ No newline at end of file diff --git a/src/factory/PizzaStoreAbstractFactory/AbstractFactories/Abstractions/IPizzaIngredientFactory.cs b/src/factory/PizzaStoreAbstractFactory/AbstractFactories/Abstractions/IPizzaIngredientFactory.cs new file mode 100644 index 0000000..efaaf6c --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/AbstractFactories/Abstractions/IPizzaIngredientFactory.cs @@ -0,0 +1,14 @@ +using PizzaStoreAbstractFactory.Products.Ingredients; + +namespace PizzaStoreAbstractFactory.AbstractFactories +{ + public interface IPizzaIngredientFactory + { + IDough CreateDough(); + ISauce CreateSauce(); + ICheese CreateCheese(); + IVeggies[] CreateVeggies(); + IPepperoni CreatePepperoni(); + IClams CreateClam(); + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/AbstractFactories/ChicagoPizzaIngredientFactory.cs b/src/factory/PizzaStoreAbstractFactory/AbstractFactories/ChicagoPizzaIngredientFactory.cs new file mode 100644 index 0000000..f65261d --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/AbstractFactories/ChicagoPizzaIngredientFactory.cs @@ -0,0 +1,38 @@ +using PizzaStoreAbstractFactory.Products.Ingredients; + +namespace PizzaStoreAbstractFactory.AbstractFactories +{ + public class ChicagoPizzaIngredientFactory : IPizzaIngredientFactory + { + public ICheese CreateCheese() + { + return new MozzarellaCheese(); + } + + public IClams CreateClam() + { + return new FrozenClams(); + } + + public IDough CreateDough() + { + return new ThickCrustDough(); + } + + public IPepperoni CreatePepperoni() + { + return new SlicedPepperoni(); + } + + public ISauce CreateSauce() + { + return new PlumTomatoSauce(); + } + + public IVeggies[] CreateVeggies() + { + IVeggies[] veggies = { new BlackOlives(), new Spinach(), new Eggplant() }; + return veggies; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/AbstractFactories/NYPizzaIngredientFactory.cs b/src/factory/PizzaStoreAbstractFactory/AbstractFactories/NYPizzaIngredientFactory.cs new file mode 100644 index 0000000..d01fce7 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/AbstractFactories/NYPizzaIngredientFactory.cs @@ -0,0 +1,38 @@ +using PizzaStoreAbstractFactory.Products.Ingredients; + +namespace PizzaStoreAbstractFactory.AbstractFactories +{ + public class NYPizzaIngredientFactory : IPizzaIngredientFactory + { + public ICheese CreateCheese() + { + return new ReggianoCheese(); + } + + public IClams CreateClam() + { + return new FreshClams(); + } + + public IDough CreateDough() + { + return new ThinCrustDough(); + } + + public IPepperoni CreatePepperoni() + { + return new SlicedPepperoni(); + } + + public ISauce CreateSauce() + { + return new MarinaraSauce(); + } + + public IVeggies[] CreateVeggies() + { + IVeggies[] veggies = { new Garlic(), new Onion(), new Mushroom(), new RedPepper() }; + return veggies; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/FactoryMethods/Abstractions/PizzaStore.cs b/src/factory/PizzaStoreAbstractFactory/FactoryMethods/Abstractions/PizzaStore.cs new file mode 100644 index 0000000..27204d8 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/FactoryMethods/Abstractions/PizzaStore.cs @@ -0,0 +1,21 @@ +using System; +using PizzaStoreAbstractFactory.Products.Pizzas; + +namespace PizzaStoreAbstractFactory.FactoryMethods +{ + public abstract class PizzaStore + { + protected abstract Pizza CreatePizza(string item); + + public Pizza OrderPizza(string type) + { + Pizza pizza = CreatePizza(type); + Console.WriteLine("--- Making a " + pizza.Name + " ---"); + pizza.Prepare(); + pizza.Bake(); + pizza.Cut(); + pizza.Box(); + return pizza; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/FactoryMethods/ChicagoPizzaStore.cs b/src/factory/PizzaStoreAbstractFactory/FactoryMethods/ChicagoPizzaStore.cs new file mode 100644 index 0000000..4a6b287 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/FactoryMethods/ChicagoPizzaStore.cs @@ -0,0 +1,32 @@ +using PizzaStoreAbstractFactory.AbstractFactories; +using PizzaStoreAbstractFactory.Products.Pizzas; + +namespace PizzaStoreAbstractFactory.FactoryMethods +{ + public class ChicagoPizzaStore : PizzaStore + { + protected override Pizza CreatePizza(string item) + { + Pizza pizza = null; + IPizzaIngredientFactory ingredientFactory = new ChicagoPizzaIngredientFactory(); + + switch (item) + { + case "cheese": + pizza = new CheesePizza(ingredientFactory) { Name = "Chicago Style Cheese Pizza" }; + break; + case "veggie": + pizza = new VeggiePizza(ingredientFactory) { Name = "Chicago Style Veggie Pizza" }; + break; + case "clam": + pizza = new ClamPizza(ingredientFactory) { Name = "Chicago Style Clam Pizza" }; + break; + case "pepperoni": + pizza = new PepperoniPizza(ingredientFactory) { Name = "Chicago Style Pepperoni Pizza" }; + break; + } + + return pizza; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/FactoryMethods/NYPizzaStore.cs b/src/factory/PizzaStoreAbstractFactory/FactoryMethods/NYPizzaStore.cs new file mode 100644 index 0000000..8ab64f9 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/FactoryMethods/NYPizzaStore.cs @@ -0,0 +1,32 @@ +using PizzaStoreAbstractFactory.AbstractFactories; +using PizzaStoreAbstractFactory.Products.Pizzas; + +namespace PizzaStoreAbstractFactory.FactoryMethods +{ + public class NYPizzaStore : PizzaStore + { + protected override Pizza CreatePizza(string item) + { + Pizza pizza = null; + IPizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory(); + + switch (item) + { + case "cheese": + pizza = new CheesePizza(ingredientFactory) { Name = "New York Style Cheese Pizza" }; + break; + case "veggie": + pizza = new VeggiePizza(ingredientFactory) { Name = "New York Style Veggie Pizza" }; + break; + case "clam": + pizza = new ClamPizza(ingredientFactory) { Name = "New York Style Clam Pizza" }; + break; + case "pepperoni": + pizza = new PepperoniPizza(ingredientFactory) { Name = "New York Style Pepperoni Pizza" }; + break; + } + + return pizza; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/PizzaStoreAbstractFactory.csproj b/src/factory/PizzaStoreAbstractFactory/PizzaStoreAbstractFactory.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/PizzaStoreAbstractFactory.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/ICheese.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/ICheese.cs new file mode 100644 index 0000000..cfd2321 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/ICheese.cs @@ -0,0 +1,7 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public interface ICheese + { + string ToString(); + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IClams.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IClams.cs new file mode 100644 index 0000000..bb7e078 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IClams.cs @@ -0,0 +1,7 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public interface IClams + { + string ToString(); + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IDough.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IDough.cs new file mode 100644 index 0000000..d7d8d56 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IDough.cs @@ -0,0 +1,7 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public interface IDough + { + string ToString(); + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IPepperoni.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IPepperoni.cs new file mode 100644 index 0000000..510e62a --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IPepperoni.cs @@ -0,0 +1,7 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public interface IPepperoni + { + string ToString(); + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/ISauce.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/ISauce.cs new file mode 100644 index 0000000..f50b34d --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/ISauce.cs @@ -0,0 +1,7 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public interface ISauce + { + string ToString(); + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IVeggies.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IVeggies.cs new file mode 100644 index 0000000..828308e --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Abstractions/IVeggies.cs @@ -0,0 +1,7 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public interface IVeggies + { + string ToString(); + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/BlackOlives.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/BlackOlives.cs new file mode 100644 index 0000000..a52d283 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/BlackOlives.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class BlackOlives : IVeggies + { + string IVeggies.ToString() + { + return "Black Olives"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Eggplant.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Eggplant.cs new file mode 100644 index 0000000..a749fa0 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Eggplant.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class Eggplant : IVeggies + { + string IVeggies.ToString() + { + return "Eggplant"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/FreshClams.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/FreshClams.cs new file mode 100644 index 0000000..a1307a7 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/FreshClams.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class FreshClams : IClams + { + string IClams.ToString() + { + return "Fresh Clams from Long Island Sound"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/FrozenClams.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/FrozenClams.cs new file mode 100644 index 0000000..0466c35 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/FrozenClams.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class FrozenClams : IClams + { + string IClams.ToString() + { + return "Frozen Clams from Chesapeake Bay"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Garlic.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Garlic.cs new file mode 100644 index 0000000..d6c7abc --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Garlic.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class Garlic : IVeggies + { + string IVeggies.ToString() + { + return "Garlic"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/MarinaraSauce.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/MarinaraSauce.cs new file mode 100644 index 0000000..d3eb0d9 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/MarinaraSauce.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class MarinaraSauce : ISauce + { + string ISauce.ToString() + { + return "Marinara Sauce"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/MozzarellaCheese.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/MozzarellaCheese.cs new file mode 100644 index 0000000..3d379ee --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/MozzarellaCheese.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class MozzarellaCheese : ICheese + { + string ICheese.ToString() + { + return "MozzarellaCheese"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Mushroom.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Mushroom.cs new file mode 100644 index 0000000..86d841c --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Mushroom.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class Mushroom : IVeggies + { + string IVeggies.ToString() + { + return "Mushrooms"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Onion.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Onion.cs new file mode 100644 index 0000000..a57ec17 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Onion.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class Onion : IVeggies + { + string IVeggies.ToString() + { + return "Onion"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ParmesanCheese.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ParmesanCheese.cs new file mode 100644 index 0000000..500cfde --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ParmesanCheese.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class ParmesanCheese : ICheese + { + string ICheese.ToString() + { + return "Shredded Parmesan"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/PlumTomatoSauce.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/PlumTomatoSauce.cs new file mode 100644 index 0000000..a99d8e9 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/PlumTomatoSauce.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class PlumTomatoSauce : ISauce + { + string ISauce.ToString() + { + return "Tomato sauce with plum tomatoes"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/RedPepper.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/RedPepper.cs new file mode 100644 index 0000000..21ce070 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/RedPepper.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class RedPepper : IVeggies + { + string IVeggies.ToString() + { + return "Red Pepper"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ReggianoCheese.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ReggianoCheese.cs new file mode 100644 index 0000000..42f3d0e --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ReggianoCheese.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class ReggianoCheese : ICheese + { + string ICheese.ToString() + { + return "Reggiano Cheese"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/SlicedPepperoni.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/SlicedPepperoni.cs new file mode 100644 index 0000000..dfd9713 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/SlicedPepperoni.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class SlicedPepperoni : IPepperoni + { + string IPepperoni.ToString() + { + return "Sliced Pepperoni"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Spinach.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Spinach.cs new file mode 100644 index 0000000..40d64b5 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/Spinach.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class Spinach : IVeggies + { + string IVeggies.ToString() + { + return "Spinach"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ThickCrustDough.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ThickCrustDough.cs new file mode 100644 index 0000000..4c2f6e1 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ThickCrustDough.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class ThickCrustDough : IDough + { + string IDough.ToString() + { + return "ThickCrust style extra thick crust dough"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ThinCrustDough.cs b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ThinCrustDough.cs new file mode 100644 index 0000000..2f70a17 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Ingredients/ThinCrustDough.cs @@ -0,0 +1,10 @@ +namespace PizzaStoreAbstractFactory.Products.Ingredients +{ + public class ThinCrustDough : IDough + { + string IDough.ToString() + { + return "Thin Crust Dough"; + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/Abstractions/Pizza.cs b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/Abstractions/Pizza.cs new file mode 100644 index 0000000..56761aa --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/Abstractions/Pizza.cs @@ -0,0 +1,75 @@ +using System; +using System.Text; +using PizzaStoreAbstractFactory.Products.Ingredients; + +namespace PizzaStoreAbstractFactory.Products.Pizzas +{ + public abstract class Pizza + { + public string Name { get; set; } + + public IDough Dough { get; protected set; } + public ISauce Sauce { get; protected set; } + public IVeggies[] Veggies { get; protected set; } + public ICheese Cheese { get; protected set; } + public IPepperoni Pepperoni { get; protected set; } + public IClams Clam { get; protected set; } + + public abstract void Prepare(); + + public virtual void Bake() + { + Console.WriteLine("Bake for 25 minutes at 350"); + } + + public virtual void Cut() + { + Console.WriteLine("Cutting the pizza into diagonal slices"); + } + + public virtual void Box() + { + Console.WriteLine("Place pizza in official PizzaStore box"); + } + + public override string ToString() + { + var result = new StringBuilder(); + result.AppendLine("---- " + Name + " ----"); + if (Dough != null) + { + result.AppendLine(Dough.ToString()); + } + if (Sauce != null) + { + result.AppendLine(Sauce.ToString()); + } + if (Cheese != null) + { + result.AppendLine(Cheese.ToString()); + } + if (Veggies != null) + { + for (int i = 0; i < Veggies.Length; i++) + { + result.Append(Veggies[i].ToString()); + if (i < Veggies.Length - 1) + { + result.Append(", "); + } + } + result.Append("\n"); + } + if (Clam != null) + { + result.AppendLine(Clam.ToString()); + } + if (Pepperoni != null) + { + result.AppendLine(Pepperoni.ToString()); + } + + return result.ToString(); + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/CheesePizza.cs b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/CheesePizza.cs new file mode 100644 index 0000000..a4056fe --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/CheesePizza.cs @@ -0,0 +1,23 @@ +using System; +using PizzaStoreAbstractFactory.AbstractFactories; + +namespace PizzaStoreAbstractFactory.Products.Pizzas +{ + public class CheesePizza : Pizza + { + private readonly IPizzaIngredientFactory _ingredientFactory; + + public CheesePizza(IPizzaIngredientFactory ingredientFactory) + { + _ingredientFactory = ingredientFactory; + } + + public override void Prepare() + { + Console.WriteLine("Preparing " + Name); + Dough = _ingredientFactory.CreateDough(); + Sauce = _ingredientFactory.CreateSauce(); + Cheese = _ingredientFactory.CreateCheese(); + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/ClamPizza.cs b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/ClamPizza.cs new file mode 100644 index 0000000..5a10b9a --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/ClamPizza.cs @@ -0,0 +1,24 @@ +using System; +using PizzaStoreAbstractFactory.AbstractFactories; + +namespace PizzaStoreAbstractFactory.Products.Pizzas +{ + public class ClamPizza : Pizza + { + private readonly IPizzaIngredientFactory _ingredientFactory; + + public ClamPizza(IPizzaIngredientFactory ingredientFactory) + { + _ingredientFactory = ingredientFactory; + } + + public override void Prepare() + { + Console.WriteLine("Preparing " + Name); + Dough = _ingredientFactory.CreateDough(); + Sauce = _ingredientFactory.CreateSauce(); + Cheese = _ingredientFactory.CreateCheese(); + Clam = _ingredientFactory.CreateClam(); + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/PepperoniPizza.cs b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/PepperoniPizza.cs new file mode 100644 index 0000000..23e4b07 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/PepperoniPizza.cs @@ -0,0 +1,25 @@ +using System; +using PizzaStoreAbstractFactory.AbstractFactories; + +namespace PizzaStoreAbstractFactory.Products.Pizzas +{ + public class PepperoniPizza : Pizza + { + private readonly IPizzaIngredientFactory _ingredientFactory; + + public PepperoniPizza(IPizzaIngredientFactory ingredientFactory) + { + _ingredientFactory = ingredientFactory; + } + + public override void Prepare() + { + Console.WriteLine("Preparing " + Name); + Dough = _ingredientFactory.CreateDough(); + Sauce = _ingredientFactory.CreateSauce(); + Cheese = _ingredientFactory.CreateCheese(); + Veggies = _ingredientFactory.CreateVeggies(); + Pepperoni = _ingredientFactory.CreatePepperoni(); + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/VeggiePizza.cs b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/VeggiePizza.cs new file mode 100644 index 0000000..aa34e8d --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Products/Pizzas/VeggiePizza.cs @@ -0,0 +1,24 @@ +using System; +using PizzaStoreAbstractFactory.AbstractFactories; + +namespace PizzaStoreAbstractFactory.Products.Pizzas +{ + public class VeggiePizza : Pizza + { + private readonly IPizzaIngredientFactory _ingredientFactory; + + public VeggiePizza(IPizzaIngredientFactory ingredientFactory) + { + _ingredientFactory = ingredientFactory; + } + + public override void Prepare() + { + Console.WriteLine("Preparing " + Name); + Dough = _ingredientFactory.CreateDough(); + Sauce = _ingredientFactory.CreateSauce(); + Cheese = _ingredientFactory.CreateCheese(); + Veggies = _ingredientFactory.CreateVeggies(); + } + } +} diff --git a/src/factory/PizzaStoreAbstractFactory/Program.cs b/src/factory/PizzaStoreAbstractFactory/Program.cs new file mode 100644 index 0000000..51e4b88 --- /dev/null +++ b/src/factory/PizzaStoreAbstractFactory/Program.cs @@ -0,0 +1,39 @@ +using System; +using PizzaStoreAbstractFactory.FactoryMethods; +using PizzaStoreAbstractFactory.Products.Pizzas; + +namespace PizzaStoreAbstractFactory +{ + public class Program + { + public static void Main(string[] args) + { + PizzaStore nyStore = new NYPizzaStore(); + PizzaStore chicagoStore = new ChicagoPizzaStore(); + + Pizza pizza = nyStore.OrderPizza("cheese"); + Console.WriteLine("Ethan ordered a " + pizza + "\n"); + + pizza = chicagoStore.OrderPizza("cheese"); + Console.WriteLine("Joel ordered a " + pizza + "\n"); + + pizza = nyStore.OrderPizza("clam"); + Console.WriteLine("Ethan ordered a " + pizza + "\n"); + + pizza = chicagoStore.OrderPizza("clam"); + Console.WriteLine("Joel ordered a " + pizza + "\n"); + + pizza = nyStore.OrderPizza("pepperoni"); + Console.WriteLine("Ethan ordered a " + pizza + "\n"); + + pizza = chicagoStore.OrderPizza("pepperoni"); + Console.WriteLine("Joel ordered a " + pizza + "\n"); + + pizza = nyStore.OrderPizza("veggie"); + Console.WriteLine("Ethan ordered a " + pizza + "\n"); + + pizza = chicagoStore.OrderPizza("veggie"); + Console.WriteLine("Joel ordered a " + pizza + "\n"); + } + } +} diff --git a/src/factory/PizzaStoreFactoryMethod/FactoryMethods/Abstractions/PizzaStore.cs b/src/factory/PizzaStoreFactoryMethod/FactoryMethods/Abstractions/PizzaStore.cs new file mode 100644 index 0000000..cc0eea6 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/FactoryMethods/Abstractions/PizzaStore.cs @@ -0,0 +1,21 @@ +using System; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.FactoryMethods +{ + public abstract class PizzaStore + { + protected abstract Pizza CreatePizza(string item); + + public Pizza OrderPizza(string type) + { + Pizza pizza = CreatePizza(type); + Console.WriteLine("--- Making a " + pizza.Name + " ---"); + pizza.Prepare(); + pizza.Bake(); + pizza.Cut(); + pizza.Box(); + return pizza; + } + } +} diff --git a/src/factory/PizzaStoreFactoryMethod/FactoryMethods/ChicagoPizzaStore.cs b/src/factory/PizzaStoreFactoryMethod/FactoryMethods/ChicagoPizzaStore.cs new file mode 100644 index 0000000..d80d650 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/FactoryMethods/ChicagoPizzaStore.cs @@ -0,0 +1,31 @@ +using PizzaStoreFactoryMethod.FactoryMethods; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.FactoryMethods +{ + public class ChicagoPizzaStore : PizzaStore + { + protected override Pizza CreatePizza(string item) + { + Pizza pizza = null; + + switch (item) + { + case "cheese": + pizza = new ChicagoStyleCheesePizza(); + break; + case "veggie": + pizza = new ChicagoStyleVeggiePizza(); + break; + case "clam": + pizza = new ChicagoStyleClamPizza(); + break; + case "pepperoni": + pizza = new ChicagoStylePepperoniPizza(); + break; + } + + return pizza; + } + } +} diff --git a/src/factory/PizzaStoreFactoryMethod/FactoryMethods/NYPizzaStore.cs b/src/factory/PizzaStoreFactoryMethod/FactoryMethods/NYPizzaStore.cs new file mode 100644 index 0000000..ad1556f --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/FactoryMethods/NYPizzaStore.cs @@ -0,0 +1,30 @@ +using PizzaStoreFactoryMethod.FactoryMethods; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.FactoryMethods +{ + public class NYPizzaStore : PizzaStore + { + protected override Pizza CreatePizza(string item) + { + Pizza pizza = null; + switch (item) + { + case "cheese": + pizza = new NYStyleCheesePizza(); + break; + case "veggie": + pizza = new NYStyleVeggiePizza(); + break; + case "clam": + pizza = new NYStyleClamPizza(); + break; + case "pepperoni": + pizza = new NYStylePepperoniPizza(); + break; + } + + return pizza; + } + } +} diff --git a/src/factory/PizzaStoreFactoryMethod/PizzaStoreFactoryMethod.csproj b/src/factory/PizzaStoreFactoryMethod/PizzaStoreFactoryMethod.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/PizzaStoreFactoryMethod.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/Abstractions/Pizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/Abstractions/Pizza.cs new file mode 100644 index 0000000..60c1535 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/Abstractions/Pizza.cs @@ -0,0 +1,56 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public abstract class Pizza + { + public string Name { get; protected set; } + public string Dough { get; protected set; } + public string Sauce { get; protected set; } + public List Toppings { get; protected set; } = new List(); + + public virtual void Prepare() + { + Console.WriteLine("Preparing " + Name); + Console.WriteLine("Tossing dough..."); + Console.WriteLine("Adding sauce..."); + Console.WriteLine("Adding toppings: "); + foreach (var topping in Toppings) + { + Console.WriteLine(" " + topping); + } + } + + public virtual void Bake() + { + Console.WriteLine("Bake for 25 minutes at 350"); + } + + public virtual void Cut() + { + Console.WriteLine("Cutting the pizza into diagonal slices"); + } + + public virtual void Box() + { + Console.WriteLine("Place pizza in official PizzaStore box"); + } + + public override string ToString() + { + var result = new StringBuilder(); + + result.AppendLine("---- " + Name + " ----"); + result.AppendLine(Dough); + result.AppendLine(Sauce); + foreach (var topping in Toppings) + { + result.AppendLine(topping); + } + + return result.ToString(); + } + } +} diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleCheesePizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleCheesePizza.cs new file mode 100644 index 0000000..70b6573 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleCheesePizza.cs @@ -0,0 +1,22 @@ +using System; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public class ChicagoStyleCheesePizza : Pizza + { + public ChicagoStyleCheesePizza() + { + Name = "Chicago Style Deep Dish Cheese Pizza"; + Dough = "Extra Thick Crust Dough"; + Sauce = "Shredded Mozzarella Cheese"; + + Toppings.Add("Shredded Mozzarella Cheese"); + } + + public override void Cut() + { + Console.WriteLine("Cutting the pizza into square slices"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleClamPizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleClamPizza.cs new file mode 100644 index 0000000..37635a4 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleClamPizza.cs @@ -0,0 +1,23 @@ +using System; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public class ChicagoStyleClamPizza : Pizza + { + public ChicagoStyleClamPizza() + { + Name = "Chicago Style Clam Pizza"; + Dough = "Extra Thick Crust Dough"; + Sauce = "Plum Tomato Sauce"; + + Toppings.Add("Shredded Mozzarella Cheese"); + Toppings.Add("Frozen Clams from Chesapeake Bay"); + } + + public override void Cut() + { + Console.WriteLine("Cutting the pizza into square slices"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStylePepperoniPizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStylePepperoniPizza.cs new file mode 100644 index 0000000..422b5f2 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStylePepperoniPizza.cs @@ -0,0 +1,26 @@ +using System; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public class ChicagoStylePepperoniPizza : Pizza + { + public ChicagoStylePepperoniPizza() + { + Name = "Chicago Style Pepperoni Pizza"; + Dough = "Extra Thick Crust Dough"; + Sauce = "Plum Tomato Sauce"; + + Toppings.Add("Shredded Mozzarella Cheese"); + Toppings.Add("Black Olives"); + Toppings.Add("Spinach"); + Toppings.Add("Eggplant"); + Toppings.Add("Sliced Pepperoni"); + } + + public override void Cut() + { + Console.WriteLine("Cutting the pizza into square slices"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleVeggiePizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleVeggiePizza.cs new file mode 100644 index 0000000..3eff183 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/ChicagoStyleVeggiePizza.cs @@ -0,0 +1,25 @@ +using System; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public class ChicagoStyleVeggiePizza : Pizza + { + public ChicagoStyleVeggiePizza() + { + Name = "Chicago Deep Dish Veggie Pizza"; + Dough = "Extra Thick Crust Dough"; + Sauce = "Plum Tomato Sauce"; + + Toppings.Add("Shredded Mozzarella Cheese"); + Toppings.Add("Black Olives"); + Toppings.Add("Spinach"); + Toppings.Add("Eggplant"); + } + + public override void Cut() + { + Console.WriteLine("Cutting the pizza into square slices"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleCheesePizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleCheesePizza.cs new file mode 100644 index 0000000..94a8fb9 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleCheesePizza.cs @@ -0,0 +1,16 @@ +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public class NYStyleCheesePizza : Pizza + { + public NYStyleCheesePizza() + { + Name = "NY Style Sauce and Cheese Pizza"; + Dough = "Thin Crust Dough"; + Sauce = "Marinara Sauce"; + + Toppings.Add("Grated Reggiano Cheese"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleClamPizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleClamPizza.cs new file mode 100644 index 0000000..0f6bfb8 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleClamPizza.cs @@ -0,0 +1,17 @@ +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public class NYStyleClamPizza : Pizza + { + public NYStyleClamPizza() + { + Name = "NY Style Clam Pizza"; + Dough = "Thin Crust Dough"; + Sauce = "Marinara Sauce"; + + Toppings.Add("Grated Reggiano Cheese"); + Toppings.Add("Fresh Clams from Long Island Sound"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStylePepperoniPizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStylePepperoniPizza.cs new file mode 100644 index 0000000..1977a30 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStylePepperoniPizza.cs @@ -0,0 +1,21 @@ +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public class NYStylePepperoniPizza : Pizza + { + public NYStylePepperoniPizza() + { + Name = "NY Style Pepperoni Pizza"; + Dough = "Thin Crust Dough"; + Sauce = "Marinara Sauce"; + + Toppings.Add("Grated Reggiano Cheese"); + Toppings.Add("Sliced Pepperoni"); + Toppings.Add("Garlic"); + Toppings.Add("Onion"); + Toppings.Add("Mushrooms"); + Toppings.Add("Red Pepper"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleVeggiePizza.cs b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleVeggiePizza.cs new file mode 100644 index 0000000..0e6163c --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Products/Pizzas/NYStyleVeggiePizza.cs @@ -0,0 +1,26 @@ +using System; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod.Products.Pizzas +{ + public class NYStyleVeggiePizza : Pizza + { + public NYStyleVeggiePizza() + { + Name = "NY Style Veggie Pizza"; + Dough = "Thin Crust Dough"; + Sauce = "Marinara Sauce"; + + Toppings.Add("Grated Reggiano Cheese"); + Toppings.Add("Garlic"); + Toppings.Add("Onion"); + Toppings.Add("Mushrooms"); + Toppings.Add("Red Pepper"); + } + + public override void Cut() + { + Console.WriteLine("Cutting the pizza into square slices"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreFactoryMethod/Program.cs b/src/factory/PizzaStoreFactoryMethod/Program.cs new file mode 100644 index 0000000..7dae4e6 --- /dev/null +++ b/src/factory/PizzaStoreFactoryMethod/Program.cs @@ -0,0 +1,39 @@ +using System; +using PizzaStoreFactoryMethod.FactoryMethods; +using PizzaStoreFactoryMethod.Products.Pizzas; + +namespace PizzaStoreFactoryMethod +{ + public class Program + { + public static void Main(string[] args) + { + PizzaStore nyStore = new NYPizzaStore(); + PizzaStore chicagoStore = new ChicagoPizzaStore(); + + Pizza pizza = nyStore.OrderPizza("cheese"); + Console.WriteLine("Ethan ordered a " + pizza + "\n"); + + pizza = chicagoStore.OrderPizza("cheese"); + Console.WriteLine("Joel ordered a " + pizza + "\n"); + + pizza = nyStore.OrderPizza("clam"); + Console.WriteLine("Ethan ordered a " + pizza + "\n"); + + pizza = chicagoStore.OrderPizza("clam"); + Console.WriteLine("Joel ordered a " + pizza + "\n"); + + pizza = nyStore.OrderPizza("pepperoni"); + Console.WriteLine("Ethan ordered a " + pizza + "\n"); + + pizza = chicagoStore.OrderPizza("pepperoni"); + Console.WriteLine("Joel ordered a " + pizza + "\n"); + + pizza = nyStore.OrderPizza("veggie"); + Console.WriteLine("Ethan ordered a " + pizza + "\n"); + + pizza = chicagoStore.OrderPizza("veggie"); + Console.WriteLine("Joel ordered a " + pizza + "\n"); + } + } +} diff --git a/src/factory/PizzaStoreSimpleFactory/PizzaStore.cs b/src/factory/PizzaStoreSimpleFactory/PizzaStore.cs new file mode 100644 index 0000000..b0b0bf1 --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/PizzaStore.cs @@ -0,0 +1,27 @@ +using PizzaStoreSimpleFactory.Pizzas; +using PizzaStoreSimpleFactory.SimpleFactories; + +namespace PizzaStoreSimpleFactory +{ + public class PizzaStore + { + private readonly SimplePizzaFactory _factory; + + public PizzaStore(SimplePizzaFactory factory) + { + _factory = factory; + } + + public Pizza OrderPizza(string type) + { + Pizza pizza = _factory.CreatePizza(type); + + pizza.Prepare(); + pizza.Bake(); + pizza.Cut(); + pizza.Box(); + + return pizza; + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreSimpleFactory/PizzaStoreSimpleFactory.csproj b/src/factory/PizzaStoreSimpleFactory/PizzaStoreSimpleFactory.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/PizzaStoreSimpleFactory.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/factory/PizzaStoreSimpleFactory/Pizzas/Abstractions/Pizza.cs b/src/factory/PizzaStoreSimpleFactory/Pizzas/Abstractions/Pizza.cs new file mode 100644 index 0000000..2f92c92 --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/Pizzas/Abstractions/Pizza.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace PizzaStoreSimpleFactory.Pizzas +{ + public abstract class Pizza + { + public string Name { get; protected set; } + public string Dough { get; protected set; } + public string Sauce { get; protected set; } + public List Toppings { get; protected set; } = new List(); + + public virtual void Prepare() + { + Console.WriteLine("Preparing " + Name); + } + + public virtual void Bake() + { + Console.WriteLine("Bake for 25 minutes at 350"); + } + + public virtual void Cut() + { + Console.WriteLine("Cutting the pizza into diagonal slices"); + } + + public virtual void Box() + { + Console.WriteLine("Place pizza in official PizzaStore box"); + } + + public override string ToString() + { + var result = new StringBuilder(); + + result.AppendLine("---- " + Name + " ----"); + result.AppendLine(Dough); + result.AppendLine(Sauce); + foreach (var topping in Toppings) + { + result.AppendLine(topping); + } + + return result.ToString(); + } + } +} diff --git a/src/factory/PizzaStoreSimpleFactory/Pizzas/CheesePizza.cs b/src/factory/PizzaStoreSimpleFactory/Pizzas/CheesePizza.cs new file mode 100644 index 0000000..fd38b91 --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/Pizzas/CheesePizza.cs @@ -0,0 +1,17 @@ +using PizzaStoreSimpleFactory.Pizzas; + +namespace PizzaStoreSimpleFactory.Pizzas +{ + public class CheesePizza : Pizza + { + public CheesePizza() + { + Name = "Cheese Pizza"; + Dough = "Regular Crust"; + Sauce = "Marinara Pizza Sauce"; + + Toppings.Add("Fresh Mozzarella"); + Toppings.Add("Parmesan"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreSimpleFactory/Pizzas/ClamPizza.cs b/src/factory/PizzaStoreSimpleFactory/Pizzas/ClamPizza.cs new file mode 100644 index 0000000..84ddbda --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/Pizzas/ClamPizza.cs @@ -0,0 +1,17 @@ +using PizzaStoreSimpleFactory.Pizzas; + +namespace PizzaStoreSimpleFactory.Pizzas +{ + public class ClamPizza : Pizza + { + public ClamPizza() + { + Name = "Clam Pizza"; + Dough = "Thin crust"; + Sauce = "White garlic sauce"; + + Toppings.Add("Clams"); + Toppings.Add("Grated parmesan cheese"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreSimpleFactory/Pizzas/PepperoniPizza.cs b/src/factory/PizzaStoreSimpleFactory/Pizzas/PepperoniPizza.cs new file mode 100644 index 0000000..8e1cf96 --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/Pizzas/PepperoniPizza.cs @@ -0,0 +1,18 @@ +using PizzaStoreSimpleFactory.Pizzas; + +namespace PizzaStoreSimpleFactory.Pizzas +{ + public class PepperoniPizza : Pizza + { + public PepperoniPizza() + { + Name = "Pepperoni Pizza"; + Dough = "Crust"; + Sauce = "Marinara sauce"; + + Toppings.Add("Sliced Pepperoni"); + Toppings.Add("Sliced Onion"); + Toppings.Add("Grated parmesan cheese"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreSimpleFactory/Pizzas/VeggiePizza.cs b/src/factory/PizzaStoreSimpleFactory/Pizzas/VeggiePizza.cs new file mode 100644 index 0000000..cce518a --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/Pizzas/VeggiePizza.cs @@ -0,0 +1,21 @@ +using PizzaStoreSimpleFactory.Pizzas; + +namespace PizzaStoreSimpleFactory.Pizzas +{ + public class VeggiePizza : Pizza + { + public VeggiePizza() + { + Name = "Veggie Pizza"; + Dough = "Crust"; + Sauce = "Marinara sauce"; + + Toppings.Add("Shredded mozzarella"); + Toppings.Add("Grated parmesan"); + Toppings.Add("Diced onion"); + Toppings.Add("Sliced mushrooms"); + Toppings.Add("Sliced red pepper"); + Toppings.Add("Sliced black olives"); + } + } +} \ No newline at end of file diff --git a/src/factory/PizzaStoreSimpleFactory/Program.cs b/src/factory/PizzaStoreSimpleFactory/Program.cs new file mode 100644 index 0000000..602b759 --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/Program.cs @@ -0,0 +1,21 @@ +using System; +using PizzaStoreSimpleFactory.Pizzas; +using PizzaStoreSimpleFactory.SimpleFactories; + +namespace PizzaStoreSimpleFactory +{ + public class Program + { + public static void Main(string[] args) + { + SimplePizzaFactory factory = new SimplePizzaFactory(); + PizzaStore store = new PizzaStore(factory); + + Pizza pizza = store.OrderPizza("cheese"); + Console.WriteLine("We ordered a " + pizza.Name); + + pizza = store.OrderPizza("veggie"); + Console.WriteLine("We ordered a " + pizza.Name); + } + } +} diff --git a/src/factory/PizzaStoreSimpleFactory/SimpleFactories/SimplePizzaFactory.cs b/src/factory/PizzaStoreSimpleFactory/SimpleFactories/SimplePizzaFactory.cs new file mode 100644 index 0000000..ef19240 --- /dev/null +++ b/src/factory/PizzaStoreSimpleFactory/SimpleFactories/SimplePizzaFactory.cs @@ -0,0 +1,29 @@ +using PizzaStoreSimpleFactory.Pizzas; + +namespace PizzaStoreSimpleFactory.SimpleFactories +{ + public class SimplePizzaFactory + { + public Pizza CreatePizza(string item) + { + Pizza pizza = null; + switch (item) + { + case "cheese": + pizza = new CheesePizza(); + break; + case "veggie": + pizza = new VeggiePizza(); + break; + case "clam": + pizza = new ClamPizza(); + break; + case "pepperoni": + pizza = new PepperoniPizza(); + break; + } + + return pizza; + } + } +} \ No newline at end of file diff --git a/src/factory/README.md b/src/factory/README.md new file mode 100644 index 0000000..c158a0c --- /dev/null +++ b/src/factory/README.md @@ -0,0 +1,6 @@ +**The Factory Method Pattern** defines an interface for creating an object, but lets subclasses decide which class to instantiate. +Factory Method lets a class defer instantiation to subclasses. + + +**The Abstract Factory Pattern** provides an interface for creating families of related or dependent objects without +specifying their concrete classes. \ No newline at end of file diff --git a/src/iterator/DinerMerger/AlternatingDinerMenuIterator.cs b/src/iterator/DinerMerger/AlternatingDinerMenuIterator.cs new file mode 100644 index 0000000..bb5980e --- /dev/null +++ b/src/iterator/DinerMerger/AlternatingDinerMenuIterator.cs @@ -0,0 +1,26 @@ + +using System; + +namespace DinerMerger +{ + public class AlternatingDinerMenuIterator : IIterator + { + private readonly MenuItem[] _items; + private int _position; + + public AlternatingDinerMenuIterator(MenuItem[] items) + { + _items = items; + _position = (int)DateTime.Now.DayOfWeek % 2; + } + + public bool HasNext() => _position < _items.Length && _items[_position] != null; + + public MenuItem Next() => _items[_position+=2]; + + public override string ToString() + { + return "Alternating Diner Menu Iterator"; + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMerger/DinerMenu.cs b/src/iterator/DinerMerger/DinerMenu.cs new file mode 100644 index 0000000..a7ad484 --- /dev/null +++ b/src/iterator/DinerMerger/DinerMenu.cs @@ -0,0 +1,50 @@ +using System; + +namespace DinerMerger +{ + public class DinerMenu : IMenu + { + private const int MAX_ITEMS = 6; + private int _numberOfItems; + private readonly MenuItem[] _menuItems; + + public DinerMenu() + { + _menuItems = new MenuItem[MAX_ITEMS]; + + AddItem("Vegetarian BLT", "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99m); + AddItem("BLT", "Bacon with lettuce & tomato on whole wheat", false, 2.99m); + AddItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29m); + AddItem("Hotdog", "A hot dog, with sauerkraut, relish, onions, topped with cheese", false, 3.05m); + AddItem("Steamed Veggies and Brown Rice", "Steamed vegetables over brown rice", true, 3.99m); + AddItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89m); + } + + public void AddItem(string name, string description, + bool vegetarian, decimal price) + { + var menuItem = new MenuItem(name, description, vegetarian, price); + if (_numberOfItems >= MAX_ITEMS) + { + Console.WriteLine("Sorry, menu is full! Can't add item to menu"); + } + else + { + _menuItems[_numberOfItems++] = menuItem; + } + } + + public MenuItem[] GetMenuItems() + { + return _menuItems; + } + + public IIterator CreateIterator() + { + return new DinerMenuIterator(_menuItems); + // To test Alternating menu items, comment out above line, + // and uncomment the line below. + //return new AlternatingDinerMenuIterator(menuItems); + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMerger/DinerMenuIterator.cs b/src/iterator/DinerMerger/DinerMenuIterator.cs new file mode 100644 index 0000000..156886e --- /dev/null +++ b/src/iterator/DinerMerger/DinerMenuIterator.cs @@ -0,0 +1,17 @@ +namespace DinerMerger +{ + public class DinerMenuIterator : IIterator + { + private readonly MenuItem[] _items; + private int _position; + + public DinerMenuIterator(MenuItem[] items) + { + _items = items; + } + + public bool HasNext() => _position < _items.Length && _items[_position] != null; + + public MenuItem Next() => _items[_position++]; + } +} \ No newline at end of file diff --git a/src/iterator/DinerMerger/DinerMerger.csproj b/src/iterator/DinerMerger/DinerMerger.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/iterator/DinerMerger/DinerMerger.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/iterator/DinerMerger/IIterator.cs b/src/iterator/DinerMerger/IIterator.cs new file mode 100644 index 0000000..0fec967 --- /dev/null +++ b/src/iterator/DinerMerger/IIterator.cs @@ -0,0 +1,8 @@ +namespace DinerMerger +{ + public interface IIterator + { + bool HasNext(); + MenuItem Next(); + } +} \ No newline at end of file diff --git a/src/iterator/DinerMerger/IMenu.cs b/src/iterator/DinerMerger/IMenu.cs new file mode 100644 index 0000000..d54d1b6 --- /dev/null +++ b/src/iterator/DinerMerger/IMenu.cs @@ -0,0 +1,7 @@ +namespace DinerMerger +{ + public interface IMenu + { + IIterator CreateIterator(); + } +} \ No newline at end of file diff --git a/src/iterator/DinerMerger/MenuItem.cs b/src/iterator/DinerMerger/MenuItem.cs new file mode 100644 index 0000000..6388791 --- /dev/null +++ b/src/iterator/DinerMerger/MenuItem.cs @@ -0,0 +1,25 @@ +using System.Globalization; + +namespace DinerMerger +{ + public class MenuItem + { + public MenuItem(string name, string description, bool vegetarian, decimal price) + { + Name = name; + Description = description; + IsVegetarian = vegetarian; + Price = price; + } + + public string Name { get; } + public string Description { get; } + public bool IsVegetarian { get; } + public decimal Price { get; } + + public override string ToString() + { + return $"{Name}, ${Price.ToString(CultureInfo.InvariantCulture)} \r\n {Description}"; + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMerger/PancakeHouseMenu.cs b/src/iterator/DinerMerger/PancakeHouseMenu.cs new file mode 100644 index 0000000..abe84b1 --- /dev/null +++ b/src/iterator/DinerMerger/PancakeHouseMenu.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; + +namespace DinerMerger +{ + public class PancakeHouseMenu : IMenu + { + private readonly List _menuItems; + + public PancakeHouseMenu() + { + _menuItems = new List(); + + AddItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99m); + AddItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99m); + AddItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49m); + AddItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59m); + } + + public void AddItem(string name, string description, + bool vegetarian, decimal price) + { + var menuItem = new MenuItem(name, description, vegetarian, price); + _menuItems.Add(menuItem); + } + + public List GetMenuItems() + { + return _menuItems; + } + + public IIterator CreateIterator() + { + return new PancakeHouseMenuIterator(_menuItems); + } + + public override string ToString() + { + return "Objectville Pancake House Menu"; + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMerger/PancakeHouseMenuIterator.cs b/src/iterator/DinerMerger/PancakeHouseMenuIterator.cs new file mode 100644 index 0000000..2de5249 --- /dev/null +++ b/src/iterator/DinerMerger/PancakeHouseMenuIterator.cs @@ -0,0 +1,25 @@ +using System.Collections.Generic; + +namespace DinerMerger +{ + public class PancakeHouseMenuIterator : IIterator + { + private readonly List _items; + private int _position; + + public PancakeHouseMenuIterator(List items) + { + _items = items; + } + + public bool HasNext() + { + return _position < _items.Count; + } + + public MenuItem Next() + { + return _items[_position++]; + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMerger/Program.cs b/src/iterator/DinerMerger/Program.cs new file mode 100644 index 0000000..d2a248f --- /dev/null +++ b/src/iterator/DinerMerger/Program.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DinerMerger +{ + public class Program + { + public static void Main(string[] args) + { + PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); + DinerMenu dinerMenu = new DinerMenu(); + + Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu); + + // Without iterators + //printMenu(); + + // With iterators + waitress.PrintMenu(); + } + } +} diff --git a/src/iterator/DinerMerger/Waitress.cs b/src/iterator/DinerMerger/Waitress.cs new file mode 100644 index 0000000..94a3eb7 --- /dev/null +++ b/src/iterator/DinerMerger/Waitress.cs @@ -0,0 +1,70 @@ +using System; +using System.Globalization; + +namespace DinerMerger +{ + public class Waitress + { + private readonly IMenu _pancakeHouseMenu; + private readonly IMenu _dinerMenu; + + public Waitress(IMenu pancakeHouseMenu, IMenu dinerMenu) + { + _pancakeHouseMenu = pancakeHouseMenu; + _dinerMenu = dinerMenu; + } + + public void PrintMenu() + { + IIterator pancakeIterator = _pancakeHouseMenu.CreateIterator(); + IIterator dinerIterator = _dinerMenu.CreateIterator(); + + Console.WriteLine("MENU\n----\nBREAKFAST"); + PrintMenu(pancakeIterator); + Console.WriteLine("\nLUNCH"); + PrintMenu(dinerIterator); + } + + public void PrintVegetarianMenu() + { + PrintVegetarianMenu(_pancakeHouseMenu.CreateIterator()); + PrintVegetarianMenu(_dinerMenu.CreateIterator()); + } + + private void PrintMenu(IIterator iterator) + { + while (iterator.HasNext()) + { + MenuItem menuItem = iterator.Next(); + Console.WriteLine($"{menuItem.Name}, {menuItem.Price.ToString(CultureInfo.InvariantCulture)} -- "); + Console.WriteLine(menuItem.Description); + } + } + + private void PrintVegetarianMenu(IIterator iterator) + { + while (iterator.HasNext()) + { + MenuItem menuItem = iterator.Next(); + if (menuItem.IsVegetarian) + { + Console.WriteLine($"{menuItem.Name} \t\t {menuItem.Price.ToString(CultureInfo.InvariantCulture)}"); + Console.WriteLine("\t" + menuItem.Description); + } + } + } + + private bool IsVegetarian(string name, IIterator iterator) + { + while (iterator.HasNext()) + { + MenuItem menuItem = iterator.Next(); + if (menuItem.Name == name && menuItem.IsVegetarian) + { + return true; + } + } + return false; + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMergerCafe/CafeMenu.cs b/src/iterator/DinerMergerCafe/CafeMenu.cs new file mode 100644 index 0000000..27159a5 --- /dev/null +++ b/src/iterator/DinerMergerCafe/CafeMenu.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; + +namespace DinerMergerCafe +{ + public class CafeMenu : IMenu + { + private readonly Dictionary _menuItems = new Dictionary(); + + public CafeMenu() + { + AddItem("Veggie Burger and Air Fries", "Veggie burger on a whole wheat bun, lettuce, tomato, and fries", true, 3.99m); + AddItem("Soup of the day", "A cup of the soup of the day, with a side salad", false, 3.69m); + AddItem("Burrito", "A large burrito, with whole pinto beans, salsa, guacamole", true, 4.29m); + } + + public void AddItem(string name, string description, + bool vegetarian, decimal price) + { + var menuItem = new MenuItem(name, description, vegetarian, price); + _menuItems.Add(menuItem.Name, menuItem); + } + + public Dictionary GetItems() + { + return _menuItems; + } + + public IEnumerator CreateIterator() + { + return _menuItems.Values.GetEnumerator(); + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMergerCafe/DinerMenu.cs b/src/iterator/DinerMergerCafe/DinerMenu.cs new file mode 100644 index 0000000..ae60736 --- /dev/null +++ b/src/iterator/DinerMergerCafe/DinerMenu.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; + +namespace DinerMergerCafe +{ + public class DinerMenu : IMenu + { + private const int MAX_ITEMS = 6; + private int _numberOfItems; + private readonly MenuItem[] _menuItems; + + public DinerMenu() + { + _menuItems = new MenuItem[MAX_ITEMS]; + + AddItem("Vegetarian BLT", "(Fakin') Bacon with lettuce & tomato on whole wheat", true, 2.99m); + AddItem("BLT", "Bacon with lettuce & tomato on whole wheat", false, 2.99m); + AddItem("Soup of the day", "Soup of the day, with a side of potato salad", false, 3.29m); + AddItem("Hotdog", "A hot dog, with sauerkraut, relish, onions, topped with cheese", false, 3.05m); + AddItem("Steamed Veggies and Brown Rice", "Steamed vegetables over brown rice", true, 3.99m); + AddItem("Pasta", "Spaghetti with Marinara Sauce, and a slice of sourdough bread", true, 3.89m); + } + + public void AddItem(string name, string description, + bool vegetarian, decimal price) + { + var menuItem = new MenuItem(name, description, vegetarian, price); + if (_numberOfItems >= MAX_ITEMS) + { + Console.WriteLine("Sorry, menu is full! Can't add item to menu"); + } + else + { + _menuItems[_numberOfItems++] = menuItem; + } + } + + public MenuItem[] GetMenuItems() + { + return _menuItems; + } + + public IEnumerator CreateIterator() + { + return new DinerMenuIterator(_menuItems); + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMergerCafe/DinerMenuIterator.cs b/src/iterator/DinerMergerCafe/DinerMenuIterator.cs new file mode 100644 index 0000000..c7cfd5a --- /dev/null +++ b/src/iterator/DinerMergerCafe/DinerMenuIterator.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace DinerMergerCafe +{ + public class DinerMenuIterator : IEnumerator + { + private readonly IEnumerator _enumerator; + + public DinerMenuIterator(MenuItem[] menuItems) + { + _enumerator = menuItems.GetEnumerator(); + } + + public void Dispose() + { + } + + public bool MoveNext() + { + return _enumerator.MoveNext(); + } + + public void Reset() + { + _enumerator.Reset(); + } + + public MenuItem Current => (MenuItem) _enumerator.Current; + + object IEnumerator.Current => Current; + } +} \ No newline at end of file diff --git a/src/iterator/DinerMergerCafe/DinerMergerCafe.csproj b/src/iterator/DinerMergerCafe/DinerMergerCafe.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/iterator/DinerMergerCafe/DinerMergerCafe.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/iterator/DinerMergerCafe/IMenu.cs b/src/iterator/DinerMergerCafe/IMenu.cs new file mode 100644 index 0000000..5db3d82 --- /dev/null +++ b/src/iterator/DinerMergerCafe/IMenu.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace DinerMergerCafe +{ + public interface IMenu + { + IEnumerator CreateIterator(); + } +} \ No newline at end of file diff --git a/src/iterator/DinerMergerCafe/MenuItem.cs b/src/iterator/DinerMergerCafe/MenuItem.cs new file mode 100644 index 0000000..bb0d285 --- /dev/null +++ b/src/iterator/DinerMergerCafe/MenuItem.cs @@ -0,0 +1,25 @@ +using System.Globalization; + +namespace DinerMergerCafe +{ + public class MenuItem + { + public MenuItem(string name, string description, bool vegetarian, decimal price) + { + Name = name; + Description = description; + IsVegetarian = vegetarian; + Price = price; + } + + public string Name { get; } + public string Description { get; } + public bool IsVegetarian { get; } + public decimal Price { get; } + + public override string ToString() + { + return $"{Name}, ${Price.ToString(CultureInfo.InvariantCulture)} \r\n {Description}"; + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMergerCafe/PancakeHouseMenu.cs b/src/iterator/DinerMergerCafe/PancakeHouseMenu.cs new file mode 100644 index 0000000..ef0b72f --- /dev/null +++ b/src/iterator/DinerMergerCafe/PancakeHouseMenu.cs @@ -0,0 +1,41 @@ +using System.Collections.Generic; + +namespace DinerMergerCafe +{ + public class PancakeHouseMenu : IMenu + { + private readonly List _menuItems; + + public PancakeHouseMenu() + { + _menuItems = new List(); + + AddItem("K&B's Pancake Breakfast", "Pancakes with scrambled eggs, and toast", true, 2.99m); + AddItem("Regular Pancake Breakfast", "Pancakes with fried eggs, sausage", false, 2.99m); + AddItem("Blueberry Pancakes", "Pancakes made with fresh blueberries", true, 3.49m); + AddItem("Waffles", "Waffles, with your choice of blueberries or strawberries", true, 3.59m); + } + + public void AddItem(string name, string description, + bool vegetarian, decimal price) + { + var menuItem = new MenuItem(name, description, vegetarian, price); + _menuItems.Add(menuItem); + } + + public List GetMenuItems() + { + return _menuItems; + } + + public IEnumerator CreateIterator() + { + return _menuItems.GetEnumerator(); + } + + public override string ToString() + { + return "Objectville Pancake House Menu"; + } + } +} \ No newline at end of file diff --git a/src/iterator/DinerMergerCafe/Program.cs b/src/iterator/DinerMergerCafe/Program.cs new file mode 100644 index 0000000..221d44e --- /dev/null +++ b/src/iterator/DinerMergerCafe/Program.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace DinerMergerCafe +{ + public class Program + { + public static void Main(string[] args) + { + PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); + DinerMenu dinerMenu = new DinerMenu(); + CafeMenu cafeMenu = new CafeMenu(); + + Waitress waitress = new Waitress(pancakeHouseMenu, dinerMenu, cafeMenu); + + waitress.PrintMenu(); + waitress.PrintVegetarianMenu(); + + Console.WriteLine("\nCustomer asks, is the Hotdog vegetarian?"); + Console.Write("Waitress says: "); + Console.WriteLine(waitress.IsItemVegetarian("Hotdog") ? "Yes" : "No"); + Console.WriteLine("\nCustomer asks, are the Waffles vegetarian?"); + Console.Write("Waitress says: "); + Console.WriteLine(waitress.IsItemVegetarian("Waffles") ? "Yes" : "No"); + } + } +} diff --git a/src/iterator/DinerMergerCafe/Waitress.cs b/src/iterator/DinerMergerCafe/Waitress.cs new file mode 100644 index 0000000..047a01f --- /dev/null +++ b/src/iterator/DinerMergerCafe/Waitress.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Globalization; + +namespace DinerMergerCafe +{ + public class Waitress + { + private readonly IMenu _pancakeHouseMenu; + private readonly IMenu _dinerMenu; + private readonly IMenu _cafeMenu; + + public Waitress(IMenu pancakeHouseMenu, IMenu dinerMenu, IMenu cafeMenu) + { + _pancakeHouseMenu = pancakeHouseMenu; + _dinerMenu = dinerMenu; + _cafeMenu = cafeMenu; + } + + public void PrintMenu() + { + var pancakeIterator = _pancakeHouseMenu.CreateIterator(); + var dinerIterator = _dinerMenu.CreateIterator(); + var cafeIterator = _cafeMenu.CreateIterator(); + + Console.WriteLine("MENU\n----\nBREAKFAST"); + PrintMenu(pancakeIterator); + Console.WriteLine("\nLUNCH"); + PrintMenu(dinerIterator); + Console.WriteLine("\nDINNER"); + PrintMenu(cafeIterator); + } + + public void PrintVegetarianMenu() + { + Console.WriteLine("\nVEGETARIAN MENU\n---------------"); + PrintVegetarianMenu(_pancakeHouseMenu.CreateIterator()); + PrintVegetarianMenu(_dinerMenu.CreateIterator()); + PrintVegetarianMenu(_cafeMenu.CreateIterator()); + } + + public bool IsItemVegetarian(string name) + { + IEnumerator pancakeIterator = _pancakeHouseMenu.CreateIterator(); + if (IsVegetarian(name, pancakeIterator)) + { + return true; + } + IEnumerator dinerIterator = _dinerMenu.CreateIterator(); + if (IsVegetarian(name, dinerIterator)) + { + return true; + } + IEnumerator cafeIterator = _cafeMenu.CreateIterator(); + if (IsVegetarian(name, cafeIterator)) + { + return true; + } + return false; + } + + private void PrintMenu(IEnumerator iterator) + { + while (iterator.MoveNext()) + { + MenuItem menuItem = iterator.Current; + Console.WriteLine($"{menuItem.Name}, {menuItem.Price.ToString(CultureInfo.InvariantCulture)} -- "); + Console.WriteLine(menuItem.Description); + } + } + + private void PrintVegetarianMenu(IEnumerator iterator) + { + while (iterator.MoveNext()) + { + MenuItem menuItem = iterator.Current; + if (menuItem.IsVegetarian) + { + Console.WriteLine($"{menuItem.Name} \t\t {menuItem.Price.ToString(CultureInfo.InvariantCulture)}"); + Console.WriteLine("\t" + menuItem.Description); + } + } + } + + private bool IsVegetarian(string name, IEnumerator iterator) + { + while (iterator.MoveNext()) + { + MenuItem menuItem = iterator.Current; + if (menuItem.Name == name && menuItem.IsVegetarian) + { + return true; + } + } + return false; + } + } +} \ No newline at end of file diff --git a/src/iterator/README.md b/src/iterator/README.md new file mode 100644 index 0000000..2c1ca99 --- /dev/null +++ b/src/iterator/README.md @@ -0,0 +1,2 @@ +**The Iterator Pattern** provides a way to access the elements of an aggregate object sequentially without exposing its underlying +representation. \ No newline at end of file diff --git a/src/observer/README.md b/src/observer/README.md new file mode 100644 index 0000000..5d61d5e --- /dev/null +++ b/src/observer/README.md @@ -0,0 +1,2 @@ +**The Observer Pattern** defines a one-to-many dependency between objects so that when one object changes state, +all of its dependents are notified and updated automatically. \ No newline at end of file diff --git a/src/observer/WeatherStation.Observable/Observers/Abstractions/IDisplayElement.cs b/src/observer/WeatherStation.Observable/Observers/Abstractions/IDisplayElement.cs new file mode 100644 index 0000000..e735446 --- /dev/null +++ b/src/observer/WeatherStation.Observable/Observers/Abstractions/IDisplayElement.cs @@ -0,0 +1,7 @@ +namespace WeatherStation.Observable.Observers +{ + public interface IDisplayElement + { + void Display(); + } +} diff --git a/src/observer/WeatherStation.Observable/Observers/CurrentConditionsDisplay.cs b/src/observer/WeatherStation.Observable/Observers/CurrentConditionsDisplay.cs new file mode 100644 index 0000000..981338e --- /dev/null +++ b/src/observer/WeatherStation.Observable/Observers/CurrentConditionsDisplay.cs @@ -0,0 +1,41 @@ +using System; +using WeatherStation.Observable.Subjects; + +namespace WeatherStation.Observable.Observers +{ + public class CurrentConditionsDisplay : IObserver, IDisplayElement + { + private double _temperature; + private double _humidity; + private readonly IDisposable _unsubscriber; + + public CurrentConditionsDisplay(IObservable weatherData) + { + if(weatherData != null) + _unsubscriber = weatherData.Subscribe(this); + } + + public void Display() + { + Console.WriteLine("Current conditions: " + _temperature + "F degrees and " + _humidity + "% humidity"); + } + + public void OnCompleted() + { + Console.WriteLine("Weather station has completed transmitting data"); + _unsubscriber.Dispose(); + } + + public void OnError(Exception error) + { + Console.WriteLine("Error occured {0}", error); + } + + public void OnNext(WeatherData value) + { + _temperature = value.Temperature; + _humidity = value.Humidity; + Display(); + } + } +} diff --git a/src/observer/WeatherStation.Observable/Observers/ForecastDisplay.cs b/src/observer/WeatherStation.Observable/Observers/ForecastDisplay.cs new file mode 100644 index 0000000..bbc6a38 --- /dev/null +++ b/src/observer/WeatherStation.Observable/Observers/ForecastDisplay.cs @@ -0,0 +1,54 @@ +using System; +using WeatherStation.Observable.Subjects; + +namespace WeatherStation.Observable.Observers +{ + public class ForecastDisplay : IObserver, IDisplayElement + { + private double _currentPressure = 29.92d; + private double _lastPressure; + private readonly IDisposable _unsubscriber; + + public ForecastDisplay(IObservable weatherData) + { + if (weatherData != null) + _unsubscriber = weatherData.Subscribe(this); + } + + public void Display() + { + Console.Write("Forecast: "); + if (_currentPressure > _lastPressure) + { + Console.WriteLine("Improving weather on the way!"); + } + else if (_currentPressure == _lastPressure) + { + Console.WriteLine("More of the same"); + } + else if (_currentPressure < _lastPressure) + { + Console.WriteLine("Watch out for cooler, rainy weather"); + } + } + + public void OnCompleted() + { + Console.WriteLine("Weather station has completed transmitting data"); + _unsubscriber.Dispose(); + } + + public void OnError(Exception error) + { + Console.WriteLine("Error occured {0}", error); + } + + public void OnNext(WeatherData value) + { + _lastPressure = _currentPressure; + _currentPressure = value.Pressure; + + Display(); + } + } +} diff --git a/src/observer/WeatherStation.Observable/Observers/HeatIndexDisplay.cs b/src/observer/WeatherStation.Observable/Observers/HeatIndexDisplay.cs new file mode 100644 index 0000000..0cd5cc7 --- /dev/null +++ b/src/observer/WeatherStation.Observable/Observers/HeatIndexDisplay.cs @@ -0,0 +1,52 @@ +using System; +using WeatherStation.Observable.Subjects; + +namespace WeatherStation.Observable.Observers +{ + public class HeatIndexDisplay : IObserver, IDisplayElement + { + private double _heatIndex; + private readonly IDisposable _unsubscriber; + + public HeatIndexDisplay(IObservable weatherData) + { + if (weatherData != null) + _unsubscriber = weatherData.Subscribe(this); + } + + public void Display() + { + Console.WriteLine("Heat index is " + _heatIndex); + } + + private double ComputeHeatIndex(double t, double rh) + { + double index = (16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) + + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) + + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) + + (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 * + (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) + + (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) + + 0.000000000843296 * (t * t * rh * rh * rh)) - + (0.0000000000481975 * (t * t * t * rh * rh * rh)); + return index; + } + + public void OnCompleted() + { + Console.WriteLine("Weather station has completed transmitting data"); + _unsubscriber.Dispose(); + } + + public void OnError(Exception error) + { + Console.WriteLine("Error occured {0}", error); + } + + public void OnNext(WeatherData value) + { + _heatIndex = ComputeHeatIndex(value.Temperature, value.Humidity); + Display(); + } + } +} diff --git a/src/observer/WeatherStation.Observable/Observers/StatisticsDisplay.cs b/src/observer/WeatherStation.Observable/Observers/StatisticsDisplay.cs new file mode 100644 index 0000000..c3b884a --- /dev/null +++ b/src/observer/WeatherStation.Observable/Observers/StatisticsDisplay.cs @@ -0,0 +1,55 @@ +using System; +using WeatherStation.Observable.Subjects; + +namespace WeatherStation.Observable.Observers +{ + public class StatisticsDisplay : IObserver, IDisplayElement + { + private double _maxTemp; + private double _minTemp = 200; + private double _tempSum; + private int _numReadings; + private readonly IDisposable _unsubscriber; + + public StatisticsDisplay(IObservable weatherData) + { + if (weatherData != null) + _unsubscriber = weatherData.Subscribe(this); + } + + public void Display() + { + Console.WriteLine("Avg/Max/Min temperature = " + (_tempSum / _numReadings) + + "/" + _maxTemp + "/" + _minTemp); + } + + public void OnCompleted() + { + Console.WriteLine("Weather station has completed transmitting data"); + _unsubscriber.Dispose(); + } + + public void OnError(Exception error) + { + Console.WriteLine("Error occured {0}", error); + } + + public void OnNext(WeatherData value) + { + _tempSum += value.Temperature; + _numReadings++; + + if (value.Temperature > _maxTemp) + { + _maxTemp = value.Temperature; + } + + if (value.Temperature < _minTemp) + { + _minTemp = value.Temperature; + } + + Display(); + } + } +} diff --git a/src/observer/WeatherStation.Observable/Program.cs b/src/observer/WeatherStation.Observable/Program.cs new file mode 100644 index 0000000..aa8a94f --- /dev/null +++ b/src/observer/WeatherStation.Observable/Program.cs @@ -0,0 +1,23 @@ +using System; +using WeatherStation.Observable.Observers; +using WeatherStation.Observable.Subjects; + +namespace WeatherStation.Observable +{ + public class Program + { + public static void Main(string[] args) + { + WeatherData weatherData = new WeatherData(); + + CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); + StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); + ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); + HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData); + + weatherData.SetMeasurements(80, 65, 30.4d); + weatherData.SetMeasurements(82, 70, 29.2d); + weatherData.SetMeasurements(78, 90, 29.2d); + } + } +} diff --git a/src/observer/WeatherStation.Observable/Subjects/WeatherData.cs b/src/observer/WeatherStation.Observable/Subjects/WeatherData.cs new file mode 100644 index 0000000..95bd5d7 --- /dev/null +++ b/src/observer/WeatherStation.Observable/Subjects/WeatherData.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; + +namespace WeatherStation.Observable.Subjects +{ + public class WeatherData : IObservable + { + private readonly List> _observers; + + public double Temperature { get; private set; } + public double Humidity { get; private set; } + public double Pressure { get; private set; } + + public WeatherData() + { + _observers = new List>(); + } + + public void MeasurementsChanged() + { + NotifyObservers(); + } + + public void SetMeasurements(double temperature, double humidity, double pressure) + { + Temperature = temperature; + Humidity = humidity; + Pressure = pressure; + MeasurementsChanged(); + } + + public void NotifyObservers() + { + foreach (var observer in _observers) + { + observer.OnNext(this); + } + } + + public IDisposable Subscribe(IObserver observer) + { + if (!_observers.Contains(observer)) + _observers.Add(observer); + + return new Unsubscriber(_observers, observer); + } + + private class Unsubscriber : IDisposable + { + private readonly List> _observers; + private readonly IObserver _observer; + + public Unsubscriber(List> observers, IObserver observer) + { + _observers = observers; + _observer = observer; + } + + public void Dispose() + { + if (_observer != null && _observers.Contains(_observer)) + _observers.Remove(_observer); + } + } + } +} diff --git a/src/observer/WeatherStation.Observable/WeatherStation.Observable.csproj b/src/observer/WeatherStation.Observable/WeatherStation.Observable.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/observer/WeatherStation.Observable/WeatherStation.Observable.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/observer/WeatherStation/Observers/Abstractions/IDisplayElement.cs b/src/observer/WeatherStation/Observers/Abstractions/IDisplayElement.cs new file mode 100644 index 0000000..fe2586f --- /dev/null +++ b/src/observer/WeatherStation/Observers/Abstractions/IDisplayElement.cs @@ -0,0 +1,7 @@ +namespace WeatherStation.Observers +{ + public interface IDisplayElement + { + void Display(); + } +} diff --git a/src/observer/WeatherStation/Observers/Abstractions/IObserver.cs b/src/observer/WeatherStation/Observers/Abstractions/IObserver.cs new file mode 100644 index 0000000..a9df2e2 --- /dev/null +++ b/src/observer/WeatherStation/Observers/Abstractions/IObserver.cs @@ -0,0 +1,7 @@ +namespace WeatherStation.Observers +{ + public interface IObserver + { + void Update(double temp, double humidity, double pressure); + } +} diff --git a/src/observer/WeatherStation/Observers/CurrentConditionsDisplay.cs b/src/observer/WeatherStation/Observers/CurrentConditionsDisplay.cs new file mode 100644 index 0000000..e73cced --- /dev/null +++ b/src/observer/WeatherStation/Observers/CurrentConditionsDisplay.cs @@ -0,0 +1,30 @@ +using System; +using WeatherStation.Subjects; + +namespace WeatherStation.Observers +{ + public class CurrentConditionsDisplay : IObserver, IDisplayElement + { + private double _temperature; + private double _humidity; + private readonly ISubject _weatherData; + + public CurrentConditionsDisplay(ISubject weatherData) + { + _weatherData = weatherData; + _weatherData.RegisterObserver(this); + } + + public void Display() + { + Console.WriteLine("Current conditions: " + _temperature + "F degrees and " + _humidity + "% humidity"); + } + + public void Update(double temp, double humidity, double pressure) + { + _temperature = temp; + _humidity = humidity; + Display(); + } + } +} diff --git a/src/observer/WeatherStation/Observers/ForecastDisplay.cs b/src/observer/WeatherStation/Observers/ForecastDisplay.cs new file mode 100644 index 0000000..363ecd2 --- /dev/null +++ b/src/observer/WeatherStation/Observers/ForecastDisplay.cs @@ -0,0 +1,43 @@ +using System; +using WeatherStation.Subjects; + +namespace WeatherStation.Observers +{ + public class ForecastDisplay : IObserver, IDisplayElement + { + private double _currentPressure = 29.92d; + private double _lastPressure; + private readonly ISubject _weatherData; + + public ForecastDisplay(ISubject weatherData) + { + _weatherData = weatherData; + _weatherData.RegisterObserver(this); + } + + public void Display() + { + Console.Write("Forecast: "); + if (_currentPressure > _lastPressure) + { + Console.WriteLine("Improving weather on the way!"); + } + else if (_currentPressure == _lastPressure) + { + Console.WriteLine("More of the same"); + } + else if (_currentPressure < _lastPressure) + { + Console.WriteLine("Watch out for cooler, rainy weather"); + } + } + + public void Update(double temp, double humidity, double pressure) + { + _lastPressure = _currentPressure; + _currentPressure = pressure; + + Display(); + } + } +} diff --git a/src/observer/WeatherStation/Observers/HeatIndexDisplay.cs b/src/observer/WeatherStation/Observers/HeatIndexDisplay.cs new file mode 100644 index 0000000..aa94b37 --- /dev/null +++ b/src/observer/WeatherStation/Observers/HeatIndexDisplay.cs @@ -0,0 +1,41 @@ +using System; +using WeatherStation.Subjects; + +namespace WeatherStation.Observers +{ + public class HeatIndexDisplay : IObserver, IDisplayElement + { + private double _heatIndex; + private readonly ISubject _weatherData; + + public HeatIndexDisplay(ISubject weatherData) + { + _weatherData = weatherData; + _weatherData.RegisterObserver(this); + } + + public void Display() + { + Console.WriteLine("Heat index is " + _heatIndex); + } + + public void Update(double temp, double humidity, double pressure) + { + _heatIndex = ComputeHeatIndex(temp, humidity); + Display(); + } + + private double ComputeHeatIndex(double t, double rh) + { + double index = (16.923 + (0.185212 * t) + (5.37941 * rh) - (0.100254 * t * rh) + + (0.00941695 * (t * t)) + (0.00728898 * (rh * rh)) + + (0.000345372 * (t * t * rh)) - (0.000814971 * (t * rh * rh)) + + (0.0000102102 * (t * t * rh * rh)) - (0.000038646 * (t * t * t)) + (0.0000291583 * + (rh * rh * rh)) + (0.00000142721 * (t * t * t * rh)) + + (0.000000197483 * (t * rh * rh * rh)) - (0.0000000218429 * (t * t * t * rh * rh)) + + 0.000000000843296 * (t * t * rh * rh * rh)) - + (0.0000000000481975 * (t * t * t * rh * rh * rh)); + return index; + } + } +} diff --git a/src/observer/WeatherStation/Observers/StatisticsDisplay.cs b/src/observer/WeatherStation/Observers/StatisticsDisplay.cs new file mode 100644 index 0000000..5d6081b --- /dev/null +++ b/src/observer/WeatherStation/Observers/StatisticsDisplay.cs @@ -0,0 +1,44 @@ +using System; +using WeatherStation.Subjects; + +namespace WeatherStation.Observers +{ + public class StatisticsDisplay : IObserver, IDisplayElement + { + private double _maxTemp; + private double _minTemp = 200; + private double _tempSum; + private int _numReadings; + private readonly ISubject _weatherData; + + public StatisticsDisplay(ISubject weatherData) + { + _weatherData = weatherData; + _weatherData.RegisterObserver(this); + } + + public void Display() + { + Console.WriteLine("Avg/Max/Min temperature = " + (_tempSum / _numReadings) + + "/" + _maxTemp + "/" + _minTemp); + } + + public void Update(double temp, double humidity, double pressure) + { + _tempSum += temp; + _numReadings++; + + if (temp > _maxTemp) + { + _maxTemp = temp; + } + + if (temp < _minTemp) + { + _minTemp = temp; + } + + Display(); + } + } +} diff --git a/src/observer/WeatherStation/Program.cs b/src/observer/WeatherStation/Program.cs new file mode 100644 index 0000000..27b8d30 --- /dev/null +++ b/src/observer/WeatherStation/Program.cs @@ -0,0 +1,23 @@ +using System; +using WeatherStation.Observers; +using WeatherStation.Subjects; + +namespace WeatherStation +{ + public class Program + { + public static void Main(string[] args) + { + WeatherData weatherData = new WeatherData(); + + CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); + StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); + ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); + HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData); + + weatherData.SetMeasurements(80, 65, 30.4d); + weatherData.SetMeasurements(82, 70, 29.2d); + weatherData.SetMeasurements(78, 90, 29.2d); + } + } +} diff --git a/src/observer/WeatherStation/Subjects/Abstractions/ISubject.cs b/src/observer/WeatherStation/Subjects/Abstractions/ISubject.cs new file mode 100644 index 0000000..734d67e --- /dev/null +++ b/src/observer/WeatherStation/Subjects/Abstractions/ISubject.cs @@ -0,0 +1,11 @@ +using WeatherStation.Observers; + +namespace WeatherStation.Subjects +{ + public interface ISubject + { + void RegisterObserver(IObserver o); + void RemoveObserver(IObserver o); + void NotifyObservers(); + } +} diff --git a/src/observer/WeatherStation/Subjects/WeatherData.cs b/src/observer/WeatherStation/Subjects/WeatherData.cs new file mode 100644 index 0000000..cc7fa0b --- /dev/null +++ b/src/observer/WeatherStation/Subjects/WeatherData.cs @@ -0,0 +1,49 @@ +using System.Collections.Generic; +using WeatherStation.Observers; + +namespace WeatherStation.Subjects +{ + public class WeatherData : ISubject + { + private readonly List _observers; + private double _temperature; + private double _humidity; + private double _pressure; + + public WeatherData() + { + _observers = new List(); + } + + public void MeasurementsChanged() + { + NotifyObservers(); + } + + public void SetMeasurements(double temperature, double humidity, double pressure) + { + _temperature = temperature; + _humidity = humidity; + _pressure = pressure; + MeasurementsChanged(); + } + + public void NotifyObservers() + { + foreach (var observer in _observers) + { + observer.Update(_temperature, _humidity, _pressure); + } + } + + public void RegisterObserver(IObserver o) + { + _observers.Add(o); + } + + public void RemoveObserver(IObserver o) + { + _observers.Remove(o); + } + } +} diff --git a/src/observer/WeatherStation/WeatherStation.csproj b/src/observer/WeatherStation/WeatherStation.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/observer/WeatherStation/WeatherStation.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/proxy/README.md b/src/proxy/README.md new file mode 100644 index 0000000..e8734e6 --- /dev/null +++ b/src/proxy/README.md @@ -0,0 +1 @@ +**The Proxy Pattern** provides a surrogate or placeholder for another object to control access to it. \ No newline at end of file diff --git a/src/singleton/ChocolateBoiler/ChocolateBoiler.cs b/src/singleton/ChocolateBoiler/ChocolateBoiler.cs new file mode 100644 index 0000000..59fcd77 --- /dev/null +++ b/src/singleton/ChocolateBoiler/ChocolateBoiler.cs @@ -0,0 +1,61 @@ +using System; + +namespace ChocolateBoiler +{ + public class ChocolateBoiler + { + private static ChocolateBoiler _uniqueInstance; + + public static ChocolateBoiler Instance + { + get + { + if (_uniqueInstance == null) + { + Console.WriteLine("Creating unique instance of Chocolate Boiler"); + _uniqueInstance = new ChocolateBoiler(); + } + + Console.WriteLine("Returning instance of Chocolate Boiler"); + return _uniqueInstance; + } + } + + public bool Empty { get; private set; } + public bool Boiled { get; private set; } + + private ChocolateBoiler() + { + Empty = true; + Boiled = false; + } + + public void Fill() + { + if (Empty) + { + Empty = false; + Boiled = false; + // fill the boiler with a milk/chocolate mixture + } + } + + public void Drain() + { + if (!Empty && !Boiled) + { + // drain the boiled milk and chocolate + Empty = true; + } + } + + public void Boil() + { + if (!Empty && !Boiled) + { + // bring the contents to a boil + Boiled = true; + } + } + } +} \ No newline at end of file diff --git a/src/singleton/ChocolateBoiler/ChocolateBoiler.csproj b/src/singleton/ChocolateBoiler/ChocolateBoiler.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/singleton/ChocolateBoiler/ChocolateBoiler.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/singleton/ChocolateBoiler/Program.cs b/src/singleton/ChocolateBoiler/Program.cs new file mode 100644 index 0000000..24c04b9 --- /dev/null +++ b/src/singleton/ChocolateBoiler/Program.cs @@ -0,0 +1,18 @@ +using System; + +namespace ChocolateBoiler +{ + public class Program + { + public static void Main(string[] args) + { + ChocolateBoiler boiler = ChocolateBoiler.Instance; + boiler.Fill(); + boiler.Boil(); + boiler.Drain(); + + // will return the existing instance + ChocolateBoiler boiler2 = ChocolateBoiler.Instance; + } + } +} diff --git a/src/singleton/MultipleSingleton/MultipleSingleton.csproj b/src/singleton/MultipleSingleton/MultipleSingleton.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/singleton/MultipleSingleton/MultipleSingleton.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/singleton/MultipleSingleton/Program.cs b/src/singleton/MultipleSingleton/Program.cs new file mode 100644 index 0000000..62fee8f --- /dev/null +++ b/src/singleton/MultipleSingleton/Program.cs @@ -0,0 +1,18 @@ +using System; +using MultipleSingleton.Singletons.Subclasses; + +namespace MultipleSingleton +{ + public class Program + { + public static void Main(string[] args) + { + var foo = CoolerSingleton.Instance; + var bar = HotterSingleton.Instance; + + Console.WriteLine(foo == bar); + Console.WriteLine(foo); + Console.WriteLine(bar); + } + } +} diff --git a/src/singleton/MultipleSingleton/Singletons/ClassicSingleton.cs b/src/singleton/MultipleSingleton/Singletons/ClassicSingleton.cs new file mode 100644 index 0000000..1588303 --- /dev/null +++ b/src/singleton/MultipleSingleton/Singletons/ClassicSingleton.cs @@ -0,0 +1,12 @@ +namespace MultipleSingleton.Singletons +{ + // NOTE: This is not thread safe! + public class ClassicSingleton + { + private static ClassicSingleton _uniqueInstance; + + public static ClassicSingleton Instance => _uniqueInstance ?? (_uniqueInstance = new ClassicSingleton()); + + private ClassicSingleton() { } + } +} \ No newline at end of file diff --git a/src/singleton/MultipleSingleton/Singletons/DclSingleton.cs b/src/singleton/MultipleSingleton/Singletons/DclSingleton.cs new file mode 100644 index 0000000..b8d4d39 --- /dev/null +++ b/src/singleton/MultipleSingleton/Singletons/DclSingleton.cs @@ -0,0 +1,28 @@ +namespace MultipleSingleton.Singletons +{ + public class DclSingleton + { + private static volatile DclSingleton _uniqueInstance; + private static readonly object SyncLock = new object(); + + public static DclSingleton Instance + { + get + { + if (_uniqueInstance == null) + { + lock (SyncLock) + { + if (_uniqueInstance == null) + { + _uniqueInstance = new DclSingleton(); + } + } + } + return _uniqueInstance; + } + } + + private DclSingleton() { } + } +} \ No newline at end of file diff --git a/src/singleton/MultipleSingleton/Singletons/StaticSingleton.cs b/src/singleton/MultipleSingleton/Singletons/StaticSingleton.cs new file mode 100644 index 0000000..99d0589 --- /dev/null +++ b/src/singleton/MultipleSingleton/Singletons/StaticSingleton.cs @@ -0,0 +1,9 @@ +namespace MultipleSingleton.Singletons +{ + public class StaticSingleton + { + public static StaticSingleton Instance { get; } = new StaticSingleton(); + + private StaticSingleton() { } + } +} \ No newline at end of file diff --git a/src/singleton/MultipleSingleton/Singletons/Subclasses/BaseSingleton.cs b/src/singleton/MultipleSingleton/Singletons/Subclasses/BaseSingleton.cs new file mode 100644 index 0000000..b764a4f --- /dev/null +++ b/src/singleton/MultipleSingleton/Singletons/Subclasses/BaseSingleton.cs @@ -0,0 +1,26 @@ +namespace MultipleSingleton.Singletons +{ + public class BaseSingleton + { + protected static BaseSingleton UniqueInstance; + private static readonly object SyncLock = new object(); + + public static BaseSingleton Instance + { + get + { + lock (SyncLock) + { + if (UniqueInstance == null) + { + UniqueInstance = new BaseSingleton(); + } + } + + return UniqueInstance; + } + } + + protected BaseSingleton() { } + } +} \ No newline at end of file diff --git a/src/singleton/MultipleSingleton/Singletons/Subclasses/CoolerSingleton.cs b/src/singleton/MultipleSingleton/Singletons/Subclasses/CoolerSingleton.cs new file mode 100644 index 0000000..f48587f --- /dev/null +++ b/src/singleton/MultipleSingleton/Singletons/Subclasses/CoolerSingleton.cs @@ -0,0 +1,9 @@ +namespace MultipleSingleton.Singletons.Subclasses +{ + public class CoolerSingleton : BaseSingleton + { + protected new static BaseSingleton UniqueInstance; + + private CoolerSingleton() { } + } +} \ No newline at end of file diff --git a/src/singleton/MultipleSingleton/Singletons/Subclasses/HotterSingleton.cs b/src/singleton/MultipleSingleton/Singletons/Subclasses/HotterSingleton.cs new file mode 100644 index 0000000..2e1d736 --- /dev/null +++ b/src/singleton/MultipleSingleton/Singletons/Subclasses/HotterSingleton.cs @@ -0,0 +1,7 @@ +namespace MultipleSingleton.Singletons.Subclasses +{ + public class HotterSingleton : BaseSingleton + { + private HotterSingleton(){} + } +} \ No newline at end of file diff --git a/src/singleton/MultipleSingleton/Singletons/ThreadSafeSingleton.cs b/src/singleton/MultipleSingleton/Singletons/ThreadSafeSingleton.cs new file mode 100644 index 0000000..ae0440e --- /dev/null +++ b/src/singleton/MultipleSingleton/Singletons/ThreadSafeSingleton.cs @@ -0,0 +1,26 @@ +namespace MultipleSingleton.Singletons +{ + public class ThreadSafeSingleton + { + private static ThreadSafeSingleton _uniqueInstance; + private static readonly object SyncLock = new object(); + + public static ThreadSafeSingleton Instance + { + get + { + lock (SyncLock) + { + if (_uniqueInstance == null) + { + _uniqueInstance = new ThreadSafeSingleton(); + } + } + + return _uniqueInstance; + } + } + + private ThreadSafeSingleton() { } + } +} \ No newline at end of file diff --git a/src/singleton/README.md b/src/singleton/README.md new file mode 100644 index 0000000..7fc517b --- /dev/null +++ b/src/singleton/README.md @@ -0,0 +1 @@ +**The Singleton Pattern** ensures a class has only one instance, and provides a global point of access to it. \ No newline at end of file diff --git a/src/state/Gumball/Gumball.csproj b/src/state/Gumball/Gumball.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/state/Gumball/Gumball.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/state/Gumball/GumballMachine.cs b/src/state/Gumball/GumballMachine.cs new file mode 100644 index 0000000..7bc6ff0 --- /dev/null +++ b/src/state/Gumball/GumballMachine.cs @@ -0,0 +1,155 @@ +using System; +using System.Text; + +namespace Gumball +{ + public class GumballMachine + { + private const int SOLD_OUT = 0; + private const int NO_QUARTER = 1; + private const int HAS_QUARTER = 2; + private const int SOLD = 3; + + private int _state = SOLD_OUT; + private int _count; + + public GumballMachine(int count) + { + _count = count; + if (count > 1) + { + _state = NO_QUARTER; + } + } + + public void InsertQuarter() + { + if (_state == HAS_QUARTER) + { + Console.WriteLine("You can't insert another quarter"); + } + else if (_state == NO_QUARTER) + { + _state = HAS_QUARTER; + Console.WriteLine("You inserted a quarter"); + } + else if (_state == SOLD_OUT) + { + Console.WriteLine("You can't insert a quarter, the machine is sold out"); + } + else if (_state == SOLD) + { + Console.WriteLine("Please wait, we're already giving you a gumball"); + } + } + + public void EjectQuarter() + { + if (_state == HAS_QUARTER) + { + Console.WriteLine("Quarter returned"); + _state = NO_QUARTER; + } + else if (_state == NO_QUARTER) + { + Console.WriteLine("You haven't inserted a quarter"); + } + else if (_state == SOLD) + { + Console.WriteLine("Sorry, you already turned the crank"); + } + else if (_state == SOLD_OUT) + { + Console.WriteLine("You can't eject, you haven't inserted a quarter yet"); + } + } + + public void TurnCrank() + { + if (_state == SOLD) + { + Console.WriteLine("Turning twice doesn't get you another gumball!"); + } + else if (_state == NO_QUARTER) + { + Console.WriteLine("You turned but there's no quarter"); + } + else if (_state == SOLD_OUT) + { + Console.WriteLine("You turned, but there are no gumballs"); + } + else if (_state == HAS_QUARTER) + { + Console.WriteLine("You turned..."); + _state = SOLD; + Dispense(); + } + } + + private void Dispense() + { + if (_state == SOLD) + { + Console.WriteLine("A gumball comes rolling out the slot"); + _count = _count - 1; + if (_count == 0) + { + Console.WriteLine("Oops, out of gumballs!"); + _state = SOLD_OUT; + } + else { + _state = NO_QUARTER; + } + } + else if (_state == NO_QUARTER) + { + Console.WriteLine("You need to pay first"); + } + else if (_state == SOLD_OUT) + { + Console.WriteLine("No gumball dispensed"); + } + else if (_state == HAS_QUARTER) + { + Console.WriteLine("No gumball dispensed"); + } + } + + public void Refill(int numGumBalls) + { + _count = numGumBalls; + _state = NO_QUARTER; + } + + public override string ToString() + { + StringBuilder result = new StringBuilder(); + result.Append("\nMighty Gumball, Inc."); + result.Append("\nC#-enabled Standing Gumball Model #2016\n"); + result.Append("Inventory: " + _count + " gumball"); + if (_count != 1) + { + result.Append("s"); + } + result.Append("\nMachine is "); + if (_state == SOLD_OUT) + { + result.Append("sold out"); + } + else if (_state == NO_QUARTER) + { + result.Append("waiting for quarter"); + } + else if (_state == HAS_QUARTER) + { + result.Append("waiting for turn of crank"); + } + else if (_state == SOLD) + { + result.Append("delivering a gumball"); + } + result.Append("\n"); + return result.ToString(); + } + } +} \ No newline at end of file diff --git a/src/state/Gumball/Program.cs b/src/state/Gumball/Program.cs new file mode 100644 index 0000000..64cb225 --- /dev/null +++ b/src/state/Gumball/Program.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Gumball +{ + public class Program + { + public static void Main(string[] args) + { + GumballMachine gumballMachine = new GumballMachine(5); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.EjectQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.EjectQuarter(); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + } + } +} diff --git a/src/state/GumballState/GumballMachine.cs b/src/state/GumballState/GumballMachine.cs new file mode 100644 index 0000000..bfa233d --- /dev/null +++ b/src/state/GumballState/GumballMachine.cs @@ -0,0 +1,84 @@ +using System; +using System.Text; +using GumballState.States; + +namespace GumballState +{ + //Context + public class GumballMachine + { + public GumballMachine(int numberGumballs) + { + SoldOutState = new SoldOutState(this); + NoQuarterState = new NoQuarterState(this); + HasQuarterState = new HasQuarterState(this); + SoldState = new SoldState(this); + + Count = numberGumballs; + + State = numberGumballs > 0 ? NoQuarterState : SoldOutState; + } + + public int Count { get; private set; } + public IState State { get; private set; } + + public IState SoldOutState { get; } + public IState NoQuarterState { get; } + public IState HasQuarterState { get; } + public IState SoldState { get; } + + public void InsertQuarter() + { + State.InsertQuarter(); + } + + public void EjectQuarter() + { + State.EjectQuarter(); + } + + public void TurnCrank() + { + State.TurnCrank(); + State.Dispense(); + } + + public void ReleaseBall() + { + Console.WriteLine("A gumball comes rolling out the slot..."); + if (Count != 0) + { + Count--; + } + } + + public void Refill(int count) + { + Count += count; + Console.WriteLine($"The gumball machine was just refilled; it's new count is: {Count.ToString()}"); + State.Refill(); + } + + public void SetState(IState state) + { + State = state; + } + + public override string ToString() + { + StringBuilder result = new StringBuilder(); + + result.Append("\nMighty Gumball, Inc."); + result.Append("\nC#-enabled Standing Gumball Model #2016"); + result.Append("\nInventory: " + Count.ToString() + " gumball"); + if (Count != 1) + { + result.Append("s"); + } + result.Append("\n"); + result.Append("Machine is " + State + "\n"); + + return result.ToString(); + } + } +} \ No newline at end of file diff --git a/src/state/GumballState/GumballState.csproj b/src/state/GumballState/GumballState.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/state/GumballState/GumballState.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/state/GumballState/Program.cs b/src/state/GumballState/Program.cs new file mode 100644 index 0000000..5bd5e6e --- /dev/null +++ b/src/state/GumballState/Program.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace GumballState +{ + public class Program + { + public static void Main(string[] args) + { + GumballMachine gumballMachine = new GumballMachine(2); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + gumballMachine.Refill(5); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + } + } +} diff --git a/src/state/GumballState/States/Abstractions/IState.cs b/src/state/GumballState/States/Abstractions/IState.cs new file mode 100644 index 0000000..3bfd5f9 --- /dev/null +++ b/src/state/GumballState/States/Abstractions/IState.cs @@ -0,0 +1,12 @@ +namespace GumballState.States +{ + public interface IState + { + void InsertQuarter(); + void EjectQuarter(); + void TurnCrank(); + void Dispense(); + + void Refill(); + } +} \ No newline at end of file diff --git a/src/state/GumballState/States/HasQuarterState.cs b/src/state/GumballState/States/HasQuarterState.cs new file mode 100644 index 0000000..982eceb --- /dev/null +++ b/src/state/GumballState/States/HasQuarterState.cs @@ -0,0 +1,43 @@ +using System; + +namespace GumballState.States +{ + public class HasQuarterState : IState + { + private readonly GumballMachine _gumballMachine; + + public HasQuarterState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("You can't insert another quarter"); + } + + public void EjectQuarter() + { + Console.WriteLine("Quarter returned"); + _gumballMachine.SetState(_gumballMachine.NoQuarterState); + } + + public void TurnCrank() + { + Console.WriteLine("You turned..."); + _gumballMachine.SetState(_gumballMachine.SoldState); + } + + public void Dispense() + { + Console.WriteLine("No gumball dispensed"); + } + + public void Refill() { } + + public override string ToString() + { + return "waiting for turn of crank"; + } + } +} \ No newline at end of file diff --git a/src/state/GumballState/States/NoQuarterState.cs b/src/state/GumballState/States/NoQuarterState.cs new file mode 100644 index 0000000..ace5b4b --- /dev/null +++ b/src/state/GumballState/States/NoQuarterState.cs @@ -0,0 +1,42 @@ +using System; + +namespace GumballState.States +{ + public class NoQuarterState : IState + { + private readonly GumballMachine _gumballMachine; + + public NoQuarterState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("You inserted a quarter"); + _gumballMachine.SetState(_gumballMachine.HasQuarterState); + } + + public void EjectQuarter() + { + Console.WriteLine("You haven't inserted a quarter"); + } + + public void TurnCrank() + { + Console.WriteLine("You turned, but there's no quarter"); + } + + public void Dispense() + { + Console.WriteLine("You need to pay first"); + } + + public void Refill() { } + + public override string ToString() + { + return "waiting for quarter"; + } + } +} \ No newline at end of file diff --git a/src/state/GumballState/States/SoldOutState.cs b/src/state/GumballState/States/SoldOutState.cs new file mode 100644 index 0000000..2e03cf3 --- /dev/null +++ b/src/state/GumballState/States/SoldOutState.cs @@ -0,0 +1,44 @@ +using System; + +namespace GumballState.States +{ + public class SoldOutState : IState + { + private readonly GumballMachine _gumballMachine; + + public SoldOutState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("You can't insert a quarter, the machine is sold out"); + } + + public void EjectQuarter() + { + Console.WriteLine("You can't eject, you haven't inserted a quarter yet"); + } + + public void TurnCrank() + { + Console.WriteLine("You turned, but there are no gumballs"); + } + + public void Dispense() + { + Console.WriteLine("No gumball dispensed"); + } + + public void Refill() + { + _gumballMachine.SetState(_gumballMachine.NoQuarterState); + } + + public override string ToString() + { + return "sold out"; + } + } +} \ No newline at end of file diff --git a/src/state/GumballState/States/SoldState.cs b/src/state/GumballState/States/SoldState.cs new file mode 100644 index 0000000..af40bfa --- /dev/null +++ b/src/state/GumballState/States/SoldState.cs @@ -0,0 +1,50 @@ +using System; + +namespace GumballState.States +{ + public class SoldState : IState + { + private readonly GumballMachine _gumballMachine; + + public SoldState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("Please wait, we're already giving you a gumball"); + } + + public void EjectQuarter() + { + Console.WriteLine("Sorry, you already turned the crank"); + } + + public void TurnCrank() + { + Console.WriteLine("Turning twice doesn't get you another gumball!"); + } + + public void Dispense() + { + _gumballMachine.ReleaseBall(); + if (_gumballMachine.Count > 0) + { + _gumballMachine.SetState(_gumballMachine.NoQuarterState); + } + else + { + Console.WriteLine("Oops, out of gumballs!"); + _gumballMachine.SetState(_gumballMachine.SoldOutState); + } + } + + public void Refill() { } + + public override string ToString() + { + return "dispensing a gumball"; + } + } +} \ No newline at end of file diff --git a/src/state/GumballStateWinner/GumballMachine.cs b/src/state/GumballStateWinner/GumballMachine.cs new file mode 100644 index 0000000..96b643e --- /dev/null +++ b/src/state/GumballStateWinner/GumballMachine.cs @@ -0,0 +1,86 @@ +using System; +using System.Text; +using GumballStateWinner.States; + +namespace GumballStateWinner +{ + //Context + public class GumballMachine + { + public GumballMachine(int numberGumballs) + { + SoldOutState = new SoldOutState(this); + NoQuarterState = new NoQuarterState(this); + HasQuarterState = new HasQuarterState(this); + SoldState = new SoldState(this); + WinnerState = new WinnerState(this); + + Count = numberGumballs; + + State = numberGumballs > 0 ? NoQuarterState : SoldOutState; + } + + public int Count { get; private set; } + public IState State { get; private set; } + + public IState SoldOutState { get; } + public IState NoQuarterState { get; } + public IState HasQuarterState { get; } + public IState SoldState { get; } + public IState WinnerState { get; } + + public void InsertQuarter() + { + State.InsertQuarter(); + } + + public void EjectQuarter() + { + State.EjectQuarter(); + } + + public void TurnCrank() + { + State.TurnCrank(); + State.Dispense(); + } + + public void ReleaseBall() + { + Console.WriteLine("A gumball comes rolling out the slot..."); + if (Count != 0) + { + Count--; + } + } + + public void Refill(int count) + { + Count += count; + Console.WriteLine($"The gumball machine was just refilled; it's new count is: {Count.ToString()}"); + State.Refill(); + } + + public void SetState(IState state) + { + State = state; + } + + public override string ToString() + { + StringBuilder result = new StringBuilder(); + + result.Append("\nMighty Gumball, Inc."); + result.Append("\nC#-enabled Standing Gumball Model #2016"); + result.Append("\nInventory: " + Count.ToString() + " gumball"); + if (Count != 1) + { + result.Append("s"); + } + result.Append("\n"); + result.Append("Machine is " + State + "\n"); + + return result.ToString(); + } + } +} \ No newline at end of file diff --git a/src/state/GumballStateWinner/GumballStateWinner.csproj b/src/state/GumballStateWinner/GumballStateWinner.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/state/GumballStateWinner/GumballStateWinner.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/state/GumballStateWinner/Program.cs b/src/state/GumballStateWinner/Program.cs new file mode 100644 index 0000000..0fc482d --- /dev/null +++ b/src/state/GumballStateWinner/Program.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace GumballStateWinner +{ + public class Program + { + public static void Main(string[] args) + { + GumballMachine gumballMachine = new GumballMachine(10); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + gumballMachine.InsertQuarter(); + gumballMachine.TurnCrank(); + + Console.WriteLine(gumballMachine); + } + } +} diff --git a/src/state/GumballStateWinner/States/Abstractions/IState.cs b/src/state/GumballStateWinner/States/Abstractions/IState.cs new file mode 100644 index 0000000..031e976 --- /dev/null +++ b/src/state/GumballStateWinner/States/Abstractions/IState.cs @@ -0,0 +1,12 @@ +namespace GumballStateWinner.States +{ + public interface IState + { + void InsertQuarter(); + void EjectQuarter(); + void TurnCrank(); + void Dispense(); + + void Refill(); + } +} \ No newline at end of file diff --git a/src/state/GumballStateWinner/States/HasQuarterState.cs b/src/state/GumballStateWinner/States/HasQuarterState.cs new file mode 100644 index 0000000..c6b0f0c --- /dev/null +++ b/src/state/GumballStateWinner/States/HasQuarterState.cs @@ -0,0 +1,52 @@ +using System; + +namespace GumballStateWinner.States +{ + public class HasQuarterState : IState + { + private readonly GumballMachine _gumballMachine; + private readonly Random _random = new Random(); + + public HasQuarterState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("You can't insert another quarter"); + } + + public void EjectQuarter() + { + Console.WriteLine("Quarter returned"); + _gumballMachine.SetState(_gumballMachine.NoQuarterState); + } + + public void TurnCrank() + { + Console.WriteLine("You turned..."); + int winner = _random.Next(10); + if (winner == 0 && _gumballMachine.Count > 1) + { + _gumballMachine.SetState(_gumballMachine.WinnerState); + } + else + { + _gumballMachine.SetState(_gumballMachine.SoldState); + } + } + + public void Dispense() + { + Console.WriteLine("No gumball dispensed"); + } + + public void Refill() { } + + public override string ToString() + { + return "waiting for turn of crank"; + } + } +} \ No newline at end of file diff --git a/src/state/GumballStateWinner/States/NoQuarterState.cs b/src/state/GumballStateWinner/States/NoQuarterState.cs new file mode 100644 index 0000000..46b1510 --- /dev/null +++ b/src/state/GumballStateWinner/States/NoQuarterState.cs @@ -0,0 +1,42 @@ +using System; + +namespace GumballStateWinner.States +{ + public class NoQuarterState : IState + { + private readonly GumballMachine _gumballMachine; + + public NoQuarterState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("You inserted a quarter"); + _gumballMachine.SetState(_gumballMachine.HasQuarterState); + } + + public void EjectQuarter() + { + Console.WriteLine("You haven't inserted a quarter"); + } + + public void TurnCrank() + { + Console.WriteLine("You turned, but there's no quarter"); + } + + public void Dispense() + { + Console.WriteLine("You need to pay first"); + } + + public void Refill() { } + + public override string ToString() + { + return "waiting for quarter"; + } + } +} \ No newline at end of file diff --git a/src/state/GumballStateWinner/States/SoldOutState.cs b/src/state/GumballStateWinner/States/SoldOutState.cs new file mode 100644 index 0000000..c811bf2 --- /dev/null +++ b/src/state/GumballStateWinner/States/SoldOutState.cs @@ -0,0 +1,44 @@ +using System; + +namespace GumballStateWinner.States +{ + public class SoldOutState : IState + { + private readonly GumballMachine _gumballMachine; + + public SoldOutState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("You can't insert a quarter, the machine is sold out"); + } + + public void EjectQuarter() + { + Console.WriteLine("You can't eject, you haven't inserted a quarter yet"); + } + + public void TurnCrank() + { + Console.WriteLine("You turned, but there are no gumballs"); + } + + public void Dispense() + { + Console.WriteLine("No gumball dispensed"); + } + + public void Refill() + { + _gumballMachine.SetState(_gumballMachine.NoQuarterState); + } + + public override string ToString() + { + return "sold out"; + } + } +} \ No newline at end of file diff --git a/src/state/GumballStateWinner/States/SoldState.cs b/src/state/GumballStateWinner/States/SoldState.cs new file mode 100644 index 0000000..dd45782 --- /dev/null +++ b/src/state/GumballStateWinner/States/SoldState.cs @@ -0,0 +1,50 @@ +using System; + +namespace GumballStateWinner.States +{ + public class SoldState : IState + { + private readonly GumballMachine _gumballMachine; + + public SoldState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("Please wait, we're already giving you a gumball"); + } + + public void EjectQuarter() + { + Console.WriteLine("Sorry, you already turned the crank"); + } + + public void TurnCrank() + { + Console.WriteLine("Turning twice doesn't get you another gumball!"); + } + + public void Dispense() + { + _gumballMachine.ReleaseBall(); + if (_gumballMachine.Count > 0) + { + _gumballMachine.SetState(_gumballMachine.NoQuarterState); + } + else + { + Console.WriteLine("Oops, out of gumballs!"); + _gumballMachine.SetState(_gumballMachine.SoldOutState); + } + } + + public void Refill() { } + + public override string ToString() + { + return "dispensing a gumball"; + } + } +} \ No newline at end of file diff --git a/src/state/GumballStateWinner/States/WinnerState.cs b/src/state/GumballStateWinner/States/WinnerState.cs new file mode 100644 index 0000000..2d306bb --- /dev/null +++ b/src/state/GumballStateWinner/States/WinnerState.cs @@ -0,0 +1,59 @@ +using System; + +namespace GumballStateWinner.States +{ + public class WinnerState : IState + { + private readonly GumballMachine _gumballMachine; + + public WinnerState(GumballMachine gumballMachine) + { + _gumballMachine = gumballMachine; + } + + public void InsertQuarter() + { + Console.WriteLine("Please wait, we're already giving you a Gumball"); + } + + public void EjectQuarter() + { + Console.WriteLine("Please wait, we're already giving you a Gumball"); + } + + public void TurnCrank() + { + Console.WriteLine("Please wait, we're already giving you a Gumball"); + } + + public void Dispense() + { + _gumballMachine.ReleaseBall(); + if (_gumballMachine.Count == 0) + { + _gumballMachine.SetState(_gumballMachine.SoldOutState); + } + else + { + _gumballMachine.ReleaseBall(); + Console.WriteLine("YOU'RE A WINNER! You got two gumballs for your quarter"); + if (_gumballMachine.Count > 0) + { + _gumballMachine.SetState(_gumballMachine.NoQuarterState); + } + else + { + Console.WriteLine("Oops, out of gumballs!"); + _gumballMachine.SetState(_gumballMachine.SoldOutState); + } + } + } + + public void Refill() { } + + public override string ToString() + { + return "dispensing two gumballs for your quarter, because YOU'RE A WINNER!"; + } + } +} \ No newline at end of file diff --git a/src/state/README.md b/src/state/README.md new file mode 100644 index 0000000..dbe5acf --- /dev/null +++ b/src/state/README.md @@ -0,0 +1,2 @@ +**The State Pattern** allows an object to alter its behavior when its internal state changes. +The object will appear to change its class. \ No newline at end of file diff --git a/src/strategy/MiniDuckSimulator/Behaviors/Abstractions/IFlyBehavior.cs b/src/strategy/MiniDuckSimulator/Behaviors/Abstractions/IFlyBehavior.cs new file mode 100644 index 0000000..dc79195 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Behaviors/Abstractions/IFlyBehavior.cs @@ -0,0 +1,7 @@ +namespace MiniDuckSimulator.Behaviors +{ + public interface IFlyBehavior + { + void Fly(); + } +} diff --git a/src/strategy/MiniDuckSimulator/Behaviors/Abstractions/IQuackBehavior.cs b/src/strategy/MiniDuckSimulator/Behaviors/Abstractions/IQuackBehavior.cs new file mode 100644 index 0000000..20cd9a8 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Behaviors/Abstractions/IQuackBehavior.cs @@ -0,0 +1,7 @@ +namespace MiniDuckSimulator.Behaviors +{ + public interface IQuackBehavior + { + void Quack(); + } +} diff --git a/src/strategy/MiniDuckSimulator/Behaviors/FlyNoWay.cs b/src/strategy/MiniDuckSimulator/Behaviors/FlyNoWay.cs new file mode 100644 index 0000000..43dd60c --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Behaviors/FlyNoWay.cs @@ -0,0 +1,12 @@ +using System; + +namespace MiniDuckSimulator.Behaviors +{ + public class FlyNoWay : IFlyBehavior + { + public void Fly() + { + Console.WriteLine("I can't fly"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Behaviors/FlyRocketPowered.cs b/src/strategy/MiniDuckSimulator/Behaviors/FlyRocketPowered.cs new file mode 100644 index 0000000..d4a427b --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Behaviors/FlyRocketPowered.cs @@ -0,0 +1,12 @@ +using System; + +namespace MiniDuckSimulator.Behaviors +{ + public class FlyRocketPowered : IFlyBehavior + { + public void Fly() + { + Console.WriteLine("I'm flying with a rocket!"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Behaviors/FlyWithWings.cs b/src/strategy/MiniDuckSimulator/Behaviors/FlyWithWings.cs new file mode 100644 index 0000000..1d42ead --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Behaviors/FlyWithWings.cs @@ -0,0 +1,12 @@ +using System; + +namespace MiniDuckSimulator.Behaviors +{ + public class FlyWithWings : IFlyBehavior + { + public void Fly() + { + Console.WriteLine("I'm flying!!"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Behaviors/MuteQuack.cs b/src/strategy/MiniDuckSimulator/Behaviors/MuteQuack.cs new file mode 100644 index 0000000..f4897c7 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Behaviors/MuteQuack.cs @@ -0,0 +1,12 @@ +using System; + +namespace MiniDuckSimulator.Behaviors +{ + public class MuteQuack : IQuackBehavior + { + public void Quack() + { + Console.WriteLine("<< Silence >>"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Behaviors/Quack.cs b/src/strategy/MiniDuckSimulator/Behaviors/Quack.cs new file mode 100644 index 0000000..7151360 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Behaviors/Quack.cs @@ -0,0 +1,12 @@ +using System; + +namespace MiniDuckSimulator.Behaviors +{ + public class Quack : IQuackBehavior + { + void IQuackBehavior.Quack() + { + Console.WriteLine("Quack"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Behaviors/Squeak.cs b/src/strategy/MiniDuckSimulator/Behaviors/Squeak.cs new file mode 100644 index 0000000..468c8e3 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Behaviors/Squeak.cs @@ -0,0 +1,12 @@ +using System; + +namespace MiniDuckSimulator.Behaviors +{ + public class Squeak : IQuackBehavior + { + public void Quack() + { + Console.WriteLine("Squeak"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Ducks/Abstractions/Duck.cs b/src/strategy/MiniDuckSimulator/Ducks/Abstractions/Duck.cs new file mode 100644 index 0000000..2265154 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Ducks/Abstractions/Duck.cs @@ -0,0 +1,32 @@ +using System; +using MiniDuckSimulator.Behaviors; + +namespace MiniDuckSimulator.Ducks +{ + public abstract class Duck + { + public IFlyBehavior FlyBehavior { get; set; } + public IQuackBehavior QuackBehavior { get; set; } + + protected Duck() + { + } + + public abstract void Display(); + + public void PerformFly() + { + FlyBehavior.Fly(); + } + + public void PerformQuack() + { + QuackBehavior.Quack(); + } + + public void Swim() + { + Console.WriteLine("All ducks float, even decoys!"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Ducks/DecoyDuck.cs b/src/strategy/MiniDuckSimulator/Ducks/DecoyDuck.cs new file mode 100644 index 0000000..4c59653 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Ducks/DecoyDuck.cs @@ -0,0 +1,19 @@ +using System; +using MiniDuckSimulator.Behaviors; + +namespace MiniDuckSimulator.Ducks +{ + public class DecoyDuck : Duck + { + public DecoyDuck() + { + QuackBehavior = new MuteQuack(); + FlyBehavior = new FlyNoWay(); + } + + public override void Display() + { + Console.WriteLine("I'm a duck Decoy"); + } + } +} \ No newline at end of file diff --git a/src/strategy/MiniDuckSimulator/Ducks/MallardDuck.cs b/src/strategy/MiniDuckSimulator/Ducks/MallardDuck.cs new file mode 100644 index 0000000..4c36e15 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Ducks/MallardDuck.cs @@ -0,0 +1,19 @@ +using System; +using MiniDuckSimulator.Behaviors; + +namespace MiniDuckSimulator.Ducks +{ + public class MallardDuck : Duck + { + public MallardDuck() + { + QuackBehavior = new Quack(); + FlyBehavior = new FlyWithWings(); + } + + public override void Display() + { + Console.WriteLine("I'm a real Mallard duck"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Ducks/ModelDuck.cs b/src/strategy/MiniDuckSimulator/Ducks/ModelDuck.cs new file mode 100644 index 0000000..3161865 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Ducks/ModelDuck.cs @@ -0,0 +1,19 @@ +using System; +using MiniDuckSimulator.Behaviors; + +namespace MiniDuckSimulator.Ducks +{ + public class ModelDuck : Duck + { + public ModelDuck() + { + FlyBehavior = new FlyNoWay(); + QuackBehavior = new Quack(); + } + + public override void Display() + { + Console.WriteLine("I'm a model duck"); + } + } +} diff --git a/src/strategy/MiniDuckSimulator/Ducks/RedHeadDuck.cs b/src/strategy/MiniDuckSimulator/Ducks/RedHeadDuck.cs new file mode 100644 index 0000000..20707d1 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Ducks/RedHeadDuck.cs @@ -0,0 +1,19 @@ +using System; +using MiniDuckSimulator.Behaviors; + +namespace MiniDuckSimulator.Ducks +{ + public class RedHeadDuck : Duck + { + public RedHeadDuck() + { + QuackBehavior = new Quack(); + FlyBehavior = new FlyWithWings(); + } + + public override void Display() + { + Console.WriteLine("I'm a real Red Headed duck"); + } + } +} \ No newline at end of file diff --git a/src/strategy/MiniDuckSimulator/Ducks/RubberDuck.cs b/src/strategy/MiniDuckSimulator/Ducks/RubberDuck.cs new file mode 100644 index 0000000..dd03cd5 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Ducks/RubberDuck.cs @@ -0,0 +1,19 @@ +using System; +using MiniDuckSimulator.Behaviors; + +namespace MiniDuckSimulator.Ducks +{ + public class RubberDuck : Duck + { + public RubberDuck() + { + QuackBehavior = new Squeak(); + FlyBehavior = new FlyNoWay(); + } + + public override void Display() + { + Console.WriteLine("I'm a rubber duckie"); + } + } +} \ No newline at end of file diff --git a/src/strategy/MiniDuckSimulator/MiniDuckSimulator.csproj b/src/strategy/MiniDuckSimulator/MiniDuckSimulator.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/strategy/MiniDuckSimulator/MiniDuckSimulator.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/strategy/MiniDuckSimulator/Program.cs b/src/strategy/MiniDuckSimulator/Program.cs new file mode 100644 index 0000000..2757e1f --- /dev/null +++ b/src/strategy/MiniDuckSimulator/Program.cs @@ -0,0 +1,26 @@ +using System; +using MiniDuckSimulator.Behaviors; +using MiniDuckSimulator.Ducks; + +namespace MiniDuckSimulator +{ + public class Program + { + public static void Main(string[] args) + { + MallardDuck mallard = new MallardDuck(); + RubberDuck rubberDuckie = new RubberDuck(); + DecoyDuck decoy = new DecoyDuck(); + + ModelDuck model = new ModelDuck(); + + mallard.PerformQuack(); + rubberDuckie.PerformQuack(); + decoy.PerformQuack(); + + model.PerformFly(); + model.FlyBehavior = new FlyRocketPowered(); + model.PerformFly(); + } + } +} diff --git a/src/strategy/README.md b/src/strategy/README.md new file mode 100644 index 0000000..545eb92 --- /dev/null +++ b/src/strategy/README.md @@ -0,0 +1,2 @@ +**The Strategy Pattern** defines a family of algorithms, encapsulates each one, and makes them interchangeable. +Strategy lets the algorithm vary independently from clients that use it. \ No newline at end of file diff --git a/src/templatemethod/Barista/Barista.csproj b/src/templatemethod/Barista/Barista.csproj new file mode 100644 index 0000000..a488b70 --- /dev/null +++ b/src/templatemethod/Barista/Barista.csproj @@ -0,0 +1,8 @@ + + + + netcoreapp3.1 + Exe + + + diff --git a/src/templatemethod/Barista/CaffeineBeverage.cs b/src/templatemethod/Barista/CaffeineBeverage.cs new file mode 100644 index 0000000..804e481 --- /dev/null +++ b/src/templatemethod/Barista/CaffeineBeverage.cs @@ -0,0 +1,28 @@ +using System; + +namespace Barista +{ + public abstract class CaffeineBeverage + { + public void PrepareRecipe() + { + BoilWater(); + Brew(); + PourInCup(); + AddCondiments(); + } + + public abstract void Brew(); + public abstract void AddCondiments(); + + public virtual void BoilWater() + { + Console.WriteLine("Boiling water"); + } + + public virtual void PourInCup() + { + Console.WriteLine("Pouring into cup"); + } + } +} \ No newline at end of file diff --git a/src/templatemethod/Barista/CaffeineBeverageWithHook.cs b/src/templatemethod/Barista/CaffeineBeverageWithHook.cs new file mode 100644 index 0000000..8c69558 --- /dev/null +++ b/src/templatemethod/Barista/CaffeineBeverageWithHook.cs @@ -0,0 +1,33 @@ +using System; + +namespace Barista +{ + public abstract class CaffeineBeverageWithHook + { + public void PrepareRecipe() + { + BoilWater(); + Brew(); + PourInCup(); + if (CustomerWantsCondiments) + { + AddCondiments(); + } + } + + public abstract void Brew(); + public abstract void AddCondiments(); + + public virtual void BoilWater() + { + Console.WriteLine("Boiling water"); + } + + public virtual void PourInCup() + { + Console.WriteLine("Pouring into cup"); + } + + public virtual bool CustomerWantsCondiments => true; + } +} \ No newline at end of file diff --git a/src/templatemethod/Barista/Coffee.cs b/src/templatemethod/Barista/Coffee.cs new file mode 100644 index 0000000..398f841 --- /dev/null +++ b/src/templatemethod/Barista/Coffee.cs @@ -0,0 +1,17 @@ +using System; + +namespace Barista +{ + public class Coffee : CaffeineBeverage + { + public override void Brew() + { + Console.WriteLine("Dripping Coffee through filter"); + } + + public override void AddCondiments() + { + Console.WriteLine("Adding Sugar and Milk"); + } + } +} \ No newline at end of file diff --git a/src/templatemethod/Barista/CoffeeWithHook.cs b/src/templatemethod/Barista/CoffeeWithHook.cs new file mode 100644 index 0000000..bd071b7 --- /dev/null +++ b/src/templatemethod/Barista/CoffeeWithHook.cs @@ -0,0 +1,28 @@ +using System; + +namespace Barista +{ + public class CoffeeWithHook : CaffeineBeverageWithHook + { + public override void Brew() + { + Console.WriteLine("Dripping Coffee through filter"); + } + + public override void AddCondiments() + { + Console.WriteLine("Adding Sugar and Milk"); + } + + public override bool CustomerWantsCondiments => GetUserInput().ToUpper().StartsWith("Y"); + + private string GetUserInput() + { + Console.WriteLine("Would you like milk and sugar with your coffee (y/n)? "); + + string answer = Console.ReadLine(); + + return answer ?? "no"; + } + } +} \ No newline at end of file diff --git a/src/templatemethod/Barista/Program.cs b/src/templatemethod/Barista/Program.cs new file mode 100644 index 0000000..05e41b2 --- /dev/null +++ b/src/templatemethod/Barista/Program.cs @@ -0,0 +1,29 @@ +using System; + +namespace Barista +{ + public class Program + { + public static void Main(string[] args) + { + Tea tea = new Tea(); + Coffee coffee = new Coffee(); + + Console.WriteLine("Making tea..."); + tea.PrepareRecipe(); + + Console.WriteLine("\nMaking coffee..."); + coffee.PrepareRecipe(); + + + TeaWithHook teaHook = new TeaWithHook(); + CoffeeWithHook coffeeHook = new CoffeeWithHook(); + + Console.WriteLine("\nMaking tea..."); + teaHook.PrepareRecipe(); + + Console.WriteLine("\nMaking coffee..."); + coffeeHook.PrepareRecipe(); + } + } +} diff --git a/src/templatemethod/Barista/Tea.cs b/src/templatemethod/Barista/Tea.cs new file mode 100644 index 0000000..c22e90b --- /dev/null +++ b/src/templatemethod/Barista/Tea.cs @@ -0,0 +1,17 @@ +using System; + +namespace Barista +{ + public class Tea : CaffeineBeverage + { + public override void Brew() + { + Console.WriteLine("Steeping the tea"); + } + + public override void AddCondiments() + { + Console.WriteLine("Adding Lemon"); + } + } +} \ No newline at end of file diff --git a/src/templatemethod/Barista/TeaWithHook.cs b/src/templatemethod/Barista/TeaWithHook.cs new file mode 100644 index 0000000..f289b1a --- /dev/null +++ b/src/templatemethod/Barista/TeaWithHook.cs @@ -0,0 +1,28 @@ +using System; + +namespace Barista +{ + public class TeaWithHook : CaffeineBeverageWithHook + { + public override void Brew() + { + Console.WriteLine("Steeping the tea"); + } + + public override void AddCondiments() + { + Console.WriteLine("Adding Lemon"); + } + + public override bool CustomerWantsCondiments => GetUserInput().ToUpper().StartsWith("Y"); + + private string GetUserInput() + { + Console.WriteLine("Would you like lemon with your tea (y/n)? "); + + string answer = Console.ReadLine(); + + return answer ?? "no"; + } + } +} \ No newline at end of file diff --git a/src/templatemethod/DuckSort/Duck.cs b/src/templatemethod/DuckSort/Duck.cs new file mode 100644 index 0000000..c4fbf9b --- /dev/null +++ b/src/templatemethod/DuckSort/Duck.cs @@ -0,0 +1,28 @@ +using System; + +namespace DuckSort +{ + public class Duck : IComparable + { + public Duck(string name, int weight) + { + Name = name; + Weight = weight; + } + + public string Name { get; } + public int Weight { get; } + + public int CompareTo(Duck other) + { + if (Weight < other.Weight) + { + return -1; + } + + return Weight == other.Weight ? 0 : 1; //Weight > other.Weight; + } + + public override string ToString() => $"{Name} weighs {Weight.ToString()}"; + } +} \ No newline at end of file diff --git a/src/templatemethod/DuckSort/DuckSort.csproj b/src/templatemethod/DuckSort/DuckSort.csproj new file mode 100644 index 0000000..cb63190 --- /dev/null +++ b/src/templatemethod/DuckSort/DuckSort.csproj @@ -0,0 +1,7 @@ + + + + netcoreapp3.1 + + + diff --git a/src/templatemethod/DuckSort/Program.cs b/src/templatemethod/DuckSort/Program.cs new file mode 100644 index 0000000..93e5e31 --- /dev/null +++ b/src/templatemethod/DuckSort/Program.cs @@ -0,0 +1,37 @@ +using System; +using System.Collections.Generic; + +namespace DuckSort +{ + public class Program + { + public static void Main(string[] args) + { + Duck[] ducks = + { + new Duck("Daffy", 8), + new Duck("Dewey", 2), + new Duck("Howard", 7), + new Duck("Louie", 2), + new Duck("Donald", 10), + new Duck("Huey", 2) + }; + + Console.WriteLine("Before sorting:"); + Display(ducks); + + Array.Sort(ducks); + + Console.WriteLine("\nAfter sorting:"); + Display(ducks); + } + + public static void Display(IEnumerable ducks) + { + foreach (var duck in ducks) + { + Console.WriteLine(duck); + } + } + } +} diff --git a/src/templatemethod/README.md b/src/templatemethod/README.md new file mode 100644 index 0000000..0a141c7 --- /dev/null +++ b/src/templatemethod/README.md @@ -0,0 +1,2 @@ +**The Template Method Pattern** defines the skeleton of an algorithm in a method, deferring some steps to subclasses. +Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. \ No newline at end of file