├── .gitattributes ├── .gitignore ├── AbstractFactory ├── AbstractFactory.csproj ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── RecipeFactory.cs ├── Adapter ├── Adapter.csproj ├── App.config ├── Meats.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Bridge ├── App.config ├── Bridge.csproj ├── OrderingSystem.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Builder ├── App.config ├── Builder.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── SandwichAssemblyLine.cs ├── ChainOfResponsibility ├── App.config ├── ChainOfResponsibility.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── RestaurantRequest.cs ├── Command ├── App.config ├── Command.csproj ├── Ordering.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Composite ├── App.config ├── Composite.csproj ├── Drinks.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Decorator ├── App.config ├── Decorator.csproj ├── FarmFresh.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── DesignPatterns.sln ├── Facade ├── App.config ├── Facade.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── Restaurant.cs ├── FactoryMethodPattern ├── App.config ├── FactoryMethod.csproj ├── Ingredients.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Flyweight ├── App.config ├── Flyweight.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── Sliders.cs ├── Interpreter ├── App.config ├── FoodLanguage.cs ├── Interpreter.csproj ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Iterator ├── App.config ├── Iterator.csproj ├── JellyBeans.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Mediator ├── App.config ├── Mediator.csproj ├── MovieTheatreConcessions.cs ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Memento ├── App.config ├── FoodSupplier.cs ├── Memento.csproj ├── Program.cs └── Properties │ └── AssemblyInfo.cs ├── Observer ├── App.config ├── Observer.csproj ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── VeggiePrices.cs ├── Prototype ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── Prototype.csproj └── Sandwiches.cs ├── Proxy ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── Proxy.csproj └── Server.cs ├── README.md ├── Singleton ├── App.config ├── Program.cs ├── Properties │ └── AssemblyInfo.cs ├── Singleton.csproj └── TheBell.cs ├── State ├── App.config ├── BeefDoneness.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── State.csproj ├── Strategy ├── App.config ├── CookMethod.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── Strategy.csproj ├── TemplateMethod ├── App.config ├── FreshBread.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── TemplateMethod.csproj └── Visitor ├── App.config ├── Patron.cs ├── Program.cs ├── Properties └── AssemblyInfo.cs └── Visitor.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 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | # DNX 42 | project.lock.json 43 | artifacts/ 44 | 45 | *_i.c 46 | *_p.c 47 | *_i.h 48 | *.ilk 49 | *.meta 50 | *.obj 51 | *.pch 52 | *.pdb 53 | *.pgc 54 | *.pgd 55 | *.rsp 56 | *.sbr 57 | *.tlb 58 | *.tli 59 | *.tlh 60 | *.tmp 61 | *.tmp_proj 62 | *.log 63 | *.vspscc 64 | *.vssscc 65 | .builds 66 | *.pidb 67 | *.svclog 68 | *.scc 69 | 70 | # Chutzpah Test files 71 | _Chutzpah* 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opensdf 78 | *.sdf 79 | *.cachefile 80 | 81 | # Visual Studio profiler 82 | *.psess 83 | *.vsp 84 | *.vspx 85 | 86 | # TFS 2012 Local Workspace 87 | $tf/ 88 | 89 | # Guidance Automation Toolkit 90 | *.gpState 91 | 92 | # ReSharper is a .NET coding add-in 93 | _ReSharper*/ 94 | *.[Rr]e[Ss]harper 95 | *.DotSettings.user 96 | 97 | # JustCode is a .NET coding add-in 98 | .JustCode 99 | 100 | # TeamCity is a build add-in 101 | _TeamCity* 102 | 103 | # DotCover is a Code Coverage Tool 104 | *.dotCover 105 | 106 | # NCrunch 107 | _NCrunch_* 108 | .*crunch*.local.xml 109 | 110 | # MightyMoose 111 | *.mm.* 112 | AutoTest.Net/ 113 | 114 | # Web workbench (sass) 115 | .sass-cache/ 116 | 117 | # Installshield output folder 118 | [Ee]xpress/ 119 | 120 | # DocProject is a documentation generator add-in 121 | DocProject/buildhelp/ 122 | DocProject/Help/*.HxT 123 | DocProject/Help/*.HxC 124 | DocProject/Help/*.hhc 125 | DocProject/Help/*.hhk 126 | DocProject/Help/*.hhp 127 | DocProject/Help/Html2 128 | DocProject/Help/html 129 | 130 | # Click-Once directory 131 | publish/ 132 | 133 | # Publish Web Output 134 | *.[Pp]ublish.xml 135 | *.azurePubxml 136 | ## TODO: Comment the next line if you want to checkin your 137 | ## web deploy settings but do note that will include unencrypted 138 | ## passwords 139 | #*.pubxml 140 | 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # Uncomment if necessary however generally it will be regenerated when needed 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Visual Studio cache files 160 | # files ending in .cache can be ignored 161 | *.[Cc]ache 162 | # but keep track of directories ending in .cache 163 | !*.[Cc]ache/ 164 | 165 | # Others 166 | ClientBin/ 167 | [Ss]tyle[Cc]op.* 168 | ~$* 169 | *~ 170 | *.dbmdl 171 | *.dbproj.schemaview 172 | *.pfx 173 | *.publishsettings 174 | node_modules/ 175 | orleans.codegen.cs 176 | 177 | # RIA/Silverlight projects 178 | Generated_Code/ 179 | 180 | # Backup & report files from converting an old project file 181 | # to a newer Visual Studio version. Backup files are not needed, 182 | # because we have git ;-) 183 | _UpgradeReport_Files/ 184 | Backup*/ 185 | UpgradeLog*.XML 186 | UpgradeLog*.htm 187 | 188 | # SQL Server files 189 | *.mdf 190 | *.ldf 191 | 192 | # Business Intelligence projects 193 | *.rdl.data 194 | *.bim.layout 195 | *.bim_*.settings 196 | 197 | # Microsoft Fakes 198 | FakesAssemblies/ 199 | 200 | # Node.js Tools for Visual Studio 201 | .ntvs_analysis.dat 202 | 203 | # Visual Studio 6 build log 204 | *.plg 205 | 206 | # Visual Studio 6 workspace options file 207 | *.opt 208 | 209 | # LightSwitch generated files 210 | GeneratedArtifacts/ 211 | _Pvt_Extensions/ 212 | ModelManifest.xml 213 | -------------------------------------------------------------------------------- /AbstractFactory/AbstractFactory.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {67F0E1E6-0E12-40F5-B71F-EC9E217914F7} 8 | Exe 9 | Properties 10 | AbstractFactory 11 | AbstractFactory 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /AbstractFactory/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /AbstractFactory/Program.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 AbstractFactory 8 | { 9 | class Program 10 | { 11 | /// 12 | /// The Abstract Factory pattern provides an interface for creating related families of objects 13 | /// without needing to specify the concrete implementations. This pattern is critical for ideas 14 | /// like Dependency Injection. 15 | /// 16 | /// 17 | static void Main(string[] args) 18 | { 19 | Console.WriteLine("Who are you? (A)dult or (C)hild?"); 20 | char input = Console.ReadKey().KeyChar; 21 | RecipeFactory factory; 22 | switch(input) 23 | { 24 | case 'A': 25 | factory = new AdultCuisineFactory(); 26 | break; 27 | 28 | case 'C': 29 | factory = new KidCuisineFactory(); 30 | break; 31 | 32 | default: 33 | throw new NotImplementedException(); 34 | 35 | } 36 | 37 | var sandwich = factory.CreateSandwich(); 38 | var dessert = factory.CreateDessert(); 39 | 40 | Console.WriteLine("\nSandwich: " + sandwich.GetType().Name); 41 | Console.WriteLine("Dessert: " + dessert.GetType().Name); 42 | 43 | Console.ReadKey(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /AbstractFactory/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("AbstractFactory")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("AbstractFactory")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("67f0e1e6-0e12-40f5-b71f-ec9e217914f7")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /AbstractFactory/RecipeFactory.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 AbstractFactory 8 | { 9 | /// 10 | /// An abstract product. 11 | /// 12 | abstract class Sandwich { } 13 | 14 | /// 15 | /// An abstract product. 16 | /// 17 | abstract class Dessert { } 18 | 19 | /// 20 | /// The Abstract Factory class, which defines methods for creating abstract objects. 21 | /// 22 | abstract class RecipeFactory 23 | { 24 | public abstract Sandwich CreateSandwich(); 25 | public abstract Dessert CreateDessert(); 26 | } 27 | 28 | /// 29 | /// A concrete product 30 | /// 31 | class BLT : Sandwich { } 32 | 33 | /// 34 | /// A concrete product 35 | /// 36 | class CremeBrulee : Dessert { } 37 | 38 | 39 | /// 40 | /// A concrete factory which creates concrete objects by implementing the abstract factory's methods. 41 | /// 42 | class AdultCuisineFactory : RecipeFactory 43 | { 44 | public override Sandwich CreateSandwich() 45 | { 46 | return new BLT(); 47 | } 48 | 49 | public override Dessert CreateDessert() 50 | { 51 | return new CremeBrulee(); 52 | } 53 | } 54 | 55 | /// 56 | /// A concrete product 57 | /// 58 | class PBandJ : Sandwich { } 59 | 60 | /// 61 | /// A concrete product 62 | /// 63 | class IceCreamSundae : Dessert { } 64 | 65 | /// 66 | /// A concrete factory which creates concrete objects by implementing the abstract factory's methods. 67 | /// 68 | class KidCuisineFactory : RecipeFactory 69 | { 70 | public override Sandwich CreateSandwich() 71 | { 72 | return new PBandJ(); 73 | } 74 | 75 | public override Dessert CreateDessert() 76 | { 77 | return new IceCreamSundae(); 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Adapter/Adapter.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {7EAE4108-CF20-42E8-B011-8D9F2F891026} 8 | Exe 9 | Properties 10 | Adapter 11 | Adapter 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Adapter/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Adapter/Meats.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 Adapter 8 | { 9 | public enum TemperatureType 10 | { 11 | Fahrenheit, 12 | Celsius 13 | } 14 | /// 15 | /// The legacy API which must be converted to the new structure (aka the Adaptee participant) 16 | /// 17 | class MeatDatabase 18 | { 19 | // Temps from http://www.foodsafety.gov/keep/charts/mintemp.html 20 | public float GetSafeCookTemp(string meat, TemperatureType tempType) 21 | { 22 | if (tempType == TemperatureType.Fahrenheit) 23 | { 24 | switch (meat) 25 | { 26 | case "beef": 27 | case "pork": 28 | return 145f; 29 | 30 | case "chicken": 31 | case "turkey": 32 | return 165f; 33 | 34 | default: 35 | return 165f; 36 | } 37 | } 38 | else 39 | { 40 | switch (meat) 41 | { 42 | case "beef": 43 | case "veal": 44 | case "pork": 45 | return 63f; 46 | 47 | case "chicken": 48 | case "turkey": 49 | return 74f; 50 | 51 | default: 52 | return 74f; 53 | } 54 | } 55 | } 56 | 57 | public int GetCaloriesPerOunce(string meat) 58 | { 59 | switch (meat.ToLower()) 60 | { 61 | case "beef": return 71; 62 | case "pork": return 69; 63 | case "chicken": return 66; 64 | case "turkey": return 38; 65 | default: return 0; 66 | } 67 | } 68 | 69 | public double GetProteinPerOunce(string meat) 70 | { 71 | switch (meat.ToLower()) 72 | { 73 | case "beef": return 7.33f; 74 | case "pork": return 7.67f; 75 | case "chicken": return 8.57f; 76 | case "turkey": return 8.5f; 77 | default: return 0d; 78 | } 79 | } 80 | } 81 | 82 | /// 83 | /// The Target participant, which represents details about a particular kind of meat. 84 | /// 85 | class Meat 86 | { 87 | protected string MeatName; 88 | protected float SafeCookTempFahrenheit; 89 | protected float SafeCookTempCelsius; 90 | protected double CaloriesPerOunce; 91 | protected double ProteinPerOunce; 92 | 93 | // Constructor 94 | public Meat(string meat) 95 | { 96 | this.MeatName = meat; 97 | } 98 | 99 | public virtual void LoadData() 100 | { 101 | Console.WriteLine("\nMeat: {0} ------ ", MeatName); 102 | } 103 | } 104 | 105 | /// 106 | /// The Adapter class, which wraps the Meat class and initializes that class's values. 107 | /// 108 | class MeatDetails : Meat 109 | { 110 | private MeatDatabase _meatDatabase; 111 | 112 | // Constructor 113 | public MeatDetails(string name) 114 | : base(name) 115 | { 116 | } 117 | 118 | public override void LoadData() 119 | { 120 | // The Adaptee 121 | _meatDatabase = new MeatDatabase(); 122 | 123 | SafeCookTempFahrenheit = _meatDatabase.GetSafeCookTemp(MeatName, TemperatureType.Fahrenheit); 124 | SafeCookTempCelsius = _meatDatabase.GetSafeCookTemp(MeatName, TemperatureType.Celsius); 125 | CaloriesPerOunce = _meatDatabase.GetCaloriesPerOunce(MeatName); 126 | ProteinPerOunce = _meatDatabase.GetProteinPerOunce(MeatName); 127 | 128 | base.LoadData(); 129 | Console.WriteLine(" Safe Cook Temp (F): {0}", SafeCookTempFahrenheit); 130 | Console.WriteLine(" Safe Cook Temp (C): {0}", SafeCookTempCelsius); 131 | Console.WriteLine(" Calories per Ounce: {0}", CaloriesPerOunce); 132 | Console.WriteLine(" Protein per Ounce: {0}", ProteinPerOunce); 133 | } 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /Adapter/Program.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 Adapter 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | //Non-adapted 14 | Meat unknown = new Meat("Beef"); 15 | unknown.LoadData(); 16 | 17 | //Adapted 18 | MeatDetails beef = new MeatDetails("Beef"); 19 | beef.LoadData(); 20 | 21 | MeatDetails turkey = new MeatDetails("Turkey"); 22 | turkey.LoadData(); 23 | 24 | MeatDetails chicken = new MeatDetails("Chicken"); 25 | chicken.LoadData(); 26 | 27 | Console.ReadKey(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Adapter/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Adapter")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Adapter")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("7eae4108-cf20-42e8-b011-8d9f2f891026")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Bridge/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Bridge/Bridge.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {08ED1B4B-711C-4500-A380-F9454288E691} 8 | Exe 9 | Properties 10 | Bridge 11 | Bridge 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Bridge/OrderingSystem.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 Bridge 8 | { 9 | /// 10 | /// Implementor which defines an interface for placing an order 11 | /// 12 | public interface IOrderingSystem 13 | { 14 | void Place(string order); 15 | } 16 | 17 | /// 18 | /// Abstraction which represents the sent order and maintains a reference to the restaurant where the order is going. 19 | /// 20 | public abstract class SendOrder 21 | { 22 | //Reference to the Implementor 23 | public IOrderingSystem _restaurant; 24 | 25 | public abstract void Send(); 26 | } 27 | 28 | /// 29 | /// Refined abstraction for a dairy-free order 30 | /// 31 | public class SendDairyFreeOrder : SendOrder 32 | { 33 | public override void Send() 34 | { 35 | _restaurant.Place("Dairy-Free Order"); 36 | } 37 | } 38 | 39 | /// 40 | /// Refined abstraction for a gluten free order 41 | /// 42 | public class SendGlutenFreeOrder : SendOrder 43 | { 44 | public override void Send() 45 | { 46 | _restaurant.Place("Gluten-Free Order"); 47 | } 48 | } 49 | 50 | /// 51 | /// Concrete implementor for an ordering system at a diner. 52 | /// 53 | public class DinerOrders : IOrderingSystem 54 | { 55 | public void Place(string order) 56 | { 57 | Console.WriteLine("Placing order for " + order + " at the Diner."); 58 | } 59 | } 60 | 61 | /// 62 | /// Concrete implementor for an ordering system at a fancy restaurant. 63 | /// 64 | public class FancyRestaurantOrders : IOrderingSystem 65 | { 66 | public void Place(string order) 67 | { 68 | Console.WriteLine("Placing order for " + order + " at the Fancy Restaurant."); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Bridge/Program.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 Bridge 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | SendOrder _sendOrder = new SendDairyFreeOrder(); 14 | _sendOrder._restaurant = new DinerOrders(); 15 | _sendOrder.Send(); 16 | 17 | _sendOrder._restaurant = new FancyRestaurantOrders(); 18 | _sendOrder.Send(); 19 | 20 | _sendOrder = new SendGlutenFreeOrder(); 21 | _sendOrder._restaurant = new DinerOrders(); 22 | _sendOrder.Send(); 23 | 24 | _sendOrder._restaurant = new FancyRestaurantOrders(); 25 | _sendOrder.Send(); 26 | 27 | Console.ReadKey(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Bridge/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Bridge")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Bridge")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("08ed1b4b-711c-4500-a380-f9454288e691")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Builder/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Builder/Builder.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {ECD40C19-C21B-453B-A57C-22BC884F12CB} 8 | Exe 9 | Properties 10 | Builder 11 | Builder 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Builder/Program.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 Builder 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | SandwichBuilder builder; 14 | 15 | // Create deli with sandwich assembly line 16 | AssemblyLine shop = new AssemblyLine(); 17 | 18 | // Construct and display sandwiches 19 | builder = new HamAndCheese(); 20 | shop.Assemble(builder); 21 | builder.Sandwich.Show(); 22 | 23 | builder = new BLT(); 24 | shop.Assemble(builder); 25 | builder.Sandwich.Show(); 26 | 27 | builder = new TurkeyClub(); 28 | shop.Assemble(builder); 29 | builder.Sandwich.Show(); 30 | 31 | // Wait for user 32 | Console.ReadKey(); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Builder/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Builder")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Builder")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("ecd40c19-c21b-453b-a57c-22bc884f12cb")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Builder/SandwichAssemblyLine.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 Builder 8 | { 9 | /// 10 | /// The Director 11 | /// 12 | class AssemblyLine 13 | { 14 | // Builder uses a complex series of steps 15 | // 16 | public void Assemble(SandwichBuilder sandwichBuilder) 17 | { 18 | sandwichBuilder.AddBread(); 19 | sandwichBuilder.AddMeats(); 20 | sandwichBuilder.AddCheese(); 21 | sandwichBuilder.AddVeggies(); 22 | sandwichBuilder.AddCondiments(); 23 | } 24 | } 25 | 26 | /// 27 | /// The Builder abstract class 28 | /// 29 | abstract class SandwichBuilder 30 | { 31 | protected Sandwich sandwich; 32 | 33 | // Gets sandwich instance 34 | public Sandwich Sandwich 35 | { 36 | get { return sandwich; } 37 | } 38 | 39 | // Abstract build methods 40 | public abstract void AddBread(); 41 | public abstract void AddMeats(); 42 | public abstract void AddCheese(); 43 | public abstract void AddVeggies(); 44 | public abstract void AddCondiments(); 45 | } 46 | 47 | /// 48 | /// A Concrete Builder class 49 | /// 50 | class TurkeyClub : SandwichBuilder 51 | { 52 | public TurkeyClub() 53 | { 54 | sandwich = new Sandwich("Turkey Club"); 55 | } 56 | 57 | public override void AddBread() 58 | { 59 | sandwich["bread"] = "12-Grain"; 60 | } 61 | 62 | public override void AddMeats() 63 | { 64 | sandwich["meat"] = "Turkey"; 65 | } 66 | 67 | public override void AddCheese() 68 | { 69 | sandwich["cheese"] = "Swiss"; 70 | } 71 | 72 | public override void AddVeggies() 73 | { 74 | sandwich["veggies"] = "Lettuce, Tomato"; 75 | } 76 | 77 | public override void AddCondiments() 78 | { 79 | sandwich["condiments"] = "Mayo"; 80 | } 81 | } 82 | 83 | 84 | /// 85 | /// A Concrete Builder class 86 | /// 87 | class BLT : SandwichBuilder 88 | { 89 | public BLT() 90 | { 91 | sandwich = new Sandwich("BLT"); 92 | } 93 | 94 | public override void AddBread() 95 | { 96 | sandwich["bread"] = "Wheat"; 97 | } 98 | 99 | public override void AddMeats() 100 | { 101 | sandwich["meat"] = "Bacon"; 102 | } 103 | 104 | public override void AddCheese() 105 | { 106 | sandwich["cheese"] = "None"; 107 | } 108 | 109 | public override void AddVeggies() 110 | { 111 | sandwich["veggies"] = "Lettuce, Tomato"; 112 | } 113 | 114 | public override void AddCondiments() 115 | { 116 | sandwich["condiments"] = "Mayo, Mustard"; 117 | } 118 | } 119 | 120 | /// 121 | /// A Concrete Builder class 122 | /// 123 | class HamAndCheese : SandwichBuilder 124 | { 125 | public HamAndCheese() 126 | { 127 | sandwich = new Sandwich("Ham and Cheese"); 128 | } 129 | 130 | public override void AddBread() 131 | { 132 | sandwich["bread"] = "White"; 133 | } 134 | 135 | public override void AddMeats() 136 | { 137 | sandwich["meat"] = "Ham"; 138 | } 139 | 140 | public override void AddCheese() 141 | { 142 | sandwich["cheese"] = "American"; 143 | } 144 | 145 | public override void AddVeggies() 146 | { 147 | sandwich["veggies"] = "None"; 148 | } 149 | 150 | public override void AddCondiments() 151 | { 152 | sandwich["condiments"] = "Mayo"; 153 | } 154 | } 155 | 156 | /// 157 | /// The Product class 158 | /// 159 | class Sandwich 160 | { 161 | private string _sandwichType; 162 | private Dictionary _ingredients = new Dictionary(); 163 | 164 | // Constructor 165 | public Sandwich(string sandwichType) 166 | { 167 | this._sandwichType = sandwichType; 168 | } 169 | 170 | // Indexer 171 | public string this[string key] 172 | { 173 | get { return _ingredients[key]; } 174 | set { _ingredients[key] = value; } 175 | } 176 | 177 | public void Show() 178 | { 179 | Console.WriteLine("\n---------------------------"); 180 | Console.WriteLine("Sandwich: {0}", _sandwichType); 181 | Console.WriteLine(" Bread: {0}", _ingredients["bread"]); 182 | Console.WriteLine(" Meat: {0}", _ingredients["meat"]); 183 | Console.WriteLine(" Cheese: {0}", _ingredients["cheese"]); 184 | Console.WriteLine(" Veggies: {0}", _ingredients["veggies"]); 185 | Console.WriteLine(" Condiments: {0}", _ingredients["condiments"]); 186 | } 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /ChainOfResponsibility/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ChainOfResponsibility/ChainOfResponsibility.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {E3356AE6-5B85-4163-B647-EFD7DC544FFC} 8 | Exe 9 | Properties 10 | ChainOfResponsibility 11 | ChainOfResponsibility 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /ChainOfResponsibility/Program.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 ChainOfResponsibility 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | //Create the chain links 14 | Approver jennifer = new HeadChef(); 15 | Approver mitchell = new PurchasingManager(); 16 | Approver olivia = new GeneralManager(); 17 | 18 | //Create the chain 19 | jennifer.SetSupervisor(mitchell); 20 | mitchell.SetSupervisor(olivia); 21 | 22 | // Generate and process purchase requests 23 | PurchaseOrder p = new PurchaseOrder(1, 20, 69, "Spices"); 24 | jennifer.ProcessRequest(p); 25 | 26 | p = new PurchaseOrder(2, 300, 1389, "Fresh Veggies"); 27 | jennifer.ProcessRequest(p); 28 | 29 | p = new PurchaseOrder(3, 500, 4823.99, "Beef"); 30 | jennifer.ProcessRequest(p); 31 | 32 | p = new PurchaseOrder(4, 4, 12099, "Ovens"); 33 | jennifer.ProcessRequest(p); 34 | 35 | // Wait for user 36 | Console.ReadKey(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /ChainOfResponsibility/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("ChainOfResponsibility")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("ChainOfResponsibility")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e3356ae6-5b85-4163-b647-efd7dc544ffc")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /ChainOfResponsibility/RestaurantRequest.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 ChainOfResponsibility 8 | { 9 | /// 10 | /// The Handler abstract class. Every class which inherits from this will be responsible for a kind of request for the restaurant. 11 | /// 12 | abstract class Approver 13 | { 14 | protected Approver Supervisor; 15 | 16 | public void SetSupervisor(Approver supervisor) 17 | { 18 | this.Supervisor = supervisor; 19 | } 20 | 21 | public abstract void ProcessRequest(PurchaseOrder purchase); 22 | } 23 | 24 | /// 25 | /// A concrete Handler class 26 | /// 27 | class HeadChef : Approver 28 | { 29 | public override void ProcessRequest(PurchaseOrder purchase) 30 | { 31 | if (purchase.Price < 1000) 32 | { 33 | Console.WriteLine("{0} approved purchase request #{1}", 34 | this.GetType().Name, purchase.RequestNumber); 35 | } 36 | else if (Supervisor != null) 37 | { 38 | Supervisor.ProcessRequest(purchase); 39 | } 40 | } 41 | } 42 | 43 | /// 44 | /// A concrete Handler class 45 | /// 46 | class PurchasingManager : Approver 47 | { 48 | public override void ProcessRequest(PurchaseOrder purchase) 49 | { 50 | if (purchase.Price < 2500) 51 | { 52 | Console.WriteLine("{0} approved purchase request #{1}", 53 | this.GetType().Name, purchase.RequestNumber); 54 | } 55 | else if (Supervisor != null) 56 | { 57 | Supervisor.ProcessRequest(purchase); 58 | } 59 | } 60 | } 61 | 62 | /// 63 | /// A concrete Handler class 64 | /// 65 | class GeneralManager : Approver 66 | { 67 | public override void ProcessRequest(PurchaseOrder purchase) 68 | { 69 | if (purchase.Price < 10000) 70 | { 71 | Console.WriteLine("{0} approved purchase request #{1}", 72 | this.GetType().Name, purchase.RequestNumber); 73 | } 74 | else 75 | { 76 | Console.WriteLine( 77 | "Purchase request #{0} requires an executive meeting!", 78 | purchase.RequestNumber); 79 | } 80 | } 81 | } 82 | 83 | /// 84 | /// The details of the purchase request. 85 | /// 86 | class PurchaseOrder 87 | { 88 | 89 | // Constructor 90 | public PurchaseOrder(int number, double amount, double price, string name) 91 | { 92 | RequestNumber = number; 93 | Amount = amount; 94 | Price = price; 95 | Name = name; 96 | 97 | Console.WriteLine("Purchase request for " + name + " (" + amount + " for $" + price.ToString() + ") has been submitted."); 98 | } 99 | 100 | public int RequestNumber { get; set; } 101 | public double Amount { get; set; } 102 | public double Price { get; set; } 103 | public string Name { get; set; } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /Command/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Command/Command.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {442F8983-D130-4EDC-85FF-BD3372E40A25} 8 | Exe 9 | Properties 10 | Command 11 | Command 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Command/Ordering.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 Command 8 | { 9 | 10 | /// 11 | /// The Invoker class 12 | /// 13 | public class Patron 14 | { 15 | private OrderCommand _orderCommand; 16 | private MenuItem _menuItem; 17 | private FastFoodOrder _order; 18 | 19 | public Patron() 20 | { 21 | _order = new FastFoodOrder(); 22 | } 23 | 24 | public void SetCommand(int commandOption) 25 | { 26 | _orderCommand = new CommandFactory().GetCommand(commandOption); 27 | } 28 | 29 | public void SetMenuItem(MenuItem item) 30 | { 31 | _menuItem = item; 32 | } 33 | 34 | public void ExecuteCommand() 35 | { 36 | _order.ExecuteCommand(_orderCommand, _menuItem); 37 | } 38 | 39 | public void ShowCurrentOrder() 40 | { 41 | _order.ShowCurrentItems(); 42 | } 43 | } 44 | 45 | /// 46 | /// The Receiver 47 | /// 48 | public class FastFoodOrder 49 | { 50 | public List currentItems { get; set; } 51 | public FastFoodOrder() 52 | { 53 | currentItems = new List(); 54 | } 55 | 56 | public void ExecuteCommand(OrderCommand command, MenuItem item) 57 | { 58 | command.Execute(this.currentItems, item); 59 | } 60 | 61 | public void ShowCurrentItems() 62 | { 63 | foreach(var item in currentItems) 64 | { 65 | item.Display(); 66 | } 67 | Console.WriteLine("-----------------------"); 68 | } 69 | 70 | } 71 | 72 | /// 73 | /// Represents an item being ordered from this restaurant. 74 | /// 75 | public class MenuItem 76 | { 77 | public string Name { get; set; } 78 | public int Amount { get; set; } 79 | public double Price { get; set; } 80 | 81 | public MenuItem(string name, int amount, double price) 82 | { 83 | Name = name; 84 | Amount = amount; 85 | Price = price; 86 | } 87 | 88 | public void Display() 89 | { 90 | Console.WriteLine("\nName: " + Name); 91 | Console.WriteLine("Amount: " + Amount.ToString()); 92 | Console.WriteLine("Price: $" + Price.ToString()); 93 | } 94 | } 95 | 96 | /// 97 | /// The Command abstract class 98 | /// 99 | public abstract class OrderCommand 100 | { 101 | public abstract void Execute(List order, MenuItem newItem); 102 | } 103 | 104 | /// 105 | /// A concrete command 106 | /// 107 | public class AddCommand : OrderCommand 108 | { 109 | public override void Execute(List currentItems, MenuItem newItem) 110 | { 111 | currentItems.Add(newItem); 112 | } 113 | } 114 | 115 | /// 116 | /// A concrete command 117 | /// 118 | public class RemoveCommand : OrderCommand 119 | { 120 | public override void Execute(List currentItems, MenuItem newItem) 121 | { 122 | currentItems.Remove(currentItems.Where(x=>x.Name == newItem.Name).First()); 123 | } 124 | } 125 | 126 | /// 127 | /// A concrete command 128 | /// 129 | public class ModifyCommand : OrderCommand 130 | { 131 | public override void Execute(List currentItems, MenuItem newItem) 132 | { 133 | var item = currentItems.Where(x => x.Name == newItem.Name).First(); 134 | item.Price = newItem.Price; 135 | item.Amount = newItem.Amount; 136 | } 137 | } 138 | 139 | public class CommandFactory 140 | { 141 | //Factory method 142 | public OrderCommand GetCommand(int commandOption) 143 | { 144 | switch (commandOption) 145 | { 146 | case 1: 147 | return new AddCommand(); 148 | case 2: 149 | return new ModifyCommand(); 150 | case 3: 151 | return new RemoveCommand(); 152 | default: 153 | return new AddCommand(); 154 | } 155 | } 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /Command/Program.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 Command 8 | { 9 | class Program 10 | { 11 | /// 12 | /// The Command pattern encapsulates commands as objects, allowing them to be executed by a Receiver class and kicked off 13 | /// by an Invoker object. This enables more complex architectures such as CQRS (Command/Query Responsibility Segregation). 14 | /// 15 | /// 16 | static void Main(string[] args) 17 | { 18 | Patron patron = new Patron(); 19 | patron.SetCommand(1 /*Add*/); 20 | patron.SetMenuItem(new MenuItem("French Fries", 2, 1.99)); 21 | patron.ExecuteCommand(); 22 | 23 | patron.SetCommand(1 /*Add*/); 24 | patron.SetMenuItem(new MenuItem("Hamburger", 2, 2.59)); 25 | patron.ExecuteCommand(); 26 | 27 | patron.SetCommand(1 /*Add*/); 28 | patron.SetMenuItem(new MenuItem("Drink", 2, 1.19)); 29 | patron.ExecuteCommand(); 30 | 31 | patron.ShowCurrentOrder(); 32 | 33 | //Remove the french fries 34 | patron.SetCommand(3 /*Add*/); 35 | patron.SetMenuItem(new MenuItem("French Fries", 2, 1.99)); 36 | patron.ExecuteCommand(); 37 | 38 | patron.ShowCurrentOrder(); 39 | 40 | //Now we want 4 hamburgers rather than 2 41 | patron.SetCommand(2 /*Add*/); 42 | patron.SetMenuItem(new MenuItem("Hamburger", 4, 2.59)); 43 | patron.ExecuteCommand(); 44 | 45 | patron.ShowCurrentOrder(); 46 | 47 | Console.ReadKey(); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Command/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Command")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Command")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("442f8983-d130-4edc-85ff-bd3372e40a25")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Composite/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Composite/Composite.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {3BCA8A82-0242-434C-9299-C6CD5D49F301} 8 | Exe 9 | Properties 10 | Composite 11 | Composite 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Composite/Drinks.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 Composite 8 | { 9 | /// 10 | /// Component abstract class 11 | /// 12 | public abstract class SoftDrink 13 | { 14 | public int Calories { get; set; } 15 | 16 | public List Flavors { get; set; } 17 | 18 | public SoftDrink(int calories) 19 | { 20 | Calories = calories; 21 | Flavors = new List(); 22 | } 23 | 24 | /// 25 | /// "Flatten" method, returns all available flavors 26 | /// 27 | public void DisplayCalories() 28 | { 29 | Console.WriteLine(this.GetType().Name + ": " + this.Calories.ToString() + " calories."); 30 | foreach (var drink in this.Flavors) 31 | { 32 | drink.DisplayCalories(); 33 | } 34 | } 35 | } 36 | 37 | /// 38 | /// Leaf class 39 | /// 40 | public class VanillaCola : SoftDrink 41 | { 42 | public VanillaCola(int calories) : base(calories) { } 43 | } 44 | 45 | /// 46 | /// Leaf class 47 | /// 48 | public class CherryCola : SoftDrink 49 | { 50 | public CherryCola(int calories) : base(calories) { } 51 | } 52 | 53 | /// 54 | /// Leaf class 55 | /// 56 | public class StrawberryRootBeer : SoftDrink 57 | { 58 | public StrawberryRootBeer(int calories) : base(calories) { } 59 | } 60 | 61 | /// 62 | /// Leaf class 63 | /// 64 | public class VanillaRootBeer : SoftDrink 65 | { 66 | public VanillaRootBeer(int calories) : base(calories) { } 67 | } 68 | 69 | /// 70 | /// Composite class 71 | /// 72 | public class Cola : SoftDrink 73 | { 74 | public Cola(int calories) : base(calories) { } 75 | } 76 | 77 | /// 78 | /// Leaf class 79 | /// 80 | public class LemonLime : SoftDrink 81 | { 82 | public LemonLime(int calories) : base(calories) { } 83 | } 84 | 85 | /// 86 | /// Composite class 87 | /// 88 | public class RootBeer : SoftDrink 89 | { 90 | public RootBeer(int calories) : base(calories) { } 91 | } 92 | 93 | /// 94 | /// Composite class, root node 95 | /// 96 | public class SodaWater : SoftDrink 97 | { 98 | public SodaWater(int calories) : base(calories) { } 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Composite/Program.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 Composite 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var colas = new Cola(210); 14 | colas.Flavors.Add(new VanillaCola(215)); 15 | colas.Flavors.Add(new CherryCola(210)); 16 | 17 | var lemonLime = new LemonLime(185); 18 | 19 | var rootBeers = new RootBeer(195); 20 | rootBeers.Flavors.Add(new VanillaRootBeer(200)); 21 | rootBeers.Flavors.Add(new StrawberryRootBeer(200)); 22 | 23 | SodaWater sodaWater = new SodaWater(180); 24 | sodaWater.Flavors.Add(colas); 25 | sodaWater.Flavors.Add(lemonLime); 26 | sodaWater.Flavors.Add(rootBeers); 27 | 28 | sodaWater.DisplayCalories(); 29 | 30 | Console.ReadKey(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Composite/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Composite")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Composite")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("3bca8a82-0242-434c-9299-c6cd5d49f301")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Decorator/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Decorator/Decorator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {0FC531E9-3BA9-4DAB-9C6B-813D873F80AF} 8 | Exe 9 | Properties 10 | Decorator 11 | Decorator 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Decorator/FarmFresh.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 Decorator 8 | { 9 | /// 10 | /// The abstract Component class 11 | /// 12 | abstract class RestaurantDish 13 | { 14 | public abstract void Display(); 15 | } 16 | 17 | /// 18 | /// A Concrete component class 19 | /// 20 | class FreshSalad : RestaurantDish 21 | { 22 | private string _greens; 23 | private string _cheese; //I am going to use this pun everywhere I can 24 | private string _dressing; 25 | 26 | public FreshSalad(string greens, string cheese, string dressing) 27 | { 28 | _greens = greens; 29 | _cheese = cheese; 30 | _dressing = dressing; 31 | } 32 | 33 | public override void Display() 34 | { 35 | Console.WriteLine("\nFresh Salad:"); 36 | Console.WriteLine(" Greens: {0}", _greens); 37 | Console.WriteLine(" Cheese: {0}", _cheese); 38 | Console.WriteLine(" Dressing: {0}", _dressing); 39 | } 40 | } 41 | 42 | /// 43 | /// A Concrete component class 44 | /// 45 | class Pasta : RestaurantDish 46 | { 47 | private string _pastaType; 48 | private string _sauce; 49 | 50 | public Pasta(string pastaType, string sauce) 51 | { 52 | _pastaType = pastaType; 53 | _sauce = sauce; 54 | } 55 | 56 | public override void Display() 57 | { 58 | Console.WriteLine("\nClassic Pasta:"); 59 | Console.WriteLine(" Pasta: {0}", _pastaType); 60 | Console.WriteLine(" Sauce: {0}", _sauce); 61 | } 62 | } 63 | 64 | /// 65 | /// The abstract Decorator class. 66 | /// 67 | abstract class Decorator : RestaurantDish 68 | { 69 | protected RestaurantDish _dish; 70 | 71 | public Decorator(RestaurantDish dish) 72 | { 73 | _dish = dish; 74 | } 75 | 76 | public override void Display() 77 | { 78 | _dish.Display(); 79 | } 80 | } 81 | 82 | /// 83 | /// A concrete Decorator. This class will impart "responsibilities" onto the dishes (e.g. whether or not those dishes have enough ingredients left to order them) 84 | /// 85 | class Available : Decorator 86 | { 87 | public int NumAvailable { get; set; } //How many can we make? 88 | protected List customers = new List(); 89 | public Available(RestaurantDish dish, int numAvailable) : base(dish) 90 | { 91 | NumAvailable = numAvailable; 92 | } 93 | 94 | public void OrderItem(string name) 95 | { 96 | if (NumAvailable > 0) 97 | { 98 | customers.Add(name); 99 | NumAvailable--; 100 | } 101 | else 102 | { 103 | Console.WriteLine("\nNot enough ingredients for " + name + "'s order!"); 104 | } 105 | } 106 | 107 | public override void Display() 108 | { 109 | base.Display(); 110 | 111 | foreach(var customer in customers) 112 | { 113 | Console.WriteLine("Ordered by " + customer); 114 | } 115 | } 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /Decorator/Program.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 Decorator 8 | { 9 | class Program 10 | { 11 | /// 12 | /// We run a farm-fresh restaurant. 13 | /// That means that we can only make dishes which we have the ingredients for. 14 | /// Sometimes we run out of ingredients, so we can't make those dishes. 15 | /// 16 | /// 17 | static void Main(string[] args) 18 | { 19 | //Step 1: Define some dishes, and how many of each we can make 20 | FreshSalad caesarSalad = new FreshSalad("Crisp romaine lettuce", "Freshly-grated Parmesan cheese", "House-made Caesar dressing"); 21 | caesarSalad.Display(); 22 | 23 | Pasta fettuccineAlfredo = new Pasta("Fresh-made daily pasta", "Creamly garlic alfredo sauce"); 24 | fettuccineAlfredo.Display(); 25 | 26 | Console.WriteLine("\nMaking these dishes available."); 27 | 28 | //Step 2: Decorate the dishes; now if we attempt to order them once we're out of ingredients, we can notify the customer 29 | Available caesarAvailable = new Available(caesarSalad, 3); 30 | Available alfredoAvailable = new Available(fettuccineAlfredo, 4); 31 | 32 | //Step 3: Order a bunch of dishes 33 | caesarAvailable.OrderItem("John"); 34 | caesarAvailable.OrderItem("Sally"); 35 | caesarAvailable.OrderItem("Manush"); 36 | 37 | alfredoAvailable.OrderItem("Sally"); 38 | alfredoAvailable.OrderItem("Francis"); 39 | alfredoAvailable.OrderItem("Venkat"); 40 | alfredoAvailable.OrderItem("Diana"); 41 | alfredoAvailable.OrderItem("Dennis"); //There won't be enough for this order. 42 | 43 | caesarAvailable.Display(); 44 | alfredoAvailable.Display(); 45 | 46 | Console.ReadKey(); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Decorator/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Decorator")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Decorator")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("0fc531e9-3ba9-4dab-9c6b-813d873f80af")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Facade/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Facade/Facade.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F622287B-B0EF-48F7-98AC-158FB71C2480} 8 | Exe 9 | Properties 10 | Facade 11 | Facade 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Facade/Program.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 Facade 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | Server server = new Server(); 14 | 15 | Console.WriteLine("Hello! I'll be your server today. What is your name?"); 16 | var name = Console.ReadLine(); 17 | 18 | Patron patron = new Patron(name); 19 | 20 | Console.WriteLine("Hello " + patron.Name + ". What appetizer would you like? (1-15):"); 21 | var appID = int.Parse(Console.ReadLine()); 22 | 23 | Console.WriteLine("That's a good one. What entree would you like? (1-20):"); 24 | var entreeID = int.Parse(Console.ReadLine()); 25 | 26 | Console.WriteLine("A great choice! Finally, what drink would you like? (1-60):"); 27 | var drinkID = int.Parse(Console.ReadLine()); 28 | 29 | Console.WriteLine("I'll get that order in right away."); 30 | 31 | server.PlaceOrder(patron, appID, entreeID, drinkID); 32 | 33 | Console.ReadKey(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Facade/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Facade")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Facade")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f622287b-b0ef-48f7-98ac-158fb71c2480")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Facade/Restaurant.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 Facade 8 | { 9 | /// 10 | /// All items sold in the restaurant must inherit from this. 11 | /// 12 | class FoodItem { public int DishID; } 13 | 14 | /// 15 | /// Each section of the kitchen must implement this interface. 16 | /// 17 | interface KitchenSection 18 | { 19 | FoodItem PrepDish(int DishID); 20 | } 21 | 22 | /// 23 | /// Orders placed by Patrons. 24 | /// 25 | class Order 26 | { 27 | public FoodItem Appetizer { get; set; } 28 | public FoodItem Entree { get; set; } 29 | public FoodItem Drink { get; set; } 30 | } 31 | 32 | /// 33 | /// Patron of the restaurant 34 | /// 35 | class Patron 36 | { 37 | private string _name; 38 | 39 | public Patron(string name) 40 | { 41 | this._name = name; 42 | } 43 | 44 | public string Name 45 | { 46 | get { return _name; } 47 | } 48 | } 49 | 50 | 51 | /// 52 | /// A division of the kitchen. 53 | /// 54 | class ColdPrep : KitchenSection 55 | { 56 | public FoodItem PrepDish(int dishID) 57 | { 58 | Console.WriteLine("Cold Prep is preparing Appetizer #" + dishID); 59 | //Go prep the appetizer 60 | return new FoodItem() 61 | { 62 | DishID = dishID 63 | }; 64 | } 65 | } 66 | 67 | /// 68 | /// A division of the kitchen. 69 | /// 70 | class HotPrep : KitchenSection 71 | { 72 | public FoodItem PrepDish(int dishID) 73 | { 74 | Console.WriteLine("Hot Prep is preparing Entree #" + dishID); 75 | //Go prep the entree 76 | return new FoodItem() 77 | { 78 | DishID = dishID 79 | }; 80 | } 81 | } 82 | 83 | /// 84 | /// A division of the kitchen. 85 | /// 86 | class Bar : KitchenSection 87 | { 88 | public FoodItem PrepDish(int dishID) 89 | { 90 | Console.WriteLine("The Bar is preparing Drink #" + dishID); 91 | //Go mix the drink 92 | return new FoodItem() 93 | { 94 | DishID = dishID 95 | }; 96 | } 97 | } 98 | 99 | /// 100 | /// The actual "Facade" class, which hides the complexity of the KitchenSection classes. 101 | /// After all, there's no reason a patron should order each part of their meal individually. 102 | /// 103 | class Server 104 | { 105 | private ColdPrep _coldPrep = new ColdPrep(); 106 | private Bar _bar = new Bar(); 107 | private HotPrep _hotPrep = new HotPrep(); 108 | 109 | public Order PlaceOrder(Patron patron, int coldAppID, int hotEntreeID, int drinkID) 110 | { 111 | Console.WriteLine("{0} places order for cold app #" + coldAppID.ToString() 112 | + ", hot entree #" + hotEntreeID.ToString() 113 | + ", and drink #" + drinkID.ToString() + ".", patron.Name); 114 | 115 | Order order = new Order(); 116 | 117 | order.Appetizer = _coldPrep.PrepDish(coldAppID); 118 | order.Entree = _hotPrep.PrepDish(hotEntreeID); 119 | order.Drink = _bar.PrepDish(drinkID); 120 | 121 | return order; 122 | } 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /FactoryMethodPattern/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /FactoryMethodPattern/FactoryMethod.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {18A0C631-63F2-466B-9195-9AD75543FF66} 8 | Exe 9 | Properties 10 | FactoryMethodPattern 11 | FactoryMethodPattern 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /FactoryMethodPattern/Ingredients.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 FactoryMethod 8 | { 9 | /// 10 | /// Product 11 | /// 12 | abstract class Ingredient { } 13 | 14 | /// 15 | /// Concrete Product 16 | /// 17 | class Bread : Ingredient { } 18 | 19 | /// 20 | /// Concrete Product 21 | /// 22 | class Turkey : Ingredient { } 23 | 24 | /// 25 | /// Concrete Product 26 | /// 27 | class Lettuce : Ingredient { } 28 | 29 | /// 30 | /// Concrete Product 31 | /// 32 | class Mayonnaise : Ingredient { } 33 | 34 | 35 | /// 36 | /// Creator 37 | /// 38 | abstract class Sandwich 39 | { 40 | private List _ingredients = new List(); 41 | 42 | public Sandwich() 43 | { 44 | CreateIngredients(); 45 | } 46 | 47 | //Factory method 48 | public abstract void CreateIngredients(); 49 | 50 | public List Ingredients 51 | { 52 | get { return _ingredients; } 53 | } 54 | } 55 | 56 | /// 57 | /// Concrete Creator 58 | /// 59 | class TurkeySandwich : Sandwich 60 | { 61 | public override void CreateIngredients() 62 | { 63 | Ingredients.Add(new Bread()); 64 | Ingredients.Add(new Mayonnaise()); 65 | Ingredients.Add(new Lettuce()); 66 | Ingredients.Add(new Turkey()); 67 | Ingredients.Add(new Turkey()); 68 | Ingredients.Add(new Bread()); 69 | } 70 | } 71 | 72 | /// 73 | /// Concrete Creator 74 | /// 75 | class Dagwood : Sandwich //OM NOM NOM 76 | { 77 | public override void CreateIngredients() 78 | { 79 | Ingredients.Add(new Bread()); 80 | Ingredients.Add(new Turkey()); 81 | Ingredients.Add(new Turkey()); 82 | Ingredients.Add(new Lettuce()); 83 | Ingredients.Add(new Lettuce()); 84 | Ingredients.Add(new Mayonnaise()); 85 | Ingredients.Add(new Bread()); 86 | Ingredients.Add(new Turkey()); 87 | Ingredients.Add(new Turkey()); 88 | Ingredients.Add(new Lettuce()); 89 | Ingredients.Add(new Lettuce()); 90 | Ingredients.Add(new Mayonnaise()); 91 | Ingredients.Add(new Bread()); 92 | Ingredients.Add(new Turkey()); 93 | Ingredients.Add(new Turkey()); 94 | Ingredients.Add(new Lettuce()); 95 | Ingredients.Add(new Lettuce()); 96 | Ingredients.Add(new Mayonnaise()); 97 | Ingredients.Add(new Bread()); 98 | Ingredients.Add(new Turkey()); 99 | Ingredients.Add(new Turkey()); 100 | Ingredients.Add(new Lettuce()); 101 | Ingredients.Add(new Lettuce()); 102 | Ingredients.Add(new Mayonnaise()); 103 | Ingredients.Add(new Bread()); 104 | Ingredients.Add(new Turkey()); 105 | Ingredients.Add(new Turkey()); 106 | Ingredients.Add(new Lettuce()); 107 | Ingredients.Add(new Lettuce()); 108 | Ingredients.Add(new Mayonnaise()); 109 | Ingredients.Add(new Bread()); 110 | Ingredients.Add(new Turkey()); 111 | Ingredients.Add(new Turkey()); 112 | Ingredients.Add(new Lettuce()); 113 | Ingredients.Add(new Lettuce()); 114 | Ingredients.Add(new Mayonnaise()); 115 | Ingredients.Add(new Bread()); 116 | } 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /FactoryMethodPattern/Program.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 FactoryMethod 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | List sandwiches = new List(); 14 | sandwiches.Add(new TurkeySandwich()); 15 | sandwiches.Add(new Dagwood()); 16 | 17 | foreach(var sandwich in sandwiches) 18 | { 19 | Console.WriteLine("\nSandwich: " + sandwich.GetType().Name + " "); 20 | foreach(var ingredient in sandwich.Ingredients) 21 | { 22 | Console.WriteLine("Ingredient: " + ingredient.GetType().Name); 23 | } 24 | } 25 | 26 | Console.ReadKey(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /FactoryMethodPattern/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("FactoryMethodPattern")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("FactoryMethodPattern")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("18a0c631-63f2-466b-9195-9ad75543ff66")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Flyweight/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Flyweight/Flyweight.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {9719E34D-9233-47A0-B655-399DA51A2042} 8 | Exe 9 | Properties 10 | Flyweight 11 | Flyweight 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Flyweight/Program.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 Flyweight 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | // Build a slider order using patron's input 14 | Console.WriteLine("Please enter your slider order (use characters B, V, Q with no spaces):"); 15 | var order = Console.ReadLine(); 16 | char[] chars = order.ToCharArray(); 17 | 18 | SliderFactory factory = new SliderFactory(); 19 | 20 | int orderTotal = 0; 21 | 22 | //Get the slider from the factory 23 | foreach (char c in chars) 24 | { 25 | orderTotal++; 26 | Slider character = factory.GetSlider(c); 27 | character.Display(orderTotal); 28 | } 29 | 30 | Console.ReadKey(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Flyweight/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Flyweight")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Flyweight")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("9719e34d-9233-47a0-b655-399da51a2042")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Flyweight/Sliders.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 Flyweight 8 | { 9 | /// 10 | /// The Flyweight Factory class 11 | /// 12 | class SliderFactory 13 | { 14 | private Dictionary _sliders = 15 | new Dictionary(); 16 | 17 | public Slider GetSlider(char key) 18 | { 19 | Slider slider = null; 20 | if (_sliders.ContainsKey(key)) //If we've already created an instance of the requested type of slider, just use that. 21 | { 22 | slider = _sliders[key]; 23 | } 24 | else //Otherwise, create a brand new slider instance. 25 | { 26 | switch (key) 27 | { 28 | case 'B': slider = new BaconMaster(); break; 29 | case 'V': slider = new VeggieSlider(); break; 30 | case 'Q': slider = new BBQKing(); break; 31 | } 32 | _sliders.Add(key, slider); 33 | } 34 | return slider; 35 | } 36 | } 37 | 38 | /// 39 | /// The 'Flyweight' abstract class 40 | /// 41 | abstract class Slider 42 | { 43 | protected string Name; 44 | protected string Cheese; 45 | protected string Toppings; 46 | protected decimal Price; 47 | 48 | public abstract void Display(int orderTotal); 49 | } 50 | 51 | /// 52 | /// A Concrete Flyweight class 53 | /// 54 | class BaconMaster : Slider 55 | { 56 | public BaconMaster() 57 | { 58 | Name = "Bacon Master"; 59 | Cheese = "American"; 60 | Toppings = "lots of bacon"; 61 | Price = 2.39m; 62 | } 63 | 64 | public override void Display(int orderTotal) 65 | { 66 | Console.WriteLine("Slider #" + orderTotal + ": " + Name + " - topped with " + Cheese + " cheese and " + Toppings + "! $" + Price.ToString()); 67 | } 68 | } 69 | 70 | /// 71 | /// A Concrete Flyweight class 72 | /// 73 | class VeggieSlider : Slider 74 | { 75 | public VeggieSlider() 76 | { 77 | Name = "Veggie Slider"; 78 | Cheese = "Swiss"; 79 | Toppings = "lettuce, onion, tomato, and pickles"; 80 | Price = 1.99m; 81 | } 82 | 83 | public override void Display(int orderTotal) 84 | { 85 | Console.WriteLine("Slider #" + orderTotal + ": " + Name + " - topped with " + Cheese + " cheese and " + Toppings + "! $" + Price.ToString()); 86 | } 87 | 88 | } 89 | 90 | /// 91 | /// A Concrete Flyweight class 92 | /// 93 | class BBQKing : Slider 94 | { 95 | public BBQKing() 96 | { 97 | Name = "BBQ King"; 98 | Cheese = "American"; 99 | Toppings = "Onion rings, lettuce, and BBQ sauce"; 100 | Price = 2.49m; 101 | } 102 | 103 | public override void Display(int orderTotal) 104 | { 105 | Console.WriteLine("Slider #" + orderTotal + ": " + Name + " - topped with " + Cheese + " cheese and " + Toppings + "! $" + Price.ToString()); 106 | } 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /Interpreter/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Interpreter/FoodLanguage.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 Interpreter 8 | { 9 | /// 10 | /// The 'Context' class 11 | /// 12 | class Context 13 | { 14 | private string _input; 15 | private int _output; 16 | 17 | // Constructor 18 | public Context(string input) 19 | { 20 | this._input = input; 21 | } 22 | 23 | // Gets or sets input 24 | public string Input 25 | { 26 | get { return _input; } 27 | set { _input = value; } 28 | } 29 | 30 | // Gets or sets output 31 | public int Output 32 | { 33 | get { return _output; } 34 | set { _output = value; } 35 | } 36 | } 37 | 38 | /// 39 | /// The 'AbstractExpression' class 40 | /// 41 | abstract class Expression 42 | { 43 | public void Interpret(Context context) 44 | { 45 | if (context.Input.Length == 0) 46 | return; 47 | 48 | if (context.Input.StartsWith(Nine())) 49 | { 50 | context.Output += (9 * Multiplier()); 51 | context.Input = context.Input.Substring(2); 52 | } 53 | else if (context.Input.StartsWith(Four())) 54 | { 55 | context.Output += (4 * Multiplier()); 56 | context.Input = context.Input.Substring(2); 57 | } 58 | else if (context.Input.StartsWith(Five())) 59 | { 60 | context.Output += (5 * Multiplier()); 61 | context.Input = context.Input.Substring(1); 62 | } 63 | 64 | while (context.Input.StartsWith(One())) 65 | { 66 | context.Output += (1 * Multiplier()); 67 | context.Input = context.Input.Substring(1); 68 | } 69 | } 70 | 71 | public abstract string One(); 72 | public abstract string Four(); 73 | public abstract string Five(); 74 | public abstract string Nine(); 75 | public abstract int Multiplier(); 76 | } 77 | 78 | /// 79 | /// A 'TerminalExpression' class 80 | /// 81 | /// Thousand checks for the Roman Numeral M 82 | /// 83 | /// 84 | class ThousandExpression : Expression 85 | { 86 | public override string One() { return "M"; } 87 | public override string Four() { return " "; } 88 | public override string Five() { return " "; } 89 | public override string Nine() { return " "; } 90 | public override int Multiplier() { return 1000; } 91 | } 92 | 93 | /// 94 | /// A 'TerminalExpression' class 95 | /// 96 | /// Hundred checks C, CD, D or CM 97 | /// 98 | /// 99 | class HundredExpression : Expression 100 | { 101 | public override string One() { return "C"; } 102 | public override string Four() { return "CD"; } 103 | public override string Five() { return "D"; } 104 | public override string Nine() { return "CM"; } 105 | public override int Multiplier() { return 100; } 106 | } 107 | 108 | /// 109 | /// A 'TerminalExpression' class 110 | /// 111 | /// Ten checks for X, XL, L and XC 112 | /// 113 | /// 114 | class TenExpression : Expression 115 | { 116 | public override string One() { return "X"; } 117 | public override string Four() { return "XL"; } 118 | public override string Five() { return "L"; } 119 | public override string Nine() { return "XC"; } 120 | public override int Multiplier() { return 10; } 121 | } 122 | 123 | /// 124 | /// A 'TerminalExpression' class 125 | /// 126 | /// One checks for I, II, III, IV, V, VI, VI, VII, VIII, IX 127 | /// 128 | /// 129 | class OneExpression : Expression 130 | { 131 | public override string One() { return "I"; } 132 | public override string Four() { return "IV"; } 133 | public override string Five() { return "V"; } 134 | public override string Nine() { return "IX"; } 135 | public override int Multiplier() { return 1; } 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /Interpreter/Interpreter.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {F17100BC-5EA9-4C5C-BCA6-47700D7A959D} 8 | Exe 9 | Properties 10 | Interpreter 11 | Interpreter 12 | v4.6 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 61 | -------------------------------------------------------------------------------- /Interpreter/Program.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 Interpreter 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | string roman = "MCMXXVIII"; 14 | Context context = new Context(roman); 15 | 16 | // Build the 'parse tree' 17 | List tree = new List(); 18 | tree.Add(new ThousandExpression()); 19 | tree.Add(new HundredExpression()); 20 | tree.Add(new TenExpression()); 21 | tree.Add(new OneExpression()); 22 | 23 | // Interpret 24 | foreach (Expression exp in tree) 25 | { 26 | exp.Interpret(context); 27 | } 28 | 29 | Console.WriteLine("{0} = {1}", 30 | roman, context.Output); 31 | 32 | // Wait for user 33 | Console.ReadKey(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Interpreter/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Interpreter")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Interpreter")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f17100bc-5ea9-4c5c-bca6-47700d7a959d")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Iterator/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Iterator/Iterator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {0B9618CC-8F2A-48BE-B3D7-352537CC53D9} 8 | Exe 9 | Properties 10 | Iterator 11 | Iterator 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Iterator/JellyBeans.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Iterator 9 | { 10 | /// 11 | /// Our collection item. Mostly because I'm a sucker for jelly beans. 12 | /// 13 | class JellyBean 14 | { 15 | private string _flavor; 16 | 17 | // Constructor 18 | public JellyBean(string flavor) 19 | { 20 | this._flavor = flavor; 21 | } 22 | 23 | 24 | public string Flavor 25 | { 26 | get { return _flavor; } 27 | } 28 | } 29 | 30 | /// 31 | /// The aggregate interface 32 | /// 33 | interface ICandyCollection 34 | { 35 | JellyBeanIterator CreateIterator(); 36 | } 37 | 38 | /// 39 | /// The concrete aggregate class 40 | /// 41 | class JellyBeanCollection : ICandyCollection 42 | { 43 | private ArrayList _items = new ArrayList(); 44 | 45 | public JellyBeanIterator CreateIterator() 46 | { 47 | return new JellyBeanIterator(this); 48 | } 49 | 50 | // Gets jelly bean count 51 | public int Count 52 | { 53 | get { return _items.Count; } 54 | } 55 | 56 | // Indexer 57 | public object this[int index] 58 | { 59 | get { return _items[index]; } 60 | set { _items.Add(value); } 61 | } 62 | } 63 | 64 | /// 65 | /// The 'Iterator' interface 66 | /// 67 | interface IJellyBeanIterator 68 | { 69 | JellyBean First(); 70 | JellyBean Next(); 71 | bool IsDone { get; } 72 | JellyBean CurrentBean { get; } 73 | } 74 | 75 | /// 76 | /// The 'ConcreteIterator' class 77 | /// 78 | class JellyBeanIterator : IJellyBeanIterator 79 | { 80 | private JellyBeanCollection _jellyBeans; 81 | private int _current = 0; 82 | private int _step = 1; 83 | 84 | // Constructor 85 | public JellyBeanIterator(JellyBeanCollection beans) 86 | { 87 | this._jellyBeans = beans; 88 | } 89 | 90 | // Gets first jelly bean 91 | public JellyBean First() 92 | { 93 | _current = 0; 94 | return _jellyBeans[_current] as JellyBean; 95 | } 96 | 97 | // Gets next jelly bean 98 | public JellyBean Next() 99 | { 100 | _current += _step; 101 | if (!IsDone) 102 | return _jellyBeans[_current] as JellyBean; 103 | else 104 | return null; 105 | } 106 | 107 | // Gets current iterator candy 108 | public JellyBean CurrentBean 109 | { 110 | get { return _jellyBeans[_current] as JellyBean; } 111 | } 112 | 113 | // Gets whether iteration is complete 114 | public bool IsDone 115 | { 116 | get { return _current >= _jellyBeans.Count; } 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /Iterator/Program.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 Iterator 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | // Build a collection of jelly beans 14 | JellyBeanCollection collection = new JellyBeanCollection(); 15 | collection[0] = new JellyBean("Cherry"); 16 | collection[1] = new JellyBean("Bubble Gum"); 17 | collection[2] = new JellyBean("Root Beer"); 18 | collection[3] = new JellyBean("French Vanilla"); 19 | collection[4] = new JellyBean("Licorice"); 20 | collection[5] = new JellyBean("Buttered Popcorn"); 21 | collection[6] = new JellyBean("Juicy Pear"); 22 | collection[7] = new JellyBean("Cinnamon"); 23 | collection[8] = new JellyBean("Coconut"); 24 | 25 | // Create iterator 26 | JellyBeanIterator iterator = collection.CreateIterator(); 27 | 28 | Console.WriteLine("Gimme all the jelly beans!"); 29 | 30 | for (JellyBean item = iterator.First(); 31 | !iterator.IsDone; item = iterator.Next()) 32 | { 33 | Console.WriteLine(item.Flavor); 34 | } 35 | 36 | Console.ReadKey(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /Iterator/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Iterator")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Iterator")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("0b9618cc-8f2a-48be-b3d7-352537cc53d9")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Mediator/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Mediator/Mediator.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {FE3E3012-8661-47B3-959A-9FC4EB7355CB} 8 | Exe 9 | Properties 10 | Mediator 11 | Mediator 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Mediator/MovieTheatreConcessions.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 Mediator 8 | { 9 | /// 10 | /// The Mediator interface, which defines a send message method which the concrete mediators must implement. 11 | /// 12 | interface Mediator 13 | { 14 | void SendMessage(string message, ConcessionStand concessionStand); 15 | } 16 | 17 | /// 18 | /// The Concrete Mediator class, which implement the send message method and keep track of all participants in the conversation. 19 | /// 20 | class ConcessionsMediator : Mediator 21 | { 22 | private NorthConcessionStand _northConcessions; 23 | private SouthConcessionStand _southConcessions; 24 | 25 | public NorthConcessionStand NorthConcessions 26 | { 27 | set { _northConcessions = value; } 28 | } 29 | 30 | public SouthConcessionStand SouthConcessions 31 | { 32 | set { _southConcessions = value; } 33 | } 34 | 35 | public void SendMessage(string message, ConcessionStand colleague) 36 | { 37 | if (colleague == _northConcessions) 38 | { 39 | _southConcessions.Notify(message); 40 | } 41 | else 42 | { 43 | _northConcessions.Notify(message); 44 | } 45 | } 46 | } 47 | 48 | /// 49 | /// The Colleague abstract class, representing an entity involved in the conversation which should receive messages. 50 | /// 51 | abstract class ConcessionStand 52 | { 53 | protected Mediator mediator; 54 | 55 | public ConcessionStand(Mediator mediator) 56 | { 57 | this.mediator = mediator; 58 | } 59 | } 60 | 61 | /// 62 | /// A Concrete Colleague class 63 | /// 64 | class NorthConcessionStand : ConcessionStand 65 | { 66 | // Constructor 67 | public NorthConcessionStand(Mediator mediator) : base(mediator) 68 | { 69 | } 70 | 71 | public void Send(string message) 72 | { 73 | Console.WriteLine("North Concession Stand sends message: " + message); 74 | mediator.SendMessage(message, this); 75 | } 76 | 77 | public void Notify(string message) 78 | { 79 | Console.WriteLine("North Concession Stand gets message: " + message); 80 | } 81 | } 82 | 83 | /// 84 | /// A Concrete Colleague class 85 | /// 86 | class SouthConcessionStand : ConcessionStand 87 | { 88 | public SouthConcessionStand(Mediator mediator) : base(mediator) 89 | { 90 | } 91 | 92 | public void Send(string message) 93 | { 94 | Console.WriteLine("South Concession Stand sends message: " + message); 95 | mediator.SendMessage(message, this); 96 | } 97 | 98 | public void Notify(string message) 99 | { 100 | Console.WriteLine("South Concession Stand gets message: " + message); 101 | } 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /Mediator/Program.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 Mediator 8 | { 9 | class Program 10 | { 11 | /// 12 | /// The Mediator pattern allows us to create an object which defines how other objects interact or 13 | /// communicate with each other. This pattern promotes loose coupling by keeping the interacting 14 | /// objects from referring to each other explicitly. 15 | /// 16 | /// 17 | static void Main(string[] args) 18 | { 19 | ConcessionsMediator mediator = new ConcessionsMediator(); 20 | 21 | NorthConcessionStand leftKitchen = new NorthConcessionStand(mediator); 22 | SouthConcessionStand rightKitchen = new SouthConcessionStand(mediator); 23 | 24 | mediator.NorthConcessions = leftKitchen; 25 | mediator.SouthConcessions = rightKitchen; 26 | 27 | leftKitchen.Send("Can you send some popcorn?"); 28 | rightKitchen.Send("Sure thing, Kenny's on his way."); 29 | 30 | rightKitchen.Send("Do you have any extra hot dogs? We've had a rush on them over here."); 31 | leftKitchen.Send("Just a couple, we'll send Kenny back with them."); 32 | 33 | Console.ReadKey(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Mediator/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Mediator")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Mediator")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("fe3e3012-8661-47b3-959a-9fc4eb7355cb")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Memento/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Memento/FoodSupplier.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 Memento 8 | { 9 | /// 10 | /// The Originator class, which is the class for which we want to save Mementos for its state. 11 | /// 12 | class FoodSupplier 13 | { 14 | private string _name; 15 | private string _phone; 16 | private string _address; 17 | 18 | public string Name 19 | { 20 | get { return _name; } 21 | set 22 | { 23 | _name = value; 24 | Console.WriteLine("Proprietor: " + _name); 25 | } 26 | } 27 | 28 | public string Phone 29 | { 30 | get { return _phone; } 31 | set 32 | { 33 | _phone = value; 34 | Console.WriteLine("Phone Number: " + _phone); 35 | } 36 | } 37 | 38 | public string Address 39 | { 40 | get { return _address; } 41 | set 42 | { 43 | _address = value; 44 | Console.WriteLine("Address: " + _address); 45 | } 46 | } 47 | 48 | public FoodSupplierMemento SaveMemento() 49 | { 50 | Console.WriteLine("\nSaving current state\n"); 51 | return new FoodSupplierMemento(_name, _phone, _address); 52 | } 53 | 54 | public void RestoreMemento(FoodSupplierMemento memento) 55 | { 56 | Console.WriteLine("\nRestoring previous state\n"); 57 | Name = memento.Name; 58 | Phone = memento.PhoneNumber; 59 | Address = memento.Address; 60 | } 61 | } 62 | 63 | /// 64 | /// The Memento class 65 | /// 66 | class FoodSupplierMemento 67 | { 68 | public string Name { get; set; } 69 | public string PhoneNumber { get; set; } 70 | public string Address { get; set; } 71 | 72 | public FoodSupplierMemento(string name, string phone, string address) 73 | { 74 | Name = name; 75 | PhoneNumber = phone; 76 | Address = address; 77 | } 78 | } 79 | 80 | /// 81 | /// The Caretaker class. This class never examines the contents of any Memento and is 82 | /// responsible for keeping that memento. 83 | /// 84 | class SupplierMemory 85 | { 86 | private FoodSupplierMemento _memento; 87 | 88 | public FoodSupplierMemento Memento 89 | { 90 | set { _memento = value; } 91 | get { return _memento; } 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /Memento/Memento.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {77DCDAD8-B41F-426A-9BFD-6759E550F980} 8 | Exe 9 | Properties 10 | Memento 11 | Memento 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Memento/Program.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 Memento 8 | { 9 | class Program 10 | { 11 | /// 12 | /// In the Memento pattern, we need to capture and externalize an object's state so that the object can be 13 | /// restored to that state at a later time. A good example of this is undo/redo operations. 14 | /// 15 | /// 16 | static void Main(string[] args) 17 | { 18 | //Here's a new supplier for our restaurant 19 | FoodSupplier s = new FoodSupplier(); 20 | s.Name = "Harold Karstark"; 21 | s.Phone = "(482) 449-1172"; 22 | 23 | // Let's store that entry in our database. 24 | SupplierMemory m = new SupplierMemory(); 25 | m.Memento = s.SaveMemento(); 26 | 27 | // Continue changing originator 28 | s.Address = "548 S Main St. Nowhere, KS"; 29 | 30 | // Crap, gotta undo that entry, I entered the wrong address 31 | s.RestoreMemento(m.Memento); 32 | 33 | Console.ReadKey(); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Memento/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Memento")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Memento")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("77dcdad8-b41f-426a-9bfd-6759e550f980")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Observer/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Observer/Observer.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {118BF8BC-5145-4BF6-81CD-C1B188515B9C} 8 | Exe 9 | Properties 10 | Observer 11 | Observer 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Observer/Program.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 Observer 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | // Create price watch for Carrots and attach restaurants that buy carrots from suppliers. 14 | Carrots carrots = new Carrots(0.82); 15 | carrots.Attach(new Restaurant("Mackay's", 0.77)); 16 | carrots.Attach(new Restaurant("Johnny's Sports Bar", 0.74)); 17 | carrots.Attach(new Restaurant("Salad Kingdom", 0.75)); 18 | 19 | // Fluctuating carrot prices will notify subscribing restaurants. 20 | carrots.PricePerPound = 0.79; 21 | carrots.PricePerPound = 0.76; 22 | carrots.PricePerPound = 0.74; 23 | carrots.PricePerPound = 0.81; 24 | 25 | Console.ReadKey(); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Observer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Observer")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Observer")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("118bf8bc-5145-4bf6-81cd-c1b188515b9c")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Observer/VeggiePrices.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 Observer 8 | { 9 | /// 10 | /// The Subject abstract class 11 | /// 12 | abstract class Veggies 13 | { 14 | private double _pricePerPound; 15 | private List _restaurants = new List(); 16 | 17 | public Veggies(double pricePerPound) 18 | { 19 | _pricePerPound = pricePerPound; 20 | } 21 | 22 | public void Attach(IRestaurant restaurant) 23 | { 24 | _restaurants.Add(restaurant); 25 | } 26 | 27 | public void Detach(IRestaurant restaurant) 28 | { 29 | _restaurants.Remove(restaurant); 30 | } 31 | 32 | public void Notify() 33 | { 34 | foreach (IRestaurant restaurant in _restaurants) 35 | { 36 | restaurant.Update(this); 37 | } 38 | 39 | Console.WriteLine(""); 40 | } 41 | 42 | public double PricePerPound 43 | { 44 | get { return _pricePerPound; } 45 | set 46 | { 47 | if (_pricePerPound != value) 48 | { 49 | _pricePerPound = value; 50 | Notify(); 51 | } 52 | } 53 | } 54 | } 55 | 56 | /// 57 | /// The ConcreteSubject class 58 | /// 59 | class Carrots : Veggies 60 | { 61 | public Carrots(double price) : base(price) 62 | { 63 | } 64 | } 65 | 66 | /// 67 | /// The Observer interface 68 | /// 69 | interface IRestaurant 70 | { 71 | void Update(Veggies veggies); 72 | } 73 | 74 | /// 75 | /// The ConcreteObserver class 76 | /// 77 | class Restaurant : IRestaurant 78 | { 79 | private string _name; 80 | private double _purchaseThreshold; 81 | 82 | public Restaurant(string name, double purchaseThreshold) 83 | { 84 | _name = name; 85 | _purchaseThreshold = purchaseThreshold; 86 | } 87 | 88 | public void Update(Veggies veggie) 89 | { 90 | Console.WriteLine("Notified {0} of {1}'s " + " price change to {2:C} per pound.", _name, veggie.GetType().Name, veggie.PricePerPound); 91 | if(veggie.PricePerPound < _purchaseThreshold) 92 | { 93 | Console.WriteLine(_name + " wants to buy some " + veggie.GetType().Name + "!"); 94 | } 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /Prototype/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Prototype/Program.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 Prototype 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | SandwichMenu sandwichMenu = new SandwichMenu(); 14 | 15 | // Initialize with default sandwiches 16 | sandwichMenu["BLT"] = new Sandwich("Wheat", "Bacon", "", "Lettuce, Tomato"); 17 | sandwichMenu["PB&J"] = new Sandwich("White", "", "", "Peanut Butter, Jelly"); 18 | sandwichMenu["Turkey"] = new Sandwich("Rye", "Turkey", "Swiss", "Lettuce, Onion, Tomato"); 19 | 20 | // Catering manager adds custom sandwiches 21 | sandwichMenu["LoadedBLT"] = new Sandwich("Wheat", "Turkey, Bacon", "American", "Lettuce, Tomato, Onion, Olives"); 22 | sandwichMenu["ThreeMeatCombo"] = new Sandwich("Rye", "Turkey, Ham, Salami", "Provolone", "Lettuce, Onion"); 23 | sandwichMenu["Vegetarian"] = new Sandwich("Wheat", "", "", "Lettuce, Onion, Tomato, Olives, Spinach"); 24 | 25 | // Now we can clone the sandwiches. 26 | Sandwich sandwich1 = sandwichMenu["BLT"].Clone() as Sandwich; 27 | Sandwich sandwich2 = sandwichMenu["ThreeMeatCombo"].Clone() as Sandwich; 28 | Sandwich sandwich3 = sandwichMenu["Vegetarian"].Clone() as Sandwich; 29 | 30 | Console.ReadKey(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Prototype/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Prototype")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Prototype")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("bfd7d1e0-10d2-4158-87a3-0e5a58b98278")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Prototype/Prototype.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {BFD7D1E0-10D2-4158-87A3-0E5A58B98278} 8 | Exe 9 | Properties 10 | Prototype 11 | Prototype 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Prototype/Sandwiches.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 Prototype 8 | { 9 | /// 10 | /// The Prototype abstract class 11 | /// 12 | abstract class SandwichPrototype 13 | { 14 | public abstract SandwichPrototype Clone(); 15 | } 16 | 17 | class Sandwich : SandwichPrototype 18 | { 19 | private string Bread; 20 | private string Meat; 21 | private string Cheese; //I will use this pun everywhere I can 22 | private string Veggies; 23 | 24 | public Sandwich(string bread, string meat, string cheese, string veggies) 25 | { 26 | Bread = bread; 27 | Meat = meat; 28 | Cheese = cheese; 29 | Veggies = veggies; 30 | } 31 | 32 | public override SandwichPrototype Clone() 33 | { 34 | string ingredientList = GetIngredientList(); 35 | Console.WriteLine("Cloning sandwich with ingredients: {0}", ingredientList.Remove(ingredientList.LastIndexOf(","))); 36 | 37 | return MemberwiseClone() as SandwichPrototype; 38 | } 39 | 40 | private string GetIngredientList() 41 | { 42 | var ingredientList = ""; 43 | if (!string.IsNullOrWhiteSpace(Bread)) 44 | { 45 | ingredientList += Bread + ", "; 46 | } 47 | if (!string.IsNullOrWhiteSpace(Meat)) 48 | { 49 | ingredientList += Meat + ", "; 50 | } 51 | if (!string.IsNullOrWhiteSpace(Cheese)) 52 | { 53 | ingredientList += Cheese + ", "; 54 | } 55 | if (!string.IsNullOrWhiteSpace(Veggies)) 56 | { 57 | ingredientList += Veggies + ", "; 58 | } 59 | return ingredientList; 60 | } 61 | } 62 | 63 | class SandwichMenu 64 | { 65 | private Dictionary _sandwiches = new Dictionary(); 66 | 67 | public SandwichPrototype this[string name] 68 | { 69 | get { return _sandwiches[name]; } 70 | set { _sandwiches.Add(name, value); } 71 | } 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /Proxy/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Proxy/Program.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 Proxy 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | NewServerProxy proxy = new NewServerProxy(); 14 | 15 | //Manage orders from a table 16 | Console.WriteLine("What would you like to order? "); 17 | string order = Console.ReadLine(); 18 | proxy.TakeOrder(order); 19 | Console.WriteLine("Sure thing! Here's your " + proxy.DeliverOrder() + "."); 20 | Console.WriteLine("How would you like to pay?"); 21 | string payment = Console.ReadLine(); 22 | proxy.ProcessPayment(payment); 23 | 24 | Console.ReadKey(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Proxy/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Proxy")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Proxy")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("82184690-fce4-48d9-bd74-73f2b43c0e97")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Proxy/Proxy.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {82184690-FCE4-48D9-BD74-73F2B43C0E97} 8 | Exe 9 | Properties 10 | Proxy 11 | Proxy 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Proxy/Server.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 Proxy 8 | { 9 | /// 10 | /// The Subject interface which both the real subject and proxy will need to implement 11 | /// 12 | public interface IServer 13 | { 14 | void TakeOrder(string order); 15 | string DeliverOrder(); 16 | void ProcessPayment(string payment); 17 | } 18 | 19 | /// 20 | /// The real subject class which the Proxy can stand in for 21 | /// 22 | class Server : IServer 23 | { 24 | private string Order; 25 | public void TakeOrder(string order) 26 | { 27 | Console.WriteLine("Server takes order for " + order + "."); 28 | Order = order; 29 | } 30 | 31 | public string DeliverOrder() 32 | { 33 | return Order; 34 | } 35 | 36 | public void ProcessPayment(string payment) 37 | { 38 | Console.WriteLine("Payment for order (" + payment + ") processed."); 39 | } 40 | } 41 | 42 | /// 43 | /// The Proxy class, which can substitute for the Real Subject. 44 | /// 45 | class NewServerProxy : IServer 46 | { 47 | private string Order; 48 | private Server _server = new Server(); 49 | 50 | public void TakeOrder(string order) 51 | { 52 | Console.WriteLine("New trainee server takes order for " + order + "."); 53 | Order = order; 54 | } 55 | 56 | public string DeliverOrder() 57 | { 58 | return Order; 59 | } 60 | 61 | public void ProcessPayment(string payment) 62 | { 63 | Console.WriteLine("New trainee cannot process payments yet!"); 64 | _server.ProcessPayment(payment); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DesignPatterns 2 | This solution contains all my example projects for my blog's [Daily Design Pattern series](https://www.exceptionnotfound.net/introducing-the-daily-design-pattern/), and is also used in my [Demonstrating Common Design Patterns with C# and Food](https://exceptionnotfound.net/speaking-engagements/) presentation. 3 | 4 | You can also get these examples, along with updated text and examples about them, as an eBook by [signing up to be a paid subscriber](https://exceptionnotfound.net/signup/) on my blog! 5 | 6 | This series covers 22 of the Gang of Four's patterns. Here's the individual posts: 7 | 8 | Creational Patterns: 9 | [Abstract Factory](https://www.exceptionnotfound.net/the-daily-design-pattern-abstract-factory/) 10 | [Builder](https://www.exceptionnotfound.net/builder-the-daily-design-pattern/) 11 | [Factory Method](https://www.exceptionnotfound.net/the-daily-design-pattern-factory-method/) 12 | [Prototype](https://www.exceptionnotfound.net/prototype-the-daily-design-pattern/) 13 | [Singleton](https://www.exceptionnotfound.net/singleton-the-daily-design-pattern/) 14 | 15 | 16 | Structural Patterns: 17 | [Adapter](https://www.exceptionnotfound.net/the-daily-design-pattern-adapter/) 18 | [Bridge](https://www.exceptionnotfound.net/the-daily-design-pattern-bridge/) 19 | [Composite](https://www.exceptionnotfound.net/composite-the-daily-design-pattern/) 20 | [Decorator](https://www.exceptionnotfound.net/decorator-the-daily-design-pattern/) 21 | [Facade](https://www.exceptionnotfound.net/the-daily-design-pattern-facade/) 22 | [Flyweight](https://www.exceptionnotfound.net/flyweight-the-daily-design-pattern/) 23 | [Proxy](https://www.exceptionnotfound.net/proxy-the-daily-design-pattern/) 24 | 25 | 26 | Behavioral Patterns: 27 | [Chain of Responsibility](https://www.exceptionnotfound.net/chain-of-responsibility-the-daily-design-pattern/) 28 | [Command](https://www.exceptionnotfound.net/command-the-daily-design-pattern/) 29 | [Iterator](https://www.exceptionnotfound.net/the-daily-design-pattern-iterator/) 30 | [Mediator](https://www.exceptionnotfound.net/mediator-the-daily-design-pattern/) 31 | [Memento](https://www.exceptionnotfound.net/the-daily-design-pattern-memento/) 32 | [Observer](https://www.exceptionnotfound.net/the-daily-design-pattern-observer/) 33 | [State](https://www.exceptionnotfound.net/state-the-daily-design-pattern/) 34 | [Strategy](https://www.exceptionnotfound.net/strategy-the-daily-design-pattern/) 35 | [Template Method](https://www.exceptionnotfound.net/the-daily-design-pattern-template-method/) 36 | [Visitor](https://www.exceptionnotfound.net/visitor-the-daily-design-pattern/) 37 | 38 | If these posts and examples helped you, please consider [buying me a coffee](https://www.buymeacoffee.com/exceptionnotfnd). Your support funds all of Exception Not Found's projects and helps keep traditional ads off the site. Thanks! 39 | -------------------------------------------------------------------------------- /Singleton/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Singleton/Program.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 Singleton 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | var bell = TheBell.Instance; 14 | bell.Ring(); 15 | 16 | Console.ReadKey(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Singleton/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Singleton")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Singleton")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("93a3ebc2-54b8-431a-9864-693a2d6d0223")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Singleton/Singleton.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {93A3EBC2-54B8-431A-9864-693A2D6D0223} 8 | Exe 9 | Properties 10 | Singleton 11 | Singleton 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Singleton/TheBell.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 Singleton 8 | { 9 | /// 10 | /// Singleton 11 | /// 12 | public sealed class TheBell 13 | { 14 | private static TheBell bellConnection; 15 | private static object syncRoot = new Object(); 16 | private TheBell() 17 | { 18 | 19 | } 20 | 21 | /// 22 | /// We implement this method to ensure thread safety for our singleton. 23 | /// 24 | public static TheBell Instance 25 | { 26 | get 27 | { 28 | lock(syncRoot) 29 | { 30 | if(bellConnection == null) 31 | { 32 | bellConnection = new TheBell(); 33 | } 34 | } 35 | 36 | return bellConnection; 37 | } 38 | } 39 | 40 | public void Ring() 41 | { 42 | Console.WriteLine("Ding! Order up!"); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /State/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /State/Program.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 State 8 | { 9 | class Program 10 | { 11 | /// 12 | /// The State pattern encapsulates states of an object as objects themselves, and uses a Context class 13 | /// (the Steak class in this example) to store the current state of the object and the object itself. 14 | /// 15 | /// 16 | static void Main(string[] args) 17 | { 18 | //Let's cook a steak! 19 | Steak steak = new Steak("T-Bone"); 20 | 21 | // Apply temperature changes 22 | steak.AddTemp(120); 23 | steak.AddTemp(15); 24 | steak.AddTemp(15); 25 | steak.RemoveTemp(10); //Yes I know cooking doesn't work this way, bear with me. 26 | steak.RemoveTemp(15); 27 | steak.AddTemp(20); 28 | steak.AddTemp(20); 29 | steak.AddTemp(20); 30 | 31 | Console.ReadKey(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /State/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("State")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("State")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("4dfb974c-29c5-471a-a6e1-2daf17203a8f")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /State/State.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {4DFB974C-29C5-471A-A6E1-2DAF17203A8F} 8 | Exe 9 | Properties 10 | State 11 | State 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Strategy/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Strategy/CookMethod.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 Strategy 8 | { 9 | /// 10 | /// The Strategy abstract class, which defines an interface common to all supported strategy algorithms. 11 | /// 12 | abstract class CookStrategy 13 | { 14 | public abstract void Cook(string food); 15 | } 16 | 17 | /// 18 | /// A Concrete Strategy class 19 | /// 20 | class Grilling : CookStrategy 21 | { 22 | public override void Cook(string food) 23 | { 24 | Console.WriteLine("\nCooking " + food + " by grilling it."); 25 | } 26 | } 27 | 28 | /// 29 | /// A Concrete Strategy class 30 | /// 31 | class OvenBaking : CookStrategy 32 | { 33 | public override void Cook(string food) 34 | { 35 | Console.WriteLine("\nCooking " + food + " by oven baking it."); 36 | } 37 | } 38 | 39 | /// 40 | /// A Concrete Strategy class 41 | /// 42 | class DeepFrying : CookStrategy 43 | { 44 | public override void Cook(string food) 45 | { 46 | Console.WriteLine("\nCooking " + food + " by deep frying it"); 47 | } 48 | } 49 | 50 | /// 51 | /// The Context class, which maintains a reference to the chosen Strategy. 52 | /// 53 | class CookingMethod 54 | { 55 | private string Food; 56 | private CookStrategy _cookStrategy; 57 | 58 | public void SetCookStrategy(CookStrategy cookStrategy) 59 | { 60 | this._cookStrategy = cookStrategy; 61 | } 62 | 63 | public void SetFood(string name) 64 | { 65 | Food = name; 66 | } 67 | 68 | public void Cook() 69 | { 70 | _cookStrategy.Cook(Food); 71 | Console.WriteLine(); 72 | } 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /Strategy/Program.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 Strategy 8 | { 9 | class Program 10 | { 11 | /// 12 | /// The Strategy pattern is a method by which we can move repeated if/then logic to polymorphism. 13 | /// In this example, we want to cook some food using a variety of different methods. 14 | /// In a naive example, we'd need an IF statement for each cooking method. 15 | /// If we have a lot of cooking methods, we'll also have a lot of IF statements. 16 | /// But in this example, we can define a Strategy for each cook method and simply set which one we want at runtime. 17 | /// 18 | /// 19 | static void Main(string[] args) 20 | { 21 | CookingMethod cookMethod = new CookingMethod(); 22 | 23 | Console.WriteLine("What food would you like to cook?"); 24 | var food = Console.ReadLine(); 25 | cookMethod.SetFood(food); 26 | 27 | Console.WriteLine("What cooking strategy would you like to use (1-3)?"); 28 | int input = int.Parse(Console.ReadKey().KeyChar.ToString()); 29 | 30 | 31 | switch(input) 32 | { 33 | case 1: 34 | cookMethod.SetCookStrategy(new Grilling()); 35 | cookMethod.Cook(); 36 | break; 37 | 38 | case 2: 39 | cookMethod.SetCookStrategy(new OvenBaking()); 40 | cookMethod.Cook(); 41 | break; 42 | 43 | case 3: 44 | cookMethod.SetCookStrategy(new DeepFrying()); 45 | cookMethod.Cook(); 46 | break; 47 | 48 | default: 49 | Console.WriteLine("Invalid Selection!"); 50 | break; 51 | } 52 | Console.ReadKey(); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Strategy/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Strategy")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Strategy")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e7a921c3-e17f-4469-ac2f-0bb33e804641")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Strategy/Strategy.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {E7A921C3-E17F-4469-AC2F-0BB33E804641} 8 | Exe 9 | Properties 10 | Strategy 11 | Strategy 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /TemplateMethod/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /TemplateMethod/FreshBread.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 TemplateMethod 8 | { 9 | /// 10 | /// The AbstractClass which contains the template method. 11 | /// 12 | abstract class Bread 13 | { 14 | public abstract void MixIngredients(); 15 | 16 | public abstract void Bake(); 17 | 18 | public virtual void Slice() 19 | { 20 | Console.WriteLine("Slicing the " + GetType().Name + " bread!"); 21 | } 22 | 23 | // The template method 24 | public void Make() 25 | { 26 | MixIngredients(); 27 | Bake(); 28 | Slice(); 29 | } 30 | } 31 | 32 | class TwelveGrain : Bread 33 | { 34 | public override void MixIngredients() 35 | { 36 | Console.WriteLine("Gathering Ingredients for 12-Grain Bread."); 37 | } 38 | 39 | public override void Bake() 40 | { 41 | Console.WriteLine("Baking the 12-Grain Bread. (25 minutes)"); 42 | } 43 | } 44 | 45 | class Sourdough : Bread 46 | { 47 | public override void MixIngredients() 48 | { 49 | Console.WriteLine("Gathering Ingredients for Sourdough Bread."); 50 | } 51 | 52 | public override void Bake() 53 | { 54 | Console.WriteLine("Baking the Sourdough Bread. (20 minutes)"); 55 | } 56 | } 57 | 58 | class WholeWheat : Bread 59 | { 60 | public override void MixIngredients() 61 | { 62 | Console.WriteLine("Gathering Ingredients for Whole Wheat Bread."); 63 | } 64 | 65 | public override void Bake() 66 | { 67 | Console.WriteLine("Baking the Whole Wheat Bread. (15 minutes)"); 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /TemplateMethod/Program.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 TemplateMethod 8 | { 9 | class Program 10 | { 11 | /// 12 | /// The Template Method pattern allows an abstract to define the skeleton or outline of an algorithm, 13 | /// but leave the implementation of the individual steps in that algorithm up to the deriving classes. 14 | /// 15 | /// 16 | static void Main(string[] args) 17 | { 18 | Sourdough sourdough = new Sourdough(); 19 | sourdough.Make(); 20 | 21 | TwelveGrain twelveGrain = new TwelveGrain(); 22 | twelveGrain.Make(); 23 | 24 | WholeWheat wholeWheat = new WholeWheat(); 25 | wholeWheat.Make(); 26 | 27 | Console.ReadKey(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /TemplateMethod/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("TemplateMethod")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("TemplateMethod")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("e7aa4325-5787-44b9-9ced-2f5275d5543c")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /TemplateMethod/TemplateMethod.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {E7AA4325-5787-44B9-9CED-2F5275D5543C} 8 | Exe 9 | Properties 10 | TemplateMethod 11 | TemplateMethod 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | -------------------------------------------------------------------------------- /Visitor/App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Visitor/Patron.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 Visitor 8 | { 9 | /// 10 | /// The Visitor interface, which declares a Visit operation for each Concrete Visitor to implement. 11 | /// 12 | interface IVisitor 13 | { 14 | void Visit(Element element); 15 | } 16 | 17 | /// 18 | /// A Concrete Visitor class. 19 | /// 20 | class IncomeVisitor : IVisitor 21 | { 22 | public void Visit(Element element) 23 | { 24 | Employee employee = element as Employee; 25 | 26 | // We've had a great year, so 10% pay raises for everyone! 27 | employee.AnnualSalary *= 1.10; 28 | Console.WriteLine("{0} {1}'s new income: {2:C}", employee.GetType().Name, employee.Name, employee.AnnualSalary); 29 | } 30 | } 31 | 32 | /// 33 | /// A Concrete Visitor class 34 | /// 35 | class PaidTimeOffVisitor : IVisitor 36 | { 37 | public void Visit(Element element) 38 | { 39 | Employee employee = element as Employee; 40 | 41 | // And because you all helped have such a great year, all my employees get three extra paid time off days each! 42 | employee.PaidTimeOffDays += 3; 43 | Console.WriteLine("{0} {1}'s new vacation days: {2}", employee.GetType().Name, employee.Name, employee.PaidTimeOffDays); 44 | } 45 | } 46 | 47 | /// 48 | /// The Element abstract class. All this does is define an Accept operation, which needs to be implemented by any class that can be visited. 49 | /// 50 | abstract class Element 51 | { 52 | public abstract void Accept(IVisitor visitor); 53 | } 54 | 55 | /// 56 | /// The Concrete Element class, which implements all operations defined by the Element. 57 | /// 58 | class Employee : Element 59 | { 60 | public string Name { get; set; } 61 | public double AnnualSalary { get; set; } 62 | public int PaidTimeOffDays { get; set; } 63 | 64 | public Employee(string name, double annualSalary, int paidTimeOffDays) 65 | { 66 | Name = name; 67 | AnnualSalary = annualSalary; 68 | PaidTimeOffDays = paidTimeOffDays; 69 | } 70 | 71 | public override void Accept(IVisitor visitor) 72 | { 73 | visitor.Visit(this); 74 | } 75 | } 76 | 77 | /// 78 | /// The Object Structure class, which is a collection of Concrete Elements. This could be implemented using another pattern such as Composite. 79 | /// 80 | class Employees 81 | { 82 | private List _employees = new List(); 83 | 84 | public void Attach(Employee employee) 85 | { 86 | _employees.Add(employee); 87 | } 88 | 89 | public void Detach(Employee employee) 90 | { 91 | _employees.Remove(employee); 92 | } 93 | 94 | public void Accept(IVisitor visitor) 95 | { 96 | foreach (Employee e in _employees) 97 | { 98 | e.Accept(visitor); 99 | } 100 | Console.WriteLine(); 101 | } 102 | } 103 | 104 | 105 | class LineCook : Employee 106 | { 107 | public LineCook() : base("Dmitri", 32000, 7) 108 | { 109 | } 110 | } 111 | 112 | class HeadChef : Employee 113 | { 114 | public HeadChef() : base("Jackson", 69015, 21) 115 | { 116 | } 117 | } 118 | 119 | class GeneralManager : Employee 120 | { 121 | public GeneralManager() : base("Amanda", 78000, 24) 122 | { 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /Visitor/Program.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 Visitor 8 | { 9 | class Program 10 | { 11 | /// 12 | /// The Visitor pattern represents an operation to be performed on the elements of an object structure. 13 | /// Whereas Strategy exposes items to a standardized interface, Visitor details a mechanism by which 14 | /// objects can accept a reference to another object (the visitor) which exposes an interface that the target 15 | /// can call upon itself. 16 | /// 17 | /// 18 | static void Main(string[] args) 19 | { 20 | // Who are my employees? 21 | Employees e = new Employees(); 22 | e.Attach(new LineCook()); 23 | e.Attach(new HeadChef()); 24 | e.Attach(new GeneralManager()); 25 | 26 | // Employees are visited, giving them 10% raises and 3 extra paid time off days. 27 | e.Accept(new IncomeVisitor()); 28 | e.Accept(new PaidTimeOffVisitor()); 29 | 30 | Console.ReadKey(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Visitor/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Visitor")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Visitor")] 13 | [assembly: AssemblyCopyright("Copyright © 2016")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("4667507a-40a0-4697-b092-9ef70b3b2c73")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Visitor/Visitor.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {4667507A-40A0-4697-B092-9EF70B3B2C73} 8 | Exe 9 | Properties 10 | Visitor 11 | Visitor 12 | v4.7.2 13 | 512 14 | true 15 | 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 62 | --------------------------------------------------------------------------------