diff --git a/AdapterPattern/AdapterPattern.csproj b/AdapterPattern/AdapterPattern.csproj index ce1697a..74abf5c 100644 --- a/AdapterPattern/AdapterPattern.csproj +++ b/AdapterPattern/AdapterPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/AdapterPattern/MallardDuck.cs b/AdapterPattern/MallardDuck.cs index 24c1ede..b0602cf 100644 --- a/AdapterPattern/MallardDuck.cs +++ b/AdapterPattern/MallardDuck.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace AdapterPattern { diff --git a/AdapterPattern/Program.cs b/AdapterPattern/Program.cs index e0095ad..cb46974 100644 --- a/AdapterPattern/Program.cs +++ b/AdapterPattern/Program.cs @@ -1,5 +1,4 @@ -using System; - + namespace AdapterPattern { internal static class Program diff --git a/AdapterPattern/TurkeyAdapter.cs b/AdapterPattern/TurkeyAdapter.cs index 708eae1..adf13e0 100644 --- a/AdapterPattern/TurkeyAdapter.cs +++ b/AdapterPattern/TurkeyAdapter.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; namespace AdapterPattern { diff --git a/AdapterPattern/WildTurkey.cs b/AdapterPattern/WildTurkey.cs index 2e79fa7..b6e04ac 100644 --- a/AdapterPattern/WildTurkey.cs +++ b/AdapterPattern/WildTurkey.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace AdapterPattern { diff --git a/BridgePattern/BridgePattern.csproj b/BridgePattern/BridgePattern.csproj index 51b7f6c..74abf5c 100644 --- a/BridgePattern/BridgePattern.csproj +++ b/BridgePattern/BridgePattern.csproj @@ -1,8 +1,10 @@ - - Exe - netcoreapp2.1 - + + Exe + net6.0 + enable + enable + diff --git a/BridgePattern/FlyingEnchantment.cs b/BridgePattern/FlyingEnchantment.cs index 4678965..4aa5b38 100644 --- a/BridgePattern/FlyingEnchantment.cs +++ b/BridgePattern/FlyingEnchantment.cs @@ -2,7 +2,7 @@ namespace BridgePattern { - public class FlyingEnchantment:IEnchantment + public class FlyingEnchantment : IEnchantment { public void OnActivate() { diff --git a/BridgePattern/Hammer.cs b/BridgePattern/Hammer.cs index 99a2786..55e6782 100644 --- a/BridgePattern/Hammer.cs +++ b/BridgePattern/Hammer.cs @@ -2,7 +2,7 @@ namespace BridgePattern { - public class Hammer:IWeapon + public class Hammer : IWeapon { private readonly IEnchantment _enchantment; public Hammer(IEnchantment enchantment) diff --git a/BridgePattern/Program.cs b/BridgePattern/Program.cs index 2ee4591..c07d035 100644 --- a/BridgePattern/Program.cs +++ b/BridgePattern/Program.cs @@ -8,7 +8,7 @@ private static void Main() sword.Wield(); sword.Swing(); sword.Unwield(); - + IWeapon hammer = new Hammer(new SoulEatingEnchantment()); hammer.Wield(); hammer.Swing(); diff --git a/BridgePattern/SoulEatingEnchantment.cs b/BridgePattern/SoulEatingEnchantment.cs index 6b8d36d..dfb9b8a 100644 --- a/BridgePattern/SoulEatingEnchantment.cs +++ b/BridgePattern/SoulEatingEnchantment.cs @@ -2,7 +2,7 @@ namespace BridgePattern { - public class SoulEatingEnchantment:IEnchantment + public class SoulEatingEnchantment : IEnchantment { public void OnActivate() { diff --git a/BridgePattern/Sword.cs b/BridgePattern/Sword.cs index 2145b4b..23198d5 100644 --- a/BridgePattern/Sword.cs +++ b/BridgePattern/Sword.cs @@ -2,7 +2,7 @@ namespace BridgePattern { - public class Sword:IWeapon + public class Sword : IWeapon { private readonly IEnchantment _enchantment; diff --git a/BuilderPattern/BuilderPattern.csproj b/BuilderPattern/BuilderPattern.csproj index ce1697a..74abf5c 100644 --- a/BuilderPattern/BuilderPattern.csproj +++ b/BuilderPattern/BuilderPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/BuilderPattern/Cook.cs b/BuilderPattern/Cook.cs index dd40620..980bb3d 100644 --- a/BuilderPattern/Cook.cs +++ b/BuilderPattern/Cook.cs @@ -1,24 +1,30 @@ -namespace BuilderPattern { +namespace BuilderPattern +{ // This class can also be called the Director - public class Cook { + public class Cook + { private IBuilder _builder; - public Cook(IBuilder builder) { + public Cook(IBuilder builder) + { AcceptBuilder(builder); } - public void ChangeBuilder(IBuilder builder) { + public void ChangeBuilder(IBuilder builder) + { AcceptBuilder(builder); } - public Hamburger Build() { + public Hamburger Build() + { _builder.AddIngredients(); _builder.AddShape(); _builder.AddSize(); return _builder.Build(); } - private void AcceptBuilder(IBuilder builder) { + private void AcceptBuilder(IBuilder builder) + { _builder = builder; _builder.Reset(); } diff --git a/BuilderPattern/Hamburger.cs b/BuilderPattern/Hamburger.cs index 604f138..68ec0f6 100644 --- a/BuilderPattern/Hamburger.cs +++ b/BuilderPattern/Hamburger.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; - + namespace BuilderPattern { public class Hamburger @@ -9,8 +8,9 @@ public class Hamburger public string[] Ingredients { get; set; } public override string ToString() { - var hamburger=""; - foreach (var ingredient in Ingredients) { + var hamburger = ""; + foreach (var ingredient in Ingredients) + { hamburger += $"{ingredient} "; } return $"Ingredients: {hamburger}, Size: {Size}, Shape: {Shape}"; diff --git a/BuilderPattern/IBuilder.cs b/BuilderPattern/IBuilder.cs index 34d96a2..2730841 100644 --- a/BuilderPattern/IBuilder.cs +++ b/BuilderPattern/IBuilder.cs @@ -1,5 +1,7 @@ -namespace BuilderPattern { - public interface IBuilder { +namespace BuilderPattern +{ + public interface IBuilder + { void AddIngredients(); void AddShape(); void AddSize(); diff --git a/BuilderPattern/MyHamburgerBuilder.cs b/BuilderPattern/MyHamburgerBuilder.cs index 61782f7..4ce1cbf 100644 --- a/BuilderPattern/MyHamburgerBuilder.cs +++ b/BuilderPattern/MyHamburgerBuilder.cs @@ -1,23 +1,29 @@ namespace BuilderPattern { - public class MyHamburgerBuilder : IBuilder { + public class MyHamburgerBuilder : IBuilder + { private Hamburger _hamburger; - public void AddIngredients() { + public void AddIngredients() + { _hamburger.Ingredients = new string[] { "Bread", "Meat", "Tomato", "Salad", "Mayonnaise" }; } - public void AddShape() { + public void AddShape() + { _hamburger.Shape = "Kite"; } - public void AddSize() { + public void AddSize() + { _hamburger.Size = 10; //inches } - public void Reset() { + public void Reset() + { _hamburger = new Hamburger(); } - public Hamburger Build() { + public Hamburger Build() + { return _hamburger; } diff --git a/BuilderPattern/WifesHamburgerBuilder.cs b/BuilderPattern/WifesHamburgerBuilder.cs index 9724fc6..887dcc1 100644 --- a/BuilderPattern/WifesHamburgerBuilder.cs +++ b/BuilderPattern/WifesHamburgerBuilder.cs @@ -1,25 +1,30 @@ -using System; +namespace BuilderPattern +{ -namespace BuilderPattern { - - public class WifesHamburgerBuilder : IBuilder { + public class WifesHamburgerBuilder : IBuilder + { private Hamburger _hamburger; - public void AddIngredients() { + public void AddIngredients() + { _hamburger.Ingredients = new string[] { "Bread", "Salad" }; } - public void AddShape() { + public void AddShape() + { _hamburger.Shape = "Cuboid"; } - public void AddSize() { + public void AddSize() + { _hamburger.Size = 6; //inches } - public void Reset() { + public void Reset() + { _hamburger = new Hamburger(); } - public Hamburger Build() { + public Hamburger Build() + { return _hamburger; } } diff --git a/ChainOfResponsibilityPattern/ChainOfResponsibilityPattern.csproj b/ChainOfResponsibilityPattern/ChainOfResponsibilityPattern.csproj index 23df604..74abf5c 100644 --- a/ChainOfResponsibilityPattern/ChainOfResponsibilityPattern.csproj +++ b/ChainOfResponsibilityPattern/ChainOfResponsibilityPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.1 + net6.0 + enable + enable diff --git a/CommandPattern/CommandPattern.csproj b/CommandPattern/CommandPattern.csproj index ce1697a..74abf5c 100644 --- a/CommandPattern/CommandPattern.csproj +++ b/CommandPattern/CommandPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/CommandPattern/Program.cs b/CommandPattern/Program.cs index d12a569..d719ca9 100644 --- a/CommandPattern/Program.cs +++ b/CommandPattern/Program.cs @@ -33,11 +33,11 @@ private static void Main() Console.WriteLine(); var light = new Light("Hall"); - ICommand[] partyOn = {new LightOffCommand(light), bikeDoorOpen, carDoorOpen}; - ICommand[] partyOff = {new LightOnCommand(light), bikeDoorClose, carDoorClose}; + ICommand[] partyOn = { new LightOffCommand(light), bikeDoorOpen, carDoorOpen }; + ICommand[] partyOff = { new LightOnCommand(light), bikeDoorClose, carDoorClose }; - remote[2] = new OnOffStruct {On = new MacroCommand(partyOn), Off = new MacroCommand(partyOff)}; + remote[2] = new OnOffStruct { On = new MacroCommand(partyOn), Off = new MacroCommand(partyOff) }; try { diff --git a/CompositePattern/CompositePattern.csproj b/CompositePattern/CompositePattern.csproj index ce1697a..74abf5c 100644 --- a/CompositePattern/CompositePattern.csproj +++ b/CompositePattern/CompositePattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/CompositePattern/Menu.cs b/CompositePattern/Menu.cs index 5049303..6be36c6 100644 --- a/CompositePattern/Menu.cs +++ b/CompositePattern/Menu.cs @@ -26,7 +26,7 @@ public override void Remove(MenuComponent component) public override MenuComponent GetChild(int i) { - return _components[i]; + return _components[i]; } public override string Name { get; } diff --git a/CompositePattern/MenuComponent.cs b/CompositePattern/MenuComponent.cs index 07c8c03..38928e4 100644 --- a/CompositePattern/MenuComponent.cs +++ b/CompositePattern/MenuComponent.cs @@ -6,7 +6,7 @@ public class MenuComponent { public virtual void Add(MenuComponent component) { - throw new NotImplementedException(); + throw new NotImplementedException(); } public virtual void Remove(MenuComponent component) diff --git a/CompositePattern/MenuItem.cs b/CompositePattern/MenuItem.cs index 50222e8..ab7c2f1 100644 --- a/CompositePattern/MenuItem.cs +++ b/CompositePattern/MenuItem.cs @@ -18,7 +18,7 @@ public MenuItem(string name, string description, double price, bool isveg) public override double Price { get; } - public override bool Vegetarian { get; } + public override bool Vegetarian { get; } public override void Print() { diff --git a/CompositePattern/Program.cs b/CompositePattern/Program.cs index ea24810..1c24613 100644 --- a/CompositePattern/Program.cs +++ b/CompositePattern/Program.cs @@ -7,14 +7,14 @@ public static void Main() var breakfast = new Menu("Breakfast", "Pancake House"); var lunch = new Menu("Lunch", "Deli Diner"); - var dinner = new Menu("Dinner","Dinneroni"); + var dinner = new Menu("Dinner", "Dinneroni"); var dessert = new Menu("Dessert", "Ice Cream"); var menu = new Menu("All", "McDonalds"); - breakfast.Add(new MenuItem("Waffles","Butterscotch waffles",140,false)); - breakfast.Add(new MenuItem("Corn Flakes","Kellogs",80,true)); + breakfast.Add(new MenuItem("Waffles", "Butterscotch waffles", 140, false)); + breakfast.Add(new MenuItem("Corn Flakes", "Kellogs", 80, true)); lunch.Add(new MenuItem("Burger", "Cheese and Onion Burger", 250, true)); lunch.Add(new MenuItem("Sandwich", "Chicken Sandwich", 280, false)); @@ -23,7 +23,7 @@ public static void Main() dinner.Add(new MenuItem("Pasta", "Chicken Pasta", 280, false)); dessert.Add(new MenuItem("Ice Cream", "Vanilla and Chocolate", 120, true)); - dessert.Add(new MenuItem("Cake", "Choclate Cake Slice",180, false)); + dessert.Add(new MenuItem("Cake", "Choclate Cake Slice", 180, false)); dinner.Add(dessert); menu.Add(breakfast); diff --git a/DecoratorPattern/DecoratorPattern.csproj b/DecoratorPattern/DecoratorPattern.csproj index ce1697a..74abf5c 100644 --- a/DecoratorPattern/DecoratorPattern.csproj +++ b/DecoratorPattern/DecoratorPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/DecoratorPattern/MochaCondiment.cs b/DecoratorPattern/MochaCondiment.cs index ae2b45d..b0e76a5 100644 --- a/DecoratorPattern/MochaCondiment.cs +++ b/DecoratorPattern/MochaCondiment.cs @@ -9,8 +9,9 @@ public MochaCondiment(Beverage beverage) this._beverage = beverage; } - public override string Description { - get + public override string Description + { + get { if (_beverage.Description.StartsWith("Mocha")) { diff --git a/DecoratorPattern/WhipCondiment.cs b/DecoratorPattern/WhipCondiment.cs index 8863b67..550b929 100644 --- a/DecoratorPattern/WhipCondiment.cs +++ b/DecoratorPattern/WhipCondiment.cs @@ -12,7 +12,7 @@ public WhipCondiment(Beverage beverage) public override string Description { get - { + { if (_beverage.Description.StartsWith("Whip")) { return "Double " + _beverage.Description; diff --git a/DesignPatternsDotNetCore.sln b/DesignPatternsDotNetCore.sln index 5f9a763..3a7677a 100644 --- a/DesignPatternsDotNetCore.sln +++ b/DesignPatternsDotNetCore.sln @@ -1,6 +1,6 @@ Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.29920.165 +# Visual Studio Version 17 +VisualStudioVersion = 17.3.32825.248 MinimumVisualStudioVersion = 15.0.26124.0 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdapterPattern", "AdapterPattern\AdapterPattern.csproj", "{79553F75-E8DC-4988-B511-A79CC6A9CDF7}" EndProject @@ -38,7 +38,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatorPattern", "Mediator EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuilderPattern", "BuilderPattern\BuilderPattern.csproj", "{274786D8-2E30-40D7-81B5-DFA3872CF9B6}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SingletonPattern.Tests", "SingletonPattern.Tests\SingletonPattern.Tests.csproj", "{7A21A074-AEBB-4B33-80BA-D7DFBE87A449}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -278,6 +280,18 @@ Global {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x64.Build.0 = Release|Any CPU {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x86.ActiveCfg = Release|Any CPU {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x86.Build.0 = Release|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|x64.ActiveCfg = Debug|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|x64.Build.0 = Debug|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|x86.ActiveCfg = Debug|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|x86.Build.0 = Debug|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|Any CPU.Build.0 = Release|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|x64.ActiveCfg = Release|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|x64.Build.0 = Release|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|x86.ActiveCfg = Release|Any CPU + {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FacadePattern/Dimmer.cs b/FacadePattern/Dimmer.cs index d9cfef3..11c35df 100644 --- a/FacadePattern/Dimmer.cs +++ b/FacadePattern/Dimmer.cs @@ -1,8 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace FacadePattern { diff --git a/FacadePattern/DvdPlayer.cs b/FacadePattern/DvdPlayer.cs index ffaa58c..f442655 100644 --- a/FacadePattern/DvdPlayer.cs +++ b/FacadePattern/DvdPlayer.cs @@ -12,14 +12,14 @@ public void Insert(Dvd dvd) { _dvd = dvd; Console.WriteLine($"Inserting {dvd.Movie}"); - + } public void Play() => Console.WriteLine($"Playing {_dvd.Movie}"); public void Pause() { - Console.WriteLine($"Pausing at {_time = (new Random()).Next(_time,_time + 120)}"); + Console.WriteLine($"Pausing at {_time = (new Random()).Next(_time, _time + 120)}"); } public void Resume() diff --git a/FacadePattern/FacadePattern.csproj b/FacadePattern/FacadePattern.csproj index ce1697a..74abf5c 100644 --- a/FacadePattern/FacadePattern.csproj +++ b/FacadePattern/FacadePattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/FacadePattern/HometheaterFacade.cs b/FacadePattern/HometheaterFacade.cs index 2d09049..703828e 100644 --- a/FacadePattern/HometheaterFacade.cs +++ b/FacadePattern/HometheaterFacade.cs @@ -5,8 +5,8 @@ public class HomeTheatreFacade private Dimmer _dimmer; private Dvd _dvd; private DvdPlayer _dvdPlayer; - - public HomeTheatreFacade(Dimmer dimmer,Dvd dvd, DvdPlayer dvdPlayer) + + public HomeTheatreFacade(Dimmer dimmer, Dvd dvd, DvdPlayer dvdPlayer) { _dvd = dvd; _dimmer = dimmer; diff --git a/FacadePattern/Program.cs b/FacadePattern/Program.cs index a695029..074539c 100644 --- a/FacadePattern/Program.cs +++ b/FacadePattern/Program.cs @@ -9,7 +9,7 @@ private static void Main() var dimmer = new Dimmer(); var dvdPlayer = new DvdPlayer(); var dvd = new Dvd("Gone with the Wind 2 : Electric Bugaloo"); - var homeTheater = new HomeTheatreFacade(dimmer,dvd,dvdPlayer); + var homeTheater = new HomeTheatreFacade(dimmer, dvd, dvdPlayer); homeTheater.WatchMovie(); Console.WriteLine(); diff --git a/FactoryPattern/Abstract Factory/Ingredients/Intefaces/ICheese.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ICheese.cs similarity index 100% rename from FactoryPattern/Abstract Factory/Ingredients/Intefaces/ICheese.cs rename to FactoryPattern/Abstract Factory/Ingredients/Interfaces/ICheese.cs diff --git a/FactoryPattern/Abstract Factory/Ingredients/Intefaces/IClam.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IClam.cs similarity index 100% rename from FactoryPattern/Abstract Factory/Ingredients/Intefaces/IClam.cs rename to FactoryPattern/Abstract Factory/Ingredients/Interfaces/IClam.cs diff --git a/FactoryPattern/Abstract Factory/Ingredients/Intefaces/IDough.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IDough.cs similarity index 100% rename from FactoryPattern/Abstract Factory/Ingredients/Intefaces/IDough.cs rename to FactoryPattern/Abstract Factory/Ingredients/Interfaces/IDough.cs diff --git a/FactoryPattern/Abstract Factory/Ingredients/Intefaces/ISauce.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ISauce.cs similarity index 100% rename from FactoryPattern/Abstract Factory/Ingredients/Intefaces/ISauce.cs rename to FactoryPattern/Abstract Factory/Ingredients/Interfaces/ISauce.cs diff --git a/FactoryPattern/Abstract Factory/Ingredients/Intefaces/IVeggies.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IVeggies.cs similarity index 100% rename from FactoryPattern/Abstract Factory/Ingredients/Intefaces/IVeggies.cs rename to FactoryPattern/Abstract Factory/Ingredients/Interfaces/IVeggies.cs diff --git a/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs b/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs index 63df3a3..234bdb0 100644 --- a/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs +++ b/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs @@ -2,25 +2,29 @@ { class ChicagoPizzaFactory : PizzaFactory { - protected override Pizza Create(string type) + private readonly string chicagoCheese = "Chicago Cheese"; + private readonly string chicagoClam = "Chicago Clam"; + private readonly string chicagoVeggie = "Chicago Veggie"; + + protected override Pizza Create(PizzaType type) { Pizza pizza; IIngredientsFactory ingredients = new ChicagoIngredientsFactory(); - - if (type.Equals("Cheese")) + + if (type == PizzaType.Cheese) { pizza = new CheesePizza(ingredients); - pizza.Name = "Chicago Cheese"; + pizza.Name = chicagoCheese; } - else if (type.Equals("Clam")) + else if (type == PizzaType.Clam) { pizza = new ClamPizza(ingredients); - pizza.Name = "Chicago Clam"; + pizza.Name = chicagoClam; } else { pizza = new VeggiePizza(ingredients); - pizza.Name = "Chicago Veggie"; + pizza.Name = chicagoVeggie; } pizza.Color = "red"; return pizza; diff --git a/FactoryPattern/Factory Method/NYPizzaFactory.cs b/FactoryPattern/Factory Method/NYPizzaFactory.cs index d1a2776..de2d0b7 100644 --- a/FactoryPattern/Factory Method/NYPizzaFactory.cs +++ b/FactoryPattern/Factory Method/NYPizzaFactory.cs @@ -2,22 +2,26 @@ { class NyPizzaFactory : PizzaFactory { - protected override Pizza Create(string type) + private readonly string nyStyleCheese = "NY Style Cheese"; + private readonly string nyStyleClam = "NY Style Clam"; + private readonly string nyStyleVeggie = "NY Style Veggie"; + + protected override Pizza Create(PizzaType type) { Pizza pizza; IIngredientsFactory ingredients = new NyIngredientsFactory(); - if (type.Equals("Cheese")) + if (type == PizzaType.Cheese) { - pizza = new CheesePizza(ingredients) {Name = "NY Style Cheese"}; + pizza = new CheesePizza(ingredients) { Name = nyStyleCheese }; } - else if (type.Equals("Clam")) + else if (type == PizzaType.Clam) { - pizza = new ClamPizza(ingredients) {Name = "NY Style Clam"}; + pizza = new ClamPizza(ingredients) { Name = nyStyleClam }; } else { - pizza = new VeggiePizza(ingredients) {Name = "NY Style Veggie"}; + pizza = new VeggiePizza(ingredients) { Name = nyStyleVeggie }; } pizza.Color = "blue"; return pizza; diff --git a/FactoryPattern/Factory Method/PizzaFactory.cs b/FactoryPattern/Factory Method/PizzaFactory.cs index 31ead68..f0b7a38 100644 --- a/FactoryPattern/Factory Method/PizzaFactory.cs +++ b/FactoryPattern/Factory Method/PizzaFactory.cs @@ -2,7 +2,7 @@ { abstract class PizzaFactory { - public Pizza Order(string type) + public Pizza Order(PizzaType type) { var pizza = Create(type); pizza.Prepare(); @@ -12,6 +12,6 @@ public Pizza Order(string type) return pizza; } - protected abstract Pizza Create(string type); + protected abstract Pizza Create(PizzaType type); } } diff --git a/FactoryPattern/FactoryPattern.csproj b/FactoryPattern/FactoryPattern.csproj index 8ffe015..74abf5c 100644 --- a/FactoryPattern/FactoryPattern.csproj +++ b/FactoryPattern/FactoryPattern.csproj @@ -2,11 +2,9 @@ Exe - netcoreapp2.1 + net6.0 + enable + enable - - - - diff --git a/FactoryPattern/Helper.cs b/FactoryPattern/Helper.cs new file mode 100644 index 0000000..4a897e8 --- /dev/null +++ b/FactoryPattern/Helper.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FactoryPattern +{ + internal class Helper + { + } + + internal enum PizzaType + { + Cheese, + Clam + } +} diff --git a/FactoryPattern/Pizza/CheesePizza.cs b/FactoryPattern/Pizza/CheesePizza.cs index 60dac3c..c7e26b5 100644 --- a/FactoryPattern/Pizza/CheesePizza.cs +++ b/FactoryPattern/Pizza/CheesePizza.cs @@ -12,7 +12,7 @@ public CheesePizza(IIngredientsFactory ing) } internal override void Prepare() { - Console.WriteLine("Preparing " + Name + " Using"); + Console.WriteLine("Preparing " + Name + " Using"); Console.Write("Dough: " + _ingredients.CreateDough().Name + ", Cheese: " + _ingredients.CreateCheese().Name + ", Sauce: " + _ingredients.CreateSauce().Name + ", Veggies: "); Console.WriteLine(); foreach (var val in _ingredients.CreateVeggies()) diff --git a/FactoryPattern/Program.cs b/FactoryPattern/Program.cs index d16bc6f..0612463 100644 --- a/FactoryPattern/Program.cs +++ b/FactoryPattern/Program.cs @@ -8,11 +8,11 @@ static void Main() { Console.WriteLine("Yankees fan orders:"); var yankees = new NyPizzaFactory(); - yankees.Order("Cheese"); + yankees.Order(PizzaType.Cheese); Console.WriteLine(); Console.WriteLine("Cubs fan orders:"); var cubs = new ChicagoPizzaFactory(); - cubs.Order("Clam"); + cubs.Order(PizzaType.Clam); } } } \ No newline at end of file diff --git a/FlyweightPattern/BubbleMilkTea.cs b/FlyweightPattern/BubbleMilkTea.cs index 476cd3e..c58ab9b 100644 --- a/FlyweightPattern/BubbleMilkTea.cs +++ b/FlyweightPattern/BubbleMilkTea.cs @@ -2,7 +2,7 @@ namespace FlyweightPattern { - public class BubbleMilkTea: IBeverage + public class BubbleMilkTea : IBeverage { public BubbleMilkTea() { diff --git a/FlyweightPattern/BubbleTeaShop.cs b/FlyweightPattern/BubbleTeaShop.cs index 4b30646..e6bcbae 100644 --- a/FlyweightPattern/BubbleTeaShop.cs +++ b/FlyweightPattern/BubbleTeaShop.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Reflection.Metadata.Ecma335; namespace FlyweightPattern { @@ -17,7 +16,7 @@ public BubbleTeaShop() private void TakeOrders() { var factory = new BeverageFlyweightFactory(); - + takeAwayOrders.Add(factory.MakeBeverage(BeverageType.BubbleMilk)); takeAwayOrders.Add(factory.MakeBeverage(BeverageType.BubbleMilk)); takeAwayOrders.Add(factory.MakeBeverage(BeverageType.CoconutMilk)); diff --git a/FlyweightPattern/CoconutMilkTea.cs b/FlyweightPattern/CoconutMilkTea.cs index b5c89b2..f96035b 100644 --- a/FlyweightPattern/CoconutMilkTea.cs +++ b/FlyweightPattern/CoconutMilkTea.cs @@ -2,13 +2,13 @@ namespace FlyweightPattern { - public class CoconutMilkTea: IBeverage + public class CoconutMilkTea : IBeverage { public CoconutMilkTea() { Console.WriteLine("Initializing a Coconut Milk Tea instance"); } - + public void Drink() { Console.WriteLine("hmmm... this is coconut milk tea"); diff --git a/FlyweightPattern/FlyweightPattern.csproj b/FlyweightPattern/FlyweightPattern.csproj index 51b7f6c..74abf5c 100644 --- a/FlyweightPattern/FlyweightPattern.csproj +++ b/FlyweightPattern/FlyweightPattern.csproj @@ -1,8 +1,10 @@ - - Exe - netcoreapp2.1 - + + Exe + net6.0 + enable + enable + diff --git a/FlyweightPattern/FoamMilkTea.cs b/FlyweightPattern/FoamMilkTea.cs index eb5c31a..958b9d7 100644 --- a/FlyweightPattern/FoamMilkTea.cs +++ b/FlyweightPattern/FoamMilkTea.cs @@ -2,14 +2,14 @@ namespace FlyweightPattern { - public class FoamMilkTea:IBeverage + public class FoamMilkTea : IBeverage { - + public FoamMilkTea() { Console.WriteLine("Initializing a Foam Milk Tea instance"); } - + public void Drink() { Console.WriteLine("hmmm... this is foam milk tea"); diff --git a/FlyweightPattern/OolingMilkTea.cs b/FlyweightPattern/OolingMilkTea.cs index 432be67..40a3715 100644 --- a/FlyweightPattern/OolingMilkTea.cs +++ b/FlyweightPattern/OolingMilkTea.cs @@ -2,7 +2,7 @@ namespace FlyweightPattern { - public class OolingMilkTea: IBeverage + public class OolingMilkTea : IBeverage { public OolingMilkTea() diff --git a/FlyweightPattern/Program.cs b/FlyweightPattern/Program.cs index 91d6ab2..462bab7 100644 --- a/FlyweightPattern/Program.cs +++ b/FlyweightPattern/Program.cs @@ -1,6 +1,4 @@ -using System; - -namespace FlyweightPattern +namespace FlyweightPattern { static class Program { diff --git a/FlyweightPattern/ProxyPattern/Dimmer.cs b/FlyweightPattern/ProxyPattern/Dimmer.cs deleted file mode 100644 index d9cfef3..0000000 --- a/FlyweightPattern/ProxyPattern/Dimmer.cs +++ /dev/null @@ -1,18 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FacadePattern -{ - public class Dimmer - { - internal void Dim(int val) - { - Console.WriteLine(val == 10 ? "Turning Lights On" : $"Dimming lights to {val}"); - } - - internal void Off() => Console.WriteLine("Switching off lights"); - } -} diff --git a/FlyweightPattern/ProxyPattern/Dvd.cs b/FlyweightPattern/ProxyPattern/Dvd.cs deleted file mode 100644 index b247c62..0000000 --- a/FlyweightPattern/ProxyPattern/Dvd.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace FacadePattern -{ - public class Dvd - { - public Dvd(string name) - { - Movie = name; - } - public string Movie { get; set; } - } -} \ No newline at end of file diff --git a/FlyweightPattern/ProxyPattern/DvdPlayer.cs b/FlyweightPattern/ProxyPattern/DvdPlayer.cs deleted file mode 100644 index ffaa58c..0000000 --- a/FlyweightPattern/ProxyPattern/DvdPlayer.cs +++ /dev/null @@ -1,30 +0,0 @@ -using System; - -namespace FacadePattern -{ - public class DvdPlayer - { - private Dvd _dvd; - private int _time = 0; - public void On() => Console.WriteLine("DVD Player powered on"); - - public void Insert(Dvd dvd) - { - _dvd = dvd; - Console.WriteLine($"Inserting {dvd.Movie}"); - - } - - public void Play() => Console.WriteLine($"Playing {_dvd.Movie}"); - - public void Pause() - { - Console.WriteLine($"Pausing at {_time = (new Random()).Next(_time,_time + 120)}"); - } - - public void Resume() - { - Console.WriteLine($"Resuming from {_time}"); - } - } -} \ No newline at end of file diff --git a/FlyweightPattern/ProxyPattern/HometheaterFacade.cs b/FlyweightPattern/ProxyPattern/HometheaterFacade.cs deleted file mode 100644 index 2d09049..0000000 --- a/FlyweightPattern/ProxyPattern/HometheaterFacade.cs +++ /dev/null @@ -1,36 +0,0 @@ -namespace FacadePattern -{ - public class HomeTheatreFacade - { - private Dimmer _dimmer; - private Dvd _dvd; - private DvdPlayer _dvdPlayer; - - public HomeTheatreFacade(Dimmer dimmer,Dvd dvd, DvdPlayer dvdPlayer) - { - _dvd = dvd; - _dimmer = dimmer; - _dvdPlayer = dvdPlayer; - } - - public void WatchMovie() - { - _dimmer.Dim(5); - _dvdPlayer.On(); - _dvdPlayer.Insert(_dvd); - _dvdPlayer.Play(); - } - - public void Pause() - { - _dimmer.Dim(10); - _dvdPlayer.Pause(); - } - - public void Resume() - { - _dimmer.Dim(5); - _dvdPlayer.Resume(); - } - } -} \ No newline at end of file diff --git a/FlyweightPattern/ProxyPattern/Program.cs b/FlyweightPattern/ProxyPattern/Program.cs deleted file mode 100644 index a695029..0000000 --- a/FlyweightPattern/ProxyPattern/Program.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; - -namespace FacadePattern -{ - internal static class Program - { - private static void Main() - { - var dimmer = new Dimmer(); - var dvdPlayer = new DvdPlayer(); - var dvd = new Dvd("Gone with the Wind 2 : Electric Bugaloo"); - var homeTheater = new HomeTheatreFacade(dimmer,dvd,dvdPlayer); - - homeTheater.WatchMovie(); - Console.WriteLine(); - homeTheater.Pause(); - Console.WriteLine(); - homeTheater.Resume(); - Console.WriteLine(); - homeTheater.Pause(); - } - } -} diff --git a/FlyweightPattern/ProxyPattern/ProxyPattern.csproj b/FlyweightPattern/ProxyPattern/ProxyPattern.csproj deleted file mode 100644 index ce1697a..0000000 --- a/FlyweightPattern/ProxyPattern/ProxyPattern.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - - Exe - netcoreapp2.0 - - - diff --git a/IteratorPattern/BreakfastMenu.cs b/IteratorPattern/BreakfastMenu.cs index c9ad3d8..f4b5073 100644 --- a/IteratorPattern/BreakfastMenu.cs +++ b/IteratorPattern/BreakfastMenu.cs @@ -20,16 +20,16 @@ public BreakfastMenu() AddItem("Waffle", "Blueberry Sauce topped breakfast Waffles", 125, false); AddItem("Sandwich", "Veggie Sandwich with tomato and cucumber", 75, true); - AddItem("Pankcakes", "Maple syrup Pancakes",110,false); - AddItem("Corn Flakes", "Cornflakes with fruits and nuts",60,true); + AddItem("Pankcakes", "Maple syrup Pancakes", 110, false); + AddItem("Corn Flakes", "Cornflakes with fruits and nuts", 60, true); } private void AddItem(string name, string description, int price, bool veg) { - var item = new Menu(name,description,price,veg); + var item = new Menu(name, description, price, veg); _items.Add(item); } - + } } \ No newline at end of file diff --git a/IteratorPattern/BreakfastMenuEnum.cs b/IteratorPattern/BreakfastMenuEnum.cs index d661b02..683cd3c 100644 --- a/IteratorPattern/BreakfastMenuEnum.cs +++ b/IteratorPattern/BreakfastMenuEnum.cs @@ -37,7 +37,7 @@ public Menu Current { try { - return (Menu) _items[_position]; + return (Menu)_items[_position]; } catch (IndexOutOfRangeException) { diff --git a/IteratorPattern/BreakfastMenuIterator.cs b/IteratorPattern/BreakfastMenuIterator.cs index 9c25928..68bd86e 100644 --- a/IteratorPattern/BreakfastMenuIterator.cs +++ b/IteratorPattern/BreakfastMenuIterator.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections; -using System.Collections.Generic; +using System.Collections; namespace IteratorPattern { diff --git a/IteratorPattern/Client.cs b/IteratorPattern/Client.cs index 21f2db8..27a1a5b 100644 --- a/IteratorPattern/Client.cs +++ b/IteratorPattern/Client.cs @@ -26,7 +26,7 @@ private void PrintMenu(IEnumerable iter) { foreach (var item in iter) { - var i = (Menu) item; + var i = (Menu)item; Console.WriteLine($"{i.Name} Rs. {i.Price} { (i.Vegetarian ? "*" : "x") } \n {i.Description} "); } diff --git a/IteratorPattern/DinnerMenu.cs b/IteratorPattern/DinnerMenu.cs index cd0c7a6..877fcc7 100644 --- a/IteratorPattern/DinnerMenu.cs +++ b/IteratorPattern/DinnerMenu.cs @@ -28,7 +28,7 @@ public DinnerMenu() private void AddItems(string name, string description, int price, bool veg) { - var item = new Menu(name,description,price,veg); + var item = new Menu(name, description, price, veg); if (_count <= Max) { diff --git a/IteratorPattern/DinnerMenuEnum.cs b/IteratorPattern/DinnerMenuEnum.cs index a567833..68929eb 100644 --- a/IteratorPattern/DinnerMenuEnum.cs +++ b/IteratorPattern/DinnerMenuEnum.cs @@ -1,6 +1,5 @@ using System; using System.Collections; -using System.Collections.Generic; namespace IteratorPattern { diff --git a/IteratorPattern/DinnerMenuIterator.cs b/IteratorPattern/DinnerMenuIterator.cs index a5fd659..c4dca48 100644 --- a/IteratorPattern/DinnerMenuIterator.cs +++ b/IteratorPattern/DinnerMenuIterator.cs @@ -1,5 +1,4 @@ using System.Collections; -using System.Collections.Generic; namespace IteratorPattern { diff --git a/IteratorPattern/IteratorPattern.csproj b/IteratorPattern/IteratorPattern.csproj index ce1697a..74abf5c 100644 --- a/IteratorPattern/IteratorPattern.csproj +++ b/IteratorPattern/IteratorPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/IteratorPattern/Menu.cs b/IteratorPattern/Menu.cs index 355ed4d..984f85f 100644 --- a/IteratorPattern/Menu.cs +++ b/IteratorPattern/Menu.cs @@ -14,6 +14,6 @@ public Menu(string name, string description, double price, bool vegetarian) Price = price; Vegetarian = vegetarian; } - + } } \ No newline at end of file diff --git a/IteratorPattern/Program.cs b/IteratorPattern/Program.cs index 6f5712d..dae6a70 100644 --- a/IteratorPattern/Program.cs +++ b/IteratorPattern/Program.cs @@ -6,8 +6,8 @@ private static void Main() { var breakfast = new BreakfastMenu(); var dinner = new DinnerMenu(); - var waiter = new Client(breakfast,dinner); + var waiter = new Client(breakfast, dinner); waiter.PrintMenu(); } - } + } } diff --git a/MediatorPattern/Customer.cs b/MediatorPattern/Customer.cs index 774863c..b3bf289 100644 --- a/MediatorPattern/Customer.cs +++ b/MediatorPattern/Customer.cs @@ -4,7 +4,7 @@ namespace MediatorPattern { class Customer : Colleague { - public Customer(Mediator mediator) : base(mediator) {} + public Customer(Mediator mediator) : base(mediator) { } public override void Notify(string message) { diff --git a/MediatorPattern/MediatorPattern.csproj b/MediatorPattern/MediatorPattern.csproj index 51b7f6c..74abf5c 100644 --- a/MediatorPattern/MediatorPattern.csproj +++ b/MediatorPattern/MediatorPattern.csproj @@ -1,8 +1,10 @@ - - Exe - netcoreapp2.1 - + + Exe + net6.0 + enable + enable + diff --git a/MediatorPattern/Program.cs b/MediatorPattern/Program.cs index 9276771..bbdddc5 100644 --- a/MediatorPattern/Program.cs +++ b/MediatorPattern/Program.cs @@ -1,6 +1,4 @@ -using System; - -namespace MediatorPattern +namespace MediatorPattern { class Program { diff --git a/MediatorPattern/Programmer.cs b/MediatorPattern/Programmer.cs index 275214e..cef0b16 100644 --- a/MediatorPattern/Programmer.cs +++ b/MediatorPattern/Programmer.cs @@ -4,7 +4,7 @@ namespace MediatorPattern { class Programmer : Colleague { - public Programmer(Mediator mediator) : base(mediator) {} + public Programmer(Mediator mediator) : base(mediator) { } public override void Notify(string message) { diff --git a/MediatorPattern/Tester.cs b/MediatorPattern/Tester.cs index e218496..0fe71d7 100644 --- a/MediatorPattern/Tester.cs +++ b/MediatorPattern/Tester.cs @@ -4,7 +4,7 @@ namespace MediatorPattern { class Tester : Colleague { - public Tester(Mediator mediator) : base(mediator) {} + public Tester(Mediator mediator) : base(mediator) { } public override void Notify(string message) { diff --git a/ObserverPattern/ObserverPattern.csproj b/ObserverPattern/ObserverPattern.csproj index ce1697a..74abf5c 100644 --- a/ObserverPattern/ObserverPattern.csproj +++ b/ObserverPattern/ObserverPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/ObserverPattern/WeatherMonitor.cs b/ObserverPattern/WeatherMonitor.cs index 5cf8942..6d65f37 100644 --- a/ObserverPattern/WeatherMonitor.cs +++ b/ObserverPattern/WeatherMonitor.cs @@ -21,7 +21,7 @@ public WeatherMonitor(string name) { _name = name; } - + public void OnCompleted() { throw new NotImplementedException(); @@ -39,7 +39,7 @@ public void OnNext(Weather value) { string op = $"| Temperature : {value.Temperature} Celsius |"; Console.Write(op); - + } if (_name.Contains("P")) { @@ -48,7 +48,7 @@ public void OnNext(Weather value) } if (_name.Contains("H")) { - string op = $"| Humidity : {value.Humidity*100} % |"; + string op = $"| Humidity : {value.Humidity * 100} % |"; Console.Write(op); } if (!(_name.Contains("T") || _name.Contains("P") || _name.Contains("H"))) diff --git a/PrototypePattern/PrototypePattern.csproj b/PrototypePattern/PrototypePattern.csproj index 23df604..74abf5c 100644 --- a/PrototypePattern/PrototypePattern.csproj +++ b/PrototypePattern/PrototypePattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.1 + net6.0 + enable + enable diff --git a/ProxyPattern/Image.cs b/ProxyPattern/Image.cs index 9c1d7b0..5041b84 100644 --- a/ProxyPattern/Image.cs +++ b/ProxyPattern/Image.cs @@ -3,6 +3,6 @@ public interface Image { void display(); - + } } diff --git a/ProxyPattern/ProxyPattern.csproj b/ProxyPattern/ProxyPattern.csproj index c73e0d1..74abf5c 100644 --- a/ProxyPattern/ProxyPattern.csproj +++ b/ProxyPattern/ProxyPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp3.1 + net6.0 + enable + enable diff --git a/ProxyPattern/RealImage.cs b/ProxyPattern/RealImage.cs index 0405fad..d0826fd 100644 --- a/ProxyPattern/RealImage.cs +++ b/ProxyPattern/RealImage.cs @@ -6,22 +6,22 @@ namespace ProxyPattern public class RealImage : Image { - private string _fileName; + private string _fileName; - public RealImage(string fileName) - { - _fileName = fileName; - loadFromDisk(_fileName); - } + public RealImage(string fileName) + { + _fileName = fileName; + loadFromDisk(_fileName); + } - public void display() - { - Console.WriteLine("Displaying " + _fileName); - } + public void display() + { + Console.WriteLine("Displaying " + _fileName); + } - private void loadFromDisk(string fileName) - { + private void loadFromDisk(string fileName) + { Console.WriteLine("Loading " + fileName); + } } -} } \ No newline at end of file diff --git a/README.md b/README.md index 238a5db..f82eb91 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,15 @@ -# DesignPatterns -Design patterns are solutions to recurring problems; guidelines on how to tackle certain problems +# Design Patterns +Design patterns are solutions to recurring problems; guidelines on how to tackle certain problems. I have included implementations of some design patterns in C# to help beginners like me get their feet wet. -There are better alternatives available for some of them in the .NET Framework, so this is by no means a comprehensive tutorial +There are better alternatives available for some of them in the .NET Framework, so this is by no means a comprehensive tutorial. -Any comments and suggestions are welcome. If you want to add a new design pattern implementation, just follow the naming conversation, fork my repo and submit a pull request. Same goes for any improvements and modifications. +Any comments and suggestions are welcome. If you want to add a new design pattern implementation, just follow the naming convention, fork my repo and submit a pull request. Same goes for any improvements and modifications. + +This was created as a C# alternative to Java while reading https://www.oreilly.com/library/view/head-first-design/0596007124/ <- take a look at it if anything confuses you. ## Types of Design Patterns --------------------------- -There are three kinds of Design Patterns +There are three kinds of Design Patterns: * Creational * Structural @@ -18,6 +20,8 @@ There are three kinds of Design Patterns * [Adapter](/AdapterPattern) * [Bridge](/BridgePattern) +* [Builder](/BuilderPattern) +* [ChainOfResponsibility](/ChainOfResponsibilityPattern) * [Command](/CommandPattern) * [Composite](/CompositePattern) * [Decorator](/DecoratorPattern) @@ -25,11 +29,13 @@ There are three kinds of Design Patterns * [Factory](/FactoryPattern) * [Flyweight](/FlyweightPattern) * [Iterator](/IteratorPattern) +* [Mediator](/MediatorPattern) * [Observer](/ObserverPattern) +* [Prototype](/PrototypePattern) +* [Proxy](/ProxyPattern) * [Singleton](/SingletonPattern) +* [SingletonPattern.Tests](/SingletonPattern.Tests) * [State](/StatePattern) * [Strategy](/StrategyPattern) * [Template](/TemplatePattern) * [Visitor](/VisitorPattern) -* [Mediator](/MediatorPattern) -* [Proxy] (/ProxyPattern) \ No newline at end of file diff --git a/SingletonPattern.Tests/SingletonPattern.Tests.csproj b/SingletonPattern.Tests/SingletonPattern.Tests.csproj new file mode 100644 index 0000000..ec8a874 --- /dev/null +++ b/SingletonPattern.Tests/SingletonPattern.Tests.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/SingletonPattern.Tests/SingletonPatternTests.cs b/SingletonPattern.Tests/SingletonPatternTests.cs new file mode 100644 index 0000000..f015dfc --- /dev/null +++ b/SingletonPattern.Tests/SingletonPatternTests.cs @@ -0,0 +1,17 @@ +namespace SingletonPattern.Tests +{ + [TestFixture] + public class SingletonPatternTests + { + [Test] + public void GetInstance_CreateInstanceTwice_AreEqual() + { + // Arrange, Act + var firstAttemptInstance = ChocolateBoiler.GetInstance(); + var secondAttemptInstance = ChocolateBoiler.GetInstance(); + + // Assert + Assert.That(firstAttemptInstance, Is.EqualTo(secondAttemptInstance)); + } + } +} diff --git a/SingletonPattern.Tests/Usings.cs b/SingletonPattern.Tests/Usings.cs new file mode 100644 index 0000000..3244567 --- /dev/null +++ b/SingletonPattern.Tests/Usings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; diff --git a/SingletonPattern/ChocolateBoiler.cs b/SingletonPattern/ChocolateBoiler.cs index 35d3e15..bdc6a2d 100644 --- a/SingletonPattern/ChocolateBoiler.cs +++ b/SingletonPattern/ChocolateBoiler.cs @@ -5,9 +5,9 @@ namespace SingletonPattern internal partial class ChocolateBoiler { private static readonly Lazy _singleton = new Lazy(() => new ChocolateBoiler()); - + public static ChocolateBoiler GetInstance() => _singleton.Value; - + private Status _boiler; private ChocolateBoiler() diff --git a/SingletonPattern/Program.cs b/SingletonPattern/Program.cs index 0609ca0..7d81245 100644 --- a/SingletonPattern/Program.cs +++ b/SingletonPattern/Program.cs @@ -11,7 +11,7 @@ static void Main() var chocoEggs = ChocolateBoiler.GetInstance(); chocoEggs.Fill(); chocoEggs.Boil(); - chocoEggs.Drain(); + chocoEggs.Drain(); } catch (Exception) { diff --git a/SingletonPattern/Properties/AssemblyInfo.cs b/SingletonPattern/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..f6a8309 --- /dev/null +++ b/SingletonPattern/Properties/AssemblyInfo.cs @@ -0,0 +1,3 @@ +using System.Runtime.CompilerServices; + +[assembly:InternalsVisibleTo("SingletonPattern.Tests")] diff --git a/SingletonPattern/SingletonPattern.csproj b/SingletonPattern/SingletonPattern.csproj index ce1697a..74abf5c 100644 --- a/SingletonPattern/SingletonPattern.csproj +++ b/SingletonPattern/SingletonPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/StatePattern/HasQuarterState.cs b/StatePattern/HasQuarterState.cs index f5e4d98..2dc106d 100644 --- a/StatePattern/HasQuarterState.cs +++ b/StatePattern/HasQuarterState.cs @@ -33,7 +33,7 @@ public void TurnCrank() { Machine.State = Machine.SoldState; } - } + } public void Dispense() { diff --git a/StatePattern/NoQuarterState.cs b/StatePattern/NoQuarterState.cs index 5f2d670..978114d 100644 --- a/StatePattern/NoQuarterState.cs +++ b/StatePattern/NoQuarterState.cs @@ -23,7 +23,7 @@ public void EjectQuarter() public void TurnCrank() { - Console.WriteLine("Can't turn crank without a quarter"); + Console.WriteLine("Can't turn crank without a quarter"); } public void Dispense() diff --git a/StatePattern/SoldState.cs b/StatePattern/SoldState.cs index 0c2d4f4..e8ecb33 100644 --- a/StatePattern/SoldState.cs +++ b/StatePattern/SoldState.cs @@ -18,7 +18,7 @@ public void InsertQuarter() public void EjectQuarter() { - Console.WriteLine("Can't eject, already turned the crank"); + Console.WriteLine("Can't eject, already turned the crank"); } public void TurnCrank() diff --git a/StatePattern/StatePattern.csproj b/StatePattern/StatePattern.csproj index ce1697a..74abf5c 100644 --- a/StatePattern/StatePattern.csproj +++ b/StatePattern/StatePattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/StrategyPattern/Program.cs b/StrategyPattern/Program.cs index 85f9495..934724b 100644 --- a/StrategyPattern/Program.cs +++ b/StrategyPattern/Program.cs @@ -6,7 +6,8 @@ internal class Duck private IFlyBehaviour _flyer; - public IQuackBehaviour Quacker { + public IQuackBehaviour Quacker + { set { _quacker = value; @@ -52,7 +53,7 @@ internal static class Program { private static void Main() { - var mallard = new MallardDuck {Quacker = new QuackNormal()}; + var mallard = new MallardDuck { Quacker = new QuackNormal() }; mallard.Display(); mallard.Flyer = new FlyWings(); mallard.Display(); diff --git a/StrategyPattern/QuackNormal.cs b/StrategyPattern/QuackNormal.cs index aaa0b1f..0dc0053 100644 --- a/StrategyPattern/QuackNormal.cs +++ b/StrategyPattern/QuackNormal.cs @@ -7,6 +7,6 @@ class QuackNormal : IQuackBehaviour public void Quack() { Console.WriteLine("Quack Quack"); - } + } } } diff --git a/StrategyPattern/StrategyPattern.csproj b/StrategyPattern/StrategyPattern.csproj index ce1697a..74abf5c 100644 --- a/StrategyPattern/StrategyPattern.csproj +++ b/StrategyPattern/StrategyPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/TemplatePattern/Program.cs b/TemplatePattern/Program.cs index cc0c9b3..eb0abae 100644 --- a/TemplatePattern/Program.cs +++ b/TemplatePattern/Program.cs @@ -13,7 +13,7 @@ static void Main() tea.WantsCondiments = true; tea.AddSugar = 5; tea.Prepare(); - + Console.WriteLine(); coffee.WantsCondiments = true; coffee.Prepare(); diff --git a/TemplatePattern/TemplatePattern.csproj b/TemplatePattern/TemplatePattern.csproj index ce1697a..74abf5c 100644 --- a/TemplatePattern/TemplatePattern.csproj +++ b/TemplatePattern/TemplatePattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.0 + net6.0 + enable + enable diff --git a/VisitorPattern/Apartment.cs b/VisitorPattern/Apartment.cs index bbf18df..63469c7 100644 --- a/VisitorPattern/Apartment.cs +++ b/VisitorPattern/Apartment.cs @@ -1,6 +1,6 @@ namespace VisitorPattern { - public class Apartment: Unit + public class Apartment : Unit { public Apartment(params Unit[] units) : base(units) { diff --git a/VisitorPattern/ApartmentVisitor.cs b/VisitorPattern/ApartmentVisitor.cs index 21e3d63..bdc6b81 100644 --- a/VisitorPattern/ApartmentVisitor.cs +++ b/VisitorPattern/ApartmentVisitor.cs @@ -2,7 +2,7 @@ namespace VisitorPattern { - public class ApartmentVisitor: IUnitVisitor + public class ApartmentVisitor : IUnitVisitor { public void VisitApartment(Apartment apartment) { diff --git a/VisitorPattern/Bedroom.cs b/VisitorPattern/Bedroom.cs index 3836d32..ee0d08e 100644 --- a/VisitorPattern/Bedroom.cs +++ b/VisitorPattern/Bedroom.cs @@ -1,6 +1,6 @@ namespace VisitorPattern { - public class Bedroom: Unit + public class Bedroom : Unit { public Bedroom(params Unit[] units) : base(units) { diff --git a/VisitorPattern/BedroomVisitor.cs b/VisitorPattern/BedroomVisitor.cs index 47cfa57..f4792ea 100644 --- a/VisitorPattern/BedroomVisitor.cs +++ b/VisitorPattern/BedroomVisitor.cs @@ -2,7 +2,7 @@ namespace VisitorPattern { - public class BedroomVisitor: IUnitVisitor + public class BedroomVisitor : IUnitVisitor { public void VisitApartment(Apartment apartment) { diff --git a/VisitorPattern/LivingRoom.cs b/VisitorPattern/LivingRoom.cs index 54699d3..31e7421 100644 --- a/VisitorPattern/LivingRoom.cs +++ b/VisitorPattern/LivingRoom.cs @@ -1,6 +1,6 @@ namespace VisitorPattern { - public class LivingRoom: Unit + public class LivingRoom : Unit { public LivingRoom(params Unit[] units) : base(units) { diff --git a/VisitorPattern/LivingRoomVisitor.cs b/VisitorPattern/LivingRoomVisitor.cs index a45ebb4..f6cf11a 100644 --- a/VisitorPattern/LivingRoomVisitor.cs +++ b/VisitorPattern/LivingRoomVisitor.cs @@ -2,7 +2,7 @@ namespace VisitorPattern { - public class LivingRoomVisitor: IUnitVisitor + public class LivingRoomVisitor : IUnitVisitor { public void VisitApartment(Apartment apartment) { diff --git a/VisitorPattern/Program.cs b/VisitorPattern/Program.cs index f328752..3d17f4d 100644 --- a/VisitorPattern/Program.cs +++ b/VisitorPattern/Program.cs @@ -12,7 +12,7 @@ static void Main() apartment.Accept(new ApartmentVisitor()); apartment.Accept(new LivingRoomVisitor()); apartment.Accept(new BedroomVisitor()); - + Console.WriteLine("Visiting a Studio"); studio.Accept(new StudioVisitor()); studio.Accept(new LivingRoomVisitor()); diff --git a/VisitorPattern/Studio.cs b/VisitorPattern/Studio.cs index 8bdc7d3..aa48f4e 100644 --- a/VisitorPattern/Studio.cs +++ b/VisitorPattern/Studio.cs @@ -1,6 +1,6 @@ namespace VisitorPattern { - public class Studio: Unit + public class Studio : Unit { public Studio(params Unit[] units) : base(units) { diff --git a/VisitorPattern/StudioVisitor.cs b/VisitorPattern/StudioVisitor.cs index 646af15..82c2521 100644 --- a/VisitorPattern/StudioVisitor.cs +++ b/VisitorPattern/StudioVisitor.cs @@ -2,7 +2,7 @@ namespace VisitorPattern { - public class StudioVisitor: IUnitVisitor + public class StudioVisitor : IUnitVisitor { public void VisitApartment(Apartment apartment) { diff --git a/VisitorPattern/VisitorPattern.csproj b/VisitorPattern/VisitorPattern.csproj index 51b7f6c..74abf5c 100644 --- a/VisitorPattern/VisitorPattern.csproj +++ b/VisitorPattern/VisitorPattern.csproj @@ -1,8 +1,10 @@ - - Exe - netcoreapp2.1 - + + Exe + net6.0 + enable + enable +