From dcd0bebfbaa69973d41a34943f32d657a3f28b8a Mon Sep 17 00:00:00 2001 From: Ashwin Chakravarthy K Date: Fri, 26 Aug 2022 16:04:04 +0530 Subject: [PATCH 1/6] net core to net 6 upgrade (#28) --- AdapterPattern/AdapterPattern.csproj | 4 +++- BridgePattern/BridgePattern.csproj | 10 ++++++---- BuilderPattern/BuilderPattern.csproj | 4 +++- .../ChainOfResponsibilityPattern.csproj | 4 +++- CommandPattern/CommandPattern.csproj | 4 +++- CompositePattern/CompositePattern.csproj | 4 +++- DecoratorPattern/DecoratorPattern.csproj | 4 +++- FacadePattern/FacadePattern.csproj | 4 +++- FactoryPattern/FactoryPattern.csproj | 4 +++- FlyweightPattern/FlyweightPattern.csproj | 10 ++++++---- IteratorPattern/IteratorPattern.csproj | 4 +++- MediatorPattern/MediatorPattern.csproj | 10 ++++++---- ObserverPattern/ObserverPattern.csproj | 4 +++- PrototypePattern/PrototypePattern.csproj | 4 +++- ProxyPattern/ProxyPattern.csproj | 4 +++- SingletonPattern/SingletonPattern.csproj | 4 +++- StatePattern/StatePattern.csproj | 4 +++- StrategyPattern/StrategyPattern.csproj | 4 +++- TemplatePattern/TemplatePattern.csproj | 4 +++- VisitorPattern/VisitorPattern.csproj | 10 ++++++---- 20 files changed, 72 insertions(+), 32 deletions(-) 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/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/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/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/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/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/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/FactoryPattern/FactoryPattern.csproj b/FactoryPattern/FactoryPattern.csproj index 23df604..74abf5c 100644 --- a/FactoryPattern/FactoryPattern.csproj +++ b/FactoryPattern/FactoryPattern.csproj @@ -2,7 +2,9 @@ Exe - netcoreapp2.1 + net6.0 + enable + enable 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/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/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/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/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/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/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/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/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/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/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 + From 463ef4ecb9f21d77c04d56693db6c938108d9b1d Mon Sep 17 00:00:00 2001 From: Volodymyr Tkachenko Date: Wed, 2 Nov 2022 19:40:39 +0200 Subject: [PATCH 2/6] Build fix: Improper Proxy pattern is removed from Flyghtweight pattern's project (#32) --- FlyweightPattern/ProxyPattern/Dimmer.cs | 14 -------- FlyweightPattern/ProxyPattern/Dvd.cs | 11 ------ FlyweightPattern/ProxyPattern/DvdPlayer.cs | 30 ---------------- .../ProxyPattern/HometheaterFacade.cs | 36 ------------------- FlyweightPattern/ProxyPattern/Program.cs | 23 ------------ .../ProxyPattern/ProxyPattern.csproj | 8 ----- 6 files changed, 122 deletions(-) delete mode 100644 FlyweightPattern/ProxyPattern/Dimmer.cs delete mode 100644 FlyweightPattern/ProxyPattern/Dvd.cs delete mode 100644 FlyweightPattern/ProxyPattern/DvdPlayer.cs delete mode 100644 FlyweightPattern/ProxyPattern/HometheaterFacade.cs delete mode 100644 FlyweightPattern/ProxyPattern/Program.cs delete mode 100644 FlyweightPattern/ProxyPattern/ProxyPattern.csproj diff --git a/FlyweightPattern/ProxyPattern/Dimmer.cs b/FlyweightPattern/ProxyPattern/Dimmer.cs deleted file mode 100644 index 11c35df..0000000 --- a/FlyweightPattern/ProxyPattern/Dimmer.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; - -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 f442655..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 703828e..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 074539c..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 - - - From ce571e3627ed26ed936aba88ab45c8bc590f3a59 Mon Sep 17 00:00:00 2001 From: Abishek Aditya Date: Wed, 2 Nov 2022 23:12:50 +0530 Subject: [PATCH 3/6] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d16ff9f..6039801 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ There are better alternatives available for some of them in the .NET Framework, 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: From 3574c8a52c6d1236710a08c6bdd9bfaf84e8e732 Mon Sep 17 00:00:00 2001 From: Ale-na98 <86318402+Ale-na98@users.noreply.github.com> Date: Wed, 2 Nov 2022 18:43:27 +0100 Subject: [PATCH 4/6] Add unit test for Singleton pattern (#29) --- DesignPatternsDotNetCore.sln | 20 +++++++++++++--- .../SingletonPattern.Tests.csproj | 23 +++++++++++++++++++ .../SingletonPatternTests.cs | 17 ++++++++++++++ SingletonPattern.Tests/Usings.cs | 1 + SingletonPattern/Properties/AssemblyInfo.cs | 3 +++ 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 SingletonPattern.Tests/SingletonPattern.Tests.csproj create mode 100644 SingletonPattern.Tests/SingletonPatternTests.cs create mode 100644 SingletonPattern.Tests/Usings.cs create mode 100644 SingletonPattern/Properties/AssemblyInfo.cs 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/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/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")] From 34b417f9b52336378416e32b32e7243d41348416 Mon Sep 17 00:00:00 2001 From: FunkyMonk8111 <100250024+FunkyMonk8111@users.noreply.github.com> Date: Wed, 10 Jan 2024 17:09:32 -0500 Subject: [PATCH 5/6] Adding missing design pattern links (#34) Added the typo or missing design pattern links per issue #22 --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6039801..f82eb91 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ There are three kinds of Design Patterns: * [Adapter](/AdapterPattern) * [Bridge](/BridgePattern) +* [Builder](/BuilderPattern) +* [ChainOfResponsibility](/ChainOfResponsibilityPattern) * [Command](/CommandPattern) * [Composite](/CompositePattern) * [Decorator](/DecoratorPattern) @@ -27,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) From 3975eae85bf104af87e94efa592548a325624f83 Mon Sep 17 00:00:00 2001 From: vikramvee Date: Thu, 11 Jan 2024 03:40:17 +0530 Subject: [PATCH 6/6] Cleaned the code by removing the string literals (#33) * Removed the use of String Literals * Removed the use of String Literals for clean code --- .../Factory Method/ChicagoPizzaFactory.cs | 16 ++++++++++------ .../Factory Method/NYPizzaFactory.cs | 16 ++++++++++------ FactoryPattern/Factory Method/PizzaFactory.cs | 4 ++-- FactoryPattern/Helper.cs | 18 ++++++++++++++++++ FactoryPattern/Program.cs | 4 ++-- 5 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 FactoryPattern/Helper.cs diff --git a/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs b/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs index 2075587..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 2c24bff..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/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/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