diff --git a/AdapterPattern/IDuck.cs b/AdapterPattern/IDuck.cs index 502c495..f28d4f8 100644 --- a/AdapterPattern/IDuck.cs +++ b/AdapterPattern/IDuck.cs @@ -1,8 +1,7 @@ -namespace AdapterPattern +namespace AdapterPattern; + +public interface IDuck { - public interface IDuck - { - void Quack(); - void Fly(); - } + void Quack(); + void Fly(); } diff --git a/AdapterPattern/ITurkey.cs b/AdapterPattern/ITurkey.cs index 5d580ad..da850ec 100644 --- a/AdapterPattern/ITurkey.cs +++ b/AdapterPattern/ITurkey.cs @@ -1,8 +1,7 @@ -namespace AdapterPattern +namespace AdapterPattern; + +public interface ITurkey { - public interface ITurkey - { - void Gobble(); - void Fly(); - } + void Gobble(); + void Fly(); } diff --git a/AdapterPattern/MallardDuck.cs b/AdapterPattern/MallardDuck.cs index b0602cf..889b7b5 100644 --- a/AdapterPattern/MallardDuck.cs +++ b/AdapterPattern/MallardDuck.cs @@ -1,17 +1,14 @@ -using System; +namespace AdapterPattern; -namespace AdapterPattern +class MallardDuck : IDuck { - class MallardDuck : IDuck + public void Quack() { - public void Quack() - { - Console.WriteLine("Quack Quack Quack"); - } + Console.WriteLine("Quack Quack Quack"); + } - public void Fly() - { - Console.WriteLine("Flies 500 Metres"); - } + public void Fly() + { + Console.WriteLine("Flies 500 Metres"); } } diff --git a/AdapterPattern/Program.cs b/AdapterPattern/Program.cs index cb46974..4fafef7 100644 --- a/AdapterPattern/Program.cs +++ b/AdapterPattern/Program.cs @@ -1,20 +1,19 @@  -namespace AdapterPattern +namespace AdapterPattern; + +internal static class Program { - internal static class Program + private static void Main() { - private static void Main() - { - var turkey = new WildTurkey(); - var adapter = new TurkeyAdapter(turkey); + var turkey = new WildTurkey(); + var adapter = new TurkeyAdapter(turkey); - Tester(adapter); - } + Tester(adapter); + } - private static void Tester(IDuck duck) - { - duck.Fly(); - duck.Quack(); - } + private static void Tester(IDuck duck) + { + duck.Fly(); + duck.Quack(); } } diff --git a/AdapterPattern/TurkeyAdapter.cs b/AdapterPattern/TurkeyAdapter.cs index adf13e0..8c0d5cb 100644 --- a/AdapterPattern/TurkeyAdapter.cs +++ b/AdapterPattern/TurkeyAdapter.cs @@ -1,27 +1,24 @@ -using System; +namespace AdapterPattern; -namespace AdapterPattern +public class TurkeyAdapter : IDuck { - public class TurkeyAdapter : IDuck - { - private readonly ITurkey _turkey; + private readonly ITurkey _turkey; - public TurkeyAdapter(ITurkey turkey) - { - _turkey = turkey; - } - public void Quack() - { - _turkey.Gobble(); - } + public TurkeyAdapter(ITurkey turkey) + { + _turkey = turkey; + } + public void Quack() + { + _turkey.Gobble(); + } - public void Fly() + public void Fly() + { + for (var i = 0; i < 5; i++) { - for (var i = 0; i < 5; i++) - { - _turkey.Fly(); - Console.WriteLine("Resting.."); - } + _turkey.Fly(); + Console.WriteLine("Resting.."); } } } \ No newline at end of file diff --git a/AdapterPattern/WildTurkey.cs b/AdapterPattern/WildTurkey.cs index b6e04ac..f2db389 100644 --- a/AdapterPattern/WildTurkey.cs +++ b/AdapterPattern/WildTurkey.cs @@ -1,17 +1,14 @@ -using System; +namespace AdapterPattern; -namespace AdapterPattern +class WildTurkey : ITurkey { - class WildTurkey : ITurkey + public void Gobble() { - public void Gobble() - { - Console.WriteLine("Gobble Gobble Gobble"); - } + Console.WriteLine("Gobble Gobble Gobble"); + } - public void Fly() - { - Console.WriteLine("Flies 100 Metres"); - } + public void Fly() + { + Console.WriteLine("Flies 100 Metres"); } } diff --git a/BridgePattern/FlyingEnchantment.cs b/BridgePattern/FlyingEnchantment.cs index 4aa5b38..72750b6 100644 --- a/BridgePattern/FlyingEnchantment.cs +++ b/BridgePattern/FlyingEnchantment.cs @@ -1,22 +1,19 @@ -using System; +namespace BridgePattern; -namespace BridgePattern +public class FlyingEnchantment : IEnchantment { - public class FlyingEnchantment : IEnchantment + public void OnActivate() { - public void OnActivate() - { - Console.WriteLine("The item begins to glow faintly."); - } + Console.WriteLine("The item begins to glow faintly."); + } - public void Apply() - { - Console.WriteLine("The item flies and strikes the enemies finally returning to owner's hand."); - } + public void Apply() + { + Console.WriteLine("The item flies and strikes the enemies finally returning to owner's hand."); + } - public void OnDeactivate() - { - Console.WriteLine("The item's glow fades."); - } + public void OnDeactivate() + { + Console.WriteLine("The item's glow fades."); } } \ No newline at end of file diff --git a/BridgePattern/Hammer.cs b/BridgePattern/Hammer.cs index 55e6782..c7edb94 100644 --- a/BridgePattern/Hammer.cs +++ b/BridgePattern/Hammer.cs @@ -1,36 +1,33 @@ -using System; +namespace BridgePattern; -namespace BridgePattern +public class Hammer : IWeapon { - public class Hammer : IWeapon + private readonly IEnchantment _enchantment; + public Hammer(IEnchantment enchantment) { - private readonly IEnchantment _enchantment; - public Hammer(IEnchantment enchantment) - { - _enchantment = enchantment; - } + _enchantment = enchantment; + } - public void Wield() - { - Console.WriteLine("The hammer is wielded."); - _enchantment.OnActivate(); - } + public void Wield() + { + Console.WriteLine("The hammer is wielded."); + _enchantment.OnActivate(); + } - public void Swing() - { - Console.WriteLine("The hammer is swinged."); - _enchantment.Apply(); - } + public void Swing() + { + Console.WriteLine("The hammer is swinged."); + _enchantment.Apply(); + } - public void Unwield() - { - Console.WriteLine("The hammer is unwielded."); - _enchantment.OnDeactivate(); - } + public void Unwield() + { + Console.WriteLine("The hammer is unwielded."); + _enchantment.OnDeactivate(); + } - public IEnchantment GetEnchantment() - { - return _enchantment; - } + public IEnchantment GetEnchantment() + { + return _enchantment; } } \ No newline at end of file diff --git a/BridgePattern/IEnchantment.cs b/BridgePattern/IEnchantment.cs index 6f958ea..247c21f 100644 --- a/BridgePattern/IEnchantment.cs +++ b/BridgePattern/IEnchantment.cs @@ -1,9 +1,8 @@ -namespace BridgePattern +namespace BridgePattern; + +public interface IEnchantment { - public interface IEnchantment - { - void OnActivate(); - void Apply(); - void OnDeactivate(); - } + void OnActivate(); + void Apply(); + void OnDeactivate(); } \ No newline at end of file diff --git a/BridgePattern/IWeapon.cs b/BridgePattern/IWeapon.cs index 2b2383b..29762b5 100644 --- a/BridgePattern/IWeapon.cs +++ b/BridgePattern/IWeapon.cs @@ -1,10 +1,9 @@ -namespace BridgePattern +namespace BridgePattern; + +public interface IWeapon { - public interface IWeapon - { - void Wield(); - void Swing(); - void Unwield(); - IEnchantment GetEnchantment(); - } + void Wield(); + void Swing(); + void Unwield(); + IEnchantment GetEnchantment(); } \ No newline at end of file diff --git a/BridgePattern/Program.cs b/BridgePattern/Program.cs index c07d035..8c8f160 100644 --- a/BridgePattern/Program.cs +++ b/BridgePattern/Program.cs @@ -1,18 +1,17 @@ -namespace BridgePattern +namespace BridgePattern; + +internal static class Program { - internal static class Program + private static void Main() { - private static void Main() - { - IWeapon sword = new Sword(new FlyingEnchantment()); - sword.Wield(); - sword.Swing(); - sword.Unwield(); + IWeapon sword = new Sword(new FlyingEnchantment()); + sword.Wield(); + sword.Swing(); + sword.Unwield(); - IWeapon hammer = new Hammer(new SoulEatingEnchantment()); - hammer.Wield(); - hammer.Swing(); - hammer.Unwield(); - } + IWeapon hammer = new Hammer(new SoulEatingEnchantment()); + hammer.Wield(); + hammer.Swing(); + hammer.Unwield(); } } \ No newline at end of file diff --git a/BridgePattern/SoulEatingEnchantment.cs b/BridgePattern/SoulEatingEnchantment.cs index dfb9b8a..17e346d 100644 --- a/BridgePattern/SoulEatingEnchantment.cs +++ b/BridgePattern/SoulEatingEnchantment.cs @@ -1,22 +1,19 @@ -using System; +namespace BridgePattern; -namespace BridgePattern +public class SoulEatingEnchantment : IEnchantment { - public class SoulEatingEnchantment : IEnchantment + public void OnActivate() { - public void OnActivate() - { - Console.WriteLine("The item spreads bloodlust."); - } + Console.WriteLine("The item spreads bloodlust."); + } - public void Apply() - { - Console.WriteLine("The item eats the soul of enemies."); - } + public void Apply() + { + Console.WriteLine("The item eats the soul of enemies."); + } - public void OnDeactivate() - { - Console.WriteLine("Bloodlust slowly disappears."); - } + public void OnDeactivate() + { + Console.WriteLine("Bloodlust slowly disappears."); } } \ No newline at end of file diff --git a/BridgePattern/Sword.cs b/BridgePattern/Sword.cs index 23198d5..043be8f 100644 --- a/BridgePattern/Sword.cs +++ b/BridgePattern/Sword.cs @@ -1,37 +1,34 @@ -using System; +namespace BridgePattern; -namespace BridgePattern +public class Sword : IWeapon { - public class Sword : IWeapon - { - private readonly IEnchantment _enchantment; + private readonly IEnchantment _enchantment; - public Sword(IEnchantment enchantment) - { - _enchantment = enchantment; - } + public Sword(IEnchantment enchantment) + { + _enchantment = enchantment; + } - public void Wield() - { - Console.WriteLine("The sword is wielded."); - _enchantment.OnActivate(); - } + public void Wield() + { + Console.WriteLine("The sword is wielded."); + _enchantment.OnActivate(); + } - public void Swing() - { - Console.WriteLine("The sword is swinged."); - _enchantment.Apply(); - } + public void Swing() + { + Console.WriteLine("The sword is swinged."); + _enchantment.Apply(); + } - public void Unwield() - { - Console.WriteLine("The sword is unwielded."); - _enchantment.OnDeactivate(); - } + public void Unwield() + { + Console.WriteLine("The sword is unwielded."); + _enchantment.OnDeactivate(); + } - public IEnchantment GetEnchantment() - { - return _enchantment; - } + public IEnchantment GetEnchantment() + { + return _enchantment; } } \ No newline at end of file diff --git a/BuilderPattern/Cook.cs b/BuilderPattern/Cook.cs index 980bb3d..43003cd 100644 --- a/BuilderPattern/Cook.cs +++ b/BuilderPattern/Cook.cs @@ -1,32 +1,31 @@ -namespace BuilderPattern -{ +namespace BuilderPattern; + - // This class can also be called the Director - public class Cook +// This class can also be called the Director +public class Cook +{ + private IBuilder _builder; + public Cook(IBuilder builder) { - private IBuilder _builder; - public Cook(IBuilder builder) - { - AcceptBuilder(builder); - } + AcceptBuilder(builder); + } - public void ChangeBuilder(IBuilder builder) - { - AcceptBuilder(builder); - } + public void ChangeBuilder(IBuilder builder) + { + AcceptBuilder(builder); + } - public Hamburger Build() - { - _builder.AddIngredients(); - _builder.AddShape(); - _builder.AddSize(); - return _builder.Build(); - } + public Hamburger Build() + { + _builder.AddIngredients(); + _builder.AddShape(); + _builder.AddSize(); + return _builder.Build(); + } - private void AcceptBuilder(IBuilder builder) - { - _builder = builder; - _builder.Reset(); - } + private void AcceptBuilder(IBuilder builder) + { + _builder = builder; + _builder.Reset(); } } diff --git a/BuilderPattern/Hamburger.cs b/BuilderPattern/Hamburger.cs index 68ec0f6..08f0f9a 100644 --- a/BuilderPattern/Hamburger.cs +++ b/BuilderPattern/Hamburger.cs @@ -1,19 +1,18 @@  -namespace BuilderPattern +namespace BuilderPattern; + +public class Hamburger { - public class Hamburger + public int Size { get; set; } + public string Shape { get; set; } + public string[] Ingredients { get; set; } + public override string ToString() { - public int Size { get; set; } - public string Shape { get; set; } - 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}"; + hamburger += $"{ingredient} "; } + return $"Ingredients: {hamburger}, Size: {Size}, Shape: {Shape}"; } } diff --git a/BuilderPattern/IBuilder.cs b/BuilderPattern/IBuilder.cs index 2730841..3bfb48a 100644 --- a/BuilderPattern/IBuilder.cs +++ b/BuilderPattern/IBuilder.cs @@ -1,11 +1,10 @@ -namespace BuilderPattern +namespace BuilderPattern; + +public interface IBuilder { - public interface IBuilder - { - void AddIngredients(); - void AddShape(); - void AddSize(); - void Reset(); - Hamburger Build(); - } + void AddIngredients(); + void AddShape(); + void AddSize(); + void Reset(); + Hamburger Build(); } diff --git a/BuilderPattern/MyHamburgerBuilder.cs b/BuilderPattern/MyHamburgerBuilder.cs index 4ce1cbf..7950b4d 100644 --- a/BuilderPattern/MyHamburgerBuilder.cs +++ b/BuilderPattern/MyHamburgerBuilder.cs @@ -1,31 +1,30 @@ -namespace BuilderPattern +namespace BuilderPattern; + +public class MyHamburgerBuilder : IBuilder { - public class MyHamburgerBuilder : IBuilder + private Hamburger _hamburger; + public void AddIngredients() { - private Hamburger _hamburger; - public void AddIngredients() - { - _hamburger.Ingredients = new string[] { "Bread", "Meat", "Tomato", "Salad", "Mayonnaise" }; - } - - public void AddShape() - { - _hamburger.Shape = "Kite"; - } + _hamburger.Ingredients = new string[] { "Bread", "Meat", "Tomato", "Salad", "Mayonnaise" }; + } - public void AddSize() - { - _hamburger.Size = 10; //inches - } - public void Reset() - { - _hamburger = new Hamburger(); - } + public void AddShape() + { + _hamburger.Shape = "Kite"; + } - public Hamburger Build() - { - return _hamburger; - } + public void AddSize() + { + _hamburger.Size = 10; //inches + } + public void Reset() + { + _hamburger = new Hamburger(); + } + public Hamburger Build() + { + return _hamburger; } + } diff --git a/BuilderPattern/Program.cs b/BuilderPattern/Program.cs index dc14219..a214876 100644 --- a/BuilderPattern/Program.cs +++ b/BuilderPattern/Program.cs @@ -1,20 +1,17 @@ -using System; +namespace BuilderPattern; -namespace BuilderPattern +class Program { - class Program + static void Main() { - static void Main() - { - var builder = new MyHamburgerBuilder(); - var cook = new Cook(builder); - var myHamburger = cook.Build(); + var builder = new MyHamburgerBuilder(); + var cook = new Cook(builder); + var myHamburger = cook.Build(); - cook.ChangeBuilder(new WifesHamburgerBuilder()); - var wifesHamburger = cook.Build(); + cook.ChangeBuilder(new WifesHamburgerBuilder()); + var wifesHamburger = cook.Build(); - Console.WriteLine($"My Hamburger: {myHamburger}"); - Console.WriteLine($"My Wife's Hamburger: {wifesHamburger}"); - } + Console.WriteLine($"My Hamburger: {myHamburger}"); + Console.WriteLine($"My Wife's Hamburger: {wifesHamburger}"); } } diff --git a/BuilderPattern/WifesHamburgerBuilder.cs b/BuilderPattern/WifesHamburgerBuilder.cs index 887dcc1..bd8b6ca 100644 --- a/BuilderPattern/WifesHamburgerBuilder.cs +++ b/BuilderPattern/WifesHamburgerBuilder.cs @@ -1,31 +1,29 @@ -namespace BuilderPattern -{ +namespace BuilderPattern; - public class WifesHamburgerBuilder : IBuilder +public class WifesHamburgerBuilder : IBuilder +{ + private Hamburger _hamburger; + public void AddIngredients() { - private Hamburger _hamburger; - public void AddIngredients() - { - _hamburger.Ingredients = new string[] { "Bread", "Salad" }; - } + _hamburger.Ingredients = new string[] { "Bread", "Salad" }; + } - public void AddShape() - { - _hamburger.Shape = "Cuboid"; - } + public void AddShape() + { + _hamburger.Shape = "Cuboid"; + } - public void AddSize() - { - _hamburger.Size = 6; //inches - } + public void AddSize() + { + _hamburger.Size = 6; //inches + } - public void Reset() - { - _hamburger = new Hamburger(); - } - public Hamburger Build() - { - return _hamburger; - } + public void Reset() + { + _hamburger = new Hamburger(); + } + public Hamburger Build() + { + return _hamburger; } } diff --git a/CommandPattern/Garage.cs b/CommandPattern/Garage.cs index cb5cec0..793792f 100644 --- a/CommandPattern/Garage.cs +++ b/CommandPattern/Garage.cs @@ -1,24 +1,21 @@ -using System; +namespace CommandPattern; -namespace CommandPattern +internal class Garage { - internal class Garage - { - private readonly string _name; + private readonly string _name; - public Garage(string name) - { - _name = name; - } + public Garage(string name) + { + _name = name; + } - internal void Open() - { - Console.WriteLine($"{_name} Garage Opened"); - } + internal void Open() + { + Console.WriteLine($"{_name} Garage Opened"); + } - internal void Close() - { - Console.WriteLine($"{_name} Garage Closed"); - } + internal void Close() + { + Console.WriteLine($"{_name} Garage Closed"); } } \ No newline at end of file diff --git a/CommandPattern/GarageDoorCloseCommand.cs b/CommandPattern/GarageDoorCloseCommand.cs index 89bbb18..3fd4d74 100644 --- a/CommandPattern/GarageDoorCloseCommand.cs +++ b/CommandPattern/GarageDoorCloseCommand.cs @@ -1,22 +1,21 @@ -namespace CommandPattern +namespace CommandPattern; + +internal class GarageDoorCloseCommand : ICommand { - internal class GarageDoorCloseCommand : ICommand - { - private readonly Garage _garage; + private readonly Garage _garage; - public GarageDoorCloseCommand(Garage g) - { - _garage = g; - } + public GarageDoorCloseCommand(Garage g) + { + _garage = g; + } - public void Execute() - { - _garage.Close(); - } + public void Execute() + { + _garage.Close(); + } - public void Undo() - { - _garage.Open(); - } + public void Undo() + { + _garage.Open(); } } \ No newline at end of file diff --git a/CommandPattern/GarageDoorOpenCommand.cs b/CommandPattern/GarageDoorOpenCommand.cs index 287d88f..c970a10 100644 --- a/CommandPattern/GarageDoorOpenCommand.cs +++ b/CommandPattern/GarageDoorOpenCommand.cs @@ -1,22 +1,21 @@ -namespace CommandPattern +namespace CommandPattern; + +internal class GarageDoorOpenCommand : ICommand { - internal class GarageDoorOpenCommand : ICommand - { - private readonly Garage _garage; + private readonly Garage _garage; - public GarageDoorOpenCommand(Garage g) - { - _garage = g; - } + public GarageDoorOpenCommand(Garage g) + { + _garage = g; + } - public void Execute() - { - _garage.Open(); - } + public void Execute() + { + _garage.Open(); + } - public void Undo() - { - _garage.Close(); - } + public void Undo() + { + _garage.Close(); } } \ No newline at end of file diff --git a/CommandPattern/ICommand.cs b/CommandPattern/ICommand.cs index a9d5add..721695e 100644 --- a/CommandPattern/ICommand.cs +++ b/CommandPattern/ICommand.cs @@ -1,8 +1,7 @@ -namespace CommandPattern +namespace CommandPattern; + +internal interface ICommand { - internal interface ICommand - { - void Execute(); - void Undo(); - } + void Execute(); + void Undo(); } \ No newline at end of file diff --git a/CommandPattern/Light.cs b/CommandPattern/Light.cs index 85c96c3..c8be06c 100644 --- a/CommandPattern/Light.cs +++ b/CommandPattern/Light.cs @@ -1,24 +1,21 @@ -using System; +namespace CommandPattern; -namespace CommandPattern +public class Light { - public class Light - { - private readonly string _name; + private readonly string _name; - public Light(string name) - { - _name = name; - } + public Light(string name) + { + _name = name; + } - internal void On() - { - Console.WriteLine($"{_name} Light On"); - } + internal void On() + { + Console.WriteLine($"{_name} Light On"); + } - internal void Off() - { - Console.WriteLine($"{_name} Light Off"); - } + internal void Off() + { + Console.WriteLine($"{_name} Light Off"); } } \ No newline at end of file diff --git a/CommandPattern/LightOffCommand.cs b/CommandPattern/LightOffCommand.cs index a156a91..9bdf1b5 100644 --- a/CommandPattern/LightOffCommand.cs +++ b/CommandPattern/LightOffCommand.cs @@ -1,22 +1,21 @@ -namespace CommandPattern +namespace CommandPattern; + +internal class LightOffCommand : ICommand { - internal class LightOffCommand : ICommand - { - private readonly Light _light; + private readonly Light _light; - public LightOffCommand(Light l) - { - _light = l; - } + public LightOffCommand(Light l) + { + _light = l; + } - public void Execute() - { - _light.Off(); - } + public void Execute() + { + _light.Off(); + } - public void Undo() - { - _light.On(); - } + public void Undo() + { + _light.On(); } } \ No newline at end of file diff --git a/CommandPattern/LightOnCommand.cs b/CommandPattern/LightOnCommand.cs index e01e621..57504db 100644 --- a/CommandPattern/LightOnCommand.cs +++ b/CommandPattern/LightOnCommand.cs @@ -1,22 +1,21 @@ -namespace CommandPattern +namespace CommandPattern; + +internal class LightOnCommand : ICommand { - internal class LightOnCommand : ICommand - { - private readonly Light _light; + private readonly Light _light; - public LightOnCommand(Light l) - { - _light = l; - } + public LightOnCommand(Light l) + { + _light = l; + } - public void Execute() - { - _light.On(); - } + public void Execute() + { + _light.On(); + } - public void Undo() - { - _light.Off(); - } + public void Undo() + { + _light.Off(); } } \ No newline at end of file diff --git a/CommandPattern/MacroCommand.cs b/CommandPattern/MacroCommand.cs index db35016..709eef0 100644 --- a/CommandPattern/MacroCommand.cs +++ b/CommandPattern/MacroCommand.cs @@ -1,24 +1,23 @@ -namespace CommandPattern +namespace CommandPattern; + +internal class MacroCommand : ICommand { - internal class MacroCommand : ICommand - { - private readonly ICommand[] _commands; + private readonly ICommand[] _commands; - public MacroCommand(ICommand[] commands) - { - _commands = commands; - } + public MacroCommand(ICommand[] commands) + { + _commands = commands; + } - public void Execute() - { - foreach (var item in _commands) - item.Execute(); - } + public void Execute() + { + foreach (var item in _commands) + item.Execute(); + } - public void Undo() - { - foreach (var item in _commands) - item.Undo(); - } + public void Undo() + { + foreach (var item in _commands) + item.Undo(); } } \ No newline at end of file diff --git a/CommandPattern/NoCommand.cs b/CommandPattern/NoCommand.cs index 95812d0..bda8d7e 100644 --- a/CommandPattern/NoCommand.cs +++ b/CommandPattern/NoCommand.cs @@ -1,17 +1,14 @@ -using System; +namespace CommandPattern; -namespace CommandPattern +internal class NoCommand : ICommand { - internal class NoCommand : ICommand + public void Execute() { - public void Execute() - { - Console.WriteLine("No Command Assigned"); - } + Console.WriteLine("No Command Assigned"); + } - public void Undo() - { - Execute(); - } + public void Undo() + { + Execute(); } } \ No newline at end of file diff --git a/CommandPattern/OnOffStruct.cs b/CommandPattern/OnOffStruct.cs index 8cbdbc4..a09e435 100644 --- a/CommandPattern/OnOffStruct.cs +++ b/CommandPattern/OnOffStruct.cs @@ -1,8 +1,7 @@ -namespace CommandPattern +namespace CommandPattern; + +internal struct OnOffStruct { - internal struct OnOffStruct - { - public ICommand On; - public ICommand Off; - } + public ICommand On; + public ICommand Off; } \ No newline at end of file diff --git a/CommandPattern/Program.cs b/CommandPattern/Program.cs index d719ca9..ab1e4c0 100644 --- a/CommandPattern/Program.cs +++ b/CommandPattern/Program.cs @@ -1,54 +1,51 @@ -using System; +namespace CommandPattern; -namespace CommandPattern +internal static class Program { - internal static class Program + private static void Main() { - private static void Main() - { - var remote = new RemoteControl(3); + var remote = new RemoteControl(3); - var bike = new Garage("Bike"); - var bikeDoorClose = new GarageDoorCloseCommand(bike); - var bikeDoorOpen = new GarageDoorOpenCommand(bike); + var bike = new Garage("Bike"); + var bikeDoorClose = new GarageDoorCloseCommand(bike); + var bikeDoorOpen = new GarageDoorOpenCommand(bike); - var car = new Garage("Car"); - var carDoorClose = new GarageDoorCloseCommand(car); - var carDoorOpen = new GarageDoorOpenCommand(car); + var car = new Garage("Car"); + var carDoorClose = new GarageDoorCloseCommand(car); + var carDoorOpen = new GarageDoorOpenCommand(car); - var garageButton = new OnOffStruct - { - On = bikeDoorOpen, - Off = bikeDoorClose - }; + var garageButton = new OnOffStruct + { + On = bikeDoorOpen, + Off = bikeDoorClose + }; - remote[0] = garageButton; - remote.PushOn(0); - remote.PushUndo(); - remote.PushUndo(); - remote.PushOff(0); + remote[0] = garageButton; + remote.PushOn(0); + remote.PushUndo(); + remote.PushUndo(); + remote.PushOff(0); - Console.WriteLine(); - var light = new Light("Hall"); + 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 - { - remote.PushOn(2); - Console.WriteLine(); - remote.PushOff(2); - } - catch (Exception) - { - Console.WriteLine("Oops"); - } + try + { + remote.PushOn(2); + Console.WriteLine(); + remote.PushOff(2); + } + catch (Exception) + { + Console.WriteLine("Oops"); } } } \ No newline at end of file diff --git a/CommandPattern/RemoteControl.cs b/CommandPattern/RemoteControl.cs index e5e39db..c1a350c 100644 --- a/CommandPattern/RemoteControl.cs +++ b/CommandPattern/RemoteControl.cs @@ -1,50 +1,49 @@ -namespace CommandPattern +namespace CommandPattern; + +internal class RemoteControl { - internal class RemoteControl + private readonly ICommand[] _offCommand; + private readonly ICommand[] _onCommand; + private ICommand _undoCommand; + + public RemoteControl(int slots) { - private readonly ICommand[] _offCommand; - private readonly ICommand[] _onCommand; - private ICommand _undoCommand; + _onCommand = new ICommand[slots]; + _offCommand = new ICommand[slots]; - public RemoteControl(int slots) + var none = new NoCommand(); + _undoCommand = none; + for (var i = 0; i < slots; i++) { - _onCommand = new ICommand[slots]; - _offCommand = new ICommand[slots]; - - var none = new NoCommand(); - _undoCommand = none; - for (var i = 0; i < slots; i++) - { - _onCommand[i] = none; - _offCommand[i] = none; - } + _onCommand[i] = none; + _offCommand[i] = none; } + } - public OnOffStruct this[int i] + public OnOffStruct this[int i] + { + set { - set - { - _onCommand[i] = value.On; - _offCommand[i] = value.Off; - } + _onCommand[i] = value.On; + _offCommand[i] = value.Off; } + } - public void PushOn(int slot) - { - _onCommand[slot].Execute(); - _undoCommand = _offCommand[slot]; - } + public void PushOn(int slot) + { + _onCommand[slot].Execute(); + _undoCommand = _offCommand[slot]; + } - public void PushOff(int slot) - { - _offCommand[slot].Execute(); - _undoCommand = _onCommand[slot]; - } + public void PushOff(int slot) + { + _offCommand[slot].Execute(); + _undoCommand = _onCommand[slot]; + } - public void PushUndo() - { - _undoCommand.Execute(); - } + public void PushUndo() + { + _undoCommand.Execute(); } } \ No newline at end of file diff --git a/CompositePattern/Client.cs b/CompositePattern/Client.cs index 8aaba56..6ba760c 100644 --- a/CompositePattern/Client.cs +++ b/CompositePattern/Client.cs @@ -1,17 +1,16 @@ -namespace CompositePattern +namespace CompositePattern; + +public class Client { - public class Client - { - private readonly MenuComponent _menus; + private readonly MenuComponent _menus; - public Client(MenuComponent menus) - { - _menus = menus; - } + public Client(MenuComponent menus) + { + _menus = menus; + } - public void Print() - { - _menus.Print(); - } + public void Print() + { + _menus.Print(); } } \ No newline at end of file diff --git a/CompositePattern/Menu.cs b/CompositePattern/Menu.cs index 6be36c6..8953eea 100644 --- a/CompositePattern/Menu.cs +++ b/CompositePattern/Menu.cs @@ -1,47 +1,43 @@ -using System; -using System.Collections.Generic; +namespace CompositePattern; -namespace CompositePattern +public class Menu : MenuComponent { - public class Menu : MenuComponent - { - List _components = new List(); + List _components = new List(); - public Menu(string name, string description) - { - Name = name; - Description = description; + public Menu(string name, string description) + { + Name = name; + Description = description; - } + } - public override void Add(MenuComponent component) - { - _components.Add(component); - } + public override void Add(MenuComponent component) + { + _components.Add(component); + } - public override void Remove(MenuComponent component) - { - _components.Remove(component); - } + public override void Remove(MenuComponent component) + { + _components.Remove(component); + } - public override MenuComponent GetChild(int i) - { - return _components[i]; - } + public override MenuComponent GetChild(int i) + { + return _components[i]; + } - public override string Name { get; } + public override string Name { get; } - public override string Description { get; } + public override string Description { get; } - public override void Print() + public override void Print() + { + Console.WriteLine(Name); + Console.WriteLine("___________"); + foreach (var menuComponent in _components) { - Console.WriteLine(Name); - Console.WriteLine("___________"); - foreach (var menuComponent in _components) - { - menuComponent.Print(); - } - Console.WriteLine(); + menuComponent.Print(); } + Console.WriteLine(); } } \ No newline at end of file diff --git a/CompositePattern/MenuComponent.cs b/CompositePattern/MenuComponent.cs index 38928e4..22ca919 100644 --- a/CompositePattern/MenuComponent.cs +++ b/CompositePattern/MenuComponent.cs @@ -1,32 +1,29 @@ -using System; +namespace CompositePattern; -namespace CompositePattern +public class MenuComponent { - public class MenuComponent + public virtual void Add(MenuComponent component) { - public virtual void Add(MenuComponent component) - { - throw new NotImplementedException(); - } + throw new NotImplementedException(); + } - public virtual void Remove(MenuComponent component) - { - throw new NotImplementedException(); - } + public virtual void Remove(MenuComponent component) + { + throw new NotImplementedException(); + } - public virtual MenuComponent GetChild(int i) - { - throw new NotImplementedException(); - } + public virtual MenuComponent GetChild(int i) + { + throw new NotImplementedException(); + } - public virtual string Name { get; } - public virtual string Description { get; } - public virtual bool Vegetarian { get; } - public virtual double Price { get; } + public virtual string Name { get; } + public virtual string Description { get; } + public virtual bool Vegetarian { get; } + public virtual double Price { get; } - public virtual void Print() - { - throw new NotImplementedException(); - } + public virtual void Print() + { + throw new NotImplementedException(); } } \ No newline at end of file diff --git a/CompositePattern/MenuItem.cs b/CompositePattern/MenuItem.cs index ab7c2f1..114ab10 100644 --- a/CompositePattern/MenuItem.cs +++ b/CompositePattern/MenuItem.cs @@ -1,28 +1,25 @@ -using System; +namespace CompositePattern; -namespace CompositePattern +public class MenuItem : MenuComponent { - public class MenuItem : MenuComponent + public MenuItem(string name, string description, double price, bool isveg) { - public MenuItem(string name, string description, double price, bool isveg) - { - Name = name; - Description = description; - Price = price; - Vegetarian = isveg; - } + Name = name; + Description = description; + Price = price; + Vegetarian = isveg; + } - public override string Name { get; } + public override string Name { get; } - public override string Description { get; } + public override string Description { get; } - public override double Price { get; } + public override double Price { get; } - public override bool Vegetarian { get; } + public override bool Vegetarian { get; } - public override void Print() - { - Console.WriteLine($"{Name} : {Price} {(Vegetarian ? '+' : '*')} \n {Description}"); - } + public override void Print() + { + Console.WriteLine($"{Name} : {Price} {(Vegetarian ? '+' : '*')} \n {Description}"); } } \ No newline at end of file diff --git a/CompositePattern/Program.cs b/CompositePattern/Program.cs index 1c24613..05ba706 100644 --- a/CompositePattern/Program.cs +++ b/CompositePattern/Program.cs @@ -1,37 +1,36 @@ -namespace CompositePattern +namespace CompositePattern; + +static class Program { - static class Program + public static void Main() { - 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 breakfast = new Menu("Breakfast", "Pancake House"); + var lunch = new Menu("Lunch", "Deli Diner"); + var dinner = new Menu("Dinner", "Dinneroni"); - var dessert = new Menu("Dessert", "Ice Cream"); + var dessert = new Menu("Dessert", "Ice Cream"); - var menu = new Menu("All", "McDonalds"); + 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)); + lunch.Add(new MenuItem("Burger", "Cheese and Onion Burger", 250, true)); + lunch.Add(new MenuItem("Sandwich", "Chicken Sandwich", 280, false)); - dinner.Add(new MenuItem("Pizza", "Cheese and Tomato Pizza", 210, true)); - dinner.Add(new MenuItem("Pasta", "Chicken Pasta", 280, false)); + dinner.Add(new MenuItem("Pizza", "Cheese and Tomato Pizza", 210, true)); + 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("Ice Cream", "Vanilla and Chocolate", 120, true)); + dessert.Add(new MenuItem("Cake", "Choclate Cake Slice", 180, false)); - dinner.Add(dessert); - menu.Add(breakfast); - menu.Add(lunch); - menu.Add(dinner); + dinner.Add(dessert); + menu.Add(breakfast); + menu.Add(lunch); + menu.Add(dinner); - menu.Print(); + menu.Print(); - } } } diff --git a/DecoratorPattern/Beverage.cs b/DecoratorPattern/Beverage.cs index c6b8ec8..2c175af 100644 --- a/DecoratorPattern/Beverage.cs +++ b/DecoratorPattern/Beverage.cs @@ -1,9 +1,8 @@ -namespace DecoratorPattern +namespace DecoratorPattern; + +abstract class Beverage { - abstract class Beverage - { - protected string _description = "No Description"; - public abstract string Description { get; } - public abstract double Cost(); - } + protected string _description = "No Description"; + public abstract string Description { get; } + public abstract double Cost(); } diff --git a/DecoratorPattern/CondimentDecorator.cs b/DecoratorPattern/CondimentDecorator.cs index 75f9561..98e330d 100644 --- a/DecoratorPattern/CondimentDecorator.cs +++ b/DecoratorPattern/CondimentDecorator.cs @@ -1,8 +1,6 @@ -namespace DecoratorPattern -{ - abstract class CondimentDecorator : Beverage - { - public abstract override string Description { get; } - } +namespace DecoratorPattern; +abstract class CondimentDecorator : Beverage +{ + public abstract override string Description { get; } } diff --git a/DecoratorPattern/DarkRoast.cs b/DecoratorPattern/DarkRoast.cs index e0b58c0..b8ad180 100644 --- a/DecoratorPattern/DarkRoast.cs +++ b/DecoratorPattern/DarkRoast.cs @@ -1,17 +1,16 @@ -namespace DecoratorPattern +namespace DecoratorPattern; + +internal class DarkRoast : Beverage { - internal class DarkRoast : Beverage + public DarkRoast() { - public DarkRoast() - { - _description = "Dark Roast"; - } + _description = "Dark Roast"; + } - public override string Description => _description; + public override string Description => _description; - public override double Cost() - { - return 1.49; - } + public override double Cost() + { + return 1.49; } } \ No newline at end of file diff --git a/DecoratorPattern/Espresso.cs b/DecoratorPattern/Espresso.cs index 1200c63..be22692 100644 --- a/DecoratorPattern/Espresso.cs +++ b/DecoratorPattern/Espresso.cs @@ -1,17 +1,16 @@ -namespace DecoratorPattern +namespace DecoratorPattern; + +class Espresso : Beverage { - class Espresso : Beverage + public Espresso() { - public Espresso() - { - _description = "Espresso"; - } + _description = "Espresso"; + } - public override string Description => _description; + public override string Description => _description; - public override double Cost() - { - return 1.99; - } + public override double Cost() + { + return 1.99; } } diff --git a/DecoratorPattern/HouseBlend.cs b/DecoratorPattern/HouseBlend.cs index 949da08..ac8b981 100644 --- a/DecoratorPattern/HouseBlend.cs +++ b/DecoratorPattern/HouseBlend.cs @@ -1,17 +1,16 @@ -namespace DecoratorPattern +namespace DecoratorPattern; + +class HouseBlend : Beverage { - class HouseBlend : Beverage + public HouseBlend() { - public HouseBlend() - { - _description = "House Blend"; - } + _description = "House Blend"; + } - public override string Description => _description; + public override string Description => _description; - public override double Cost() - { - return 2.49; - } + public override double Cost() + { + return 2.49; } } diff --git a/DecoratorPattern/MochaCondiment.cs b/DecoratorPattern/MochaCondiment.cs index b0e76a5..133306b 100644 --- a/DecoratorPattern/MochaCondiment.cs +++ b/DecoratorPattern/MochaCondiment.cs @@ -1,30 +1,29 @@ -namespace DecoratorPattern +namespace DecoratorPattern; + +class MochaCondiment : CondimentDecorator { - class MochaCondiment : CondimentDecorator - { - Beverage _beverage; + Beverage _beverage; - public MochaCondiment(Beverage beverage) - { - this._beverage = beverage; - } + public MochaCondiment(Beverage beverage) + { + this._beverage = beverage; + } - public override string Description + public override string Description + { + get { - get + if (_beverage.Description.StartsWith("Mocha")) { - if (_beverage.Description.StartsWith("Mocha")) - { - return "Double " + _beverage.Description; - } - else - return "Mocha " + _beverage.Description; + return "Double " + _beverage.Description; } + else + return "Mocha " + _beverage.Description; } + } - public override double Cost() - { - return 0.2 + _beverage.Cost(); - } + public override double Cost() + { + return 0.2 + _beverage.Cost(); } } diff --git a/DecoratorPattern/Program.cs b/DecoratorPattern/Program.cs index d26e763..01238ca 100644 --- a/DecoratorPattern/Program.cs +++ b/DecoratorPattern/Program.cs @@ -1,24 +1,21 @@ -using System; +namespace DecoratorPattern; -namespace DecoratorPattern +static class Program { - static class Program + static void Main() { - static void Main() - { - Beverage beverage = new Espresso(); - Console.WriteLine(beverage.Description + " $" + beverage.Cost()); + Beverage beverage = new Espresso(); + Console.WriteLine(beverage.Description + " $" + beverage.Cost()); - Beverage beverage2 = new DarkRoast(); - beverage2 = new MochaCondiment(beverage2); - beverage2 = new MochaCondiment(beverage2); - beverage2 = new WhipCondiment(beverage2); - Console.WriteLine(beverage2.Description + " $" + beverage2.Cost()); + Beverage beverage2 = new DarkRoast(); + beverage2 = new MochaCondiment(beverage2); + beverage2 = new MochaCondiment(beverage2); + beverage2 = new WhipCondiment(beverage2); + Console.WriteLine(beverage2.Description + " $" + beverage2.Cost()); - Beverage beverage3 = new HouseBlend(); - beverage3 = new MochaCondiment(beverage3); - beverage3 = new WhipCondiment(beverage3); - Console.WriteLine(beverage3.Description + " $" + beverage3.Cost()); - } + Beverage beverage3 = new HouseBlend(); + beverage3 = new MochaCondiment(beverage3); + beverage3 = new WhipCondiment(beverage3); + Console.WriteLine(beverage3.Description + " $" + beverage3.Cost()); } } diff --git a/DecoratorPattern/WhipCondiment.cs b/DecoratorPattern/WhipCondiment.cs index 550b929..bc20fcf 100644 --- a/DecoratorPattern/WhipCondiment.cs +++ b/DecoratorPattern/WhipCondiment.cs @@ -1,30 +1,29 @@ -namespace DecoratorPattern +namespace DecoratorPattern; + +class WhipCondiment : CondimentDecorator { - class WhipCondiment : CondimentDecorator - { - Beverage _beverage; + Beverage _beverage; - public WhipCondiment(Beverage beverage) - { - this._beverage = beverage; - } + public WhipCondiment(Beverage beverage) + { + this._beverage = beverage; + } - public override string Description + public override string Description + { + get { - get + if (_beverage.Description.StartsWith("Whip")) { - if (_beverage.Description.StartsWith("Whip")) - { - return "Double " + _beverage.Description; - } - else - return "Whip " + _beverage.Description; + return "Double " + _beverage.Description; } + else + return "Whip " + _beverage.Description; } + } - public override double Cost() - { - return 0.15 + _beverage.Cost(); - } + public override double Cost() + { + return 0.15 + _beverage.Cost(); } } diff --git a/FacadePattern/Dimmer.cs b/FacadePattern/Dimmer.cs index 11c35df..3740aa8 100644 --- a/FacadePattern/Dimmer.cs +++ b/FacadePattern/Dimmer.cs @@ -1,14 +1,11 @@ -using System; +namespace FacadePattern; -namespace FacadePattern +public class Dimmer { - public class Dimmer + internal void Dim(int val) { - internal void Dim(int val) - { - Console.WriteLine(val == 10 ? "Turning Lights On" : $"Dimming lights to {val}"); - } - - internal void Off() => Console.WriteLine("Switching off lights"); + Console.WriteLine(val == 10 ? "Turning Lights On" : $"Dimming lights to {val}"); } + + internal void Off() => Console.WriteLine("Switching off lights"); } diff --git a/FacadePattern/Dvd.cs b/FacadePattern/Dvd.cs index b247c62..61840a0 100644 --- a/FacadePattern/Dvd.cs +++ b/FacadePattern/Dvd.cs @@ -1,11 +1,10 @@ -namespace FacadePattern +namespace FacadePattern; + +public class Dvd { - public class Dvd + public Dvd(string name) { - public Dvd(string name) - { - Movie = name; - } - public string Movie { get; set; } + Movie = name; } + public string Movie { get; set; } } \ No newline at end of file diff --git a/FacadePattern/DvdPlayer.cs b/FacadePattern/DvdPlayer.cs index f442655..e73da03 100644 --- a/FacadePattern/DvdPlayer.cs +++ b/FacadePattern/DvdPlayer.cs @@ -1,30 +1,27 @@ -using System; +namespace FacadePattern; -namespace FacadePattern +public class DvdPlayer { - public class DvdPlayer - { - private Dvd _dvd; - private int _time = 0; - public void On() => Console.WriteLine("DVD Player powered on"); + 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 Insert(Dvd dvd) + { + _dvd = dvd; + Console.WriteLine($"Inserting {dvd.Movie}"); - } + } - public void Play() => Console.WriteLine($"Playing {_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 Pause() + { + Console.WriteLine($"Pausing at {_time = (new Random()).Next(_time, _time + 120)}"); + } - public void Resume() - { - Console.WriteLine($"Resuming from {_time}"); - } + public void Resume() + { + Console.WriteLine($"Resuming from {_time}"); } } \ No newline at end of file diff --git a/FacadePattern/HometheaterFacade.cs b/FacadePattern/HometheaterFacade.cs index 703828e..da1cb48 100644 --- a/FacadePattern/HometheaterFacade.cs +++ b/FacadePattern/HometheaterFacade.cs @@ -1,36 +1,35 @@ -namespace FacadePattern +namespace FacadePattern; + +public class HomeTheatreFacade { - public class HomeTheatreFacade - { - private Dimmer _dimmer; - private Dvd _dvd; - private DvdPlayer _dvdPlayer; + private Dimmer _dimmer; + private Dvd _dvd; + private DvdPlayer _dvdPlayer; - public HomeTheatreFacade(Dimmer dimmer, Dvd dvd, DvdPlayer dvdPlayer) - { - _dvd = dvd; - _dimmer = dimmer; - _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 WatchMovie() + { + _dimmer.Dim(5); + _dvdPlayer.On(); + _dvdPlayer.Insert(_dvd); + _dvdPlayer.Play(); + } - public void Pause() - { - _dimmer.Dim(10); - _dvdPlayer.Pause(); - } + public void Pause() + { + _dimmer.Dim(10); + _dvdPlayer.Pause(); + } - public void Resume() - { - _dimmer.Dim(5); - _dvdPlayer.Resume(); - } + public void Resume() + { + _dimmer.Dim(5); + _dvdPlayer.Resume(); } } \ No newline at end of file diff --git a/FacadePattern/Program.cs b/FacadePattern/Program.cs index 074539c..fe8f2f0 100644 --- a/FacadePattern/Program.cs +++ b/FacadePattern/Program.cs @@ -1,23 +1,20 @@ -using System; +namespace FacadePattern; -namespace FacadePattern +internal static class Program { - internal static class Program + private static void Main() { - 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 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(); - } + homeTheater.WatchMovie(); + Console.WriteLine(); + homeTheater.Pause(); + Console.WriteLine(); + homeTheater.Resume(); + Console.WriteLine(); + homeTheater.Pause(); } } diff --git a/FactoryPattern/Abstract Factory/ChicagoIngredientsFactory.cs b/FactoryPattern/Abstract Factory/ChicagoIngredientsFactory.cs index 53c9430..2f5fd05 100644 --- a/FactoryPattern/Abstract Factory/ChicagoIngredientsFactory.cs +++ b/FactoryPattern/Abstract Factory/ChicagoIngredientsFactory.cs @@ -1,36 +1,33 @@ -using System.Collections.Generic; +namespace FactoryPattern; -namespace FactoryPattern +internal class ChicagoIngredientsFactory : IIngredientsFactory { - internal class ChicagoIngredientsFactory : IIngredientsFactory + ICheese IIngredientsFactory.CreateCheese() { - ICheese IIngredientsFactory.CreateCheese() - { - return new Parmesan(); - } + return new Parmesan(); + } - IClam IIngredientsFactory.CreateClam() - { - return new FreshClam(); - } + IClam IIngredientsFactory.CreateClam() + { + return new FreshClam(); + } - IDough IIngredientsFactory.CreateDough() - { - return new DeepDish(); - } + IDough IIngredientsFactory.CreateDough() + { + return new DeepDish(); + } - ISauce IIngredientsFactory.CreateSauce() - { - return new PlumTomato(); - } + ISauce IIngredientsFactory.CreateSauce() + { + return new PlumTomato(); + } - IEnumerable IIngredientsFactory.CreateVeggies() - { - var oni = new Onion(); - var ccm = new Cucumber(); - var ppr = new Pepper(); - IVeggies[] arr = { oni, ccm, ppr }; - return arr; - } + IEnumerable IIngredientsFactory.CreateVeggies() + { + var oni = new Onion(); + var ccm = new Cucumber(); + var ppr = new Pepper(); + IVeggies[] arr = { oni, ccm, ppr }; + return arr; } } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/IIngredientsFactory.cs b/FactoryPattern/Abstract Factory/IIngredientsFactory.cs index 9010710..814d549 100644 --- a/FactoryPattern/Abstract Factory/IIngredientsFactory.cs +++ b/FactoryPattern/Abstract Factory/IIngredientsFactory.cs @@ -1,13 +1,10 @@ -using System.Collections.Generic; +namespace FactoryPattern; -namespace FactoryPattern +interface IIngredientsFactory { - interface IIngredientsFactory - { - IDough CreateDough(); - IEnumerable CreateVeggies(); - ISauce CreateSauce(); - ICheese CreateCheese(); - IClam CreateClam(); - } + IDough CreateDough(); + IEnumerable CreateVeggies(); + ISauce CreateSauce(); + ICheese CreateCheese(); + IClam CreateClam(); } diff --git a/FactoryPattern/Abstract Factory/Ingredients/CherryTomato.cs b/FactoryPattern/Abstract Factory/Ingredients/CherryTomato.cs index 0b5be78..b69ab32 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/CherryTomato.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/CherryTomato.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class CherryTomato : ISauce { - internal class CherryTomato : ISauce - { - public string Name => "Cherry Tomato"; - } + public string Name => "Cherry Tomato"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Cucumber.cs b/FactoryPattern/Abstract Factory/Ingredients/Cucumber.cs index 39be3a1..a99b58d 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Cucumber.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Cucumber.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class Cucumber : IVeggies { - internal class Cucumber : IVeggies - { - public string Name => "Cucumber"; - } + public string Name => "Cucumber"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/DeepDish.cs b/FactoryPattern/Abstract Factory/Ingredients/DeepDish.cs index 3d6ef79..ad09486 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/DeepDish.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/DeepDish.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class DeepDish : IDough { - internal class DeepDish : IDough - { - public string Name => "Deep Dish"; - } + public string Name => "Deep Dish"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/FreshClam.cs b/FactoryPattern/Abstract Factory/Ingredients/FreshClam.cs index e2cf85a..6845bf3 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/FreshClam.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/FreshClam.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class FreshClam : IClam { - internal class FreshClam : IClam - { - public string Name => "Fresh Clam"; - } + public string Name => "Fresh Clam"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/FrozenClam.cs b/FactoryPattern/Abstract Factory/Ingredients/FrozenClam.cs index 45bdcc9..9f5fdf0 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/FrozenClam.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/FrozenClam.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class FrozenClam : IClam { - internal class FrozenClam : IClam - { - public string Name => "Frozen Clam"; - } + public string Name => "Frozen Clam"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ICheese.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ICheese.cs index 5c9f2f7..3ba8919 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ICheese.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ICheese.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +public interface ICheese { - public interface ICheese - { - string Name { get; } - } + string Name { get; } } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IClam.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IClam.cs index 5aaa65e..e292222 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IClam.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IClam.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +public interface IClam { - public interface IClam - { - string Name { get; } - } + string Name { get; } } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IDough.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IDough.cs index 9bb5b89..da6f826 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IDough.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IDough.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +public interface IDough { - public interface IDough - { - string Name { get; } - } + string Name { get; } } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ISauce.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ISauce.cs index 647b073..a65f40d 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ISauce.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/ISauce.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +public interface ISauce { - public interface ISauce - { - string Name { get; } - } + string Name { get; } } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IVeggies.cs b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IVeggies.cs index d258bd8..c929515 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IVeggies.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Interfaces/IVeggies.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +public interface IVeggies { - public interface IVeggies - { - string Name { get; } - } + string Name { get; } } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Mozarella.cs b/FactoryPattern/Abstract Factory/Ingredients/Mozarella.cs index 30d780f..7da20af 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Mozarella.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Mozarella.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class Mozarella : ICheese { - internal class Mozarella : ICheese - { - public string Name => "Mozarella"; - } + public string Name => "Mozarella"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Olive.cs b/FactoryPattern/Abstract Factory/Ingredients/Olive.cs index f0b9d48..bdfcfb6 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Olive.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Olive.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class Olive : IVeggies { - internal class Olive : IVeggies - { - public string Name => "Olives"; - } + public string Name => "Olives"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Onion.cs b/FactoryPattern/Abstract Factory/Ingredients/Onion.cs index c752b3c..3f22c2d 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Onion.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Onion.cs @@ -1,11 +1,10 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class Onion : IVeggies { - internal class Onion : IVeggies + public Onion() { - public Onion() - { - } - - public string Name => "Onions"; } + + public string Name => "Onions"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Parmesan.cs b/FactoryPattern/Abstract Factory/Ingredients/Parmesan.cs index 05b95da..8555bc3 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Parmesan.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Parmesan.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class Parmesan : ICheese { - internal class Parmesan : ICheese - { - public string Name => "Parmesan"; - } + public string Name => "Parmesan"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/Pepper.cs b/FactoryPattern/Abstract Factory/Ingredients/Pepper.cs index 9f1a1e5..9b051b1 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/Pepper.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/Pepper.cs @@ -1,8 +1,7 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class Pepper : IVeggies { - internal class Pepper : IVeggies - { - public string Name => "Bell Peppers"; - } + public string Name => "Bell Peppers"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/PlumTomato.cs b/FactoryPattern/Abstract Factory/Ingredients/PlumTomato.cs index dfa40db..9befcf1 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/PlumTomato.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/PlumTomato.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class PlumTomato : ISauce { - internal class PlumTomato : ISauce - { - public string Name => "Plum Tomato"; - } + public string Name => "Plum Tomato"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/Ingredients/ThinCrust.cs b/FactoryPattern/Abstract Factory/Ingredients/ThinCrust.cs index 5fbe3e6..6cd872a 100644 --- a/FactoryPattern/Abstract Factory/Ingredients/ThinCrust.cs +++ b/FactoryPattern/Abstract Factory/Ingredients/ThinCrust.cs @@ -1,7 +1,6 @@ -namespace FactoryPattern +namespace FactoryPattern; + +internal class ThinCrust : IDough { - internal class ThinCrust : IDough - { - public string Name => "Thin Crust"; - } + public string Name => "Thin Crust"; } \ No newline at end of file diff --git a/FactoryPattern/Abstract Factory/NYIngredientsFactory.cs b/FactoryPattern/Abstract Factory/NYIngredientsFactory.cs index dc0ea1c..1e5ed06 100644 --- a/FactoryPattern/Abstract Factory/NYIngredientsFactory.cs +++ b/FactoryPattern/Abstract Factory/NYIngredientsFactory.cs @@ -1,33 +1,30 @@ -using System.Collections.Generic; +namespace FactoryPattern; -namespace FactoryPattern +internal class NyIngredientsFactory : IIngredientsFactory { - internal class NyIngredientsFactory : IIngredientsFactory + ICheese IIngredientsFactory.CreateCheese() { - ICheese IIngredientsFactory.CreateCheese() - { - return new Mozarella(); - } + return new Mozarella(); + } - IClam IIngredientsFactory.CreateClam() - { - return new FrozenClam(); - } + IClam IIngredientsFactory.CreateClam() + { + return new FrozenClam(); + } - IDough IIngredientsFactory.CreateDough() - { - return new ThinCrust(); - } + IDough IIngredientsFactory.CreateDough() + { + return new ThinCrust(); + } - ISauce IIngredientsFactory.CreateSauce() - { - return new CherryTomato(); - } + ISauce IIngredientsFactory.CreateSauce() + { + return new CherryTomato(); + } - IEnumerable IIngredientsFactory.CreateVeggies() - { - IVeggies[] arr = { new Onion(), new Pepper(), new Olive() }; - return arr; - } + IEnumerable IIngredientsFactory.CreateVeggies() + { + IVeggies[] arr = { new Onion(), new Pepper(), new Olive() }; + return arr; } } \ No newline at end of file diff --git a/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs b/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs index 234bdb0..0af43d9 100644 --- a/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs +++ b/FactoryPattern/Factory Method/ChicagoPizzaFactory.cs @@ -1,33 +1,32 @@ -namespace FactoryPattern +namespace FactoryPattern; + +class ChicagoPizzaFactory : PizzaFactory { - class ChicagoPizzaFactory : PizzaFactory + private readonly string chicagoCheese = "Chicago Cheese"; + private readonly string chicagoClam = "Chicago Clam"; + private readonly string chicagoVeggie = "Chicago Veggie"; + + protected override Pizza Create(PizzaType type) { - private readonly string chicagoCheese = "Chicago Cheese"; - private readonly string chicagoClam = "Chicago Clam"; - private readonly string chicagoVeggie = "Chicago Veggie"; + Pizza pizza; + IIngredientsFactory ingredients = new ChicagoIngredientsFactory(); - protected override Pizza Create(PizzaType type) + if (type == PizzaType.Cheese) { - Pizza pizza; - IIngredientsFactory ingredients = new ChicagoIngredientsFactory(); - - if (type == PizzaType.Cheese) - { - pizza = new CheesePizza(ingredients); - pizza.Name = chicagoCheese; - } - else if (type == PizzaType.Clam) - { - pizza = new ClamPizza(ingredients); - pizza.Name = chicagoClam; - } - else - { - pizza = new VeggiePizza(ingredients); - pizza.Name = chicagoVeggie; - } - pizza.Color = "red"; - return pizza; + pizza = new CheesePizza(ingredients); + pizza.Name = chicagoCheese; + } + else if (type == PizzaType.Clam) + { + pizza = new ClamPizza(ingredients); + pizza.Name = chicagoClam; + } + else + { + pizza = new VeggiePizza(ingredients); + pizza.Name = chicagoVeggie; } + pizza.Color = "red"; + return pizza; } } diff --git a/FactoryPattern/Factory Method/NYPizzaFactory.cs b/FactoryPattern/Factory Method/NYPizzaFactory.cs index de2d0b7..d8bd4e3 100644 --- a/FactoryPattern/Factory Method/NYPizzaFactory.cs +++ b/FactoryPattern/Factory Method/NYPizzaFactory.cs @@ -1,30 +1,29 @@ -namespace FactoryPattern +namespace FactoryPattern; + +class NyPizzaFactory : PizzaFactory { - class NyPizzaFactory : PizzaFactory + 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) { - private readonly string nyStyleCheese = "NY Style Cheese"; - private readonly string nyStyleClam = "NY Style Clam"; - private readonly string nyStyleVeggie = "NY Style Veggie"; + Pizza pizza; + IIngredientsFactory ingredients = new NyIngredientsFactory(); - protected override Pizza Create(PizzaType type) + if (type == PizzaType.Cheese) { - Pizza pizza; - IIngredientsFactory ingredients = new NyIngredientsFactory(); - - if (type == PizzaType.Cheese) - { - pizza = new CheesePizza(ingredients) { Name = nyStyleCheese }; - } - else if (type == PizzaType.Clam) - { - pizza = new ClamPizza(ingredients) { Name = nyStyleClam }; - } - else - { - pizza = new VeggiePizza(ingredients) { Name = nyStyleVeggie }; - } - pizza.Color = "blue"; - return pizza; + pizza = new CheesePizza(ingredients) { Name = nyStyleCheese }; + } + else if (type == PizzaType.Clam) + { + pizza = new ClamPizza(ingredients) { Name = nyStyleClam }; + } + else + { + 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 f0b7a38..ae42713 100644 --- a/FactoryPattern/Factory Method/PizzaFactory.cs +++ b/FactoryPattern/Factory Method/PizzaFactory.cs @@ -1,17 +1,16 @@ -namespace FactoryPattern +namespace FactoryPattern; + +abstract class PizzaFactory { - abstract class PizzaFactory + public Pizza Order(PizzaType type) { - public Pizza Order(PizzaType type) - { - var pizza = Create(type); - pizza.Prepare(); - pizza.Bake(); - pizza.Cut(); - pizza.Box(); - return pizza; - } - - protected abstract Pizza Create(PizzaType type); + var pizza = Create(type); + pizza.Prepare(); + pizza.Bake(); + pizza.Cut(); + pizza.Box(); + return pizza; } + + protected abstract Pizza Create(PizzaType type); } diff --git a/FactoryPattern/Helper.cs b/FactoryPattern/Helper.cs index 4a897e8..ad289f7 100644 --- a/FactoryPattern/Helper.cs +++ b/FactoryPattern/Helper.cs @@ -1,18 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace FactoryPattern; -namespace FactoryPattern +internal class Helper { - internal class Helper - { - } +} - internal enum PizzaType - { - Cheese, - Clam - } +internal enum PizzaType +{ + Cheese, + Clam } diff --git a/FactoryPattern/Pizza/CheesePizza.cs b/FactoryPattern/Pizza/CheesePizza.cs index c7e26b5..ea2e16e 100644 --- a/FactoryPattern/Pizza/CheesePizza.cs +++ b/FactoryPattern/Pizza/CheesePizza.cs @@ -1,25 +1,22 @@ -using System; +namespace FactoryPattern; -namespace FactoryPattern +class CheesePizza : Pizza { - class CheesePizza : Pizza - { - readonly IIngredientsFactory _ingredients; + readonly IIngredientsFactory _ingredients; - public CheesePizza(IIngredientsFactory ing) - { - _ingredients = ing; - } - internal override void Prepare() + public CheesePizza(IIngredientsFactory ing) + { + _ingredients = ing; + } + internal override void Prepare() + { + 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()) { - 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()) - { - Console.Write(val.Name + " "); - } - Console.WriteLine(); + Console.Write(val.Name + " "); } + Console.WriteLine(); } } diff --git a/FactoryPattern/Pizza/ClamPizza.cs b/FactoryPattern/Pizza/ClamPizza.cs index 1879cf9..9e1ca69 100644 --- a/FactoryPattern/Pizza/ClamPizza.cs +++ b/FactoryPattern/Pizza/ClamPizza.cs @@ -1,22 +1,19 @@ -using System; +namespace FactoryPattern; -namespace FactoryPattern +class ClamPizza : Pizza { - class ClamPizza : Pizza - { - readonly IIngredientsFactory _ingredients; + readonly IIngredientsFactory _ingredients; - public ClamPizza(IIngredientsFactory ing) - { - _ingredients = ing; - } + public ClamPizza(IIngredientsFactory ing) + { + _ingredients = ing; + } - internal override void Prepare() - { - Console.WriteLine("Preparing " + Name + " Using"); - Console.Write("Dough: " + _ingredients.CreateDough().Name + ", Clam: " + _ingredients.CreateClam().Name + ", Sauce: " + _ingredients.CreateSauce().Name + ", Cheese: " + _ingredients.CreateCheese().Name); - Console.WriteLine(); + internal override void Prepare() + { + Console.WriteLine("Preparing " + Name + " Using"); + Console.Write("Dough: " + _ingredients.CreateDough().Name + ", Clam: " + _ingredients.CreateClam().Name + ", Sauce: " + _ingredients.CreateSauce().Name + ", Cheese: " + _ingredients.CreateCheese().Name); + Console.WriteLine(); - } } } diff --git a/FactoryPattern/Pizza/Pizza.cs b/FactoryPattern/Pizza/Pizza.cs index e50ec8a..ff29368 100644 --- a/FactoryPattern/Pizza/Pizza.cs +++ b/FactoryPattern/Pizza/Pizza.cs @@ -1,26 +1,23 @@ -using System; +namespace FactoryPattern; -namespace FactoryPattern +abstract class Pizza { - abstract class Pizza - { - public string Color; + public string Color; - internal abstract void Prepare(); - internal void Bake() - { - Console.WriteLine("Baking at 135 degree Celsius for 20 minutes"); - } - internal void Cut() - { - Console.WriteLine("Cutting into diagonal pieces"); - } - internal void Box() - { - Console.WriteLine("Putting pizza in " + Color + " coloured box"); - } + internal abstract void Prepare(); + internal void Bake() + { + Console.WriteLine("Baking at 135 degree Celsius for 20 minutes"); + } + internal void Cut() + { + Console.WriteLine("Cutting into diagonal pieces"); + } + internal void Box() + { + Console.WriteLine("Putting pizza in " + Color + " coloured box"); + } - public string Name { protected get; set; } + public string Name { protected get; set; } - } } diff --git a/FactoryPattern/Pizza/VeggiePizza.cs b/FactoryPattern/Pizza/VeggiePizza.cs index e0796e0..93e0fdb 100644 --- a/FactoryPattern/Pizza/VeggiePizza.cs +++ b/FactoryPattern/Pizza/VeggiePizza.cs @@ -1,25 +1,22 @@ -using System; +namespace FactoryPattern; -namespace FactoryPattern +class VeggiePizza : Pizza { - class VeggiePizza : Pizza - { - readonly IIngredientsFactory _ingredients; + readonly IIngredientsFactory _ingredients; - public VeggiePizza(IIngredientsFactory ing) - { - _ingredients = ing; - } - internal override void Prepare() + public VeggiePizza(IIngredientsFactory ing) + { + _ingredients = ing; + } + internal override void Prepare() + { + 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()) { - 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()) - { - Console.Write(val.Name + " "); - } - Console.WriteLine(); + Console.Write(val.Name + " "); } + Console.WriteLine(); } } diff --git a/FactoryPattern/Program.cs b/FactoryPattern/Program.cs index 0612463..7511dfc 100644 --- a/FactoryPattern/Program.cs +++ b/FactoryPattern/Program.cs @@ -1,18 +1,15 @@ -using System; +namespace FactoryPattern; -namespace FactoryPattern +static class Program { - static class Program + static void Main() { - static void Main() - { - Console.WriteLine("Yankees fan orders:"); - var yankees = new NyPizzaFactory(); - yankees.Order(PizzaType.Cheese); - Console.WriteLine(); - Console.WriteLine("Cubs fan orders:"); - var cubs = new ChicagoPizzaFactory(); - cubs.Order(PizzaType.Clam); - } + Console.WriteLine("Yankees fan orders:"); + var yankees = new NyPizzaFactory(); + yankees.Order(PizzaType.Cheese); + Console.WriteLine(); + Console.WriteLine("Cubs fan orders:"); + var cubs = new ChicagoPizzaFactory(); + cubs.Order(PizzaType.Clam); } } \ No newline at end of file diff --git a/FlyweightPattern/BeverageFlyweightFactory.cs b/FlyweightPattern/BeverageFlyweightFactory.cs index 2863293..8e2a457 100644 --- a/FlyweightPattern/BeverageFlyweightFactory.cs +++ b/FlyweightPattern/BeverageFlyweightFactory.cs @@ -1,46 +1,42 @@ -using System; -using System.Collections.Generic; +namespace FlyweightPattern; -namespace FlyweightPattern +public class BeverageFlyweightFactory { - public class BeverageFlyweightFactory - { - private readonly Dictionary _beverages; + private readonly Dictionary _beverages; - public BeverageFlyweightFactory() - { - _beverages = new Dictionary(); - } + public BeverageFlyweightFactory() + { + _beverages = new Dictionary(); + } - public IBeverage MakeBeverage(BeverageType type) + public IBeverage MakeBeverage(BeverageType type) + { + _beverages.TryGetValue(type, out var beverage); + if (beverage == null) { - _beverages.TryGetValue(type, out var beverage); - if (beverage == null) + switch (type) { - switch (type) - { - case BeverageType.BubbleMilk: - beverage = new BubbleMilkTea(); - _beverages.Add(BeverageType.BubbleMilk, beverage); - break; - case BeverageType.FoamMilk: - beverage = new FoamMilkTea(); - _beverages.Add(BeverageType.FoamMilk, beverage); - break; - case BeverageType.OolongMilk: - beverage = new OolingMilkTea(); - _beverages.Add(BeverageType.OolongMilk, beverage); - break; - case BeverageType.CoconutMilk: - beverage = new CoconutMilkTea(); - _beverages.Add(BeverageType.CoconutMilk, beverage); - break; - default: - throw new ArgumentOutOfRangeException(nameof(type), type, null); - } + case BeverageType.BubbleMilk: + beverage = new BubbleMilkTea(); + _beverages.Add(BeverageType.BubbleMilk, beverage); + break; + case BeverageType.FoamMilk: + beverage = new FoamMilkTea(); + _beverages.Add(BeverageType.FoamMilk, beverage); + break; + case BeverageType.OolongMilk: + beverage = new OolingMilkTea(); + _beverages.Add(BeverageType.OolongMilk, beverage); + break; + case BeverageType.CoconutMilk: + beverage = new CoconutMilkTea(); + _beverages.Add(BeverageType.CoconutMilk, beverage); + break; + default: + throw new ArgumentOutOfRangeException(nameof(type), type, null); } - - return beverage; } + + return beverage; } } \ No newline at end of file diff --git a/FlyweightPattern/BeverageType.cs b/FlyweightPattern/BeverageType.cs index 4d37a0d..3e84678 100644 --- a/FlyweightPattern/BeverageType.cs +++ b/FlyweightPattern/BeverageType.cs @@ -1,7 +1,6 @@ -namespace FlyweightPattern +namespace FlyweightPattern; + +public enum BeverageType { - public enum BeverageType - { - BubbleMilk, FoamMilk, OolongMilk, CoconutMilk - } + BubbleMilk, FoamMilk, OolongMilk, CoconutMilk } \ No newline at end of file diff --git a/FlyweightPattern/BubbleMilkTea.cs b/FlyweightPattern/BubbleMilkTea.cs index c58ab9b..f052d8a 100644 --- a/FlyweightPattern/BubbleMilkTea.cs +++ b/FlyweightPattern/BubbleMilkTea.cs @@ -1,17 +1,14 @@ -using System; +namespace FlyweightPattern; -namespace FlyweightPattern +public class BubbleMilkTea : IBeverage { - public class BubbleMilkTea : IBeverage + public BubbleMilkTea() { - public BubbleMilkTea() - { - Console.WriteLine("Initializing a Bubble Milk Tea instance"); - } + Console.WriteLine("Initializing a Bubble Milk Tea instance"); + } - public void Drink() - { - Console.WriteLine("hmmm... this is bubble milk tea"); - } + public void Drink() + { + Console.WriteLine("hmmm... this is bubble milk tea"); } } \ No newline at end of file diff --git a/FlyweightPattern/BubbleTeaShop.cs b/FlyweightPattern/BubbleTeaShop.cs index e6bcbae..b39e57e 100644 --- a/FlyweightPattern/BubbleTeaShop.cs +++ b/FlyweightPattern/BubbleTeaShop.cs @@ -1,37 +1,33 @@ -using System; -using System.Collections.Generic; +namespace FlyweightPattern; -namespace FlyweightPattern +public class BubbleTeaShop { - public class BubbleTeaShop - { - private List takeAwayOrders; + private List takeAwayOrders; - public BubbleTeaShop() - { - takeAwayOrders = new List(); - TakeOrders(); - } + public BubbleTeaShop() + { + takeAwayOrders = new List(); + TakeOrders(); + } - private void TakeOrders() - { - var factory = new BeverageFlyweightFactory(); + 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)); - takeAwayOrders.Add(factory.MakeBeverage(BeverageType.FoamMilk)); - takeAwayOrders.Add(factory.MakeBeverage(BeverageType.OolongMilk)); - takeAwayOrders.Add(factory.MakeBeverage(BeverageType.OolongMilk)); - } + takeAwayOrders.Add(factory.MakeBeverage(BeverageType.BubbleMilk)); + takeAwayOrders.Add(factory.MakeBeverage(BeverageType.BubbleMilk)); + takeAwayOrders.Add(factory.MakeBeverage(BeverageType.CoconutMilk)); + takeAwayOrders.Add(factory.MakeBeverage(BeverageType.FoamMilk)); + takeAwayOrders.Add(factory.MakeBeverage(BeverageType.OolongMilk)); + takeAwayOrders.Add(factory.MakeBeverage(BeverageType.OolongMilk)); + } - public void Enumerate() + public void Enumerate() + { + Console.WriteLine("Enumerating take away orders\n"); + foreach (var beverage in takeAwayOrders) { - Console.WriteLine("Enumerating take away orders\n"); - foreach (var beverage in takeAwayOrders) - { - beverage.Drink(); - } + beverage.Drink(); } } } \ No newline at end of file diff --git a/FlyweightPattern/CoconutMilkTea.cs b/FlyweightPattern/CoconutMilkTea.cs index f96035b..82018a5 100644 --- a/FlyweightPattern/CoconutMilkTea.cs +++ b/FlyweightPattern/CoconutMilkTea.cs @@ -1,17 +1,14 @@ -using System; +namespace FlyweightPattern; -namespace FlyweightPattern +public class CoconutMilkTea : IBeverage { - public class CoconutMilkTea : IBeverage + public CoconutMilkTea() { - public CoconutMilkTea() - { - Console.WriteLine("Initializing a Coconut Milk Tea instance"); - } + Console.WriteLine("Initializing a Coconut Milk Tea instance"); + } - public void Drink() - { - Console.WriteLine("hmmm... this is coconut milk tea"); - } + public void Drink() + { + Console.WriteLine("hmmm... this is coconut milk tea"); } } \ No newline at end of file diff --git a/FlyweightPattern/FoamMilkTea.cs b/FlyweightPattern/FoamMilkTea.cs index 958b9d7..037a54d 100644 --- a/FlyweightPattern/FoamMilkTea.cs +++ b/FlyweightPattern/FoamMilkTea.cs @@ -1,18 +1,15 @@ -using System; +namespace FlyweightPattern; -namespace FlyweightPattern +public class FoamMilkTea : IBeverage { - public class FoamMilkTea : IBeverage - { - public FoamMilkTea() - { - Console.WriteLine("Initializing a Foam Milk Tea instance"); - } + public FoamMilkTea() + { + Console.WriteLine("Initializing a Foam Milk Tea instance"); + } - public void Drink() - { - Console.WriteLine("hmmm... this is foam milk tea"); - } + public void Drink() + { + Console.WriteLine("hmmm... this is foam milk tea"); } } \ No newline at end of file diff --git a/FlyweightPattern/IBeverage.cs b/FlyweightPattern/IBeverage.cs index 0a271d2..40ae06f 100644 --- a/FlyweightPattern/IBeverage.cs +++ b/FlyweightPattern/IBeverage.cs @@ -1,7 +1,6 @@ -namespace FlyweightPattern +namespace FlyweightPattern; + +public interface IBeverage { - public interface IBeverage - { - void Drink(); - } + void Drink(); } \ No newline at end of file diff --git a/FlyweightPattern/OolingMilkTea.cs b/FlyweightPattern/OolingMilkTea.cs index 40a3715..8584795 100644 --- a/FlyweightPattern/OolingMilkTea.cs +++ b/FlyweightPattern/OolingMilkTea.cs @@ -1,18 +1,15 @@ -using System; +namespace FlyweightPattern; -namespace FlyweightPattern +public class OolingMilkTea : IBeverage { - public class OolingMilkTea : IBeverage - { - public OolingMilkTea() - { - Console.WriteLine("Initializing an Oolong Milk Tea instance"); - } + public OolingMilkTea() + { + Console.WriteLine("Initializing an Oolong Milk Tea instance"); + } - public void Drink() - { - Console.WriteLine("hmmm... this is oolong milk tea"); - } + public void Drink() + { + Console.WriteLine("hmmm... this is oolong milk tea"); } } \ No newline at end of file diff --git a/FlyweightPattern/Program.cs b/FlyweightPattern/Program.cs index 462bab7..a8dfb0c 100644 --- a/FlyweightPattern/Program.cs +++ b/FlyweightPattern/Program.cs @@ -1,11 +1,10 @@ -namespace FlyweightPattern +namespace FlyweightPattern; + +static class Program { - static class Program + private static void Main() { - private static void Main() - { - var teaShop = new BubbleTeaShop(); - teaShop.Enumerate(); - } + var teaShop = new BubbleTeaShop(); + teaShop.Enumerate(); } } \ No newline at end of file diff --git a/IteratorPattern/BreakfastMenu.cs b/IteratorPattern/BreakfastMenu.cs index f4b5073..eea75cf 100644 --- a/IteratorPattern/BreakfastMenu.cs +++ b/IteratorPattern/BreakfastMenu.cs @@ -1,35 +1,34 @@ using System.Collections; -namespace IteratorPattern +namespace IteratorPattern; + +public class BreakfastMenu { - public class BreakfastMenu - { - private ArrayList _items; + private ArrayList _items; - public IEnumerable Items + public IEnumerable Items + { + get { - get - { - return new BreakfastMenuIterator(_items); - } + return new BreakfastMenuIterator(_items); } + } - public BreakfastMenu() - { - _items = new ArrayList(); + public BreakfastMenu() + { + _items = new ArrayList(); - 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("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); + } - private void AddItem(string name, string description, int price, bool veg) - { - var item = new Menu(name, description, price, veg); - _items.Add(item); - } + private void AddItem(string name, string description, int price, bool 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 683cd3c..67ca8da 100644 --- a/IteratorPattern/BreakfastMenuEnum.cs +++ b/IteratorPattern/BreakfastMenuEnum.cs @@ -1,48 +1,46 @@ -using System; -using System.Collections; +using System.Collections; -namespace IteratorPattern +namespace IteratorPattern; + +public class BreakfastMenuEnum : IEnumerator { - public class BreakfastMenuEnum : IEnumerator - { - private readonly ArrayList _items; - private int _position = -1; + private readonly ArrayList _items; + private int _position = -1; - public BreakfastMenuEnum(ArrayList items) - { - _items = items; - } + public BreakfastMenuEnum(ArrayList items) + { + _items = items; + } - public void Dispose() - { - throw new System.NotImplementedException(); - } + public void Dispose() + { + throw new System.NotImplementedException(); + } - public bool MoveNext() - { - _position++; - return (_position < _items.Count); - } + public bool MoveNext() + { + _position++; + return (_position < _items.Count); + } - public void Reset() - { - _position = -1; - } + public void Reset() + { + _position = -1; + } - object IEnumerator.Current => Current; + object IEnumerator.Current => Current; - public Menu Current + public Menu Current + { + get { - get + try + { + return (Menu)_items[_position]; + } + catch (IndexOutOfRangeException) { - try - { - return (Menu)_items[_position]; - } - catch (IndexOutOfRangeException) - { - throw new InvalidOperationException(); - } + throw new InvalidOperationException(); } } } diff --git a/IteratorPattern/BreakfastMenuIterator.cs b/IteratorPattern/BreakfastMenuIterator.cs index 68bd86e..8f29912 100644 --- a/IteratorPattern/BreakfastMenuIterator.cs +++ b/IteratorPattern/BreakfastMenuIterator.cs @@ -1,25 +1,24 @@ using System.Collections; -namespace IteratorPattern +namespace IteratorPattern; + +class BreakfastMenuIterator : IEnumerable { - class BreakfastMenuIterator : IEnumerable - { - private int _count = 0; - private ArrayList _items; + private int _count = 0; + private ArrayList _items; - public BreakfastMenuIterator(ArrayList items) - { - _items = items; - } + public BreakfastMenuIterator(ArrayList items) + { + _items = items; + } - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } - public IEnumerator GetEnumerator() - { - return new BreakfastMenuEnum(_items); - } + public IEnumerator GetEnumerator() + { + return new BreakfastMenuEnum(_items); } } diff --git a/IteratorPattern/Client.cs b/IteratorPattern/Client.cs index 27a1a5b..e9a61a8 100644 --- a/IteratorPattern/Client.cs +++ b/IteratorPattern/Client.cs @@ -1,35 +1,33 @@ -using System; -using System.Collections; +using System.Collections; -namespace IteratorPattern +namespace IteratorPattern; + +public class Client { - public class Client - { - private IEnumerable _breakfast; - private IEnumerable _dinner; + private IEnumerable _breakfast; + private IEnumerable _dinner; - public Client(BreakfastMenu breakfast, DinnerMenu dinner) - { - this._breakfast = breakfast.Items; - this._dinner = dinner.Items; - } + public Client(BreakfastMenu breakfast, DinnerMenu dinner) + { + this._breakfast = breakfast.Items; + this._dinner = dinner.Items; + } - public void PrintMenu() - { - var breakfast = _breakfast; - PrintMenu(breakfast); - var dinner = _dinner; - PrintMenu(dinner); - } + public void PrintMenu() + { + var breakfast = _breakfast; + PrintMenu(breakfast); + var dinner = _dinner; + PrintMenu(dinner); + } - private void PrintMenu(IEnumerable iter) + private void PrintMenu(IEnumerable iter) + { + foreach (var item in iter) { - foreach (var item in iter) - { - var i = (Menu)item; - Console.WriteLine($"{i.Name} Rs. {i.Price} { (i.Vegetarian ? "*" : "x") } \n {i.Description} "); + var i = (Menu)item; + Console.WriteLine($"{i.Name} Rs. {i.Price} {(i.Vegetarian ? "*" : "x")} \n {i.Description} "); - } } } } \ No newline at end of file diff --git a/IteratorPattern/DinnerMenu.cs b/IteratorPattern/DinnerMenu.cs index 877fcc7..c5e5d25 100644 --- a/IteratorPattern/DinnerMenu.cs +++ b/IteratorPattern/DinnerMenu.cs @@ -1,44 +1,42 @@ -using System; -using System.Collections; +using System.Collections; -namespace IteratorPattern +namespace IteratorPattern; + +public class DinnerMenu { - public class DinnerMenu - { - private const int Max = 1; + private const int Max = 1; - private int _count; - private Menu[] _items; + private int _count; + private Menu[] _items; - public IEnumerable Items + public IEnumerable Items + { + get { - get - { - return new DinnerMenuIterator(_items); - } + return new DinnerMenuIterator(_items); } + } - public DinnerMenu() - { - _items = new Menu[Max]; + public DinnerMenu() + { + _items = new Menu[Max]; - AddItems("Hamburger", "Hamburger with cheese and onions", 160, false); + AddItems("Hamburger", "Hamburger with cheese and onions", 160, false); - } + } + + private void AddItems(string name, string description, int price, bool veg) + { + var item = new Menu(name, description, price, veg); - private void AddItems(string name, string description, int price, bool veg) + if (_count <= Max) + { + _items[_count] = item; + _count++; + } + else { - var item = new Menu(name, description, price, veg); - - if (_count <= Max) - { - _items[_count] = item; - _count++; - } - else - { - throw new IndexOutOfRangeException(); - } + throw new IndexOutOfRangeException(); } } } \ No newline at end of file diff --git a/IteratorPattern/DinnerMenuEnum.cs b/IteratorPattern/DinnerMenuEnum.cs index 68929eb..7185f38 100644 --- a/IteratorPattern/DinnerMenuEnum.cs +++ b/IteratorPattern/DinnerMenuEnum.cs @@ -1,47 +1,45 @@ -using System; -using System.Collections; +using System.Collections; -namespace IteratorPattern +namespace IteratorPattern; + +public class DinnerMenuEnum : IEnumerator { - public class DinnerMenuEnum : IEnumerator + private readonly Menu[] _items; + private int _position = -1; + public DinnerMenuEnum(Menu[] items) { - private readonly Menu[] _items; - private int _position = -1; - public DinnerMenuEnum(Menu[] items) - { - _items = items; - } + _items = items; + } - public void Dispose() - { - throw new System.NotImplementedException(); - } + public void Dispose() + { + throw new System.NotImplementedException(); + } - public bool MoveNext() - { - _position++; - return (_position < _items.Length); - } + public bool MoveNext() + { + _position++; + return (_position < _items.Length); + } - public void Reset() - { - _position = -1; - } + public void Reset() + { + _position = -1; + } - object IEnumerator.Current => Current; + object IEnumerator.Current => Current; - public Menu Current + public Menu Current + { + get { - get + try + { + return _items[_position]; + } + catch (IndexOutOfRangeException) { - try - { - return _items[_position]; - } - catch (IndexOutOfRangeException) - { - throw new InvalidOperationException(); - } + throw new InvalidOperationException(); } } } diff --git a/IteratorPattern/DinnerMenuIterator.cs b/IteratorPattern/DinnerMenuIterator.cs index c4dca48..8339732 100644 --- a/IteratorPattern/DinnerMenuIterator.cs +++ b/IteratorPattern/DinnerMenuIterator.cs @@ -1,25 +1,24 @@ using System.Collections; -namespace IteratorPattern +namespace IteratorPattern; + +class DinnerMenuIterator : IEnumerable { - class DinnerMenuIterator : IEnumerable - { - private int _count = 0; - private Menu[] _items; + private int _count = 0; + private Menu[] _items; - public DinnerMenuIterator(Menu[] items) - { - _items = items; - } + public DinnerMenuIterator(Menu[] items) + { + _items = items; + } - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } - public IEnumerator GetEnumerator() - { - return new DinnerMenuEnum(_items); - } + public IEnumerator GetEnumerator() + { + return new DinnerMenuEnum(_items); } } diff --git a/IteratorPattern/Menu.cs b/IteratorPattern/Menu.cs index 984f85f..dbf6455 100644 --- a/IteratorPattern/Menu.cs +++ b/IteratorPattern/Menu.cs @@ -1,19 +1,18 @@ -namespace IteratorPattern -{ - public class Menu - { - public string Name { get; } - public string Description { get; } - public bool Vegetarian { get; } - public double Price { get; } +namespace IteratorPattern; - public Menu(string name, string description, double price, bool vegetarian) - { - Name = name; - Description = description; - Price = price; - Vegetarian = vegetarian; - } +public class Menu +{ + public string Name { get; } + public string Description { get; } + public bool Vegetarian { get; } + public double Price { get; } + public Menu(string name, string description, double price, bool vegetarian) + { + Name = name; + Description = description; + Price = price; + Vegetarian = vegetarian; } + } \ No newline at end of file diff --git a/IteratorPattern/Program.cs b/IteratorPattern/Program.cs index dae6a70..f08c59f 100644 --- a/IteratorPattern/Program.cs +++ b/IteratorPattern/Program.cs @@ -1,13 +1,12 @@ -namespace IteratorPattern +namespace IteratorPattern; + +static class Program { - static class Program + private static void Main() { - private static void Main() - { - var breakfast = new BreakfastMenu(); - var dinner = new DinnerMenu(); - var waiter = new Client(breakfast, dinner); - waiter.PrintMenu(); - } + var breakfast = new BreakfastMenu(); + var dinner = new DinnerMenu(); + var waiter = new Client(breakfast, dinner); + waiter.PrintMenu(); } } diff --git a/MediatorPattern/Colleague.cs b/MediatorPattern/Colleague.cs index 78c694a..2a1c515 100644 --- a/MediatorPattern/Colleague.cs +++ b/MediatorPattern/Colleague.cs @@ -1,14 +1,13 @@ -namespace MediatorPattern +namespace MediatorPattern; + +abstract class Colleague { - abstract class Colleague - { - protected Mediator mediator; + protected Mediator mediator; - public Colleague(Mediator mediator) => this.mediator = mediator; + public Colleague(Mediator mediator) => this.mediator = mediator; - public virtual void Send(string message) => this.mediator.Send(message, this); + public virtual void Send(string message) => this.mediator.Send(message, this); - public abstract void Notify(string message); + public abstract void Notify(string message); - } } \ No newline at end of file diff --git a/MediatorPattern/Customer.cs b/MediatorPattern/Customer.cs index b3bf289..f03b1c9 100644 --- a/MediatorPattern/Customer.cs +++ b/MediatorPattern/Customer.cs @@ -1,14 +1,11 @@ -using System; +namespace MediatorPattern; -namespace MediatorPattern +class Customer : Colleague { - class Customer : Colleague - { - public Customer(Mediator mediator) : base(mediator) { } + public Customer(Mediator mediator) : base(mediator) { } - public override void Notify(string message) - { - Console.WriteLine($"Message to customer: {message}"); - } + public override void Notify(string message) + { + Console.WriteLine($"Message to customer: {message}"); } } \ No newline at end of file diff --git a/MediatorPattern/ManagerMediator.cs b/MediatorPattern/ManagerMediator.cs index ceda018..755f90b 100644 --- a/MediatorPattern/ManagerMediator.cs +++ b/MediatorPattern/ManagerMediator.cs @@ -1,22 +1,21 @@ -namespace MediatorPattern +namespace MediatorPattern; + +class ManagerMediator : Mediator { - class ManagerMediator : Mediator - { - public Colleague Customer { get; set; } - public Colleague Programmer { get; set; } - public Colleague Tester { get; set; } + public Colleague Customer { get; set; } + public Colleague Programmer { get; set; } + public Colleague Tester { get; set; } - public override void Send(string message, Colleague colleague) + public override void Send(string message, Colleague colleague) + { + if (colleague == Customer) + { + Programmer.Notify(message); + } + else if (colleague == Programmer) { - if (colleague == Customer) - { - Programmer.Notify(message); - } - else if (colleague == Programmer) - { - Tester.Notify(message); - } - else Customer.Notify(message); + Tester.Notify(message); } + else Customer.Notify(message); } } \ No newline at end of file diff --git a/MediatorPattern/Mediator.cs b/MediatorPattern/Mediator.cs index 2646a13..7b71dc2 100644 --- a/MediatorPattern/Mediator.cs +++ b/MediatorPattern/Mediator.cs @@ -1,7 +1,6 @@ -namespace MediatorPattern +namespace MediatorPattern; + +abstract class Mediator { - abstract class Mediator - { - public abstract void Send(string message, Colleague colleague); - } + public abstract void Send(string message, Colleague colleague); } \ No newline at end of file diff --git a/MediatorPattern/Program.cs b/MediatorPattern/Program.cs index bbdddc5..055228c 100644 --- a/MediatorPattern/Program.cs +++ b/MediatorPattern/Program.cs @@ -1,19 +1,18 @@ -namespace MediatorPattern +namespace MediatorPattern; + +class Program { - class Program + static void Main(string[] args) { - static void Main(string[] args) - { - var mediator = new ManagerMediator(); - var customer = new Customer(mediator); - var programmer = new Programmer(mediator); - var tester = new Tester(mediator); - mediator.Customer = customer; - mediator.Programmer = programmer; - mediator.Tester = tester; - customer.Send("We have an order, need to make a program"); - programmer.Send("I have done program, need to test it"); - tester.Send("I have done testing, here is ready program for you"); - } + var mediator = new ManagerMediator(); + var customer = new Customer(mediator); + var programmer = new Programmer(mediator); + var tester = new Tester(mediator); + mediator.Customer = customer; + mediator.Programmer = programmer; + mediator.Tester = tester; + customer.Send("We have an order, need to make a program"); + programmer.Send("I have done program, need to test it"); + tester.Send("I have done testing, here is ready program for you"); } } \ No newline at end of file diff --git a/MediatorPattern/Programmer.cs b/MediatorPattern/Programmer.cs index cef0b16..7a33e70 100644 --- a/MediatorPattern/Programmer.cs +++ b/MediatorPattern/Programmer.cs @@ -1,14 +1,11 @@ -using System; +namespace MediatorPattern; -namespace MediatorPattern +class Programmer : Colleague { - class Programmer : Colleague - { - public Programmer(Mediator mediator) : base(mediator) { } + public Programmer(Mediator mediator) : base(mediator) { } - public override void Notify(string message) - { - Console.WriteLine($"Message to programmer: {message}"); - } + public override void Notify(string message) + { + Console.WriteLine($"Message to programmer: {message}"); } } \ No newline at end of file diff --git a/MediatorPattern/Tester.cs b/MediatorPattern/Tester.cs index 0fe71d7..8792071 100644 --- a/MediatorPattern/Tester.cs +++ b/MediatorPattern/Tester.cs @@ -1,14 +1,11 @@ -using System; +namespace MediatorPattern; -namespace MediatorPattern +class Tester : Colleague { - class Tester : Colleague - { - public Tester(Mediator mediator) : base(mediator) { } + public Tester(Mediator mediator) : base(mediator) { } - public override void Notify(string message) - { - Console.WriteLine($"Message to tester: {message}"); - } + public override void Notify(string message) + { + Console.WriteLine($"Message to tester: {message}"); } } \ No newline at end of file diff --git a/ObserverPattern/Program.cs b/ObserverPattern/Program.cs index 5982418..5a8e526 100644 --- a/ObserverPattern/Program.cs +++ b/ObserverPattern/Program.cs @@ -1,19 +1,18 @@ -namespace ObserverPattern +namespace ObserverPattern; + +static class Program { - static class Program + static void Main() { - static void Main() - { - var provider = new WeatherSupplier(); - var observer1 = new WeatherMonitor("TP"); - var observer2 = new WeatherMonitor("H"); - provider.WeatherConditions(32.0, 0.05, 1.5); - observer1.Subscribe(provider); - provider.WeatherConditions(33.5, 0.04, 1.7); - observer2.Subscribe(provider); - provider.WeatherConditions(37.5, 0.07, 1.2); + var provider = new WeatherSupplier(); + var observer1 = new WeatherMonitor("TP"); + var observer2 = new WeatherMonitor("H"); + provider.WeatherConditions(32.0, 0.05, 1.5); + observer1.Subscribe(provider); + provider.WeatherConditions(33.5, 0.04, 1.7); + observer2.Subscribe(provider); + provider.WeatherConditions(37.5, 0.07, 1.2); - } } } diff --git a/ObserverPattern/Unsubscriber.cs b/ObserverPattern/Unsubscriber.cs index cdd5f2a..93637d9 100644 --- a/ObserverPattern/Unsubscriber.cs +++ b/ObserverPattern/Unsubscriber.cs @@ -1,23 +1,19 @@ -using System; -using System.Collections.Generic; +namespace ObserverPattern; -namespace ObserverPattern +internal class Unsubscriber : IDisposable { - internal class Unsubscriber : IDisposable - { - private readonly List> _observers; - private readonly IObserver _observer; + private readonly List> _observers; + private readonly IObserver _observer; - internal Unsubscriber(List> observers, IObserver observer) - { - _observers = observers; - _observer = observer; - } + internal Unsubscriber(List> observers, IObserver observer) + { + _observers = observers; + _observer = observer; + } - public void Dispose() - { - if (_observers.Contains(_observer)) - _observers.Remove(_observer); - } + public void Dispose() + { + if (_observers.Contains(_observer)) + _observers.Remove(_observer); } } diff --git a/ObserverPattern/Weather.cs b/ObserverPattern/Weather.cs index 8b214bd..ef4fe2c 100644 --- a/ObserverPattern/Weather.cs +++ b/ObserverPattern/Weather.cs @@ -1,18 +1,17 @@ -namespace ObserverPattern +namespace ObserverPattern; + +class Weather { - class Weather - { - public double Pressure { get; } + public double Pressure { get; } - public double Humidity { get; } + public double Humidity { get; } - public double Temperature { get; } + public double Temperature { get; } - public Weather(double humd, double pres, double temp) - { - Temperature = temp; - Pressure = pres; - Humidity = humd; - } + public Weather(double humd, double pres, double temp) + { + Temperature = temp; + Pressure = pres; + Humidity = humd; } } diff --git a/ObserverPattern/WeatherMonitor.cs b/ObserverPattern/WeatherMonitor.cs index 6d65f37..cf3a395 100644 --- a/ObserverPattern/WeatherMonitor.cs +++ b/ObserverPattern/WeatherMonitor.cs @@ -1,61 +1,58 @@ -using System; +namespace ObserverPattern; -namespace ObserverPattern +sealed class WeatherMonitor : IObserver { - sealed class WeatherMonitor : IObserver + private IDisposable _cancellation; + private readonly string _name; + + public void Subscribe(WeatherSupplier provider) { - private IDisposable _cancellation; - private readonly string _name; + _cancellation = provider.Subscribe(this); + } - public void Subscribe(WeatherSupplier provider) - { - _cancellation = provider.Subscribe(this); - } + public void Unsubscribe() + { + _cancellation.Dispose(); + } - public void Unsubscribe() - { - _cancellation.Dispose(); - } + public WeatherMonitor(string name) + { + _name = name; + } - public WeatherMonitor(string name) + public void OnCompleted() + { + throw new NotImplementedException(); + } + + public void OnError(Exception error) + { + Console.WriteLine("Error has occured"); + } + + public void OnNext(Weather value) + { + Console.WriteLine(_name); + if (_name.Contains("T")) { - _name = name; - } + string op = $"| Temperature : {value.Temperature} Celsius |"; + Console.Write(op); - public void OnCompleted() + } + if (_name.Contains("P")) { - throw new NotImplementedException(); + string op = $"| Pressure : {value.Pressure} atm |"; + Console.Write(op); } - - public void OnError(Exception error) + if (_name.Contains("H")) { - Console.WriteLine("Error has occured"); + string op = $"| Humidity : {value.Humidity * 100} % |"; + Console.Write(op); } - - public void OnNext(Weather value) + if (!(_name.Contains("T") || _name.Contains("P") || _name.Contains("H"))) { - Console.WriteLine(_name); - if (_name.Contains("T")) - { - string op = $"| Temperature : {value.Temperature} Celsius |"; - Console.Write(op); - - } - if (_name.Contains("P")) - { - string op = $"| Pressure : {value.Pressure} atm |"; - Console.Write(op); - } - if (_name.Contains("H")) - { - string op = $"| Humidity : {value.Humidity * 100} % |"; - Console.Write(op); - } - if (!(_name.Contains("T") || _name.Contains("P") || _name.Contains("H"))) - { - OnError(new Exception()); - } - Console.WriteLine(); + OnError(new Exception()); } + Console.WriteLine(); } } diff --git a/ObserverPattern/WeatherSupplier.cs b/ObserverPattern/WeatherSupplier.cs index 92ed8c1..0c3a78e 100644 --- a/ObserverPattern/WeatherSupplier.cs +++ b/ObserverPattern/WeatherSupplier.cs @@ -1,42 +1,38 @@ -using System; -using System.Collections.Generic; +namespace ObserverPattern; -namespace ObserverPattern +class WeatherSupplier : IObservable { - class WeatherSupplier : IObservable - { - private readonly List> _observers; - private List Screens { get; } + private readonly List> _observers; + private List Screens { get; } - private List GetScreens() - { - return Screens; - } + private List GetScreens() + { + return Screens; + } - public WeatherSupplier() - { - _observers = new List>(); - Screens = new List(); - } + public WeatherSupplier() + { + _observers = new List>(); + Screens = new List(); + } - public IDisposable Subscribe(IObserver observer) + public IDisposable Subscribe(IObserver observer) + { + if (!_observers.Contains(observer)) { - if (!_observers.Contains(observer)) + _observers.Add(observer); + foreach (var item in GetScreens()) { - _observers.Add(observer); - foreach (var item in GetScreens()) - { - observer.OnNext(item); - } + observer.OnNext(item); } - return new Unsubscriber(_observers, observer); } + return new Unsubscriber(_observers, observer); + } - public void WeatherConditions(double temp = 0, double humd = 0, double pres = 0) - { - var conditions = new Weather(humd, pres, temp); - foreach (var item in _observers) - item.OnNext(conditions); - } + public void WeatherConditions(double temp = 0, double humd = 0, double pres = 0) + { + var conditions = new Weather(humd, pres, temp); + foreach (var item in _observers) + item.OnNext(conditions); } } diff --git a/PrototypePattern/Circle.cs b/PrototypePattern/Circle.cs index 6289f77..b2ca133 100644 --- a/PrototypePattern/Circle.cs +++ b/PrototypePattern/Circle.cs @@ -1,22 +1,19 @@ -using System; +namespace PrototypePattern; -namespace PrototypePattern +class Circle : IFigure { - class Circle : IFigure + readonly int _radius; + public Circle(int r) { - readonly int _radius; - public Circle(int r) - { - _radius = r; - } + _radius = r; + } - public object Clone() - { - return new Circle(_radius); - } - public void GetInfo() - { - Console.WriteLine($"Circle with radius {_radius}"); - } + public object Clone() + { + return new Circle(_radius); + } + public void GetInfo() + { + Console.WriteLine($"Circle with radius {_radius}"); } } \ No newline at end of file diff --git a/PrototypePattern/IFigure.cs b/PrototypePattern/IFigure.cs index 85879a4..98c6a56 100644 --- a/PrototypePattern/IFigure.cs +++ b/PrototypePattern/IFigure.cs @@ -1,9 +1,6 @@ -using System; +namespace PrototypePattern; -namespace PrototypePattern +interface IFigure : ICloneable { - interface IFigure : ICloneable - { - void GetInfo(); - } + void GetInfo(); } \ No newline at end of file diff --git a/PrototypePattern/Program.cs b/PrototypePattern/Program.cs index ebdecdb..31982a6 100644 --- a/PrototypePattern/Program.cs +++ b/PrototypePattern/Program.cs @@ -1,22 +1,19 @@ -using System; +namespace PrototypePattern; -namespace PrototypePattern +class Program { - class Program + static void Main(string[] args) { - static void Main(string[] args) - { - IFigure figure = new Rectangle(30, 40); - IFigure clonedFigure = (IFigure)figure.Clone(); - figure.GetInfo(); - clonedFigure.GetInfo(); + IFigure figure = new Rectangle(30, 40); + IFigure clonedFigure = (IFigure)figure.Clone(); + figure.GetInfo(); + clonedFigure.GetInfo(); - figure = new Circle(30); - clonedFigure = (IFigure)figure.Clone(); - figure.GetInfo(); - clonedFigure.GetInfo(); + figure = new Circle(30); + clonedFigure = (IFigure)figure.Clone(); + figure.GetInfo(); + clonedFigure.GetInfo(); - Console.Read(); - } + Console.Read(); } } diff --git a/PrototypePattern/Rectangle.cs b/PrototypePattern/Rectangle.cs index 9856cf2..7fd33bb 100644 --- a/PrototypePattern/Rectangle.cs +++ b/PrototypePattern/Rectangle.cs @@ -1,24 +1,21 @@ -using System; +namespace PrototypePattern; -namespace PrototypePattern +class Rectangle : IFigure { - class Rectangle : IFigure + readonly int _width; + readonly int _height; + public Rectangle(int w, int h) { - readonly int _width; - readonly int _height; - public Rectangle(int w, int h) - { - _width = w; - _height = h; - } + _width = w; + _height = h; + } - public object Clone() - { - return new Rectangle(_width, _height); - } - public void GetInfo() - { - Console.WriteLine($"Rectangle height {_height} and width {_width}"); - } + public object Clone() + { + return new Rectangle(_width, _height); + } + public void GetInfo() + { + Console.WriteLine($"Rectangle height {_height} and width {_width}"); } } \ No newline at end of file diff --git a/ProxyPattern/Image.cs b/ProxyPattern/Image.cs index 5041b84..49799c8 100644 --- a/ProxyPattern/Image.cs +++ b/ProxyPattern/Image.cs @@ -1,8 +1,7 @@ -namespace ProxyPattern +namespace ProxyPattern; + +public interface Image { - public interface Image - { - void display(); + void display(); - } } diff --git a/ProxyPattern/Program.cs b/ProxyPattern/Program.cs index b0cedf6..e44e632 100644 --- a/ProxyPattern/Program.cs +++ b/ProxyPattern/Program.cs @@ -1,19 +1,16 @@ -using System; +namespace ProxyPattern; -namespace ProxyPattern +class Program { - class Program + static void Main(string[] args) { - static void Main(string[] args) - { - Image image = new ProxyImage("testImage.jpg"); + Image image = new ProxyImage("testImage.jpg"); - //image will be loaded from disk - image.display(); - Console.WriteLine(""); + //image will be loaded from disk + image.display(); + Console.WriteLine(""); - //image will not be loaded from disk - image.display(); - } + //image will not be loaded from disk + image.display(); } } diff --git a/ProxyPattern/ProxyImage.cs b/ProxyPattern/ProxyImage.cs index bdab6de..78d9b17 100644 --- a/ProxyPattern/ProxyImage.cs +++ b/ProxyPattern/ProxyImage.cs @@ -1,23 +1,22 @@ -namespace ProxyPattern +namespace ProxyPattern; + +public class ProxyImage : Image { - public class ProxyImage : Image - { - private RealImage _realImage; - private string _fileName; + private RealImage _realImage; + private string _fileName; - public ProxyImage(string fileName) - { - _fileName = fileName; - } + public ProxyImage(string fileName) + { + _fileName = fileName; + } - public void display() + public void display() + { + if (_realImage == null) { - if (_realImage == null) - { - _realImage = new RealImage(_fileName); - } - _realImage.display(); + _realImage = new RealImage(_fileName); } + _realImage.display(); } } \ No newline at end of file diff --git a/ProxyPattern/RealImage.cs b/ProxyPattern/RealImage.cs index d0826fd..6b9c6dd 100644 --- a/ProxyPattern/RealImage.cs +++ b/ProxyPattern/RealImage.cs @@ -1,27 +1,24 @@ -using System; +namespace ProxyPattern; -namespace ProxyPattern -{ - public class RealImage : Image - { +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) - { - Console.WriteLine("Loading " + fileName); - } + private void loadFromDisk(string fileName) + { + Console.WriteLine("Loading " + fileName); } } \ No newline at end of file diff --git a/SingletonPattern.Tests/SingletonPatternTests.cs b/SingletonPattern.Tests/SingletonPatternTests.cs index f015dfc..2005bb8 100644 --- a/SingletonPattern.Tests/SingletonPatternTests.cs +++ b/SingletonPattern.Tests/SingletonPatternTests.cs @@ -1,17 +1,16 @@ -namespace SingletonPattern.Tests +namespace SingletonPattern.Tests; + +[TestFixture] +public class SingletonPatternTests { - [TestFixture] - public class SingletonPatternTests + [Test] + public void GetInstance_CreateInstanceTwice_AreEqual() { - [Test] - public void GetInstance_CreateInstanceTwice_AreEqual() - { - // Arrange, Act - var firstAttemptInstance = ChocolateBoiler.GetInstance(); - var secondAttemptInstance = ChocolateBoiler.GetInstance(); + // Arrange, Act + var firstAttemptInstance = ChocolateBoiler.GetInstance(); + var secondAttemptInstance = ChocolateBoiler.GetInstance(); - // Assert - Assert.That(firstAttemptInstance, Is.EqualTo(secondAttemptInstance)); - } + // Assert + Assert.That(firstAttemptInstance, Is.EqualTo(secondAttemptInstance)); } } diff --git a/SingletonPattern/ChocolateBoiler.cs b/SingletonPattern/ChocolateBoiler.cs index bdc6a2d..b7b561c 100644 --- a/SingletonPattern/ChocolateBoiler.cs +++ b/SingletonPattern/ChocolateBoiler.cs @@ -1,44 +1,41 @@ -using System; +namespace SingletonPattern; -namespace SingletonPattern +internal partial class ChocolateBoiler { - internal partial class ChocolateBoiler + private static readonly Lazy _singleton = new Lazy(() => new ChocolateBoiler()); + + public static ChocolateBoiler GetInstance() => _singleton.Value; + + private Status _boiler; + + private ChocolateBoiler() + { + Console.WriteLine("Starting"); + _boiler = Status.Empty; + } + + public void Fill() + { + if (!IsEmpty) return; + Console.WriteLine("Filling..."); + _boiler = Status.InProgress; + } + + public void Drain() { - private static readonly Lazy _singleton = new Lazy(() => new ChocolateBoiler()); - - public static ChocolateBoiler GetInstance() => _singleton.Value; - - private Status _boiler; - - private ChocolateBoiler() - { - Console.WriteLine("Starting"); - _boiler = Status.Empty; - } - - public void Fill() - { - if (!IsEmpty) return; - Console.WriteLine("Filling..."); - _boiler = Status.InProgress; - } - - public void Drain() - { - if (!IsBoiled) return; - Console.WriteLine("Draining..."); - _boiler = Status.Empty; - } - - public void Boil() - { - if (IsBoiled || IsEmpty) return; - Console.WriteLine("Boiling..."); - _boiler = Status.Boiled; - } - - private bool IsEmpty => (_boiler == Status.Empty); - - private bool IsBoiled => (_boiler == Status.Boiled); + if (!IsBoiled) return; + Console.WriteLine("Draining..."); + _boiler = Status.Empty; } + + public void Boil() + { + if (IsBoiled || IsEmpty) return; + Console.WriteLine("Boiling..."); + _boiler = Status.Boiled; + } + + private bool IsEmpty => (_boiler == Status.Empty); + + private bool IsBoiled => (_boiler == Status.Boiled); } diff --git a/SingletonPattern/Program.cs b/SingletonPattern/Program.cs index 7d81245..539ebbe 100644 --- a/SingletonPattern/Program.cs +++ b/SingletonPattern/Program.cs @@ -1,22 +1,19 @@ -using System; +namespace SingletonPattern; -namespace SingletonPattern +static class Program { - static class Program + static void Main() { - static void Main() + try { - try - { - var chocoEggs = ChocolateBoiler.GetInstance(); - chocoEggs.Fill(); - chocoEggs.Boil(); - chocoEggs.Drain(); - } - catch (Exception) - { - Console.Write("Oops"); - } + var chocoEggs = ChocolateBoiler.GetInstance(); + chocoEggs.Fill(); + chocoEggs.Boil(); + chocoEggs.Drain(); + } + catch (Exception) + { + Console.Write("Oops"); } } } diff --git a/SingletonPattern/Properties/AssemblyInfo.cs b/SingletonPattern/Properties/AssemblyInfo.cs index f6a8309..868ebfb 100644 --- a/SingletonPattern/Properties/AssemblyInfo.cs +++ b/SingletonPattern/Properties/AssemblyInfo.cs @@ -1,3 +1,3 @@ using System.Runtime.CompilerServices; -[assembly:InternalsVisibleTo("SingletonPattern.Tests")] +[assembly: InternalsVisibleTo("SingletonPattern.Tests")] diff --git a/SingletonPattern/Status.cs b/SingletonPattern/Status.cs index 162daf6..cc5d185 100644 --- a/SingletonPattern/Status.cs +++ b/SingletonPattern/Status.cs @@ -1,10 +1,9 @@ -namespace SingletonPattern +namespace SingletonPattern; + +internal partial class ChocolateBoiler { - internal partial class ChocolateBoiler + private enum Status { - private enum Status - { - Empty, InProgress, Boiled - } + Empty, InProgress, Boiled } } diff --git a/StatePattern/GumballMachine.cs b/StatePattern/GumballMachine.cs index 3edf65a..b3d3846 100644 --- a/StatePattern/GumballMachine.cs +++ b/StatePattern/GumballMachine.cs @@ -1,54 +1,51 @@ -using System; +namespace StatePattern; -namespace StatePattern +public class GumballMachine { - public class GumballMachine - { - public int Count { get; private set; } + public int Count { get; private set; } - public IState SoldOutState; - public IState NoQuarterState; - public IState HasQuarterState; - public IState SoldState; - public IState WinnerState; + public IState SoldOutState; + public IState NoQuarterState; + public IState HasQuarterState; + public IState SoldState; + public IState WinnerState; - public IState State { get; set; } + public IState State { get; set; } - public GumballMachine(int count) + public GumballMachine(int count) + { + Count = count; + SoldOutState = new SoldOutState(this); + NoQuarterState = new NoQuarterState(this); + HasQuarterState = new HasQuarterState(this); + SoldState = new SoldState(this); + WinnerState = new WinnerState(this); + if (Count > 0) { - Count = count; - SoldOutState = new SoldOutState(this); - NoQuarterState = new NoQuarterState(this); - HasQuarterState = new HasQuarterState(this); - SoldState = new SoldState(this); - WinnerState = new WinnerState(this); - if (Count > 0) - { - State = NoQuarterState; - } + State = NoQuarterState; } + } - public void InsertQuarter() - { - State.InsertQuarter(); - } + public void InsertQuarter() + { + State.InsertQuarter(); + } - public void EjectQuarter() - { - State.EjectQuarter(); - } + public void EjectQuarter() + { + State.EjectQuarter(); + } - public void TurnCrank() - { - State.TurnCrank(); - State.Dispense(); - } + public void TurnCrank() + { + State.TurnCrank(); + State.Dispense(); + } - public void ReleaseBall() - { - Console.WriteLine("A ball comes rolling down"); - if (Count == 0) return; - Count--; - } + public void ReleaseBall() + { + Console.WriteLine("A ball comes rolling down"); + if (Count == 0) return; + Count--; } } \ No newline at end of file diff --git a/StatePattern/HasQuarterState.cs b/StatePattern/HasQuarterState.cs index 2dc106d..ec5d7d4 100644 --- a/StatePattern/HasQuarterState.cs +++ b/StatePattern/HasQuarterState.cs @@ -1,43 +1,40 @@ -using System; +namespace StatePattern; -namespace StatePattern +public class HasQuarterState : IState { - public class HasQuarterState : IState - { - private GumballMachine Machine { get; } - readonly Random _random = new Random(DateTime.Now.Millisecond); + private GumballMachine Machine { get; } + readonly Random _random = new Random(DateTime.Now.Millisecond); - public HasQuarterState(GumballMachine gumballMachine) - { - Machine = gumballMachine; - } + public HasQuarterState(GumballMachine gumballMachine) + { + Machine = gumballMachine; + } - public void InsertQuarter() - { - Console.WriteLine("Can't insert more than one"); - } + public void InsertQuarter() + { + Console.WriteLine("Can't insert more than one"); + } - public void EjectQuarter() - { - Console.WriteLine("Quarter returned"); - Machine.State = Machine.NoQuarterState; - } + public void EjectQuarter() + { + Console.WriteLine("Quarter returned"); + Machine.State = Machine.NoQuarterState; + } - public void TurnCrank() + public void TurnCrank() + { + Console.WriteLine("You turned the crank"); + var winner = _random.Next(10); + if ((winner == 5) && (Machine.Count > 1)) + Machine.State = Machine.WinnerState; + else { - Console.WriteLine("You turned the crank"); - var winner = _random.Next(10); - if ((winner == 5) && (Machine.Count > 1)) - Machine.State = Machine.WinnerState; - else - { - Machine.State = Machine.SoldState; - } + Machine.State = Machine.SoldState; } + } - public void Dispense() - { - Console.WriteLine("Can't do that"); - } + public void Dispense() + { + Console.WriteLine("Can't do that"); } } \ No newline at end of file diff --git a/StatePattern/IState.cs b/StatePattern/IState.cs index 11a82cd..db29c76 100644 --- a/StatePattern/IState.cs +++ b/StatePattern/IState.cs @@ -1,10 +1,9 @@ -namespace StatePattern +namespace StatePattern; + +public interface IState { - public interface IState - { - void InsertQuarter(); - void EjectQuarter(); - void TurnCrank(); - void Dispense(); - } + void InsertQuarter(); + void EjectQuarter(); + void TurnCrank(); + void Dispense(); } \ No newline at end of file diff --git a/StatePattern/Legacy/GumballMachine.cs b/StatePattern/Legacy/GumballMachine.cs index e33cda2..f67d7ea 100644 --- a/StatePattern/Legacy/GumballMachine.cs +++ b/StatePattern/Legacy/GumballMachine.cs @@ -1,103 +1,100 @@ -using System; +namespace StatePattern.Legacy; -namespace StatePattern.Legacy +public class GumballMachine { - public class GumballMachine - { - private int _count; - private State _state = State.NoQuarters; + private int _count; + private State _state = State.NoQuarters; - public GumballMachine(int count) - { - _count = count; - } + public GumballMachine(int count) + { + _count = count; + } - public void InsertQuarter() + public void InsertQuarter() + { + switch (_state) { - switch (_state) - { - case State.NoQuarters: - _state = State.HasQuarters; - Console.WriteLine("Inserted a quarter"); - break; - case State.Sold: - Console.WriteLine("Please wait for current gumball to come out"); - break; - case State.HasQuarters: - Console.WriteLine("Can't add more quarters"); - break; - case State.NoGumballs: - Console.WriteLine("Out of Stock"); - break; - default: - throw new ArgumentOutOfRangeException(); - } + case State.NoQuarters: + _state = State.HasQuarters; + Console.WriteLine("Inserted a quarter"); + break; + case State.Sold: + Console.WriteLine("Please wait for current gumball to come out"); + break; + case State.HasQuarters: + Console.WriteLine("Can't add more quarters"); + break; + case State.NoGumballs: + Console.WriteLine("Out of Stock"); + break; + default: + throw new ArgumentOutOfRangeException(); } + } - public void EjectQuarter() + public void EjectQuarter() + { + switch (_state) { - switch (_state) - { - case State.NoQuarters: - Console.WriteLine("Nothing to eject"); - break; - case State.Sold: - Console.WriteLine("Sorry, you have already turned the crank"); - break; - case State.HasQuarters: - Console.WriteLine("Ejecting.."); - _state = State.NoQuarters; - break; - case State.NoGumballs: - Console.WriteLine("Can't eject, never accepted quarters"); - break; - default: - throw new ArgumentOutOfRangeException(); - } + case State.NoQuarters: + Console.WriteLine("Nothing to eject"); + break; + case State.Sold: + Console.WriteLine("Sorry, you have already turned the crank"); + break; + case State.HasQuarters: + Console.WriteLine("Ejecting.."); + _state = State.NoQuarters; + break; + case State.NoGumballs: + Console.WriteLine("Can't eject, never accepted quarters"); + break; + default: + throw new ArgumentOutOfRangeException(); } + } - public void TurnCrank() + public void TurnCrank() + { + switch (_state) { - switch (_state) - { - case State.NoQuarters: - Console.WriteLine("Insert quarter First"); - break; - case State.Sold: - Console.WriteLine("Turning twice won't get you a gumball"); - break; - case State.HasQuarters: - Console.WriteLine("Getting gumball..."); - _state = State.Sold; - Dispense(); - break; - case State.NoGumballs: - Console.WriteLine("Out of Stock"); - break; - default: - throw new ArgumentOutOfRangeException(); - } + case State.NoQuarters: + Console.WriteLine("Insert quarter First"); + break; + case State.Sold: + Console.WriteLine("Turning twice won't get you a gumball"); + break; + case State.HasQuarters: + Console.WriteLine("Getting gumball..."); + _state = State.Sold; + Dispense(); + break; + case State.NoGumballs: + Console.WriteLine("Out of Stock"); + break; + default: + throw new ArgumentOutOfRangeException(); } + } - private void Dispense() + private void Dispense() + { + switch (_state) { - switch (_state) - { - case State.NoQuarters: - Console.WriteLine("You need to pay first"); - break; - case State.Sold: - Console.WriteLine("A Gumball comes rolling out"); - _count--; - _state = _count == 0 ? _state = State.NoGumballs : State.NoQuarters; - break; - case State.HasQuarters: - case State.NoGumballs: - Console.WriteLine("Can't dispense"); - break; - default: - throw new ArgumentOutOfRangeException(); - } + case State.NoQuarters: + Console.WriteLine("You need to pay first"); + break; + case State.Sold: + Console.WriteLine("A Gumball comes rolling out"); + _count--; + _state = _count == 0 ? _state = State.NoGumballs : State.NoQuarters; + break; + case State.HasQuarters: + case State.NoGumballs: + Console.WriteLine("Can't dispense"); + break; + default: + throw new ArgumentOutOfRangeException(); } } } \ No newline at end of file diff --git a/StatePattern/Legacy/State.cs b/StatePattern/Legacy/State.cs index 44ac8b9..478faac 100644 --- a/StatePattern/Legacy/State.cs +++ b/StatePattern/Legacy/State.cs @@ -1,7 +1,6 @@ -namespace StatePattern.Legacy +namespace StatePattern.Legacy; + +public enum State { - public enum State - { - Sold, HasQuarters, NoQuarters, NoGumballs - } + Sold, HasQuarters, NoQuarters, NoGumballs } \ No newline at end of file diff --git a/StatePattern/NoQuarterState.cs b/StatePattern/NoQuarterState.cs index 978114d..5571256 100644 --- a/StatePattern/NoQuarterState.cs +++ b/StatePattern/NoQuarterState.cs @@ -1,34 +1,31 @@ -using System; +namespace StatePattern; -namespace StatePattern +public class NoQuarterState : IState { - public class NoQuarterState : IState - { - public GumballMachine Machine { get; } + public GumballMachine Machine { get; } - public NoQuarterState(GumballMachine machine) - { - Machine = machine; - } - public void InsertQuarter() - { - Console.WriteLine("Inserted a quarter"); - Machine.State = Machine.HasQuarterState; - } + public NoQuarterState(GumballMachine machine) + { + Machine = machine; + } + public void InsertQuarter() + { + Console.WriteLine("Inserted a quarter"); + Machine.State = Machine.HasQuarterState; + } - public void EjectQuarter() - { - Console.Write("Can't eject anything"); - } + public void EjectQuarter() + { + Console.Write("Can't eject anything"); + } - public void TurnCrank() - { - Console.WriteLine("Can't turn crank without a quarter"); - } + public void TurnCrank() + { + Console.WriteLine("Can't turn crank without a quarter"); + } - public void Dispense() - { - Console.WriteLine("Can't dispense"); - } + public void Dispense() + { + Console.WriteLine("Can't dispense"); } } \ No newline at end of file diff --git a/StatePattern/Program.cs b/StatePattern/Program.cs index 215a398..bb8c303 100644 --- a/StatePattern/Program.cs +++ b/StatePattern/Program.cs @@ -1,28 +1,25 @@ -using System; +namespace StatePattern; -namespace StatePattern +static class Program { - static class Program + public static void Main() { - public static void Main() - { - LegacyTest(); - Console.WriteLine(); - var gumballmachine = new GumballMachine(5); - gumballmachine.InsertQuarter(); - gumballmachine.TurnCrank(); - gumballmachine.InsertQuarter(); - gumballmachine.TurnCrank(); - } + LegacyTest(); + Console.WriteLine(); + var gumballmachine = new GumballMachine(5); + gumballmachine.InsertQuarter(); + gumballmachine.TurnCrank(); + gumballmachine.InsertQuarter(); + gumballmachine.TurnCrank(); + } - private static void LegacyTest() - { - var machine = new Legacy.GumballMachine(2); - machine.InsertQuarter(); - machine.TurnCrank(); - machine.InsertQuarter(); - machine.EjectQuarter(); - machine.InsertQuarter(); - } + private static void LegacyTest() + { + var machine = new Legacy.GumballMachine(2); + machine.InsertQuarter(); + machine.TurnCrank(); + machine.InsertQuarter(); + machine.EjectQuarter(); + machine.InsertQuarter(); } } diff --git a/StatePattern/SoldOutState.cs b/StatePattern/SoldOutState.cs index 8fad665..5cbe4a0 100644 --- a/StatePattern/SoldOutState.cs +++ b/StatePattern/SoldOutState.cs @@ -1,34 +1,31 @@ -using System; +namespace StatePattern; -namespace StatePattern +public class SoldOutState : IState { - public class SoldOutState : IState - { - public GumballMachine Machine { get; } + public GumballMachine Machine { get; } - public SoldOutState(GumballMachine gumballMachine) - { - Machine = gumballMachine; - } + public SoldOutState(GumballMachine gumballMachine) + { + Machine = gumballMachine; + } - public void InsertQuarter() - { - Console.WriteLine("Sorry! Sold Out"); - } + public void InsertQuarter() + { + Console.WriteLine("Sorry! Sold Out"); + } - public void EjectQuarter() - { - Console.WriteLine("Can't eject when sold out"); - } + public void EjectQuarter() + { + Console.WriteLine("Can't eject when sold out"); + } - public void TurnCrank() - { - Console.WriteLine("turning crank achieves nothing"); - } + public void TurnCrank() + { + Console.WriteLine("turning crank achieves nothing"); + } - public void Dispense() - { - Console.WriteLine("Can't dispense when out of stock"); - } + public void Dispense() + { + Console.WriteLine("Can't dispense when out of stock"); } } \ No newline at end of file diff --git a/StatePattern/SoldState.cs b/StatePattern/SoldState.cs index e8ecb33..5d573fa 100644 --- a/StatePattern/SoldState.cs +++ b/StatePattern/SoldState.cs @@ -1,43 +1,40 @@ -using System; +namespace StatePattern; -namespace StatePattern +public class SoldState : IState { - public class SoldState : IState + private GumballMachine Machine { get; } + + public SoldState(GumballMachine gumballMachine) { - private GumballMachine Machine { get; } + Machine = gumballMachine; + } - public SoldState(GumballMachine gumballMachine) - { - Machine = gumballMachine; - } + public void InsertQuarter() + { + Console.WriteLine("Please wait, already in progress"); + } - public void InsertQuarter() - { - Console.WriteLine("Please wait, already in progress"); - } + public void EjectQuarter() + { + Console.WriteLine("Can't eject, already turned the crank"); + } - public void EjectQuarter() - { - Console.WriteLine("Can't eject, already turned the crank"); - } + public void TurnCrank() + { + Console.WriteLine("Turning twice achieves nothing"); + } - public void TurnCrank() + public void Dispense() + { + Machine.ReleaseBall(); + if (Machine.Count > 0) { - Console.WriteLine("Turning twice achieves nothing"); + Machine.State = Machine.NoQuarterState; } - - public void Dispense() + else { - Machine.ReleaseBall(); - if (Machine.Count > 0) - { - Machine.State = Machine.NoQuarterState; - } - else - { - Console.WriteLine("Oops! No more gumballs"); - Machine.State = Machine.SoldOutState; - } + Console.WriteLine("Oops! No more gumballs"); + Machine.State = Machine.SoldOutState; } } } \ No newline at end of file diff --git a/StatePattern/WinnerState.cs b/StatePattern/WinnerState.cs index 1d6eac5..5c2ac6f 100644 --- a/StatePattern/WinnerState.cs +++ b/StatePattern/WinnerState.cs @@ -1,46 +1,43 @@ -using System; +namespace StatePattern; -namespace StatePattern +public class WinnerState : IState { - public class WinnerState : IState + private GumballMachine Machine { get; } + + public WinnerState(GumballMachine gumballMachine) { - private GumballMachine Machine { get; } + Machine = gumballMachine; + } - public WinnerState(GumballMachine gumballMachine) - { - Machine = gumballMachine; - } + public void InsertQuarter() + { + Console.WriteLine("Please wait, already in progress"); + } - public void InsertQuarter() - { - Console.WriteLine("Please wait, already in progress"); - } + public void EjectQuarter() + { + Console.WriteLine("Can't eject, already turned the crank"); + } - public void EjectQuarter() - { - Console.WriteLine("Can't eject, already turned the crank"); - } + public void TurnCrank() + { + Console.WriteLine("Turning twice achieves nothing"); + } - public void TurnCrank() + + public void Dispense() + { + Console.WriteLine("You Won!! 2 gumballs for the price of one"); + Machine.ReleaseBall(); + if (Machine.Count == 0) { - Console.WriteLine("Turning twice achieves nothing"); + Machine.State = Machine.SoldOutState; + Console.WriteLine("Oops! No more gumballs"); } - - - public void Dispense() + else { - Console.WriteLine("You Won!! 2 gumballs for the price of one"); Machine.ReleaseBall(); - if (Machine.Count == 0) - { - Machine.State = Machine.SoldOutState; - Console.WriteLine("Oops! No more gumballs"); - } - else - { - Machine.ReleaseBall(); - Machine.State = Machine.NoQuarterState; - } + Machine.State = Machine.NoQuarterState; } } } \ No newline at end of file diff --git a/StrategyPattern/FlyNope.cs b/StrategyPattern/FlyNope.cs index 86f5b32..0bcbd30 100644 --- a/StrategyPattern/FlyNope.cs +++ b/StrategyPattern/FlyNope.cs @@ -1,12 +1,9 @@ -using System; +namespace Ducks; -namespace Ducks +class FlyNope : IFlyBehaviour { - class FlyNope : IFlyBehaviour + public void Fly() { - public void Fly() - { - Console.WriteLine("I can't fly"); - } + Console.WriteLine("I can't fly"); } } diff --git a/StrategyPattern/FlyWings.cs b/StrategyPattern/FlyWings.cs index 55822d1..b9548bb 100644 --- a/StrategyPattern/FlyWings.cs +++ b/StrategyPattern/FlyWings.cs @@ -1,12 +1,9 @@ -using System; +namespace Ducks; -namespace Ducks +class FlyWings : IFlyBehaviour { - class FlyWings : IFlyBehaviour + public void Fly() { - public void Fly() - { - Console.WriteLine("Flap Flap"); - } + Console.WriteLine("Flap Flap"); } } diff --git a/StrategyPattern/IFlyBehaviour.cs b/StrategyPattern/IFlyBehaviour.cs index 389e071..ed0759a 100644 --- a/StrategyPattern/IFlyBehaviour.cs +++ b/StrategyPattern/IFlyBehaviour.cs @@ -1,7 +1,6 @@ -namespace Ducks +namespace Ducks; + +internal interface IFlyBehaviour { - internal interface IFlyBehaviour - { - void Fly(); - } + void Fly(); } diff --git a/StrategyPattern/IQuackBehaviour.cs b/StrategyPattern/IQuackBehaviour.cs index 3962ea3..afcfcaa 100644 --- a/StrategyPattern/IQuackBehaviour.cs +++ b/StrategyPattern/IQuackBehaviour.cs @@ -1,7 +1,6 @@ -namespace Ducks +namespace Ducks; + +internal interface IQuackBehaviour { - internal interface IQuackBehaviour - { - void Quack(); - } + void Quack(); } \ No newline at end of file diff --git a/StrategyPattern/Program.cs b/StrategyPattern/Program.cs index 934724b..5a001ea 100644 --- a/StrategyPattern/Program.cs +++ b/StrategyPattern/Program.cs @@ -1,63 +1,62 @@ -namespace Ducks +namespace Ducks; + +internal class Duck { - internal class Duck - { - private IQuackBehaviour _quacker; - private IFlyBehaviour _flyer; + private IQuackBehaviour _quacker; + private IFlyBehaviour _flyer; - public IQuackBehaviour Quacker + public IQuackBehaviour Quacker + { + set { - set - { - _quacker = value; - } + _quacker = value; } + } - public IFlyBehaviour Flyer + public IFlyBehaviour Flyer + { + set { - set - { - _flyer = value; - } + _flyer = value; } + } - protected void PerformQuack() - { - _quacker.Quack(); - } + protected void PerformQuack() + { + _quacker.Quack(); + } - protected void PerformFly() - { - _flyer.Fly(); - } + protected void PerformFly() + { + _flyer.Fly(); } +} - internal class MallardDuck : Duck +internal class MallardDuck : Duck +{ + public MallardDuck() { - public MallardDuck() - { - Flyer = new FlyNope(); - Quacker = new QuackNope(); - } + Flyer = new FlyNope(); + Quacker = new QuackNope(); + } - public void Display() - { - PerformFly(); - PerformQuack(); - } + public void Display() + { + PerformFly(); + PerformQuack(); } +} - internal static class Program +internal static class Program +{ + private static void Main() { - private static void Main() - { - var mallard = new MallardDuck { Quacker = new QuackNormal() }; - mallard.Display(); - mallard.Flyer = new FlyWings(); - mallard.Display(); + var mallard = new MallardDuck { Quacker = new QuackNormal() }; + mallard.Display(); + mallard.Flyer = new FlyWings(); + mallard.Display(); - } } } diff --git a/StrategyPattern/QuackNope.cs b/StrategyPattern/QuackNope.cs index d46e0c5..26ba5e7 100644 --- a/StrategyPattern/QuackNope.cs +++ b/StrategyPattern/QuackNope.cs @@ -1,12 +1,9 @@ -using System; +namespace Ducks; -namespace Ducks +internal class QuackNope : IQuackBehaviour { - internal class QuackNope : IQuackBehaviour + public void Quack() { - public void Quack() - { - Console.WriteLine("..."); - } + Console.WriteLine("..."); } } \ No newline at end of file diff --git a/StrategyPattern/QuackNormal.cs b/StrategyPattern/QuackNormal.cs index 0dc0053..a305a81 100644 --- a/StrategyPattern/QuackNormal.cs +++ b/StrategyPattern/QuackNormal.cs @@ -1,12 +1,9 @@ -using System; +namespace Ducks; -namespace Ducks +class QuackNormal : IQuackBehaviour { - class QuackNormal : IQuackBehaviour + public void Quack() { - public void Quack() - { - Console.WriteLine("Quack Quack"); - } + Console.WriteLine("Quack Quack"); } } diff --git a/StrategyPattern/QuackSqueak.cs b/StrategyPattern/QuackSqueak.cs index 8f38882..969bd6d 100644 --- a/StrategyPattern/QuackSqueak.cs +++ b/StrategyPattern/QuackSqueak.cs @@ -1,12 +1,9 @@ -using System; +namespace Ducks; -namespace Ducks +class QuackSqueak : IQuackBehaviour { - class QuackSqueak : IQuackBehaviour + public void Quack() { - public void Quack() - { - Console.WriteLine("Squeeeak"); - } + Console.WriteLine("Squeeeak"); } } diff --git a/TemplatePattern/Beverages/Beverage.cs b/TemplatePattern/Beverages/Beverage.cs index d2fd482..2fbcd2d 100644 --- a/TemplatePattern/Beverages/Beverage.cs +++ b/TemplatePattern/Beverages/Beverage.cs @@ -1,40 +1,37 @@ -using System; +namespace TemplatePattern; -namespace TemplatePattern +public abstract class Beverage { - public abstract class Beverage + // ReSharper disable once InconsistentNaming + protected int _sugar; + public void Prepare() { - // ReSharper disable once InconsistentNaming - protected int _sugar; - public void Prepare() - { - Boil(); - Brew(); - Pour(); - if (WantsCondiments) - AddCondiments(); + Boil(); + Brew(); + Pour(); + if (WantsCondiments) + AddCondiments(); - } + } - public bool WantsCondiments { private get; set; } + public bool WantsCondiments { private get; set; } - protected abstract void Brew(); + protected abstract void Brew(); - private void Boil() - { - Console.WriteLine("Boling Water"); - } + private void Boil() + { + Console.WriteLine("Boling Water"); + } - private void Pour() - { - Console.WriteLine("Pouring in Cup"); - } + private void Pour() + { + Console.WriteLine("Pouring in Cup"); + } - protected abstract void AddCondiments(); + protected abstract void AddCondiments(); - public int AddSugar { get; set; } + public int AddSugar { get; set; } - protected void Sugar() { } - } + protected void Sugar() { } } \ No newline at end of file diff --git a/TemplatePattern/Beverages/Coffee.cs b/TemplatePattern/Beverages/Coffee.cs index 0462a22..89a754a 100644 --- a/TemplatePattern/Beverages/Coffee.cs +++ b/TemplatePattern/Beverages/Coffee.cs @@ -1,18 +1,15 @@ -using System; +namespace TemplatePattern; -namespace TemplatePattern +class Coffee : Beverage { - class Coffee : Beverage + protected override void Brew() { - protected override void Brew() - { - Console.WriteLine("Add Coffee Grounds to water and boil"); - } - - protected override void AddCondiments() - { - Console.WriteLine("Add Milk and Sugar"); - } + Console.WriteLine("Add Coffee Grounds to water and boil"); + } + protected override void AddCondiments() + { + Console.WriteLine("Add Milk and Sugar"); } + } diff --git a/TemplatePattern/Beverages/Tea.cs b/TemplatePattern/Beverages/Tea.cs index 1c4b3eb..22d4d81 100644 --- a/TemplatePattern/Beverages/Tea.cs +++ b/TemplatePattern/Beverages/Tea.cs @@ -1,28 +1,25 @@ -using System; +namespace TemplatePattern; -namespace TemplatePattern +class Tea : Beverage { - class Tea : Beverage + protected override void Brew() { - protected override void Brew() - { - Console.WriteLine("Adding tea leaves to water and boil"); - } + Console.WriteLine("Adding tea leaves to water and boil"); + } - protected override void AddCondiments() - { - Console.WriteLine("Adding Lemon and Sugar"); - Sugar(); - } + protected override void AddCondiments() + { + Console.WriteLine("Adding Lemon and Sugar"); + Sugar(); + } - private new void Sugar() - { - Console.WriteLine($"adding {_sugar} spoons of sugar"); - } + private new void Sugar() + { + Console.WriteLine($"adding {_sugar} spoons of sugar"); + } - public new int AddSugar - { - set { _sugar = value; } - } + public new int AddSugar + { + set { _sugar = value; } } } \ No newline at end of file diff --git a/TemplatePattern/Comparable/Person.cs b/TemplatePattern/Comparable/Person.cs index 9a23ec7..2cf7a9e 100644 --- a/TemplatePattern/Comparable/Person.cs +++ b/TemplatePattern/Comparable/Person.cs @@ -1,32 +1,29 @@ -using System; +namespace TemplatePattern.Comparable; -namespace TemplatePattern.Comparable +class Person : IComparable { - class Person : IComparable - { - public string Name { get; } - public int Age { get; } - - public Person(string name, int age) - { - Name = name; - Age = age; - } + public string Name { get; } + public int Age { get; } - public int CompareTo(object obj) - { - var other = (Person)obj; - if (String.Compare(Name, other.Name, StringComparison.Ordinal) == 0) - { - return Age.CompareTo(other.Age); - } - return String.Compare(Name, other.Name, StringComparison.Ordinal); - } + public Person(string name, int age) + { + Name = name; + Age = age; + } - public override string ToString() + public int CompareTo(object obj) + { + var other = (Person)obj; + if (String.Compare(Name, other.Name, StringComparison.Ordinal) == 0) { - return $"{Name} : {Age} < "; + return Age.CompareTo(other.Age); } + return String.Compare(Name, other.Name, StringComparison.Ordinal); + } + public override string ToString() + { + return $"{Name} : {Age} < "; } + } \ No newline at end of file diff --git a/TemplatePattern/Program.cs b/TemplatePattern/Program.cs index eb0abae..e705b36 100644 --- a/TemplatePattern/Program.cs +++ b/TemplatePattern/Program.cs @@ -1,34 +1,31 @@ -using System; -using System.Collections.Generic; -using TemplatePattern.Comparable; +using TemplatePattern.Comparable; -namespace TemplatePattern +namespace TemplatePattern; + +internal static class Program { - internal static class Program + static void Main() { - static void Main() - { - var tea = new Tea(); - var coffee = new Coffee(); - tea.WantsCondiments = true; - tea.AddSugar = 5; - tea.Prepare(); + var tea = new Tea(); + var coffee = new Coffee(); + tea.WantsCondiments = true; + tea.AddSugar = 5; + tea.Prepare(); - Console.WriteLine(); - coffee.WantsCondiments = true; - coffee.Prepare(); + Console.WriteLine(); + coffee.WantsCondiments = true; + coffee.Prepare(); - var people = new List { new Person("Ram", 25), new Person("Abishek", 12), new Person("Ram", 18), new Person("Abishek", 18) }; - foreach (var person in people) - { - Console.Write(person); - } - people.Sort(); - Console.WriteLine(); - foreach (var person in people) - { - Console.Write(person); - } + var people = new List { new Person("Ram", 25), new Person("Abishek", 12), new Person("Ram", 18), new Person("Abishek", 18) }; + foreach (var person in people) + { + Console.Write(person); + } + people.Sort(); + Console.WriteLine(); + foreach (var person in people) + { + Console.Write(person); } } } diff --git a/VisitorPattern/Apartment.cs b/VisitorPattern/Apartment.cs index 63469c7..345206a 100644 --- a/VisitorPattern/Apartment.cs +++ b/VisitorPattern/Apartment.cs @@ -1,20 +1,19 @@ -namespace VisitorPattern +namespace VisitorPattern; + +public class Apartment : Unit { - public class Apartment : Unit + public Apartment(params Unit[] units) : base(units) { - public Apartment(params Unit[] units) : base(units) - { - } + } - public override void Accept(IUnitVisitor visitor) - { - visitor.VisitApartment(this); - base.Accept(visitor); - } + public override void Accept(IUnitVisitor visitor) + { + visitor.VisitApartment(this); + base.Accept(visitor); + } - public override string ToString() - { - return "Apartment"; - } + public override string ToString() + { + return "Apartment"; } } \ No newline at end of file diff --git a/VisitorPattern/ApartmentVisitor.cs b/VisitorPattern/ApartmentVisitor.cs index bdc6b81..783669b 100644 --- a/VisitorPattern/ApartmentVisitor.cs +++ b/VisitorPattern/ApartmentVisitor.cs @@ -1,24 +1,21 @@ -using System; +namespace VisitorPattern; -namespace VisitorPattern +public class ApartmentVisitor : IUnitVisitor { - public class ApartmentVisitor : IUnitVisitor + public void VisitApartment(Apartment apartment) { - public void VisitApartment(Apartment apartment) - { - Console.WriteLine("This is an apartment"); - } + Console.WriteLine("This is an apartment"); + } - public void VisitStudio(Studio studio) - { - } + public void VisitStudio(Studio studio) + { + } - public void VisitBedroom(Bedroom bedroom) - { - } + public void VisitBedroom(Bedroom bedroom) + { + } - public void VisitLivingRoom(LivingRoom livingRoom) - { - } + public void VisitLivingRoom(LivingRoom livingRoom) + { } } \ No newline at end of file diff --git a/VisitorPattern/Bedroom.cs b/VisitorPattern/Bedroom.cs index ee0d08e..71f3aeb 100644 --- a/VisitorPattern/Bedroom.cs +++ b/VisitorPattern/Bedroom.cs @@ -1,20 +1,19 @@ -namespace VisitorPattern +namespace VisitorPattern; + +public class Bedroom : Unit { - public class Bedroom : Unit + public Bedroom(params Unit[] units) : base(units) { - public Bedroom(params Unit[] units) : base(units) - { - } + } - public override void Accept(IUnitVisitor visitor) - { - visitor.VisitBedroom(this); - base.Accept(visitor); - } + public override void Accept(IUnitVisitor visitor) + { + visitor.VisitBedroom(this); + base.Accept(visitor); + } - public override string ToString() - { - return "Bedroom"; - } + public override string ToString() + { + return "Bedroom"; } } \ No newline at end of file diff --git a/VisitorPattern/BedroomVisitor.cs b/VisitorPattern/BedroomVisitor.cs index f4792ea..d19a2d7 100644 --- a/VisitorPattern/BedroomVisitor.cs +++ b/VisitorPattern/BedroomVisitor.cs @@ -1,24 +1,21 @@ -using System; +namespace VisitorPattern; -namespace VisitorPattern +public class BedroomVisitor : IUnitVisitor { - public class BedroomVisitor : IUnitVisitor + public void VisitApartment(Apartment apartment) { - public void VisitApartment(Apartment apartment) - { - } + } - public void VisitStudio(Studio studio) - { - } + public void VisitStudio(Studio studio) + { + } - public void VisitBedroom(Bedroom bedroom) - { - Console.WriteLine("Here is a bedroom"); - } + public void VisitBedroom(Bedroom bedroom) + { + Console.WriteLine("Here is a bedroom"); + } - public void VisitLivingRoom(LivingRoom livingRoom) - { - } + public void VisitLivingRoom(LivingRoom livingRoom) + { } } \ No newline at end of file diff --git a/VisitorPattern/IUnitVisitor.cs b/VisitorPattern/IUnitVisitor.cs index cae7d6c..e39e090 100644 --- a/VisitorPattern/IUnitVisitor.cs +++ b/VisitorPattern/IUnitVisitor.cs @@ -1,10 +1,9 @@ -namespace VisitorPattern +namespace VisitorPattern; + +public interface IUnitVisitor { - public interface IUnitVisitor - { - void VisitApartment(Apartment apartment); - void VisitStudio(Studio studio); - void VisitBedroom(Bedroom bedroom); - void VisitLivingRoom(LivingRoom livingRoom); - } + void VisitApartment(Apartment apartment); + void VisitStudio(Studio studio); + void VisitBedroom(Bedroom bedroom); + void VisitLivingRoom(LivingRoom livingRoom); } \ No newline at end of file diff --git a/VisitorPattern/LivingRoom.cs b/VisitorPattern/LivingRoom.cs index 31e7421..19126df 100644 --- a/VisitorPattern/LivingRoom.cs +++ b/VisitorPattern/LivingRoom.cs @@ -1,20 +1,19 @@ -namespace VisitorPattern +namespace VisitorPattern; + +public class LivingRoom : Unit { - public class LivingRoom : Unit + public LivingRoom(params Unit[] units) : base(units) { - public LivingRoom(params Unit[] units) : base(units) - { - } + } - public override void Accept(IUnitVisitor visitor) - { - visitor.VisitLivingRoom(this); - base.Accept(visitor); - } + public override void Accept(IUnitVisitor visitor) + { + visitor.VisitLivingRoom(this); + base.Accept(visitor); + } - public override string ToString() - { - return "Living Room"; - } + public override string ToString() + { + return "Living Room"; } } \ No newline at end of file diff --git a/VisitorPattern/LivingRoomVisitor.cs b/VisitorPattern/LivingRoomVisitor.cs index f6cf11a..0d0534f 100644 --- a/VisitorPattern/LivingRoomVisitor.cs +++ b/VisitorPattern/LivingRoomVisitor.cs @@ -1,24 +1,21 @@ -using System; +namespace VisitorPattern; -namespace VisitorPattern +public class LivingRoomVisitor : IUnitVisitor { - public class LivingRoomVisitor : IUnitVisitor + public void VisitApartment(Apartment apartment) { - public void VisitApartment(Apartment apartment) - { - } + } - public void VisitStudio(Studio studio) - { - } + public void VisitStudio(Studio studio) + { + } - public void VisitBedroom(Bedroom bedroom) - { - } + public void VisitBedroom(Bedroom bedroom) + { + } - public void VisitLivingRoom(LivingRoom livingRoom) - { - Console.WriteLine("This is the living room"); - } + public void VisitLivingRoom(LivingRoom livingRoom) + { + Console.WriteLine("This is the living room"); } } \ No newline at end of file diff --git a/VisitorPattern/Program.cs b/VisitorPattern/Program.cs index 3d17f4d..99719d4 100644 --- a/VisitorPattern/Program.cs +++ b/VisitorPattern/Program.cs @@ -1,22 +1,19 @@ -using System; +namespace VisitorPattern; -namespace VisitorPattern +class Program { - class Program + static void Main() { - static void Main() - { - var apartment = new Apartment(new LivingRoom(), new Bedroom(), new Bedroom()); - var studio = new Studio(new LivingRoom(), new Bedroom()); - Console.WriteLine("Visiting an Apartment"); - apartment.Accept(new ApartmentVisitor()); - apartment.Accept(new LivingRoomVisitor()); - apartment.Accept(new BedroomVisitor()); + var apartment = new Apartment(new LivingRoom(), new Bedroom(), new Bedroom()); + var studio = new Studio(new LivingRoom(), new Bedroom()); + Console.WriteLine("Visiting an Apartment"); + 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()); - studio.Accept(new BedroomVisitor()); - } + Console.WriteLine("Visiting a Studio"); + studio.Accept(new StudioVisitor()); + studio.Accept(new LivingRoomVisitor()); + studio.Accept(new BedroomVisitor()); } } \ No newline at end of file diff --git a/VisitorPattern/Studio.cs b/VisitorPattern/Studio.cs index aa48f4e..d2ee8d7 100644 --- a/VisitorPattern/Studio.cs +++ b/VisitorPattern/Studio.cs @@ -1,20 +1,19 @@ -namespace VisitorPattern +namespace VisitorPattern; + +public class Studio : Unit { - public class Studio : Unit + public Studio(params Unit[] units) : base(units) { - public Studio(params Unit[] units) : base(units) - { - } + } - public override void Accept(IUnitVisitor visitor) - { - visitor.VisitStudio(this); - base.Accept(visitor); - } + public override void Accept(IUnitVisitor visitor) + { + visitor.VisitStudio(this); + base.Accept(visitor); + } - public override string ToString() - { - return "Studio"; - } + public override string ToString() + { + return "Studio"; } } \ No newline at end of file diff --git a/VisitorPattern/StudioVisitor.cs b/VisitorPattern/StudioVisitor.cs index 82c2521..5eb8cf0 100644 --- a/VisitorPattern/StudioVisitor.cs +++ b/VisitorPattern/StudioVisitor.cs @@ -1,24 +1,21 @@ -using System; +namespace VisitorPattern; -namespace VisitorPattern +public class StudioVisitor : IUnitVisitor { - public class StudioVisitor : IUnitVisitor + public void VisitApartment(Apartment apartment) { - public void VisitApartment(Apartment apartment) - { - } + } - public void VisitStudio(Studio studio) - { - Console.WriteLine("This is a studio"); - } + public void VisitStudio(Studio studio) + { + Console.WriteLine("This is a studio"); + } - public void VisitBedroom(Bedroom bedroom) - { - } + public void VisitBedroom(Bedroom bedroom) + { + } - public void VisitLivingRoom(LivingRoom livingRoom) - { - } + public void VisitLivingRoom(LivingRoom livingRoom) + { } } \ No newline at end of file diff --git a/VisitorPattern/Unit.cs b/VisitorPattern/Unit.cs index 13f1b69..037ec3c 100644 --- a/VisitorPattern/Unit.cs +++ b/VisitorPattern/Unit.cs @@ -1,22 +1,21 @@ -namespace VisitorPattern +namespace VisitorPattern; + +public abstract class Unit { - public abstract class Unit - { - private Unit[] _units; + private Unit[] _units; - public Unit(params Unit[] units) - { - _units = units; - } + public Unit(params Unit[] units) + { + _units = units; + } - public virtual void Accept(IUnitVisitor visitor) + public virtual void Accept(IUnitVisitor visitor) + { + foreach (var unit in _units) { - foreach (var unit in _units) - { - unit.Accept(visitor); - } + unit.Accept(visitor); } - - public abstract string ToString(); } + + public abstract string ToString(); } \ No newline at end of file