├── .gitattributes ├── .gitignore ├── AdapterPattern ├── AdapterPattern.csproj ├── IDuck.cs ├── ITurkey.cs ├── MallardDuck.cs ├── Program.cs ├── TurkeyAdapter.cs └── WildTurkey.cs ├── BridgePattern ├── BridgePattern.csproj ├── FlyingEnchantment.cs ├── Hammer.cs ├── IEnchantment.cs ├── IWeapon.cs ├── Program.cs ├── SoulEatingEnchantment.cs └── Sword.cs ├── BuilderPattern ├── BuilderPattern.csproj ├── Cook.cs ├── Hamburger.cs ├── IBuilder.cs ├── MyHamburgerBuilder.cs ├── Program.cs └── WifesHamburgerBuilder.cs ├── CODE_OF_CONDUCT.md ├── ChainOfResponsibilityPattern ├── AdditionHandler.cs ├── BaseHandler.cs ├── ChainOfResponsibilityPattern.csproj ├── IHandler.cs ├── MultiplicationHandler.cs ├── Program.cs └── SubtractionHandler.cs ├── CommandPattern ├── CommandPattern.csproj ├── Garage.cs ├── GarageDoorCloseCommand.cs ├── GarageDoorOpenCommand.cs ├── ICommand.cs ├── Light.cs ├── LightOffCommand.cs ├── LightOnCommand.cs ├── MacroCommand.cs ├── NoCommand.cs ├── OnOffStruct.cs ├── Program.cs └── RemoteControl.cs ├── CompositePattern ├── Client.cs ├── CompositePattern.csproj ├── Menu.cs ├── MenuComponent.cs ├── MenuItem.cs └── Program.cs ├── DecoratorPattern ├── Beverage.cs ├── CondimentDecorator.cs ├── DarkRoast.cs ├── DecoratorPattern.csproj ├── Espresso.cs ├── HouseBlend.cs ├── MochaCondiment.cs ├── Program.cs └── WhipCondiment.cs ├── DesignPatternsDotNetCore.sln ├── FacadePattern ├── Dimmer.cs ├── Dvd.cs ├── DvdPlayer.cs ├── FacadePattern.csproj ├── HometheaterFacade.cs └── Program.cs ├── FactoryPattern ├── Abstract Factory │ ├── ChicagoIngredientsFactory.cs │ ├── IIngredientsFactory.cs │ ├── Ingredients │ │ ├── CherryTomato.cs │ │ ├── Cucumber.cs │ │ ├── DeepDish.cs │ │ ├── FreshClam.cs │ │ ├── FrozenClam.cs │ │ ├── Interfaces │ │ │ ├── ICheese.cs │ │ │ ├── IClam.cs │ │ │ ├── IDough.cs │ │ │ ├── ISauce.cs │ │ │ └── IVeggies.cs │ │ ├── Mozarella.cs │ │ ├── Olive.cs │ │ ├── Onion.cs │ │ ├── Parmesan.cs │ │ ├── Pepper.cs │ │ ├── PlumTomato.cs │ │ └── ThinCrust.cs │ └── NYIngredientsFactory.cs ├── Factory Method │ ├── ChicagoPizzaFactory.cs │ ├── NYPizzaFactory.cs │ └── PizzaFactory.cs ├── FactoryPattern.csproj ├── Helper.cs ├── Pizza │ ├── CheesePizza.cs │ ├── ClamPizza.cs │ ├── Pizza.cs │ └── VeggiePizza.cs └── Program.cs ├── FlyweightPattern ├── BeverageFlyweightFactory.cs ├── BeverageType.cs ├── BubbleMilkTea.cs ├── BubbleTeaShop.cs ├── CoconutMilkTea.cs ├── FlyweightPattern.csproj ├── FoamMilkTea.cs ├── IBeverage.cs ├── OolingMilkTea.cs └── Program.cs ├── IteratorPattern ├── BreakfastMenu.cs ├── BreakfastMenuEnum.cs ├── BreakfastMenuIterator.cs ├── Client.cs ├── DinnerMenu.cs ├── DinnerMenuEnum.cs ├── DinnerMenuIterator.cs ├── IteratorPattern.csproj ├── Menu.cs └── Program.cs ├── LICENSE ├── MediatorPattern ├── Colleague.cs ├── Customer.cs ├── ManagerMediator.cs ├── Mediator.cs ├── MediatorPattern.csproj ├── Program.cs ├── Programmer.cs └── Tester.cs ├── ObserverPattern ├── ObserverPattern.csproj ├── Program.cs ├── Unsubscriber.cs ├── Weather.cs ├── WeatherMonitor.cs └── WeatherSupplier.cs ├── PrototypePattern ├── Circle.cs ├── IFigure.cs ├── Program.cs ├── PrototypePattern.csproj └── Rectangle.cs ├── ProxyPattern ├── Image.cs ├── Program.cs ├── ProxyImage.cs ├── ProxyPattern.csproj └── RealImage.cs ├── README.md ├── SingletonPattern.Tests ├── SingletonPattern.Tests.csproj ├── SingletonPatternTests.cs └── Usings.cs ├── SingletonPattern ├── ChocolateBoiler.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── SingletonPattern.csproj └── Status.cs ├── StatePattern ├── GumballMachine.cs ├── HasQuarterState.cs ├── IState.cs ├── Legacy │ ├── GumballMachine.cs │ └── State.cs ├── NoQuarterState.cs ├── Program.cs ├── SoldOutState.cs ├── SoldState.cs ├── StatePattern.csproj └── WinnerState.cs ├── StrategyPattern ├── FlyNope.cs ├── FlyWings.cs ├── IFlyBehaviour.cs ├── IQuackBehaviour.cs ├── Program.cs ├── QuackNope.cs ├── QuackNormal.cs ├── QuackSqueak.cs └── StrategyPattern.csproj ├── TemplatePattern ├── Beverages │ ├── Beverage.cs │ ├── Coffee.cs │ └── Tea.cs ├── Comparable │ └── Person.cs ├── Program.cs └── TemplatePattern.csproj └── VisitorPattern ├── Apartment.cs ├── ApartmentVisitor.cs ├── Bedroom.cs ├── BedroomVisitor.cs ├── IUnitVisitor.cs ├── LivingRoom.cs ├── LivingRoomVisitor.cs ├── Program.cs ├── Studio.cs ├── StudioVisitor.cs ├── Unit.cs └── VisitorPattern.csproj /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | 11 | 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | bld/ 24 | [Bb]in/ 25 | [Oo]bj/ 26 | [Ll]og/ 27 | 28 | # Visual Studio 2015 cache/options directory 29 | .vs/ 30 | # Uncomment if you have tasks that create the project's static files in wwwroot 31 | #wwwroot/ 32 | 33 | # Visual Studio Code cache/options directory 34 | .vscode/ 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # DNX 49 | project.lock.json 50 | project.fragment.lock.json 51 | artifacts/ 52 | 53 | *_i.c 54 | *_p.c 55 | *_i.h 56 | *.ilk 57 | *.meta 58 | *.obj 59 | *.pch 60 | *.pdb 61 | *.pgc 62 | *.pgd 63 | *.rsp 64 | *.sbr 65 | *.tlb 66 | *.tli 67 | *.tlh 68 | *.tmp 69 | *.tmp_proj 70 | *.log 71 | *.vspscc 72 | *.vssscc 73 | .builds 74 | *.pidb 75 | *.svclog 76 | *.scc 77 | 78 | # Chutzpah Test files 79 | _Chutzpah* 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opendb 86 | *.opensdf 87 | *.sdf 88 | *.cachefile 89 | *.VC.db 90 | *.VC.VC.opendb 91 | 92 | # Visual Studio profiler 93 | *.psess 94 | *.vsp 95 | *.vspx 96 | *.sap 97 | 98 | # TFS 2012 Local Workspace 99 | $tf/ 100 | 101 | # Guidance Automation Toolkit 102 | *.gpState 103 | 104 | # ReSharper is a .NET coding add-in 105 | _ReSharper*/ 106 | *.[Rr]e[Ss]harper 107 | *.DotSettings.user 108 | 109 | # JustCode is a .NET coding add-in 110 | .JustCode 111 | 112 | # TeamCity is a build add-in 113 | _TeamCity* 114 | 115 | # DotCover is a Code Coverage Tool 116 | *.dotCover 117 | 118 | # NCrunch 119 | _NCrunch_* 120 | .*crunch*.local.xml 121 | nCrunchTemp_* 122 | 123 | # MightyMoose 124 | *.mm.* 125 | AutoTest.Net/ 126 | 127 | # Web workbench (sass) 128 | .sass-cache/ 129 | 130 | # Installshield output folder 131 | [Ee]xpress/ 132 | 133 | # DocProject is a documentation generator add-in 134 | DocProject/buildhelp/ 135 | DocProject/Help/*.HxT 136 | DocProject/Help/*.HxC 137 | DocProject/Help/*.hhc 138 | DocProject/Help/*.hhk 139 | DocProject/Help/*.hhp 140 | DocProject/Help/Html2 141 | DocProject/Help/html 142 | 143 | # Click-Once directory 144 | publish/ 145 | 146 | # Publish Web Output 147 | *.[Pp]ublish.xml 148 | *.azurePubxml 149 | # TODO: Comment the next line if you want to checkin your web deploy settings 150 | # but database connection strings (with potential passwords) will be unencrypted 151 | #*.pubxml 152 | *.publishproj 153 | 154 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 155 | # checkin your Azure Web App publish settings, but sensitive information contained 156 | # in these scripts will be unencrypted 157 | PublishScripts/ 158 | 159 | # NuGet Packages 160 | *.nupkg 161 | # The packages folder can be ignored because of Package Restore 162 | **/packages/* 163 | # except build/, which is used as an MSBuild target. 164 | !**/packages/build/ 165 | # Uncomment if necessary however generally it will be regenerated when needed 166 | #!**/packages/repositories.config 167 | # NuGet v3's project.json files produces more ignoreable files 168 | *.nuget.props 169 | *.nuget.targets 170 | 171 | # Microsoft Azure Build Output 172 | csx/ 173 | *.build.csdef 174 | 175 | # Microsoft Azure Emulator 176 | ecf/ 177 | rcf/ 178 | 179 | # Windows Store app package directories and files 180 | AppPackages/ 181 | BundleArtifacts/ 182 | Package.StoreAssociation.xml 183 | _pkginfo.txt 184 | 185 | # Visual Studio cache files 186 | # files ending in .cache can be ignored 187 | *.[Cc]ache 188 | # but keep track of directories ending in .cache 189 | !*.[Cc]ache/ 190 | 191 | # Others 192 | ClientBin/ 193 | ~$* 194 | *~ 195 | *.dbmdl 196 | *.dbproj.schemaview 197 | *.jfm 198 | *.pfx 199 | *.publishsettings 200 | node_modules/ 201 | orleans.codegen.cs 202 | 203 | # Since there are multiple workflows, uncomment next line to ignore bower_components 204 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 205 | #bower_components/ 206 | 207 | # RIA/Silverlight projects 208 | Generated_Code/ 209 | 210 | # Backup & report files from converting an old project file 211 | # to a newer Visual Studio version. Backup files are not needed, 212 | # because we have git ;-) 213 | _UpgradeReport_Files/ 214 | Backup*/ 215 | UpgradeLog*.XML 216 | UpgradeLog*.htm 217 | 218 | # SQL Server files 219 | *.mdf 220 | *.ldf 221 | 222 | # Business Intelligence projects 223 | *.rdl.data 224 | *.bim.layout 225 | *.bim_*.settings 226 | 227 | # Microsoft Fakes 228 | FakesAssemblies/ 229 | 230 | # GhostDoc plugin setting file 231 | *.GhostDoc.xml 232 | 233 | # Node.js Tools for Visual Studio 234 | .ntvs_analysis.dat 235 | 236 | # Visual Studio 6 build log 237 | *.plg 238 | 239 | # Visual Studio 6 workspace options file 240 | *.opt 241 | 242 | # Visual Studio LightSwitch build output 243 | **/*.HTMLClient/GeneratedArtifacts 244 | **/*.DesktopClient/GeneratedArtifacts 245 | **/*.DesktopClient/ModelManifest.xml 246 | **/*.Server/GeneratedArtifacts 247 | **/*.Server/ModelManifest.xml 248 | _Pvt_Extensions 249 | 250 | # Paket dependency manager 251 | .paket/paket.exe 252 | paket-files/ 253 | 254 | # FAKE - F# Make 255 | .fake/ 256 | 257 | # JetBrains Rider 258 | .idea/ 259 | *.sln.iml 260 | 261 | # CodeRush 262 | .cr/ 263 | 264 | # Python Tools for Visual Studio (PTVS) 265 | __pycache__/ 266 | *.pyc -------------------------------------------------------------------------------- /AdapterPattern/AdapterPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /AdapterPattern/IDuck.cs: -------------------------------------------------------------------------------- 1 | namespace AdapterPattern 2 | { 3 | public interface IDuck 4 | { 5 | void Quack(); 6 | void Fly(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /AdapterPattern/ITurkey.cs: -------------------------------------------------------------------------------- 1 | namespace AdapterPattern 2 | { 3 | public interface ITurkey 4 | { 5 | void Gobble(); 6 | void Fly(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /AdapterPattern/MallardDuck.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AdapterPattern 4 | { 5 | class MallardDuck : IDuck 6 | { 7 | public void Quack() 8 | { 9 | Console.WriteLine("Quack Quack Quack"); 10 | } 11 | 12 | public void Fly() 13 | { 14 | Console.WriteLine("Flies 500 Metres"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /AdapterPattern/Program.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace AdapterPattern 3 | { 4 | internal static class Program 5 | { 6 | private static void Main() 7 | { 8 | var turkey = new WildTurkey(); 9 | var adapter = new TurkeyAdapter(turkey); 10 | 11 | Tester(adapter); 12 | } 13 | 14 | private static void Tester(IDuck duck) 15 | { 16 | duck.Fly(); 17 | duck.Quack(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /AdapterPattern/TurkeyAdapter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AdapterPattern 4 | { 5 | public class TurkeyAdapter : IDuck 6 | { 7 | private readonly ITurkey _turkey; 8 | 9 | public TurkeyAdapter(ITurkey turkey) 10 | { 11 | _turkey = turkey; 12 | } 13 | public void Quack() 14 | { 15 | _turkey.Gobble(); 16 | } 17 | 18 | public void Fly() 19 | { 20 | for (var i = 0; i < 5; i++) 21 | { 22 | _turkey.Fly(); 23 | Console.WriteLine("Resting.."); 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /AdapterPattern/WildTurkey.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AdapterPattern 4 | { 5 | class WildTurkey : ITurkey 6 | { 7 | public void Gobble() 8 | { 9 | Console.WriteLine("Gobble Gobble Gobble"); 10 | } 11 | 12 | public void Fly() 13 | { 14 | Console.WriteLine("Flies 100 Metres"); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /BridgePattern/BridgePattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /BridgePattern/FlyingEnchantment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BridgePattern 4 | { 5 | public class FlyingEnchantment : IEnchantment 6 | { 7 | public void OnActivate() 8 | { 9 | Console.WriteLine("The item begins to glow faintly."); 10 | } 11 | 12 | public void Apply() 13 | { 14 | Console.WriteLine("The item flies and strikes the enemies finally returning to owner's hand."); 15 | } 16 | 17 | public void OnDeactivate() 18 | { 19 | Console.WriteLine("The item's glow fades."); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /BridgePattern/Hammer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BridgePattern 4 | { 5 | public class Hammer : IWeapon 6 | { 7 | private readonly IEnchantment _enchantment; 8 | public Hammer(IEnchantment enchantment) 9 | { 10 | _enchantment = enchantment; 11 | } 12 | 13 | public void Wield() 14 | { 15 | Console.WriteLine("The hammer is wielded."); 16 | _enchantment.OnActivate(); 17 | } 18 | 19 | public void Swing() 20 | { 21 | Console.WriteLine("The hammer is swinged."); 22 | _enchantment.Apply(); 23 | } 24 | 25 | public void Unwield() 26 | { 27 | Console.WriteLine("The hammer is unwielded."); 28 | _enchantment.OnDeactivate(); 29 | } 30 | 31 | public IEnchantment GetEnchantment() 32 | { 33 | return _enchantment; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /BridgePattern/IEnchantment.cs: -------------------------------------------------------------------------------- 1 | namespace BridgePattern 2 | { 3 | public interface IEnchantment 4 | { 5 | void OnActivate(); 6 | void Apply(); 7 | void OnDeactivate(); 8 | } 9 | } -------------------------------------------------------------------------------- /BridgePattern/IWeapon.cs: -------------------------------------------------------------------------------- 1 | namespace BridgePattern 2 | { 3 | public interface IWeapon 4 | { 5 | void Wield(); 6 | void Swing(); 7 | void Unwield(); 8 | IEnchantment GetEnchantment(); 9 | } 10 | } -------------------------------------------------------------------------------- /BridgePattern/Program.cs: -------------------------------------------------------------------------------- 1 | namespace BridgePattern 2 | { 3 | internal static class Program 4 | { 5 | private static void Main() 6 | { 7 | IWeapon sword = new Sword(new FlyingEnchantment()); 8 | sword.Wield(); 9 | sword.Swing(); 10 | sword.Unwield(); 11 | 12 | IWeapon hammer = new Hammer(new SoulEatingEnchantment()); 13 | hammer.Wield(); 14 | hammer.Swing(); 15 | hammer.Unwield(); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /BridgePattern/SoulEatingEnchantment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BridgePattern 4 | { 5 | public class SoulEatingEnchantment : IEnchantment 6 | { 7 | public void OnActivate() 8 | { 9 | Console.WriteLine("The item spreads bloodlust."); 10 | } 11 | 12 | public void Apply() 13 | { 14 | Console.WriteLine("The item eats the soul of enemies."); 15 | } 16 | 17 | public void OnDeactivate() 18 | { 19 | Console.WriteLine("Bloodlust slowly disappears."); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /BridgePattern/Sword.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BridgePattern 4 | { 5 | public class Sword : IWeapon 6 | { 7 | private readonly IEnchantment _enchantment; 8 | 9 | public Sword(IEnchantment enchantment) 10 | { 11 | _enchantment = enchantment; 12 | } 13 | 14 | public void Wield() 15 | { 16 | Console.WriteLine("The sword is wielded."); 17 | _enchantment.OnActivate(); 18 | } 19 | 20 | public void Swing() 21 | { 22 | Console.WriteLine("The sword is swinged."); 23 | _enchantment.Apply(); 24 | } 25 | 26 | public void Unwield() 27 | { 28 | Console.WriteLine("The sword is unwielded."); 29 | _enchantment.OnDeactivate(); 30 | } 31 | 32 | public IEnchantment GetEnchantment() 33 | { 34 | return _enchantment; 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /BuilderPattern/BuilderPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /BuilderPattern/Cook.cs: -------------------------------------------------------------------------------- 1 | namespace BuilderPattern 2 | { 3 | 4 | // This class can also be called the Director 5 | public class Cook 6 | { 7 | private IBuilder _builder; 8 | public Cook(IBuilder builder) 9 | { 10 | AcceptBuilder(builder); 11 | } 12 | 13 | public void ChangeBuilder(IBuilder builder) 14 | { 15 | AcceptBuilder(builder); 16 | } 17 | 18 | public Hamburger Build() 19 | { 20 | _builder.AddIngredients(); 21 | _builder.AddShape(); 22 | _builder.AddSize(); 23 | return _builder.Build(); 24 | } 25 | 26 | private void AcceptBuilder(IBuilder builder) 27 | { 28 | _builder = builder; 29 | _builder.Reset(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /BuilderPattern/Hamburger.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace BuilderPattern 3 | { 4 | public class Hamburger 5 | { 6 | public int Size { get; set; } 7 | public string Shape { get; set; } 8 | public string[] Ingredients { get; set; } 9 | public override string ToString() 10 | { 11 | var hamburger = ""; 12 | foreach (var ingredient in Ingredients) 13 | { 14 | hamburger += $"{ingredient} "; 15 | } 16 | return $"Ingredients: {hamburger}, Size: {Size}, Shape: {Shape}"; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /BuilderPattern/IBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace BuilderPattern 2 | { 3 | public interface IBuilder 4 | { 5 | void AddIngredients(); 6 | void AddShape(); 7 | void AddSize(); 8 | void Reset(); 9 | Hamburger Build(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /BuilderPattern/MyHamburgerBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace BuilderPattern 2 | { 3 | public class MyHamburgerBuilder : IBuilder 4 | { 5 | private Hamburger _hamburger; 6 | public void AddIngredients() 7 | { 8 | _hamburger.Ingredients = new string[] { "Bread", "Meat", "Tomato", "Salad", "Mayonnaise" }; 9 | } 10 | 11 | public void AddShape() 12 | { 13 | _hamburger.Shape = "Kite"; 14 | } 15 | 16 | public void AddSize() 17 | { 18 | _hamburger.Size = 10; //inches 19 | } 20 | public void Reset() 21 | { 22 | _hamburger = new Hamburger(); 23 | } 24 | 25 | public Hamburger Build() 26 | { 27 | return _hamburger; 28 | } 29 | 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /BuilderPattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BuilderPattern 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | var builder = new MyHamburgerBuilder(); 10 | var cook = new Cook(builder); 11 | var myHamburger = cook.Build(); 12 | 13 | cook.ChangeBuilder(new WifesHamburgerBuilder()); 14 | var wifesHamburger = cook.Build(); 15 | 16 | Console.WriteLine($"My Hamburger: {myHamburger}"); 17 | Console.WriteLine($"My Wife's Hamburger: {wifesHamburger}"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /BuilderPattern/WifesHamburgerBuilder.cs: -------------------------------------------------------------------------------- 1 | namespace BuilderPattern 2 | { 3 | 4 | public class WifesHamburgerBuilder : IBuilder 5 | { 6 | private Hamburger _hamburger; 7 | public void AddIngredients() 8 | { 9 | _hamburger.Ingredients = new string[] { "Bread", "Salad" }; 10 | } 11 | 12 | public void AddShape() 13 | { 14 | _hamburger.Shape = "Cuboid"; 15 | } 16 | 17 | public void AddSize() 18 | { 19 | _hamburger.Size = 6; //inches 20 | } 21 | 22 | public void Reset() 23 | { 24 | _hamburger = new Hamburger(); 25 | } 26 | public Hamburger Build() 27 | { 28 | return _hamburger; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation. 6 | 7 | ## Our Standards 8 | 9 | Examples of behavior that contributes to creating a positive environment include: 10 | 11 | * Using welcoming and inclusive language 12 | * Being respectful of differing viewpoints and experiences 13 | * Gracefully accepting constructive criticism 14 | * Focusing on what is best for the community 15 | * Showing empathy towards other community members 16 | 17 | Examples of unacceptable behavior by participants include: 18 | 19 | * The use of sexualized language or imagery and unwelcome sexual attention or advances 20 | * Trolling, insulting/derogatory comments, and personal or political attacks 21 | * Public or private harassment 22 | * Publishing others' private information, such as a physical or electronic address, without explicit permission 23 | * Other conduct which could reasonably be considered inappropriate in a professional setting 24 | 25 | ## Our Responsibilities 26 | 27 | Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior. 28 | 29 | Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. 30 | 31 | ## Scope 32 | 33 | This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. 34 | 35 | ## Enforcement 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at abishekaditya@outlook.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately. 38 | 39 | Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. 40 | 41 | ## Attribution 42 | 43 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version] 44 | 45 | [homepage]: http://contributor-covenant.org 46 | [version]: http://contributor-covenant.org/version/1/4/ 47 | -------------------------------------------------------------------------------- /ChainOfResponsibilityPattern/AdditionHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace ChainOfResponsibilityPattern { 6 | public class AdditionHandler : BaseHandler { 7 | public override double? Handle(double[] values, string action) { 8 | if (action.ToLower() == "add") { 9 | double result=0.0; 10 | foreach (var value in values) { 11 | result += value; 12 | } 13 | return result; 14 | } 15 | else { 16 | return _nextInLine?.Handle(values,action); 17 | } 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /ChainOfResponsibilityPattern/BaseHandler.cs: -------------------------------------------------------------------------------- 1 | namespace ChainOfResponsibilityPattern { 2 | public abstract class BaseHandler : IHandler { 3 | public void AddChain(IHandler handler) { 4 | _nextInLine = handler; 5 | } 6 | 7 | public abstract double? Handle(double[] values, string action); 8 | 9 | protected IHandler _nextInLine; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /ChainOfResponsibilityPattern/ChainOfResponsibilityPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ChainOfResponsibilityPattern/IHandler.cs: -------------------------------------------------------------------------------- 1 | namespace ChainOfResponsibilityPattern { 2 | public interface IHandler { 3 | void AddChain(IHandler handler); 4 | double? Handle(double[] values, string action); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /ChainOfResponsibilityPattern/MultiplicationHandler.cs: -------------------------------------------------------------------------------- 1 | namespace ChainOfResponsibilityPattern { 2 | public class MultiplicationHandler : BaseHandler { 3 | public override double? Handle(double[] values, string action) { 4 | if (action.ToLower() == "multiply") { 5 | var result = 1.0; 6 | foreach (var value in values) { 7 | result *= value; 8 | } 9 | return result; 10 | } 11 | else { 12 | return _nextInLine?.Handle(values, action); 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /ChainOfResponsibilityPattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ChainOfResponsibilityPattern { 4 | class Program { 5 | static void Main(string[] args) { 6 | //create handlers 7 | var additionHandler = new AdditionHandler(); 8 | var subtractionHandler = new SubtractionHandler(); 9 | var multiplicationHander = new MultiplicationHandler(); 10 | //create chain 11 | subtractionHandler.AddChain(multiplicationHander); 12 | additionHandler.AddChain(subtractionHandler); 13 | //Execution 14 | double[] numbers = new double[] { 2, 3, 4, 5 }; 15 | var additionResult = additionHandler.Handle(numbers, "Add"); 16 | var subtractionResult = additionHandler.Handle(numbers, "Minus"); 17 | var multResult = additionHandler.Handle(numbers, "Multiply"); 18 | var divisionResult = additionHandler.Handle(numbers, "divide"); // Divide is not in the chain!!! 19 | 20 | Console.WriteLine("Addition = {0}",additionResult); 21 | Console.WriteLine("Subtraction = {0}", subtractionResult); 22 | Console.WriteLine("Multiplication = {0}", multResult); 23 | Console.WriteLine("Division = {0}", divisionResult); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /ChainOfResponsibilityPattern/SubtractionHandler.cs: -------------------------------------------------------------------------------- 1 | namespace ChainOfResponsibilityPattern { 2 | public class SubtractionHandler : BaseHandler { 3 | public override double? Handle(double[] values, string action) { 4 | if (action.ToLower() == "minus") { 5 | var result = values[0]; 6 | for (int i = 1; i < values.Length; i++) { 7 | result -= values[i]; 8 | } 9 | return result; 10 | } 11 | else { 12 | return _nextInLine?.Handle(values, action); 13 | } 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /CommandPattern/CommandPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CommandPattern/Garage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CommandPattern 4 | { 5 | internal class Garage 6 | { 7 | private readonly string _name; 8 | 9 | public Garage(string name) 10 | { 11 | _name = name; 12 | } 13 | 14 | internal void Open() 15 | { 16 | Console.WriteLine($"{_name} Garage Opened"); 17 | } 18 | 19 | internal void Close() 20 | { 21 | Console.WriteLine($"{_name} Garage Closed"); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /CommandPattern/GarageDoorCloseCommand.cs: -------------------------------------------------------------------------------- 1 | namespace CommandPattern 2 | { 3 | internal class GarageDoorCloseCommand : ICommand 4 | { 5 | private readonly Garage _garage; 6 | 7 | public GarageDoorCloseCommand(Garage g) 8 | { 9 | _garage = g; 10 | } 11 | 12 | public void Execute() 13 | { 14 | _garage.Close(); 15 | } 16 | 17 | public void Undo() 18 | { 19 | _garage.Open(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /CommandPattern/GarageDoorOpenCommand.cs: -------------------------------------------------------------------------------- 1 | namespace CommandPattern 2 | { 3 | internal class GarageDoorOpenCommand : ICommand 4 | { 5 | private readonly Garage _garage; 6 | 7 | public GarageDoorOpenCommand(Garage g) 8 | { 9 | _garage = g; 10 | } 11 | 12 | public void Execute() 13 | { 14 | _garage.Open(); 15 | } 16 | 17 | public void Undo() 18 | { 19 | _garage.Close(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /CommandPattern/ICommand.cs: -------------------------------------------------------------------------------- 1 | namespace CommandPattern 2 | { 3 | internal interface ICommand 4 | { 5 | void Execute(); 6 | void Undo(); 7 | } 8 | } -------------------------------------------------------------------------------- /CommandPattern/Light.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CommandPattern 4 | { 5 | public class Light 6 | { 7 | private readonly string _name; 8 | 9 | public Light(string name) 10 | { 11 | _name = name; 12 | } 13 | 14 | internal void On() 15 | { 16 | Console.WriteLine($"{_name} Light On"); 17 | } 18 | 19 | internal void Off() 20 | { 21 | Console.WriteLine($"{_name} Light Off"); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /CommandPattern/LightOffCommand.cs: -------------------------------------------------------------------------------- 1 | namespace CommandPattern 2 | { 3 | internal class LightOffCommand : ICommand 4 | { 5 | private readonly Light _light; 6 | 7 | public LightOffCommand(Light l) 8 | { 9 | _light = l; 10 | } 11 | 12 | public void Execute() 13 | { 14 | _light.Off(); 15 | } 16 | 17 | public void Undo() 18 | { 19 | _light.On(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /CommandPattern/LightOnCommand.cs: -------------------------------------------------------------------------------- 1 | namespace CommandPattern 2 | { 3 | internal class LightOnCommand : ICommand 4 | { 5 | private readonly Light _light; 6 | 7 | public LightOnCommand(Light l) 8 | { 9 | _light = l; 10 | } 11 | 12 | public void Execute() 13 | { 14 | _light.On(); 15 | } 16 | 17 | public void Undo() 18 | { 19 | _light.Off(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /CommandPattern/MacroCommand.cs: -------------------------------------------------------------------------------- 1 | namespace CommandPattern 2 | { 3 | internal class MacroCommand : ICommand 4 | { 5 | private readonly ICommand[] _commands; 6 | 7 | public MacroCommand(ICommand[] commands) 8 | { 9 | _commands = commands; 10 | } 11 | 12 | public void Execute() 13 | { 14 | foreach (var item in _commands) 15 | item.Execute(); 16 | } 17 | 18 | public void Undo() 19 | { 20 | foreach (var item in _commands) 21 | item.Undo(); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /CommandPattern/NoCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CommandPattern 4 | { 5 | internal class NoCommand : ICommand 6 | { 7 | public void Execute() 8 | { 9 | Console.WriteLine("No Command Assigned"); 10 | } 11 | 12 | public void Undo() 13 | { 14 | Execute(); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /CommandPattern/OnOffStruct.cs: -------------------------------------------------------------------------------- 1 | namespace CommandPattern 2 | { 3 | internal struct OnOffStruct 4 | { 5 | public ICommand On; 6 | public ICommand Off; 7 | } 8 | } -------------------------------------------------------------------------------- /CommandPattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CommandPattern 4 | { 5 | internal static class Program 6 | { 7 | private static void Main() 8 | { 9 | var remote = new RemoteControl(3); 10 | 11 | 12 | var bike = new Garage("Bike"); 13 | var bikeDoorClose = new GarageDoorCloseCommand(bike); 14 | var bikeDoorOpen = new GarageDoorOpenCommand(bike); 15 | 16 | var car = new Garage("Car"); 17 | var carDoorClose = new GarageDoorCloseCommand(car); 18 | var carDoorOpen = new GarageDoorOpenCommand(car); 19 | 20 | var garageButton = new OnOffStruct 21 | { 22 | On = bikeDoorOpen, 23 | Off = bikeDoorClose 24 | }; 25 | 26 | remote[0] = garageButton; 27 | remote.PushOn(0); 28 | remote.PushUndo(); 29 | remote.PushUndo(); 30 | remote.PushOff(0); 31 | 32 | 33 | Console.WriteLine(); 34 | var light = new Light("Hall"); 35 | 36 | ICommand[] partyOn = { new LightOffCommand(light), bikeDoorOpen, carDoorOpen }; 37 | ICommand[] partyOff = { new LightOnCommand(light), bikeDoorClose, carDoorClose }; 38 | 39 | 40 | remote[2] = new OnOffStruct { On = new MacroCommand(partyOn), Off = new MacroCommand(partyOff) }; 41 | 42 | try 43 | { 44 | remote.PushOn(2); 45 | Console.WriteLine(); 46 | remote.PushOff(2); 47 | } 48 | catch (Exception) 49 | { 50 | Console.WriteLine("Oops"); 51 | } 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /CommandPattern/RemoteControl.cs: -------------------------------------------------------------------------------- 1 | namespace CommandPattern 2 | { 3 | internal class RemoteControl 4 | { 5 | private readonly ICommand[] _offCommand; 6 | private readonly ICommand[] _onCommand; 7 | private ICommand _undoCommand; 8 | 9 | public RemoteControl(int slots) 10 | { 11 | _onCommand = new ICommand[slots]; 12 | _offCommand = new ICommand[slots]; 13 | 14 | var none = new NoCommand(); 15 | _undoCommand = none; 16 | for (var i = 0; i < slots; i++) 17 | { 18 | _onCommand[i] = none; 19 | _offCommand[i] = none; 20 | } 21 | } 22 | 23 | 24 | public OnOffStruct this[int i] 25 | { 26 | set 27 | { 28 | _onCommand[i] = value.On; 29 | _offCommand[i] = value.Off; 30 | } 31 | } 32 | 33 | public void PushOn(int slot) 34 | { 35 | _onCommand[slot].Execute(); 36 | _undoCommand = _offCommand[slot]; 37 | } 38 | 39 | public void PushOff(int slot) 40 | { 41 | _offCommand[slot].Execute(); 42 | _undoCommand = _onCommand[slot]; 43 | } 44 | 45 | public void PushUndo() 46 | { 47 | _undoCommand.Execute(); 48 | } 49 | } 50 | } -------------------------------------------------------------------------------- /CompositePattern/Client.cs: -------------------------------------------------------------------------------- 1 | namespace CompositePattern 2 | { 3 | public class Client 4 | { 5 | private readonly MenuComponent _menus; 6 | 7 | public Client(MenuComponent menus) 8 | { 9 | _menus = menus; 10 | } 11 | 12 | public void Print() 13 | { 14 | _menus.Print(); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /CompositePattern/CompositePattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /CompositePattern/Menu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace CompositePattern 5 | { 6 | public class Menu : MenuComponent 7 | { 8 | List _components = new List(); 9 | 10 | public Menu(string name, string description) 11 | { 12 | Name = name; 13 | Description = description; 14 | 15 | } 16 | 17 | public override void Add(MenuComponent component) 18 | { 19 | _components.Add(component); 20 | } 21 | 22 | public override void Remove(MenuComponent component) 23 | { 24 | _components.Remove(component); 25 | } 26 | 27 | public override MenuComponent GetChild(int i) 28 | { 29 | return _components[i]; 30 | } 31 | 32 | public override string Name { get; } 33 | 34 | public override string Description { get; } 35 | 36 | public override void Print() 37 | { 38 | Console.WriteLine(Name); 39 | Console.WriteLine("___________"); 40 | foreach (var menuComponent in _components) 41 | { 42 | menuComponent.Print(); 43 | } 44 | Console.WriteLine(); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /CompositePattern/MenuComponent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CompositePattern 4 | { 5 | public class MenuComponent 6 | { 7 | public virtual void Add(MenuComponent component) 8 | { 9 | throw new NotImplementedException(); 10 | } 11 | 12 | public virtual void Remove(MenuComponent component) 13 | { 14 | throw new NotImplementedException(); 15 | } 16 | 17 | public virtual MenuComponent GetChild(int i) 18 | { 19 | throw new NotImplementedException(); 20 | } 21 | 22 | public virtual string Name { get; } 23 | public virtual string Description { get; } 24 | public virtual bool Vegetarian { get; } 25 | public virtual double Price { get; } 26 | 27 | public virtual void Print() 28 | { 29 | throw new NotImplementedException(); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /CompositePattern/MenuItem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace CompositePattern 4 | { 5 | public class MenuItem : MenuComponent 6 | { 7 | public MenuItem(string name, string description, double price, bool isveg) 8 | { 9 | Name = name; 10 | Description = description; 11 | Price = price; 12 | Vegetarian = isveg; 13 | } 14 | 15 | public override string Name { get; } 16 | 17 | public override string Description { get; } 18 | 19 | public override double Price { get; } 20 | 21 | public override bool Vegetarian { get; } 22 | 23 | public override void Print() 24 | { 25 | Console.WriteLine($"{Name} : {Price} {(Vegetarian ? '+' : '*')} \n {Description}"); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /CompositePattern/Program.cs: -------------------------------------------------------------------------------- 1 | namespace CompositePattern 2 | { 3 | static class Program 4 | { 5 | public static void Main() 6 | { 7 | 8 | var breakfast = new Menu("Breakfast", "Pancake House"); 9 | var lunch = new Menu("Lunch", "Deli Diner"); 10 | var dinner = new Menu("Dinner", "Dinneroni"); 11 | 12 | var dessert = new Menu("Dessert", "Ice Cream"); 13 | 14 | var menu = new Menu("All", "McDonalds"); 15 | 16 | breakfast.Add(new MenuItem("Waffles", "Butterscotch waffles", 140, false)); 17 | breakfast.Add(new MenuItem("Corn Flakes", "Kellogs", 80, true)); 18 | 19 | lunch.Add(new MenuItem("Burger", "Cheese and Onion Burger", 250, true)); 20 | lunch.Add(new MenuItem("Sandwich", "Chicken Sandwich", 280, false)); 21 | 22 | dinner.Add(new MenuItem("Pizza", "Cheese and Tomato Pizza", 210, true)); 23 | dinner.Add(new MenuItem("Pasta", "Chicken Pasta", 280, false)); 24 | 25 | dessert.Add(new MenuItem("Ice Cream", "Vanilla and Chocolate", 120, true)); 26 | dessert.Add(new MenuItem("Cake", "Choclate Cake Slice", 180, false)); 27 | 28 | dinner.Add(dessert); 29 | menu.Add(breakfast); 30 | menu.Add(lunch); 31 | menu.Add(dinner); 32 | 33 | menu.Print(); 34 | 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /DecoratorPattern/Beverage.cs: -------------------------------------------------------------------------------- 1 | namespace DecoratorPattern 2 | { 3 | abstract class Beverage 4 | { 5 | protected string _description = "No Description"; 6 | public abstract string Description { get; } 7 | public abstract double Cost(); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /DecoratorPattern/CondimentDecorator.cs: -------------------------------------------------------------------------------- 1 | namespace DecoratorPattern 2 | { 3 | abstract class CondimentDecorator : Beverage 4 | { 5 | public abstract override string Description { get; } 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /DecoratorPattern/DarkRoast.cs: -------------------------------------------------------------------------------- 1 | namespace DecoratorPattern 2 | { 3 | internal class DarkRoast : Beverage 4 | { 5 | public DarkRoast() 6 | { 7 | _description = "Dark Roast"; 8 | } 9 | 10 | public override string Description => _description; 11 | 12 | public override double Cost() 13 | { 14 | return 1.49; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /DecoratorPattern/DecoratorPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /DecoratorPattern/Espresso.cs: -------------------------------------------------------------------------------- 1 | namespace DecoratorPattern 2 | { 3 | class Espresso : Beverage 4 | { 5 | public Espresso() 6 | { 7 | _description = "Espresso"; 8 | } 9 | 10 | public override string Description => _description; 11 | 12 | public override double Cost() 13 | { 14 | return 1.99; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DecoratorPattern/HouseBlend.cs: -------------------------------------------------------------------------------- 1 | namespace DecoratorPattern 2 | { 3 | class HouseBlend : Beverage 4 | { 5 | public HouseBlend() 6 | { 7 | _description = "House Blend"; 8 | } 9 | 10 | public override string Description => _description; 11 | 12 | public override double Cost() 13 | { 14 | return 2.49; 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /DecoratorPattern/MochaCondiment.cs: -------------------------------------------------------------------------------- 1 | namespace DecoratorPattern 2 | { 3 | class MochaCondiment : CondimentDecorator 4 | { 5 | Beverage _beverage; 6 | 7 | public MochaCondiment(Beverage beverage) 8 | { 9 | this._beverage = beverage; 10 | } 11 | 12 | public override string Description 13 | { 14 | get 15 | { 16 | if (_beverage.Description.StartsWith("Mocha")) 17 | { 18 | return "Double " + _beverage.Description; 19 | } 20 | else 21 | return "Mocha " + _beverage.Description; 22 | } 23 | } 24 | 25 | public override double Cost() 26 | { 27 | return 0.2 + _beverage.Cost(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DecoratorPattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DecoratorPattern 4 | { 5 | static class Program 6 | { 7 | static void Main() 8 | { 9 | Beverage beverage = new Espresso(); 10 | Console.WriteLine(beverage.Description + " $" + beverage.Cost()); 11 | 12 | Beverage beverage2 = new DarkRoast(); 13 | beverage2 = new MochaCondiment(beverage2); 14 | beverage2 = new MochaCondiment(beverage2); 15 | beverage2 = new WhipCondiment(beverage2); 16 | Console.WriteLine(beverage2.Description + " $" + beverage2.Cost()); 17 | 18 | Beverage beverage3 = new HouseBlend(); 19 | beverage3 = new MochaCondiment(beverage3); 20 | beverage3 = new WhipCondiment(beverage3); 21 | Console.WriteLine(beverage3.Description + " $" + beverage3.Cost()); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /DecoratorPattern/WhipCondiment.cs: -------------------------------------------------------------------------------- 1 | namespace DecoratorPattern 2 | { 3 | class WhipCondiment : CondimentDecorator 4 | { 5 | Beverage _beverage; 6 | 7 | public WhipCondiment(Beverage beverage) 8 | { 9 | this._beverage = beverage; 10 | } 11 | 12 | public override string Description 13 | { 14 | get 15 | { 16 | if (_beverage.Description.StartsWith("Whip")) 17 | { 18 | return "Double " + _beverage.Description; 19 | } 20 | else 21 | return "Whip " + _beverage.Description; 22 | } 23 | } 24 | 25 | public override double Cost() 26 | { 27 | return 0.15 + _beverage.Cost(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DesignPatternsDotNetCore.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio Version 17 3 | VisualStudioVersion = 17.3.32825.248 4 | MinimumVisualStudioVersion = 15.0.26124.0 5 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdapterPattern", "AdapterPattern\AdapterPattern.csproj", "{79553F75-E8DC-4988-B511-A79CC6A9CDF7}" 6 | EndProject 7 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandPattern", "CommandPattern\CommandPattern.csproj", "{AC6ED373-CE32-41E4-B38A-64B8700E46C9}" 8 | EndProject 9 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompositePattern", "CompositePattern\CompositePattern.csproj", "{21754CEF-8885-4EED-91AF-73FF2514553D}" 10 | EndProject 11 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DecoratorPattern", "DecoratorPattern\DecoratorPattern.csproj", "{5F9D7DA1-FA5E-477C-87D7-2A131C99D090}" 12 | EndProject 13 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FacadePattern", "FacadePattern\FacadePattern.csproj", "{CCDDFDE2-9958-476C-91DB-9F2907887ADE}" 14 | EndProject 15 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}" 16 | EndProject 17 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ObserverPattern", "ObserverPattern\ObserverPattern.csproj", "{DDE34459-B244-43F9-8DE2-8D1E70CB6609}" 18 | EndProject 19 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SingletonPattern", "SingletonPattern\SingletonPattern.csproj", "{FA82E572-FC03-4C87-86FA-660AF06AA24A}" 20 | EndProject 21 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StatePattern", "StatePattern\StatePattern.csproj", "{1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}" 22 | EndProject 23 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StrategyPattern", "StrategyPattern\StrategyPattern.csproj", "{73D240B4-428D-4BC1-BC3C-C14D1D9D806E}" 24 | EndProject 25 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TemplatePattern", "TemplatePattern\TemplatePattern.csproj", "{928CD7B2-BB6E-4E07-834E-67B20CA6D698}" 26 | EndProject 27 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BridgePattern", "BridgePattern\BridgePattern.csproj", "{95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}" 28 | EndProject 29 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlyweightPattern", "FlyweightPattern\FlyweightPattern.csproj", "{0067EFC3-FC8D-4F1A-AA92-12323CAD0200}" 30 | EndProject 31 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VisitorPattern", "VisitorPattern\VisitorPattern.csproj", "{871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}" 32 | EndProject 33 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryPattern", "FactoryPattern\FactoryPattern.csproj", "{182B58DC-6787-4A09-8BCF-87A96737E5A6}" 34 | EndProject 35 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrototypePattern", "PrototypePattern\PrototypePattern.csproj", "{2DC00E3D-2099-4C58-B98F-B6B3F285739F}" 36 | EndProject 37 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatorPattern", "MediatorPattern\MediatorPattern.csproj", "{82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}" 38 | EndProject 39 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuilderPattern", "BuilderPattern\BuilderPattern.csproj", "{274786D8-2E30-40D7-81B5-DFA3872CF9B6}" 40 | EndProject 41 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}" 42 | EndProject 43 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SingletonPattern.Tests", "SingletonPattern.Tests\SingletonPattern.Tests.csproj", "{7A21A074-AEBB-4B33-80BA-D7DFBE87A449}" 44 | EndProject 45 | Global 46 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 47 | Debug|Any CPU = Debug|Any CPU 48 | Debug|x64 = Debug|x64 49 | Debug|x86 = Debug|x86 50 | Release|Any CPU = Release|Any CPU 51 | Release|x64 = Release|x64 52 | Release|x86 = Release|x86 53 | EndGlobalSection 54 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 55 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 56 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Debug|Any CPU.Build.0 = Debug|Any CPU 57 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Debug|x64.ActiveCfg = Debug|Any CPU 58 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Debug|x64.Build.0 = Debug|Any CPU 59 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Debug|x86.ActiveCfg = Debug|Any CPU 60 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Debug|x86.Build.0 = Debug|Any CPU 61 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Release|Any CPU.ActiveCfg = Release|Any CPU 62 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Release|Any CPU.Build.0 = Release|Any CPU 63 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Release|x64.ActiveCfg = Release|Any CPU 64 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Release|x64.Build.0 = Release|Any CPU 65 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Release|x86.ActiveCfg = Release|Any CPU 66 | {79553F75-E8DC-4988-B511-A79CC6A9CDF7}.Release|x86.Build.0 = Release|Any CPU 67 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 68 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Debug|Any CPU.Build.0 = Debug|Any CPU 69 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Debug|x64.ActiveCfg = Debug|Any CPU 70 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Debug|x64.Build.0 = Debug|Any CPU 71 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Debug|x86.ActiveCfg = Debug|Any CPU 72 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Debug|x86.Build.0 = Debug|Any CPU 73 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Release|Any CPU.ActiveCfg = Release|Any CPU 74 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Release|Any CPU.Build.0 = Release|Any CPU 75 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Release|x64.ActiveCfg = Release|Any CPU 76 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Release|x64.Build.0 = Release|Any CPU 77 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Release|x86.ActiveCfg = Release|Any CPU 78 | {AC6ED373-CE32-41E4-B38A-64B8700E46C9}.Release|x86.Build.0 = Release|Any CPU 79 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 80 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Debug|Any CPU.Build.0 = Debug|Any CPU 81 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Debug|x64.ActiveCfg = Debug|Any CPU 82 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Debug|x64.Build.0 = Debug|Any CPU 83 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Debug|x86.ActiveCfg = Debug|Any CPU 84 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Debug|x86.Build.0 = Debug|Any CPU 85 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Release|Any CPU.ActiveCfg = Release|Any CPU 86 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Release|Any CPU.Build.0 = Release|Any CPU 87 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Release|x64.ActiveCfg = Release|Any CPU 88 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Release|x64.Build.0 = Release|Any CPU 89 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Release|x86.ActiveCfg = Release|Any CPU 90 | {21754CEF-8885-4EED-91AF-73FF2514553D}.Release|x86.Build.0 = Release|Any CPU 91 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 92 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Debug|Any CPU.Build.0 = Debug|Any CPU 93 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Debug|x64.ActiveCfg = Debug|Any CPU 94 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Debug|x64.Build.0 = Debug|Any CPU 95 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Debug|x86.ActiveCfg = Debug|Any CPU 96 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Debug|x86.Build.0 = Debug|Any CPU 97 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Release|Any CPU.ActiveCfg = Release|Any CPU 98 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Release|Any CPU.Build.0 = Release|Any CPU 99 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Release|x64.ActiveCfg = Release|Any CPU 100 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Release|x64.Build.0 = Release|Any CPU 101 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Release|x86.ActiveCfg = Release|Any CPU 102 | {5F9D7DA1-FA5E-477C-87D7-2A131C99D090}.Release|x86.Build.0 = Release|Any CPU 103 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 104 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Debug|Any CPU.Build.0 = Debug|Any CPU 105 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Debug|x64.ActiveCfg = Debug|Any CPU 106 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Debug|x64.Build.0 = Debug|Any CPU 107 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Debug|x86.ActiveCfg = Debug|Any CPU 108 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Debug|x86.Build.0 = Debug|Any CPU 109 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Release|Any CPU.ActiveCfg = Release|Any CPU 110 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Release|Any CPU.Build.0 = Release|Any CPU 111 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Release|x64.ActiveCfg = Release|Any CPU 112 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Release|x64.Build.0 = Release|Any CPU 113 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Release|x86.ActiveCfg = Release|Any CPU 114 | {CCDDFDE2-9958-476C-91DB-9F2907887ADE}.Release|x86.Build.0 = Release|Any CPU 115 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 116 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Debug|Any CPU.Build.0 = Debug|Any CPU 117 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Debug|x64.ActiveCfg = Debug|Any CPU 118 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Debug|x64.Build.0 = Debug|Any CPU 119 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Debug|x86.ActiveCfg = Debug|Any CPU 120 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Debug|x86.Build.0 = Debug|Any CPU 121 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Release|Any CPU.ActiveCfg = Release|Any CPU 122 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Release|Any CPU.Build.0 = Release|Any CPU 123 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Release|x64.ActiveCfg = Release|Any CPU 124 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Release|x64.Build.0 = Release|Any CPU 125 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Release|x86.ActiveCfg = Release|Any CPU 126 | {C50111DC-B037-4A4C-ACA1-5BDE93F7D42A}.Release|x86.Build.0 = Release|Any CPU 127 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 128 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Debug|Any CPU.Build.0 = Debug|Any CPU 129 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Debug|x64.ActiveCfg = Debug|Any CPU 130 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Debug|x64.Build.0 = Debug|Any CPU 131 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Debug|x86.ActiveCfg = Debug|Any CPU 132 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Debug|x86.Build.0 = Debug|Any CPU 133 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Release|Any CPU.ActiveCfg = Release|Any CPU 134 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Release|Any CPU.Build.0 = Release|Any CPU 135 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Release|x64.ActiveCfg = Release|Any CPU 136 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Release|x64.Build.0 = Release|Any CPU 137 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Release|x86.ActiveCfg = Release|Any CPU 138 | {DDE34459-B244-43F9-8DE2-8D1E70CB6609}.Release|x86.Build.0 = Release|Any CPU 139 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 140 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Debug|Any CPU.Build.0 = Debug|Any CPU 141 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Debug|x64.ActiveCfg = Debug|Any CPU 142 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Debug|x64.Build.0 = Debug|Any CPU 143 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Debug|x86.ActiveCfg = Debug|Any CPU 144 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Debug|x86.Build.0 = Debug|Any CPU 145 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Release|Any CPU.ActiveCfg = Release|Any CPU 146 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Release|Any CPU.Build.0 = Release|Any CPU 147 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Release|x64.ActiveCfg = Release|Any CPU 148 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Release|x64.Build.0 = Release|Any CPU 149 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Release|x86.ActiveCfg = Release|Any CPU 150 | {FA82E572-FC03-4C87-86FA-660AF06AA24A}.Release|x86.Build.0 = Release|Any CPU 151 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 152 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Debug|Any CPU.Build.0 = Debug|Any CPU 153 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Debug|x64.ActiveCfg = Debug|Any CPU 154 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Debug|x64.Build.0 = Debug|Any CPU 155 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Debug|x86.ActiveCfg = Debug|Any CPU 156 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Debug|x86.Build.0 = Debug|Any CPU 157 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Release|Any CPU.ActiveCfg = Release|Any CPU 158 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Release|Any CPU.Build.0 = Release|Any CPU 159 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Release|x64.ActiveCfg = Release|Any CPU 160 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Release|x64.Build.0 = Release|Any CPU 161 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Release|x86.ActiveCfg = Release|Any CPU 162 | {1C0A4103-B8E6-4AE1-88DA-B3EBC37B2441}.Release|x86.Build.0 = Release|Any CPU 163 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 164 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Debug|Any CPU.Build.0 = Debug|Any CPU 165 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Debug|x64.ActiveCfg = Debug|Any CPU 166 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Debug|x64.Build.0 = Debug|Any CPU 167 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Debug|x86.ActiveCfg = Debug|Any CPU 168 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Debug|x86.Build.0 = Debug|Any CPU 169 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Release|Any CPU.ActiveCfg = Release|Any CPU 170 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Release|Any CPU.Build.0 = Release|Any CPU 171 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Release|x64.ActiveCfg = Release|Any CPU 172 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Release|x64.Build.0 = Release|Any CPU 173 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Release|x86.ActiveCfg = Release|Any CPU 174 | {73D240B4-428D-4BC1-BC3C-C14D1D9D806E}.Release|x86.Build.0 = Release|Any CPU 175 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 176 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Debug|Any CPU.Build.0 = Debug|Any CPU 177 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Debug|x64.ActiveCfg = Debug|Any CPU 178 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Debug|x64.Build.0 = Debug|Any CPU 179 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Debug|x86.ActiveCfg = Debug|Any CPU 180 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Debug|x86.Build.0 = Debug|Any CPU 181 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|Any CPU.ActiveCfg = Release|Any CPU 182 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|Any CPU.Build.0 = Release|Any CPU 183 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|x64.ActiveCfg = Release|Any CPU 184 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|x64.Build.0 = Release|Any CPU 185 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|x86.ActiveCfg = Release|Any CPU 186 | {928CD7B2-BB6E-4E07-834E-67B20CA6D698}.Release|x86.Build.0 = Release|Any CPU 187 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 188 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|Any CPU.Build.0 = Debug|Any CPU 189 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|x64.ActiveCfg = Debug|Any CPU 190 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|x64.Build.0 = Debug|Any CPU 191 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|x86.ActiveCfg = Debug|Any CPU 192 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Debug|x86.Build.0 = Debug|Any CPU 193 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|Any CPU.ActiveCfg = Release|Any CPU 194 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|Any CPU.Build.0 = Release|Any CPU 195 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|x64.ActiveCfg = Release|Any CPU 196 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|x64.Build.0 = Release|Any CPU 197 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|x86.ActiveCfg = Release|Any CPU 198 | {95E7F42A-74B9-4071-9A3C-86EF95A0EB1B}.Release|x86.Build.0 = Release|Any CPU 199 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 200 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Debug|Any CPU.Build.0 = Debug|Any CPU 201 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Debug|x64.ActiveCfg = Debug|Any CPU 202 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Debug|x64.Build.0 = Debug|Any CPU 203 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Debug|x86.ActiveCfg = Debug|Any CPU 204 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Debug|x86.Build.0 = Debug|Any CPU 205 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Release|Any CPU.ActiveCfg = Release|Any CPU 206 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Release|Any CPU.Build.0 = Release|Any CPU 207 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Release|x64.ActiveCfg = Release|Any CPU 208 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Release|x64.Build.0 = Release|Any CPU 209 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Release|x86.ActiveCfg = Release|Any CPU 210 | {0067EFC3-FC8D-4F1A-AA92-12323CAD0200}.Release|x86.Build.0 = Release|Any CPU 211 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 212 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Debug|Any CPU.Build.0 = Debug|Any CPU 213 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Debug|x64.ActiveCfg = Debug|Any CPU 214 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Debug|x64.Build.0 = Debug|Any CPU 215 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Debug|x86.ActiveCfg = Debug|Any CPU 216 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Debug|x86.Build.0 = Debug|Any CPU 217 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Release|Any CPU.ActiveCfg = Release|Any CPU 218 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Release|Any CPU.Build.0 = Release|Any CPU 219 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Release|x64.ActiveCfg = Release|Any CPU 220 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Release|x64.Build.0 = Release|Any CPU 221 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Release|x86.ActiveCfg = Release|Any CPU 222 | {871F7BE0-B75F-4F9E-9C4D-CE9C4ECE7A88}.Release|x86.Build.0 = Release|Any CPU 223 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 224 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Debug|Any CPU.Build.0 = Debug|Any CPU 225 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Debug|x64.ActiveCfg = Debug|Any CPU 226 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Debug|x64.Build.0 = Debug|Any CPU 227 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Debug|x86.ActiveCfg = Debug|Any CPU 228 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Debug|x86.Build.0 = Debug|Any CPU 229 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Release|Any CPU.ActiveCfg = Release|Any CPU 230 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Release|Any CPU.Build.0 = Release|Any CPU 231 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Release|x64.ActiveCfg = Release|Any CPU 232 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Release|x64.Build.0 = Release|Any CPU 233 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Release|x86.ActiveCfg = Release|Any CPU 234 | {182B58DC-6787-4A09-8BCF-87A96737E5A6}.Release|x86.Build.0 = Release|Any CPU 235 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 236 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Debug|Any CPU.Build.0 = Debug|Any CPU 237 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Debug|x64.ActiveCfg = Debug|Any CPU 238 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Debug|x64.Build.0 = Debug|Any CPU 239 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Debug|x86.ActiveCfg = Debug|Any CPU 240 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Debug|x86.Build.0 = Debug|Any CPU 241 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Release|Any CPU.ActiveCfg = Release|Any CPU 242 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Release|Any CPU.Build.0 = Release|Any CPU 243 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Release|x64.ActiveCfg = Release|Any CPU 244 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Release|x64.Build.0 = Release|Any CPU 245 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Release|x86.ActiveCfg = Release|Any CPU 246 | {2DC00E3D-2099-4C58-B98F-B6B3F285739F}.Release|x86.Build.0 = Release|Any CPU 247 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 248 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Debug|Any CPU.Build.0 = Debug|Any CPU 249 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Debug|x64.ActiveCfg = Debug|Any CPU 250 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Debug|x64.Build.0 = Debug|Any CPU 251 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Debug|x86.ActiveCfg = Debug|Any CPU 252 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Debug|x86.Build.0 = Debug|Any CPU 253 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Release|Any CPU.ActiveCfg = Release|Any CPU 254 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Release|Any CPU.Build.0 = Release|Any CPU 255 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Release|x64.ActiveCfg = Release|Any CPU 256 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Release|x64.Build.0 = Release|Any CPU 257 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Release|x86.ActiveCfg = Release|Any CPU 258 | {82A66FF6-5D66-4E39-8FF8-C8EBA0EB3A2D}.Release|x86.Build.0 = Release|Any CPU 259 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 260 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Debug|Any CPU.Build.0 = Debug|Any CPU 261 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Debug|x64.ActiveCfg = Debug|Any CPU 262 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Debug|x64.Build.0 = Debug|Any CPU 263 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Debug|x86.ActiveCfg = Debug|Any CPU 264 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Debug|x86.Build.0 = Debug|Any CPU 265 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Release|Any CPU.ActiveCfg = Release|Any CPU 266 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Release|Any CPU.Build.0 = Release|Any CPU 267 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Release|x64.ActiveCfg = Release|Any CPU 268 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Release|x64.Build.0 = Release|Any CPU 269 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Release|x86.ActiveCfg = Release|Any CPU 270 | {274786D8-2E30-40D7-81B5-DFA3872CF9B6}.Release|x86.Build.0 = Release|Any CPU 271 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 272 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Debug|Any CPU.Build.0 = Debug|Any CPU 273 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Debug|x64.ActiveCfg = Debug|Any CPU 274 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Debug|x64.Build.0 = Debug|Any CPU 275 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Debug|x86.ActiveCfg = Debug|Any CPU 276 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Debug|x86.Build.0 = Debug|Any CPU 277 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|Any CPU.ActiveCfg = Release|Any CPU 278 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|Any CPU.Build.0 = Release|Any CPU 279 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x64.ActiveCfg = Release|Any CPU 280 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x64.Build.0 = Release|Any CPU 281 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x86.ActiveCfg = Release|Any CPU 282 | {0F8297F6-FA4F-44B6-BF99-0FF71ECCF87A}.Release|x86.Build.0 = Release|Any CPU 283 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 284 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|Any CPU.Build.0 = Debug|Any CPU 285 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|x64.ActiveCfg = Debug|Any CPU 286 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|x64.Build.0 = Debug|Any CPU 287 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|x86.ActiveCfg = Debug|Any CPU 288 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Debug|x86.Build.0 = Debug|Any CPU 289 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|Any CPU.ActiveCfg = Release|Any CPU 290 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|Any CPU.Build.0 = Release|Any CPU 291 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|x64.ActiveCfg = Release|Any CPU 292 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|x64.Build.0 = Release|Any CPU 293 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|x86.ActiveCfg = Release|Any CPU 294 | {7A21A074-AEBB-4B33-80BA-D7DFBE87A449}.Release|x86.Build.0 = Release|Any CPU 295 | EndGlobalSection 296 | GlobalSection(SolutionProperties) = preSolution 297 | HideSolutionNode = FALSE 298 | EndGlobalSection 299 | GlobalSection(ExtensibilityGlobals) = postSolution 300 | SolutionGuid = {A3449832-9F8D-413F-8336-8EBEEB77353B} 301 | EndGlobalSection 302 | EndGlobal 303 | -------------------------------------------------------------------------------- /FacadePattern/Dimmer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FacadePattern 4 | { 5 | public class Dimmer 6 | { 7 | internal void Dim(int val) 8 | { 9 | Console.WriteLine(val == 10 ? "Turning Lights On" : $"Dimming lights to {val}"); 10 | } 11 | 12 | internal void Off() => Console.WriteLine("Switching off lights"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /FacadePattern/Dvd.cs: -------------------------------------------------------------------------------- 1 | namespace FacadePattern 2 | { 3 | public class Dvd 4 | { 5 | public Dvd(string name) 6 | { 7 | Movie = name; 8 | } 9 | public string Movie { get; set; } 10 | } 11 | } -------------------------------------------------------------------------------- /FacadePattern/DvdPlayer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FacadePattern 4 | { 5 | public class DvdPlayer 6 | { 7 | private Dvd _dvd; 8 | private int _time = 0; 9 | public void On() => Console.WriteLine("DVD Player powered on"); 10 | 11 | public void Insert(Dvd dvd) 12 | { 13 | _dvd = dvd; 14 | Console.WriteLine($"Inserting {dvd.Movie}"); 15 | 16 | } 17 | 18 | public void Play() => Console.WriteLine($"Playing {_dvd.Movie}"); 19 | 20 | public void Pause() 21 | { 22 | Console.WriteLine($"Pausing at {_time = (new Random()).Next(_time, _time + 120)}"); 23 | } 24 | 25 | public void Resume() 26 | { 27 | Console.WriteLine($"Resuming from {_time}"); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /FacadePattern/FacadePattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /FacadePattern/HometheaterFacade.cs: -------------------------------------------------------------------------------- 1 | namespace FacadePattern 2 | { 3 | public class HomeTheatreFacade 4 | { 5 | private Dimmer _dimmer; 6 | private Dvd _dvd; 7 | private DvdPlayer _dvdPlayer; 8 | 9 | public HomeTheatreFacade(Dimmer dimmer, Dvd dvd, DvdPlayer dvdPlayer) 10 | { 11 | _dvd = dvd; 12 | _dimmer = dimmer; 13 | _dvdPlayer = dvdPlayer; 14 | } 15 | 16 | public void WatchMovie() 17 | { 18 | _dimmer.Dim(5); 19 | _dvdPlayer.On(); 20 | _dvdPlayer.Insert(_dvd); 21 | _dvdPlayer.Play(); 22 | } 23 | 24 | public void Pause() 25 | { 26 | _dimmer.Dim(10); 27 | _dvdPlayer.Pause(); 28 | } 29 | 30 | public void Resume() 31 | { 32 | _dimmer.Dim(5); 33 | _dvdPlayer.Resume(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /FacadePattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FacadePattern 4 | { 5 | internal static class Program 6 | { 7 | private static void Main() 8 | { 9 | var dimmer = new Dimmer(); 10 | var dvdPlayer = new DvdPlayer(); 11 | var dvd = new Dvd("Gone with the Wind 2 : Electric Bugaloo"); 12 | var homeTheater = new HomeTheatreFacade(dimmer, dvd, dvdPlayer); 13 | 14 | homeTheater.WatchMovie(); 15 | Console.WriteLine(); 16 | homeTheater.Pause(); 17 | Console.WriteLine(); 18 | homeTheater.Resume(); 19 | Console.WriteLine(); 20 | homeTheater.Pause(); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/ChicagoIngredientsFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace FactoryPattern 4 | { 5 | internal class ChicagoIngredientsFactory : IIngredientsFactory 6 | { 7 | ICheese IIngredientsFactory.CreateCheese() 8 | { 9 | return new Parmesan(); 10 | } 11 | 12 | IClam IIngredientsFactory.CreateClam() 13 | { 14 | return new FreshClam(); 15 | } 16 | 17 | IDough IIngredientsFactory.CreateDough() 18 | { 19 | return new DeepDish(); 20 | } 21 | 22 | ISauce IIngredientsFactory.CreateSauce() 23 | { 24 | return new PlumTomato(); 25 | } 26 | 27 | IEnumerable IIngredientsFactory.CreateVeggies() 28 | { 29 | var oni = new Onion(); 30 | var ccm = new Cucumber(); 31 | var ppr = new Pepper(); 32 | IVeggies[] arr = { oni, ccm, ppr }; 33 | return arr; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/IIngredientsFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace FactoryPattern 4 | { 5 | interface IIngredientsFactory 6 | { 7 | IDough CreateDough(); 8 | IEnumerable CreateVeggies(); 9 | ISauce CreateSauce(); 10 | ICheese CreateCheese(); 11 | IClam CreateClam(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/CherryTomato.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class CherryTomato : ISauce 4 | { 5 | public string Name => "Cherry Tomato"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Cucumber.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class Cucumber : IVeggies 4 | { 5 | public string Name => "Cucumber"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/DeepDish.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class DeepDish : IDough 4 | { 5 | public string Name => "Deep Dish"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/FreshClam.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class FreshClam : IClam 4 | { 5 | public string Name => "Fresh Clam"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/FrozenClam.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class FrozenClam : IClam 4 | { 5 | public string Name => "Frozen Clam"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Interfaces/ICheese.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | public interface ICheese 4 | { 5 | string Name { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Interfaces/IClam.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | public interface IClam 4 | { 5 | string Name { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Interfaces/IDough.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | public interface IDough 4 | { 5 | string Name { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Interfaces/ISauce.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | public interface ISauce 4 | { 5 | string Name { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Interfaces/IVeggies.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | public interface IVeggies 4 | { 5 | string Name { get; } 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Mozarella.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class Mozarella : ICheese 4 | { 5 | public string Name => "Mozarella"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Olive.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class Olive : IVeggies 4 | { 5 | public string Name => "Olives"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Onion.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class Onion : IVeggies 4 | { 5 | public Onion() 6 | { 7 | } 8 | 9 | public string Name => "Onions"; 10 | } 11 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Parmesan.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class Parmesan : ICheese 4 | { 5 | public string Name => "Parmesan"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/Pepper.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class Pepper : IVeggies 4 | { 5 | 6 | public string Name => "Bell Peppers"; 7 | } 8 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/PlumTomato.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class PlumTomato : ISauce 4 | { 5 | public string Name => "Plum Tomato"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/Ingredients/ThinCrust.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | internal class ThinCrust : IDough 4 | { 5 | public string Name => "Thin Crust"; 6 | } 7 | } -------------------------------------------------------------------------------- /FactoryPattern/Abstract Factory/NYIngredientsFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace FactoryPattern 4 | { 5 | internal class NyIngredientsFactory : IIngredientsFactory 6 | { 7 | ICheese IIngredientsFactory.CreateCheese() 8 | { 9 | return new Mozarella(); 10 | } 11 | 12 | IClam IIngredientsFactory.CreateClam() 13 | { 14 | return new FrozenClam(); 15 | } 16 | 17 | IDough IIngredientsFactory.CreateDough() 18 | { 19 | return new ThinCrust(); 20 | } 21 | 22 | ISauce IIngredientsFactory.CreateSauce() 23 | { 24 | return new CherryTomato(); 25 | } 26 | 27 | IEnumerable IIngredientsFactory.CreateVeggies() 28 | { 29 | IVeggies[] arr = { new Onion(), new Pepper(), new Olive() }; 30 | return arr; 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /FactoryPattern/Factory Method/ChicagoPizzaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | class ChicagoPizzaFactory : PizzaFactory 4 | { 5 | private readonly string chicagoCheese = "Chicago Cheese"; 6 | private readonly string chicagoClam = "Chicago Clam"; 7 | private readonly string chicagoVeggie = "Chicago Veggie"; 8 | 9 | protected override Pizza Create(PizzaType type) 10 | { 11 | Pizza pizza; 12 | IIngredientsFactory ingredients = new ChicagoIngredientsFactory(); 13 | 14 | if (type == PizzaType.Cheese) 15 | { 16 | pizza = new CheesePizza(ingredients); 17 | pizza.Name = chicagoCheese; 18 | } 19 | else if (type == PizzaType.Clam) 20 | { 21 | pizza = new ClamPizza(ingredients); 22 | pizza.Name = chicagoClam; 23 | } 24 | else 25 | { 26 | pizza = new VeggiePizza(ingredients); 27 | pizza.Name = chicagoVeggie; 28 | } 29 | pizza.Color = "red"; 30 | return pizza; 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /FactoryPattern/Factory Method/NYPizzaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | class NyPizzaFactory : PizzaFactory 4 | { 5 | private readonly string nyStyleCheese = "NY Style Cheese"; 6 | private readonly string nyStyleClam = "NY Style Clam"; 7 | private readonly string nyStyleVeggie = "NY Style Veggie"; 8 | 9 | protected override Pizza Create(PizzaType type) 10 | { 11 | Pizza pizza; 12 | IIngredientsFactory ingredients = new NyIngredientsFactory(); 13 | 14 | if (type == PizzaType.Cheese) 15 | { 16 | pizza = new CheesePizza(ingredients) { Name = nyStyleCheese }; 17 | } 18 | else if (type == PizzaType.Clam) 19 | { 20 | pizza = new ClamPizza(ingredients) { Name = nyStyleClam }; 21 | } 22 | else 23 | { 24 | pizza = new VeggiePizza(ingredients) { Name = nyStyleVeggie }; 25 | } 26 | pizza.Color = "blue"; 27 | return pizza; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /FactoryPattern/Factory Method/PizzaFactory.cs: -------------------------------------------------------------------------------- 1 | namespace FactoryPattern 2 | { 3 | abstract class PizzaFactory 4 | { 5 | public Pizza Order(PizzaType type) 6 | { 7 | var pizza = Create(type); 8 | pizza.Prepare(); 9 | pizza.Bake(); 10 | pizza.Cut(); 11 | pizza.Box(); 12 | return pizza; 13 | } 14 | 15 | protected abstract Pizza Create(PizzaType type); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /FactoryPattern/FactoryPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /FactoryPattern/Helper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace FactoryPattern 8 | { 9 | internal class Helper 10 | { 11 | } 12 | 13 | internal enum PizzaType 14 | { 15 | Cheese, 16 | Clam 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /FactoryPattern/Pizza/CheesePizza.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FactoryPattern 4 | { 5 | class CheesePizza : Pizza 6 | { 7 | readonly IIngredientsFactory _ingredients; 8 | 9 | public CheesePizza(IIngredientsFactory ing) 10 | { 11 | _ingredients = ing; 12 | } 13 | internal override void Prepare() 14 | { 15 | Console.WriteLine("Preparing " + Name + " Using"); 16 | Console.Write("Dough: " + _ingredients.CreateDough().Name + ", Cheese: " + _ingredients.CreateCheese().Name + ", Sauce: " + _ingredients.CreateSauce().Name + ", Veggies: "); 17 | Console.WriteLine(); 18 | foreach (var val in _ingredients.CreateVeggies()) 19 | { 20 | Console.Write(val.Name + " "); 21 | } 22 | Console.WriteLine(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /FactoryPattern/Pizza/ClamPizza.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FactoryPattern 4 | { 5 | class ClamPizza : Pizza 6 | { 7 | readonly IIngredientsFactory _ingredients; 8 | 9 | public ClamPizza(IIngredientsFactory ing) 10 | { 11 | _ingredients = ing; 12 | } 13 | 14 | internal override void Prepare() 15 | { 16 | Console.WriteLine("Preparing " + Name + " Using"); 17 | Console.Write("Dough: " + _ingredients.CreateDough().Name + ", Clam: " + _ingredients.CreateClam().Name + ", Sauce: " + _ingredients.CreateSauce().Name + ", Cheese: " + _ingredients.CreateCheese().Name); 18 | Console.WriteLine(); 19 | 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /FactoryPattern/Pizza/Pizza.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FactoryPattern 4 | { 5 | abstract class Pizza 6 | { 7 | public string Color; 8 | 9 | internal abstract void Prepare(); 10 | internal void Bake() 11 | { 12 | Console.WriteLine("Baking at 135 degree Celsius for 20 minutes"); 13 | } 14 | internal void Cut() 15 | { 16 | Console.WriteLine("Cutting into diagonal pieces"); 17 | } 18 | internal void Box() 19 | { 20 | Console.WriteLine("Putting pizza in " + Color + " coloured box"); 21 | } 22 | 23 | public string Name { protected get; set; } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /FactoryPattern/Pizza/VeggiePizza.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FactoryPattern 4 | { 5 | class VeggiePizza : Pizza 6 | { 7 | readonly IIngredientsFactory _ingredients; 8 | 9 | public VeggiePizza(IIngredientsFactory ing) 10 | { 11 | _ingredients = ing; 12 | } 13 | internal override void Prepare() 14 | { 15 | Console.WriteLine("Preparing " + Name + " Using"); 16 | Console.Write("Dough: " + _ingredients.CreateDough().Name + ", Cheese: " + _ingredients.CreateCheese().Name + ", Sauce: " + _ingredients.CreateSauce().Name + ", Veggies: "); 17 | Console.WriteLine(); 18 | foreach (var val in _ingredients.CreateVeggies()) 19 | { 20 | Console.Write(val.Name + " "); 21 | } 22 | Console.WriteLine(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /FactoryPattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FactoryPattern 4 | { 5 | static class Program 6 | { 7 | static void Main() 8 | { 9 | Console.WriteLine("Yankees fan orders:"); 10 | var yankees = new NyPizzaFactory(); 11 | yankees.Order(PizzaType.Cheese); 12 | Console.WriteLine(); 13 | Console.WriteLine("Cubs fan orders:"); 14 | var cubs = new ChicagoPizzaFactory(); 15 | cubs.Order(PizzaType.Clam); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /FlyweightPattern/BeverageFlyweightFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace FlyweightPattern 5 | { 6 | public class BeverageFlyweightFactory 7 | { 8 | private readonly Dictionary _beverages; 9 | 10 | public BeverageFlyweightFactory() 11 | { 12 | _beverages = new Dictionary(); 13 | } 14 | 15 | public IBeverage MakeBeverage(BeverageType type) 16 | { 17 | _beverages.TryGetValue(type, out var beverage); 18 | if (beverage == null) 19 | { 20 | switch (type) 21 | { 22 | case BeverageType.BubbleMilk: 23 | beverage = new BubbleMilkTea(); 24 | _beverages.Add(BeverageType.BubbleMilk, beverage); 25 | break; 26 | case BeverageType.FoamMilk: 27 | beverage = new FoamMilkTea(); 28 | _beverages.Add(BeverageType.FoamMilk, beverage); 29 | break; 30 | case BeverageType.OolongMilk: 31 | beverage = new OolingMilkTea(); 32 | _beverages.Add(BeverageType.OolongMilk, beverage); 33 | break; 34 | case BeverageType.CoconutMilk: 35 | beverage = new CoconutMilkTea(); 36 | _beverages.Add(BeverageType.CoconutMilk, beverage); 37 | break; 38 | default: 39 | throw new ArgumentOutOfRangeException(nameof(type), type, null); 40 | } 41 | } 42 | 43 | return beverage; 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /FlyweightPattern/BeverageType.cs: -------------------------------------------------------------------------------- 1 | namespace FlyweightPattern 2 | { 3 | public enum BeverageType 4 | { 5 | BubbleMilk, FoamMilk, OolongMilk, CoconutMilk 6 | } 7 | } -------------------------------------------------------------------------------- /FlyweightPattern/BubbleMilkTea.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FlyweightPattern 4 | { 5 | public class BubbleMilkTea : IBeverage 6 | { 7 | public BubbleMilkTea() 8 | { 9 | Console.WriteLine("Initializing a Bubble Milk Tea instance"); 10 | } 11 | 12 | public void Drink() 13 | { 14 | Console.WriteLine("hmmm... this is bubble milk tea"); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /FlyweightPattern/BubbleTeaShop.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace FlyweightPattern 5 | { 6 | public class BubbleTeaShop 7 | { 8 | private List takeAwayOrders; 9 | 10 | public BubbleTeaShop() 11 | { 12 | takeAwayOrders = new List(); 13 | TakeOrders(); 14 | } 15 | 16 | private void TakeOrders() 17 | { 18 | var factory = new BeverageFlyweightFactory(); 19 | 20 | takeAwayOrders.Add(factory.MakeBeverage(BeverageType.BubbleMilk)); 21 | takeAwayOrders.Add(factory.MakeBeverage(BeverageType.BubbleMilk)); 22 | takeAwayOrders.Add(factory.MakeBeverage(BeverageType.CoconutMilk)); 23 | takeAwayOrders.Add(factory.MakeBeverage(BeverageType.FoamMilk)); 24 | takeAwayOrders.Add(factory.MakeBeverage(BeverageType.OolongMilk)); 25 | takeAwayOrders.Add(factory.MakeBeverage(BeverageType.OolongMilk)); 26 | } 27 | 28 | public void Enumerate() 29 | { 30 | Console.WriteLine("Enumerating take away orders\n"); 31 | foreach (var beverage in takeAwayOrders) 32 | { 33 | beverage.Drink(); 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /FlyweightPattern/CoconutMilkTea.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FlyweightPattern 4 | { 5 | public class CoconutMilkTea : IBeverage 6 | { 7 | public CoconutMilkTea() 8 | { 9 | Console.WriteLine("Initializing a Coconut Milk Tea instance"); 10 | } 11 | 12 | public void Drink() 13 | { 14 | Console.WriteLine("hmmm... this is coconut milk tea"); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /FlyweightPattern/FlyweightPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /FlyweightPattern/FoamMilkTea.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FlyweightPattern 4 | { 5 | public class FoamMilkTea : IBeverage 6 | { 7 | 8 | public FoamMilkTea() 9 | { 10 | Console.WriteLine("Initializing a Foam Milk Tea instance"); 11 | } 12 | 13 | public void Drink() 14 | { 15 | Console.WriteLine("hmmm... this is foam milk tea"); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /FlyweightPattern/IBeverage.cs: -------------------------------------------------------------------------------- 1 | namespace FlyweightPattern 2 | { 3 | public interface IBeverage 4 | { 5 | void Drink(); 6 | } 7 | } -------------------------------------------------------------------------------- /FlyweightPattern/OolingMilkTea.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FlyweightPattern 4 | { 5 | public class OolingMilkTea : IBeverage 6 | { 7 | 8 | public OolingMilkTea() 9 | { 10 | Console.WriteLine("Initializing an Oolong Milk Tea instance"); 11 | } 12 | 13 | public void Drink() 14 | { 15 | Console.WriteLine("hmmm... this is oolong milk tea"); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /FlyweightPattern/Program.cs: -------------------------------------------------------------------------------- 1 | namespace FlyweightPattern 2 | { 3 | static class Program 4 | { 5 | private static void Main() 6 | { 7 | var teaShop = new BubbleTeaShop(); 8 | teaShop.Enumerate(); 9 | } 10 | } 11 | } -------------------------------------------------------------------------------- /IteratorPattern/BreakfastMenu.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace IteratorPattern 4 | { 5 | public class BreakfastMenu 6 | { 7 | private ArrayList _items; 8 | 9 | public IEnumerable Items 10 | { 11 | get 12 | { 13 | return new BreakfastMenuIterator(_items); 14 | } 15 | } 16 | 17 | public BreakfastMenu() 18 | { 19 | _items = new ArrayList(); 20 | 21 | AddItem("Waffle", "Blueberry Sauce topped breakfast Waffles", 125, false); 22 | AddItem("Sandwich", "Veggie Sandwich with tomato and cucumber", 75, true); 23 | AddItem("Pankcakes", "Maple syrup Pancakes", 110, false); 24 | AddItem("Corn Flakes", "Cornflakes with fruits and nuts", 60, true); 25 | } 26 | 27 | private void AddItem(string name, string description, int price, bool veg) 28 | { 29 | var item = new Menu(name, description, price, veg); 30 | _items.Add(item); 31 | } 32 | 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /IteratorPattern/BreakfastMenuEnum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace IteratorPattern 5 | { 6 | public class BreakfastMenuEnum : IEnumerator 7 | { 8 | private readonly ArrayList _items; 9 | private int _position = -1; 10 | 11 | public BreakfastMenuEnum(ArrayList items) 12 | { 13 | _items = items; 14 | } 15 | 16 | public void Dispose() 17 | { 18 | throw new System.NotImplementedException(); 19 | } 20 | 21 | public bool MoveNext() 22 | { 23 | _position++; 24 | return (_position < _items.Count); 25 | } 26 | 27 | public void Reset() 28 | { 29 | _position = -1; 30 | } 31 | 32 | object IEnumerator.Current => Current; 33 | 34 | public Menu Current 35 | { 36 | get 37 | { 38 | try 39 | { 40 | return (Menu)_items[_position]; 41 | } 42 | catch (IndexOutOfRangeException) 43 | { 44 | throw new InvalidOperationException(); 45 | } 46 | } 47 | } 48 | } 49 | } -------------------------------------------------------------------------------- /IteratorPattern/BreakfastMenuIterator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace IteratorPattern 4 | { 5 | class BreakfastMenuIterator : IEnumerable 6 | { 7 | private int _count = 0; 8 | private ArrayList _items; 9 | 10 | public BreakfastMenuIterator(ArrayList items) 11 | { 12 | _items = items; 13 | } 14 | 15 | IEnumerator IEnumerable.GetEnumerator() 16 | { 17 | return GetEnumerator(); 18 | } 19 | 20 | public IEnumerator GetEnumerator() 21 | { 22 | return new BreakfastMenuEnum(_items); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /IteratorPattern/Client.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace IteratorPattern 5 | { 6 | public class Client 7 | { 8 | private IEnumerable _breakfast; 9 | private IEnumerable _dinner; 10 | 11 | public Client(BreakfastMenu breakfast, DinnerMenu dinner) 12 | { 13 | this._breakfast = breakfast.Items; 14 | this._dinner = dinner.Items; 15 | } 16 | 17 | public void PrintMenu() 18 | { 19 | var breakfast = _breakfast; 20 | PrintMenu(breakfast); 21 | var dinner = _dinner; 22 | PrintMenu(dinner); 23 | } 24 | 25 | private void PrintMenu(IEnumerable iter) 26 | { 27 | foreach (var item in iter) 28 | { 29 | var i = (Menu)item; 30 | Console.WriteLine($"{i.Name} Rs. {i.Price} { (i.Vegetarian ? "*" : "x") } \n {i.Description} "); 31 | 32 | } 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /IteratorPattern/DinnerMenu.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace IteratorPattern 5 | { 6 | public class DinnerMenu 7 | { 8 | private const int Max = 1; 9 | 10 | private int _count; 11 | private Menu[] _items; 12 | 13 | public IEnumerable Items 14 | { 15 | get 16 | { 17 | return new DinnerMenuIterator(_items); 18 | } 19 | } 20 | 21 | public DinnerMenu() 22 | { 23 | _items = new Menu[Max]; 24 | 25 | AddItems("Hamburger", "Hamburger with cheese and onions", 160, false); 26 | 27 | } 28 | 29 | private void AddItems(string name, string description, int price, bool veg) 30 | { 31 | var item = new Menu(name, description, price, veg); 32 | 33 | if (_count <= Max) 34 | { 35 | _items[_count] = item; 36 | _count++; 37 | } 38 | else 39 | { 40 | throw new IndexOutOfRangeException(); 41 | } 42 | } 43 | } 44 | } -------------------------------------------------------------------------------- /IteratorPattern/DinnerMenuEnum.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace IteratorPattern 5 | { 6 | public class DinnerMenuEnum : IEnumerator 7 | { 8 | private readonly Menu[] _items; 9 | private int _position = -1; 10 | public DinnerMenuEnum(Menu[] items) 11 | { 12 | _items = items; 13 | } 14 | 15 | public void Dispose() 16 | { 17 | throw new System.NotImplementedException(); 18 | } 19 | 20 | public bool MoveNext() 21 | { 22 | _position++; 23 | return (_position < _items.Length); 24 | } 25 | 26 | public void Reset() 27 | { 28 | _position = -1; 29 | } 30 | 31 | object IEnumerator.Current => Current; 32 | 33 | public Menu Current 34 | { 35 | get 36 | { 37 | try 38 | { 39 | return _items[_position]; 40 | } 41 | catch (IndexOutOfRangeException) 42 | { 43 | throw new InvalidOperationException(); 44 | } 45 | } 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /IteratorPattern/DinnerMenuIterator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace IteratorPattern 4 | { 5 | class DinnerMenuIterator : IEnumerable 6 | { 7 | private int _count = 0; 8 | private Menu[] _items; 9 | 10 | public DinnerMenuIterator(Menu[] items) 11 | { 12 | _items = items; 13 | } 14 | 15 | IEnumerator IEnumerable.GetEnumerator() 16 | { 17 | return GetEnumerator(); 18 | } 19 | 20 | public IEnumerator GetEnumerator() 21 | { 22 | return new DinnerMenuEnum(_items); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /IteratorPattern/IteratorPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /IteratorPattern/Menu.cs: -------------------------------------------------------------------------------- 1 | namespace IteratorPattern 2 | { 3 | public class Menu 4 | { 5 | public string Name { get; } 6 | public string Description { get; } 7 | public bool Vegetarian { get; } 8 | public double Price { get; } 9 | 10 | public Menu(string name, string description, double price, bool vegetarian) 11 | { 12 | Name = name; 13 | Description = description; 14 | Price = price; 15 | Vegetarian = vegetarian; 16 | } 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /IteratorPattern/Program.cs: -------------------------------------------------------------------------------- 1 | namespace IteratorPattern 2 | { 3 | static class Program 4 | { 5 | private static void Main() 6 | { 7 | var breakfast = new BreakfastMenu(); 8 | var dinner = new DinnerMenu(); 9 | var waiter = new Client(breakfast, dinner); 10 | waiter.PrintMenu(); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /MediatorPattern/Colleague.cs: -------------------------------------------------------------------------------- 1 | namespace MediatorPattern 2 | { 3 | abstract class Colleague 4 | { 5 | protected Mediator mediator; 6 | 7 | public Colleague(Mediator mediator) => this.mediator = mediator; 8 | 9 | public virtual void Send(string message) => this.mediator.Send(message, this); 10 | 11 | public abstract void Notify(string message); 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /MediatorPattern/Customer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MediatorPattern 4 | { 5 | class Customer : Colleague 6 | { 7 | public Customer(Mediator mediator) : base(mediator) { } 8 | 9 | public override void Notify(string message) 10 | { 11 | Console.WriteLine($"Message to customer: {message}"); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /MediatorPattern/ManagerMediator.cs: -------------------------------------------------------------------------------- 1 | namespace MediatorPattern 2 | { 3 | class ManagerMediator : Mediator 4 | { 5 | public Colleague Customer { get; set; } 6 | public Colleague Programmer { get; set; } 7 | public Colleague Tester { get; set; } 8 | 9 | public override void Send(string message, Colleague colleague) 10 | { 11 | if (colleague == Customer) 12 | { 13 | Programmer.Notify(message); 14 | } 15 | else if (colleague == Programmer) 16 | { 17 | Tester.Notify(message); 18 | } 19 | else Customer.Notify(message); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /MediatorPattern/Mediator.cs: -------------------------------------------------------------------------------- 1 | namespace MediatorPattern 2 | { 3 | abstract class Mediator 4 | { 5 | public abstract void Send(string message, Colleague colleague); 6 | } 7 | } -------------------------------------------------------------------------------- /MediatorPattern/MediatorPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /MediatorPattern/Program.cs: -------------------------------------------------------------------------------- 1 | namespace MediatorPattern 2 | { 3 | class Program 4 | { 5 | static void Main(string[] args) 6 | { 7 | var mediator = new ManagerMediator(); 8 | var customer = new Customer(mediator); 9 | var programmer = new Programmer(mediator); 10 | var tester = new Tester(mediator); 11 | mediator.Customer = customer; 12 | mediator.Programmer = programmer; 13 | mediator.Tester = tester; 14 | customer.Send("We have an order, need to make a program"); 15 | programmer.Send("I have done program, need to test it"); 16 | tester.Send("I have done testing, here is ready program for you"); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /MediatorPattern/Programmer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MediatorPattern 4 | { 5 | class Programmer : Colleague 6 | { 7 | public Programmer(Mediator mediator) : base(mediator) { } 8 | 9 | public override void Notify(string message) 10 | { 11 | Console.WriteLine($"Message to programmer: {message}"); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /MediatorPattern/Tester.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MediatorPattern 4 | { 5 | class Tester : Colleague 6 | { 7 | public Tester(Mediator mediator) : base(mediator) { } 8 | 9 | public override void Notify(string message) 10 | { 11 | Console.WriteLine($"Message to tester: {message}"); 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /ObserverPattern/ObserverPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ObserverPattern/Program.cs: -------------------------------------------------------------------------------- 1 | namespace ObserverPattern 2 | { 3 | static class Program 4 | { 5 | static void Main() 6 | { 7 | var provider = new WeatherSupplier(); 8 | var observer1 = new WeatherMonitor("TP"); 9 | var observer2 = new WeatherMonitor("H"); 10 | provider.WeatherConditions(32.0, 0.05, 1.5); 11 | observer1.Subscribe(provider); 12 | provider.WeatherConditions(33.5, 0.04, 1.7); 13 | observer2.Subscribe(provider); 14 | provider.WeatherConditions(37.5, 0.07, 1.2); 15 | 16 | 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ObserverPattern/Unsubscriber.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace ObserverPattern 5 | { 6 | internal class Unsubscriber : IDisposable 7 | { 8 | private readonly List> _observers; 9 | private readonly IObserver _observer; 10 | 11 | internal Unsubscriber(List> observers, IObserver observer) 12 | { 13 | _observers = observers; 14 | _observer = observer; 15 | } 16 | 17 | public void Dispose() 18 | { 19 | if (_observers.Contains(_observer)) 20 | _observers.Remove(_observer); 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ObserverPattern/Weather.cs: -------------------------------------------------------------------------------- 1 | namespace ObserverPattern 2 | { 3 | class Weather 4 | { 5 | public double Pressure { get; } 6 | 7 | public double Humidity { get; } 8 | 9 | public double Temperature { get; } 10 | 11 | public Weather(double humd, double pres, double temp) 12 | { 13 | Temperature = temp; 14 | Pressure = pres; 15 | Humidity = humd; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /ObserverPattern/WeatherMonitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ObserverPattern 4 | { 5 | sealed class WeatherMonitor : IObserver 6 | { 7 | private IDisposable _cancellation; 8 | private readonly string _name; 9 | 10 | public void Subscribe(WeatherSupplier provider) 11 | { 12 | _cancellation = provider.Subscribe(this); 13 | } 14 | 15 | public void Unsubscribe() 16 | { 17 | _cancellation.Dispose(); 18 | } 19 | 20 | public WeatherMonitor(string name) 21 | { 22 | _name = name; 23 | } 24 | 25 | public void OnCompleted() 26 | { 27 | throw new NotImplementedException(); 28 | } 29 | 30 | public void OnError(Exception error) 31 | { 32 | Console.WriteLine("Error has occured"); 33 | } 34 | 35 | public void OnNext(Weather value) 36 | { 37 | Console.WriteLine(_name); 38 | if (_name.Contains("T")) 39 | { 40 | string op = $"| Temperature : {value.Temperature} Celsius |"; 41 | Console.Write(op); 42 | 43 | } 44 | if (_name.Contains("P")) 45 | { 46 | string op = $"| Pressure : {value.Pressure} atm |"; 47 | Console.Write(op); 48 | } 49 | if (_name.Contains("H")) 50 | { 51 | string op = $"| Humidity : {value.Humidity * 100} % |"; 52 | Console.Write(op); 53 | } 54 | if (!(_name.Contains("T") || _name.Contains("P") || _name.Contains("H"))) 55 | { 56 | OnError(new Exception()); 57 | } 58 | Console.WriteLine(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /ObserverPattern/WeatherSupplier.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace ObserverPattern 5 | { 6 | class WeatherSupplier : IObservable 7 | { 8 | private readonly List> _observers; 9 | private List Screens { get; } 10 | 11 | private List GetScreens() 12 | { 13 | return Screens; 14 | } 15 | 16 | public WeatherSupplier() 17 | { 18 | _observers = new List>(); 19 | Screens = new List(); 20 | } 21 | 22 | public IDisposable Subscribe(IObserver observer) 23 | { 24 | if (!_observers.Contains(observer)) 25 | { 26 | _observers.Add(observer); 27 | foreach (var item in GetScreens()) 28 | { 29 | observer.OnNext(item); 30 | } 31 | } 32 | return new Unsubscriber(_observers, observer); 33 | } 34 | 35 | public void WeatherConditions(double temp = 0, double humd = 0, double pres = 0) 36 | { 37 | var conditions = new Weather(humd, pres, temp); 38 | foreach (var item in _observers) 39 | item.OnNext(conditions); 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /PrototypePattern/Circle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PrototypePattern 4 | { 5 | class Circle : IFigure 6 | { 7 | readonly int _radius; 8 | public Circle(int r) 9 | { 10 | _radius = r; 11 | } 12 | 13 | public object Clone() 14 | { 15 | return new Circle(_radius); 16 | } 17 | public void GetInfo() 18 | { 19 | Console.WriteLine($"Circle with radius {_radius}"); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /PrototypePattern/IFigure.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PrototypePattern 4 | { 5 | interface IFigure : ICloneable 6 | { 7 | void GetInfo(); 8 | } 9 | } -------------------------------------------------------------------------------- /PrototypePattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PrototypePattern 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | IFigure figure = new Rectangle(30, 40); 10 | IFigure clonedFigure = (IFigure)figure.Clone(); 11 | figure.GetInfo(); 12 | clonedFigure.GetInfo(); 13 | 14 | figure = new Circle(30); 15 | clonedFigure = (IFigure)figure.Clone(); 16 | figure.GetInfo(); 17 | clonedFigure.GetInfo(); 18 | 19 | Console.Read(); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /PrototypePattern/PrototypePattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /PrototypePattern/Rectangle.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PrototypePattern 4 | { 5 | class Rectangle : IFigure 6 | { 7 | readonly int _width; 8 | readonly int _height; 9 | public Rectangle(int w, int h) 10 | { 11 | _width = w; 12 | _height = h; 13 | } 14 | 15 | public object Clone() 16 | { 17 | return new Rectangle(_width, _height); 18 | } 19 | public void GetInfo() 20 | { 21 | Console.WriteLine($"Rectangle height {_height} and width {_width}"); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /ProxyPattern/Image.cs: -------------------------------------------------------------------------------- 1 | namespace ProxyPattern 2 | { 3 | public interface Image 4 | { 5 | void display(); 6 | 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /ProxyPattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ProxyPattern 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) 8 | { 9 | Image image = new ProxyImage("testImage.jpg"); 10 | 11 | //image will be loaded from disk 12 | image.display(); 13 | Console.WriteLine(""); 14 | 15 | //image will not be loaded from disk 16 | image.display(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /ProxyPattern/ProxyImage.cs: -------------------------------------------------------------------------------- 1 | namespace ProxyPattern 2 | { 3 | public class ProxyImage : Image 4 | { 5 | private RealImage _realImage; 6 | private string _fileName; 7 | 8 | public ProxyImage(string fileName) 9 | { 10 | _fileName = fileName; 11 | } 12 | 13 | 14 | public void display() 15 | { 16 | if (_realImage == null) 17 | { 18 | _realImage = new RealImage(_fileName); 19 | } 20 | _realImage.display(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /ProxyPattern/ProxyPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ProxyPattern/RealImage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ProxyPattern 4 | { 5 | 6 | public class RealImage : Image 7 | { 8 | 9 | private string _fileName; 10 | 11 | public RealImage(string fileName) 12 | { 13 | _fileName = fileName; 14 | loadFromDisk(_fileName); 15 | } 16 | 17 | public void display() 18 | { 19 | Console.WriteLine("Displaying " + _fileName); 20 | } 21 | 22 | private void loadFromDisk(string fileName) 23 | { 24 | Console.WriteLine("Loading " + fileName); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design Patterns 2 | Design patterns are solutions to recurring problems; guidelines on how to tackle certain problems. 3 | I have included implementations of some design patterns in C# to help beginners like me get their feet wet. 4 | There are better alternatives available for some of them in the .NET Framework, so this is by no means a comprehensive tutorial. 5 | 6 | Any comments and suggestions are welcome. If you want to add a new design pattern implementation, just follow the naming convention, fork my repo and submit a pull request. Same goes for any improvements and modifications. 7 | 8 | This was created as a C# alternative to Java while reading https://www.oreilly.com/library/view/head-first-design/0596007124/ <- take a look at it if anything confuses you. 9 | 10 | ## Types of Design Patterns 11 | --------------------------- 12 | There are three kinds of Design Patterns: 13 | 14 | * Creational 15 | * Structural 16 | * Behavioral 17 | 18 | ## List of Design Pattern Implementations 19 | ----------------------------------------- 20 | 21 | * [Adapter](/AdapterPattern) 22 | * [Bridge](/BridgePattern) 23 | * [Builder](/BuilderPattern) 24 | * [ChainOfResponsibility](/ChainOfResponsibilityPattern) 25 | * [Command](/CommandPattern) 26 | * [Composite](/CompositePattern) 27 | * [Decorator](/DecoratorPattern) 28 | * [Facade](/FacadePattern) 29 | * [Factory](/FactoryPattern) 30 | * [Flyweight](/FlyweightPattern) 31 | * [Iterator](/IteratorPattern) 32 | * [Mediator](/MediatorPattern) 33 | * [Observer](/ObserverPattern) 34 | * [Prototype](/PrototypePattern) 35 | * [Proxy](/ProxyPattern) 36 | * [Singleton](/SingletonPattern) 37 | * [SingletonPattern.Tests](/SingletonPattern.Tests) 38 | * [State](/StatePattern) 39 | * [Strategy](/StrategyPattern) 40 | * [Template](/TemplatePattern) 41 | * [Visitor](/VisitorPattern) 42 | -------------------------------------------------------------------------------- /SingletonPattern.Tests/SingletonPattern.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | false 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /SingletonPattern.Tests/SingletonPatternTests.cs: -------------------------------------------------------------------------------- 1 | namespace SingletonPattern.Tests 2 | { 3 | [TestFixture] 4 | public class SingletonPatternTests 5 | { 6 | [Test] 7 | public void GetInstance_CreateInstanceTwice_AreEqual() 8 | { 9 | // Arrange, Act 10 | var firstAttemptInstance = ChocolateBoiler.GetInstance(); 11 | var secondAttemptInstance = ChocolateBoiler.GetInstance(); 12 | 13 | // Assert 14 | Assert.That(firstAttemptInstance, Is.EqualTo(secondAttemptInstance)); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /SingletonPattern.Tests/Usings.cs: -------------------------------------------------------------------------------- 1 | global using NUnit.Framework; 2 | -------------------------------------------------------------------------------- /SingletonPattern/ChocolateBoiler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SingletonPattern 4 | { 5 | internal partial class ChocolateBoiler 6 | { 7 | private static readonly Lazy _singleton = new Lazy(() => new ChocolateBoiler()); 8 | 9 | public static ChocolateBoiler GetInstance() => _singleton.Value; 10 | 11 | private Status _boiler; 12 | 13 | private ChocolateBoiler() 14 | { 15 | Console.WriteLine("Starting"); 16 | _boiler = Status.Empty; 17 | } 18 | 19 | public void Fill() 20 | { 21 | if (!IsEmpty) return; 22 | Console.WriteLine("Filling..."); 23 | _boiler = Status.InProgress; 24 | } 25 | 26 | public void Drain() 27 | { 28 | if (!IsBoiled) return; 29 | Console.WriteLine("Draining..."); 30 | _boiler = Status.Empty; 31 | } 32 | 33 | public void Boil() 34 | { 35 | if (IsBoiled || IsEmpty) return; 36 | Console.WriteLine("Boiling..."); 37 | _boiler = Status.Boiled; 38 | } 39 | 40 | private bool IsEmpty => (_boiler == Status.Empty); 41 | 42 | private bool IsBoiled => (_boiler == Status.Boiled); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /SingletonPattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SingletonPattern 4 | { 5 | static class Program 6 | { 7 | static void Main() 8 | { 9 | try 10 | { 11 | var chocoEggs = ChocolateBoiler.GetInstance(); 12 | chocoEggs.Fill(); 13 | chocoEggs.Boil(); 14 | chocoEggs.Drain(); 15 | } 16 | catch (Exception) 17 | { 18 | Console.Write("Oops"); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /SingletonPattern/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | [assembly:InternalsVisibleTo("SingletonPattern.Tests")] 4 | -------------------------------------------------------------------------------- /SingletonPattern/SingletonPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /SingletonPattern/Status.cs: -------------------------------------------------------------------------------- 1 | namespace SingletonPattern 2 | { 3 | internal partial class ChocolateBoiler 4 | { 5 | private enum Status 6 | { 7 | Empty, InProgress, Boiled 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /StatePattern/GumballMachine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StatePattern 4 | { 5 | public class GumballMachine 6 | { 7 | public int Count { get; private set; } 8 | 9 | public IState SoldOutState; 10 | public IState NoQuarterState; 11 | public IState HasQuarterState; 12 | public IState SoldState; 13 | public IState WinnerState; 14 | 15 | public IState State { get; set; } 16 | 17 | public GumballMachine(int count) 18 | { 19 | Count = count; 20 | SoldOutState = new SoldOutState(this); 21 | NoQuarterState = new NoQuarterState(this); 22 | HasQuarterState = new HasQuarterState(this); 23 | SoldState = new SoldState(this); 24 | WinnerState = new WinnerState(this); 25 | if (Count > 0) 26 | { 27 | State = NoQuarterState; 28 | } 29 | } 30 | 31 | public void InsertQuarter() 32 | { 33 | State.InsertQuarter(); 34 | } 35 | 36 | public void EjectQuarter() 37 | { 38 | State.EjectQuarter(); 39 | } 40 | 41 | public void TurnCrank() 42 | { 43 | State.TurnCrank(); 44 | State.Dispense(); 45 | } 46 | 47 | public void ReleaseBall() 48 | { 49 | Console.WriteLine("A ball comes rolling down"); 50 | if (Count == 0) return; 51 | Count--; 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /StatePattern/HasQuarterState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StatePattern 4 | { 5 | public class HasQuarterState : IState 6 | { 7 | private GumballMachine Machine { get; } 8 | readonly Random _random = new Random(DateTime.Now.Millisecond); 9 | 10 | public HasQuarterState(GumballMachine gumballMachine) 11 | { 12 | Machine = gumballMachine; 13 | } 14 | 15 | public void InsertQuarter() 16 | { 17 | Console.WriteLine("Can't insert more than one"); 18 | } 19 | 20 | public void EjectQuarter() 21 | { 22 | Console.WriteLine("Quarter returned"); 23 | Machine.State = Machine.NoQuarterState; 24 | } 25 | 26 | public void TurnCrank() 27 | { 28 | Console.WriteLine("You turned the crank"); 29 | var winner = _random.Next(10); 30 | if ((winner == 5) && (Machine.Count > 1)) 31 | Machine.State = Machine.WinnerState; 32 | else 33 | { 34 | Machine.State = Machine.SoldState; 35 | } 36 | } 37 | 38 | public void Dispense() 39 | { 40 | Console.WriteLine("Can't do that"); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /StatePattern/IState.cs: -------------------------------------------------------------------------------- 1 | namespace StatePattern 2 | { 3 | public interface IState 4 | { 5 | void InsertQuarter(); 6 | void EjectQuarter(); 7 | void TurnCrank(); 8 | void Dispense(); 9 | } 10 | } -------------------------------------------------------------------------------- /StatePattern/Legacy/GumballMachine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StatePattern.Legacy 4 | { 5 | public class GumballMachine 6 | { 7 | private int _count; 8 | private State _state = State.NoQuarters; 9 | 10 | public GumballMachine(int count) 11 | { 12 | _count = count; 13 | } 14 | 15 | public void InsertQuarter() 16 | { 17 | switch (_state) 18 | { 19 | case State.NoQuarters: 20 | _state = State.HasQuarters; 21 | Console.WriteLine("Inserted a quarter"); 22 | break; 23 | case State.Sold: 24 | Console.WriteLine("Please wait for current gumball to come out"); 25 | break; 26 | case State.HasQuarters: 27 | Console.WriteLine("Can't add more quarters"); 28 | break; 29 | case State.NoGumballs: 30 | Console.WriteLine("Out of Stock"); 31 | break; 32 | default: 33 | throw new ArgumentOutOfRangeException(); 34 | } 35 | } 36 | 37 | public void EjectQuarter() 38 | { 39 | switch (_state) 40 | { 41 | case State.NoQuarters: 42 | Console.WriteLine("Nothing to eject"); 43 | break; 44 | case State.Sold: 45 | Console.WriteLine("Sorry, you have already turned the crank"); 46 | break; 47 | case State.HasQuarters: 48 | Console.WriteLine("Ejecting.."); 49 | _state = State.NoQuarters; 50 | break; 51 | case State.NoGumballs: 52 | Console.WriteLine("Can't eject, never accepted quarters"); 53 | break; 54 | default: 55 | throw new ArgumentOutOfRangeException(); 56 | } 57 | } 58 | 59 | public void TurnCrank() 60 | { 61 | switch (_state) 62 | { 63 | case State.NoQuarters: 64 | Console.WriteLine("Insert quarter First"); 65 | break; 66 | case State.Sold: 67 | Console.WriteLine("Turning twice won't get you a gumball"); 68 | break; 69 | case State.HasQuarters: 70 | Console.WriteLine("Getting gumball..."); 71 | _state = State.Sold; 72 | Dispense(); 73 | break; 74 | case State.NoGumballs: 75 | Console.WriteLine("Out of Stock"); 76 | break; 77 | default: 78 | throw new ArgumentOutOfRangeException(); 79 | } 80 | } 81 | 82 | private void Dispense() 83 | { 84 | switch (_state) 85 | { 86 | case State.NoQuarters: 87 | Console.WriteLine("You need to pay first"); 88 | break; 89 | case State.Sold: 90 | Console.WriteLine("A Gumball comes rolling out"); 91 | _count--; 92 | _state = _count == 0 ? _state = State.NoGumballs : State.NoQuarters; 93 | break; 94 | case State.HasQuarters: 95 | case State.NoGumballs: 96 | Console.WriteLine("Can't dispense"); 97 | break; 98 | default: 99 | throw new ArgumentOutOfRangeException(); 100 | } 101 | } 102 | } 103 | } -------------------------------------------------------------------------------- /StatePattern/Legacy/State.cs: -------------------------------------------------------------------------------- 1 | namespace StatePattern.Legacy 2 | { 3 | public enum State 4 | { 5 | Sold, HasQuarters, NoQuarters, NoGumballs 6 | } 7 | } -------------------------------------------------------------------------------- /StatePattern/NoQuarterState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StatePattern 4 | { 5 | public class NoQuarterState : IState 6 | { 7 | public GumballMachine Machine { get; } 8 | 9 | public NoQuarterState(GumballMachine machine) 10 | { 11 | Machine = machine; 12 | } 13 | public void InsertQuarter() 14 | { 15 | Console.WriteLine("Inserted a quarter"); 16 | Machine.State = Machine.HasQuarterState; 17 | } 18 | 19 | public void EjectQuarter() 20 | { 21 | Console.Write("Can't eject anything"); 22 | } 23 | 24 | public void TurnCrank() 25 | { 26 | Console.WriteLine("Can't turn crank without a quarter"); 27 | } 28 | 29 | public void Dispense() 30 | { 31 | Console.WriteLine("Can't dispense"); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /StatePattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StatePattern 4 | { 5 | static class Program 6 | { 7 | public static void Main() 8 | { 9 | LegacyTest(); 10 | Console.WriteLine(); 11 | var gumballmachine = new GumballMachine(5); 12 | gumballmachine.InsertQuarter(); 13 | gumballmachine.TurnCrank(); 14 | gumballmachine.InsertQuarter(); 15 | gumballmachine.TurnCrank(); 16 | } 17 | 18 | private static void LegacyTest() 19 | { 20 | var machine = new Legacy.GumballMachine(2); 21 | machine.InsertQuarter(); 22 | machine.TurnCrank(); 23 | machine.InsertQuarter(); 24 | machine.EjectQuarter(); 25 | machine.InsertQuarter(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /StatePattern/SoldOutState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StatePattern 4 | { 5 | public class SoldOutState : IState 6 | { 7 | public GumballMachine Machine { get; } 8 | 9 | public SoldOutState(GumballMachine gumballMachine) 10 | { 11 | Machine = gumballMachine; 12 | } 13 | 14 | public void InsertQuarter() 15 | { 16 | Console.WriteLine("Sorry! Sold Out"); 17 | } 18 | 19 | public void EjectQuarter() 20 | { 21 | Console.WriteLine("Can't eject when sold out"); 22 | } 23 | 24 | public void TurnCrank() 25 | { 26 | Console.WriteLine("turning crank achieves nothing"); 27 | } 28 | 29 | public void Dispense() 30 | { 31 | Console.WriteLine("Can't dispense when out of stock"); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /StatePattern/SoldState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StatePattern 4 | { 5 | public class SoldState : IState 6 | { 7 | private GumballMachine Machine { get; } 8 | 9 | public SoldState(GumballMachine gumballMachine) 10 | { 11 | Machine = gumballMachine; 12 | } 13 | 14 | public void InsertQuarter() 15 | { 16 | Console.WriteLine("Please wait, already in progress"); 17 | } 18 | 19 | public void EjectQuarter() 20 | { 21 | Console.WriteLine("Can't eject, already turned the crank"); 22 | } 23 | 24 | public void TurnCrank() 25 | { 26 | Console.WriteLine("Turning twice achieves nothing"); 27 | } 28 | 29 | public void Dispense() 30 | { 31 | Machine.ReleaseBall(); 32 | if (Machine.Count > 0) 33 | { 34 | Machine.State = Machine.NoQuarterState; 35 | } 36 | else 37 | { 38 | Console.WriteLine("Oops! No more gumballs"); 39 | Machine.State = Machine.SoldOutState; 40 | } 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /StatePattern/StatePattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /StatePattern/WinnerState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StatePattern 4 | { 5 | public class WinnerState : IState 6 | { 7 | private GumballMachine Machine { get; } 8 | 9 | public WinnerState(GumballMachine gumballMachine) 10 | { 11 | Machine = gumballMachine; 12 | } 13 | 14 | public void InsertQuarter() 15 | { 16 | Console.WriteLine("Please wait, already in progress"); 17 | } 18 | 19 | public void EjectQuarter() 20 | { 21 | Console.WriteLine("Can't eject, already turned the crank"); 22 | } 23 | 24 | public void TurnCrank() 25 | { 26 | Console.WriteLine("Turning twice achieves nothing"); 27 | } 28 | 29 | 30 | public void Dispense() 31 | { 32 | Console.WriteLine("You Won!! 2 gumballs for the price of one"); 33 | Machine.ReleaseBall(); 34 | if (Machine.Count == 0) 35 | { 36 | Machine.State = Machine.SoldOutState; 37 | Console.WriteLine("Oops! No more gumballs"); 38 | } 39 | else 40 | { 41 | Machine.ReleaseBall(); 42 | Machine.State = Machine.NoQuarterState; 43 | } 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /StrategyPattern/FlyNope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Ducks 4 | { 5 | class FlyNope : IFlyBehaviour 6 | { 7 | public void Fly() 8 | { 9 | Console.WriteLine("I can't fly"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StrategyPattern/FlyWings.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Ducks 4 | { 5 | class FlyWings : IFlyBehaviour 6 | { 7 | public void Fly() 8 | { 9 | Console.WriteLine("Flap Flap"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StrategyPattern/IFlyBehaviour.cs: -------------------------------------------------------------------------------- 1 | namespace Ducks 2 | { 3 | internal interface IFlyBehaviour 4 | { 5 | void Fly(); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /StrategyPattern/IQuackBehaviour.cs: -------------------------------------------------------------------------------- 1 | namespace Ducks 2 | { 3 | internal interface IQuackBehaviour 4 | { 5 | void Quack(); 6 | } 7 | } -------------------------------------------------------------------------------- /StrategyPattern/Program.cs: -------------------------------------------------------------------------------- 1 | namespace Ducks 2 | { 3 | internal class Duck 4 | { 5 | private IQuackBehaviour _quacker; 6 | private IFlyBehaviour _flyer; 7 | 8 | 9 | public IQuackBehaviour Quacker 10 | { 11 | set 12 | { 13 | _quacker = value; 14 | } 15 | } 16 | 17 | public IFlyBehaviour Flyer 18 | { 19 | set 20 | { 21 | _flyer = value; 22 | } 23 | } 24 | 25 | 26 | protected void PerformQuack() 27 | { 28 | _quacker.Quack(); 29 | } 30 | 31 | protected void PerformFly() 32 | { 33 | _flyer.Fly(); 34 | } 35 | } 36 | 37 | internal class MallardDuck : Duck 38 | { 39 | public MallardDuck() 40 | { 41 | Flyer = new FlyNope(); 42 | Quacker = new QuackNope(); 43 | } 44 | 45 | public void Display() 46 | { 47 | PerformFly(); 48 | PerformQuack(); 49 | } 50 | } 51 | 52 | internal static class Program 53 | { 54 | private static void Main() 55 | { 56 | var mallard = new MallardDuck { Quacker = new QuackNormal() }; 57 | mallard.Display(); 58 | mallard.Flyer = new FlyWings(); 59 | mallard.Display(); 60 | 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /StrategyPattern/QuackNope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Ducks 4 | { 5 | internal class QuackNope : IQuackBehaviour 6 | { 7 | public void Quack() 8 | { 9 | Console.WriteLine("..."); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /StrategyPattern/QuackNormal.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Ducks 4 | { 5 | class QuackNormal : IQuackBehaviour 6 | { 7 | public void Quack() 8 | { 9 | Console.WriteLine("Quack Quack"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StrategyPattern/QuackSqueak.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Ducks 4 | { 5 | class QuackSqueak : IQuackBehaviour 6 | { 7 | public void Quack() 8 | { 9 | Console.WriteLine("Squeeeak"); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /StrategyPattern/StrategyPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /TemplatePattern/Beverages/Beverage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TemplatePattern 4 | { 5 | public abstract class Beverage 6 | { 7 | // ReSharper disable once InconsistentNaming 8 | protected int _sugar; 9 | public void Prepare() 10 | { 11 | Boil(); 12 | Brew(); 13 | Pour(); 14 | if (WantsCondiments) 15 | AddCondiments(); 16 | 17 | } 18 | 19 | public bool WantsCondiments { private get; set; } 20 | 21 | 22 | protected abstract void Brew(); 23 | 24 | private void Boil() 25 | { 26 | Console.WriteLine("Boling Water"); 27 | } 28 | 29 | private void Pour() 30 | { 31 | Console.WriteLine("Pouring in Cup"); 32 | } 33 | 34 | protected abstract void AddCondiments(); 35 | 36 | public int AddSugar { get; set; } 37 | 38 | protected void Sugar() { } 39 | } 40 | } -------------------------------------------------------------------------------- /TemplatePattern/Beverages/Coffee.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TemplatePattern 4 | { 5 | class Coffee : Beverage 6 | { 7 | protected override void Brew() 8 | { 9 | Console.WriteLine("Add Coffee Grounds to water and boil"); 10 | } 11 | 12 | protected override void AddCondiments() 13 | { 14 | Console.WriteLine("Add Milk and Sugar"); 15 | } 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /TemplatePattern/Beverages/Tea.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TemplatePattern 4 | { 5 | class Tea : Beverage 6 | { 7 | protected override void Brew() 8 | { 9 | Console.WriteLine("Adding tea leaves to water and boil"); 10 | } 11 | 12 | protected override void AddCondiments() 13 | { 14 | Console.WriteLine("Adding Lemon and Sugar"); 15 | Sugar(); 16 | } 17 | 18 | private new void Sugar() 19 | { 20 | Console.WriteLine($"adding {_sugar} spoons of sugar"); 21 | } 22 | 23 | public new int AddSugar 24 | { 25 | set { _sugar = value; } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /TemplatePattern/Comparable/Person.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TemplatePattern.Comparable 4 | { 5 | class Person : IComparable 6 | { 7 | public string Name { get; } 8 | public int Age { get; } 9 | 10 | public Person(string name, int age) 11 | { 12 | Name = name; 13 | Age = age; 14 | } 15 | 16 | public int CompareTo(object obj) 17 | { 18 | var other = (Person)obj; 19 | if (String.Compare(Name, other.Name, StringComparison.Ordinal) == 0) 20 | { 21 | return Age.CompareTo(other.Age); 22 | } 23 | return String.Compare(Name, other.Name, StringComparison.Ordinal); 24 | } 25 | 26 | public override string ToString() 27 | { 28 | return $"{Name} : {Age} < "; 29 | } 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /TemplatePattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using TemplatePattern.Comparable; 4 | 5 | namespace TemplatePattern 6 | { 7 | internal static class Program 8 | { 9 | static void Main() 10 | { 11 | var tea = new Tea(); 12 | var coffee = new Coffee(); 13 | tea.WantsCondiments = true; 14 | tea.AddSugar = 5; 15 | tea.Prepare(); 16 | 17 | Console.WriteLine(); 18 | coffee.WantsCondiments = true; 19 | coffee.Prepare(); 20 | 21 | var people = new List { new Person("Ram", 25), new Person("Abishek", 12), new Person("Ram", 18), new Person("Abishek", 18) }; 22 | foreach (var person in people) 23 | { 24 | Console.Write(person); 25 | } 26 | people.Sort(); 27 | Console.WriteLine(); 28 | foreach (var person in people) 29 | { 30 | Console.Write(person); 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /TemplatePattern/TemplatePattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /VisitorPattern/Apartment.cs: -------------------------------------------------------------------------------- 1 | namespace VisitorPattern 2 | { 3 | public class Apartment : Unit 4 | { 5 | public Apartment(params Unit[] units) : base(units) 6 | { 7 | } 8 | 9 | public override void Accept(IUnitVisitor visitor) 10 | { 11 | visitor.VisitApartment(this); 12 | base.Accept(visitor); 13 | } 14 | 15 | public override string ToString() 16 | { 17 | return "Apartment"; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /VisitorPattern/ApartmentVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace VisitorPattern 4 | { 5 | public class ApartmentVisitor : IUnitVisitor 6 | { 7 | public void VisitApartment(Apartment apartment) 8 | { 9 | Console.WriteLine("This is an apartment"); 10 | } 11 | 12 | public void VisitStudio(Studio studio) 13 | { 14 | } 15 | 16 | public void VisitBedroom(Bedroom bedroom) 17 | { 18 | } 19 | 20 | public void VisitLivingRoom(LivingRoom livingRoom) 21 | { 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /VisitorPattern/Bedroom.cs: -------------------------------------------------------------------------------- 1 | namespace VisitorPattern 2 | { 3 | public class Bedroom : Unit 4 | { 5 | public Bedroom(params Unit[] units) : base(units) 6 | { 7 | } 8 | 9 | public override void Accept(IUnitVisitor visitor) 10 | { 11 | visitor.VisitBedroom(this); 12 | base.Accept(visitor); 13 | } 14 | 15 | public override string ToString() 16 | { 17 | return "Bedroom"; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /VisitorPattern/BedroomVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace VisitorPattern 4 | { 5 | public class BedroomVisitor : IUnitVisitor 6 | { 7 | public void VisitApartment(Apartment apartment) 8 | { 9 | } 10 | 11 | public void VisitStudio(Studio studio) 12 | { 13 | } 14 | 15 | public void VisitBedroom(Bedroom bedroom) 16 | { 17 | Console.WriteLine("Here is a bedroom"); 18 | } 19 | 20 | public void VisitLivingRoom(LivingRoom livingRoom) 21 | { 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /VisitorPattern/IUnitVisitor.cs: -------------------------------------------------------------------------------- 1 | namespace VisitorPattern 2 | { 3 | public interface IUnitVisitor 4 | { 5 | void VisitApartment(Apartment apartment); 6 | void VisitStudio(Studio studio); 7 | void VisitBedroom(Bedroom bedroom); 8 | void VisitLivingRoom(LivingRoom livingRoom); 9 | } 10 | } -------------------------------------------------------------------------------- /VisitorPattern/LivingRoom.cs: -------------------------------------------------------------------------------- 1 | namespace VisitorPattern 2 | { 3 | public class LivingRoom : Unit 4 | { 5 | public LivingRoom(params Unit[] units) : base(units) 6 | { 7 | } 8 | 9 | public override void Accept(IUnitVisitor visitor) 10 | { 11 | visitor.VisitLivingRoom(this); 12 | base.Accept(visitor); 13 | } 14 | 15 | public override string ToString() 16 | { 17 | return "Living Room"; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /VisitorPattern/LivingRoomVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace VisitorPattern 4 | { 5 | public class LivingRoomVisitor : IUnitVisitor 6 | { 7 | public void VisitApartment(Apartment apartment) 8 | { 9 | } 10 | 11 | public void VisitStudio(Studio studio) 12 | { 13 | } 14 | 15 | public void VisitBedroom(Bedroom bedroom) 16 | { 17 | } 18 | 19 | public void VisitLivingRoom(LivingRoom livingRoom) 20 | { 21 | Console.WriteLine("This is the living room"); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /VisitorPattern/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace VisitorPattern 4 | { 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | var apartment = new Apartment(new LivingRoom(), new Bedroom(), new Bedroom()); 10 | var studio = new Studio(new LivingRoom(), new Bedroom()); 11 | Console.WriteLine("Visiting an Apartment"); 12 | apartment.Accept(new ApartmentVisitor()); 13 | apartment.Accept(new LivingRoomVisitor()); 14 | apartment.Accept(new BedroomVisitor()); 15 | 16 | Console.WriteLine("Visiting a Studio"); 17 | studio.Accept(new StudioVisitor()); 18 | studio.Accept(new LivingRoomVisitor()); 19 | studio.Accept(new BedroomVisitor()); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /VisitorPattern/Studio.cs: -------------------------------------------------------------------------------- 1 | namespace VisitorPattern 2 | { 3 | public class Studio : Unit 4 | { 5 | public Studio(params Unit[] units) : base(units) 6 | { 7 | } 8 | 9 | public override void Accept(IUnitVisitor visitor) 10 | { 11 | visitor.VisitStudio(this); 12 | base.Accept(visitor); 13 | } 14 | 15 | public override string ToString() 16 | { 17 | return "Studio"; 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /VisitorPattern/StudioVisitor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace VisitorPattern 4 | { 5 | public class StudioVisitor : IUnitVisitor 6 | { 7 | public void VisitApartment(Apartment apartment) 8 | { 9 | } 10 | 11 | public void VisitStudio(Studio studio) 12 | { 13 | Console.WriteLine("This is a studio"); 14 | } 15 | 16 | public void VisitBedroom(Bedroom bedroom) 17 | { 18 | } 19 | 20 | public void VisitLivingRoom(LivingRoom livingRoom) 21 | { 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /VisitorPattern/Unit.cs: -------------------------------------------------------------------------------- 1 | namespace VisitorPattern 2 | { 3 | public abstract class Unit 4 | { 5 | private Unit[] _units; 6 | 7 | public Unit(params Unit[] units) 8 | { 9 | _units = units; 10 | } 11 | 12 | public virtual void Accept(IUnitVisitor visitor) 13 | { 14 | foreach (var unit in _units) 15 | { 16 | unit.Accept(visitor); 17 | } 18 | } 19 | 20 | public abstract string ToString(); 21 | } 22 | } -------------------------------------------------------------------------------- /VisitorPattern/VisitorPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | --------------------------------------------------------------------------------