├── .gitattributes ├── .gitignore ├── DesignPattern ├── AbstractFactory │ ├── AbstractFactoryDesignPattern.csproj │ └── Class1.cs ├── AdapterDesignPattern │ ├── AdapterDesignPattern.csproj │ └── Class1.cs ├── AggregateRootDesignPattern │ ├── AggregateRootDesignPattern.csproj │ └── Class1.cs ├── BridgeDesignPattern │ ├── BridgeDesignPattern.csproj │ └── Class1.cs ├── BuilderDesignPattern │ ├── BuilderDesignPattern.csproj │ ├── Class1 - Copy.cs │ └── Class1.cs ├── ChainOfResponsibilityDesignPattern │ ├── ChainOfResponsibilityDesignPattern.csproj │ ├── Class1.cs │ ├── IHandler.cs │ ├── Method1.cs │ ├── Method2.cs │ └── Method3.cs ├── CleanCode │ ├── Class1.cs │ └── CleanCode.csproj ├── CompositeDesignPattern │ ├── Class1.cs │ └── CompositeDesignPattern.csproj ├── ConsoleApp │ ├── ConsoleApp.csproj │ └── Program.cs ├── DecoratorDesignPattern │ ├── Class1.cs │ └── DecoratorDesignPattern.csproj ├── DesignPattern.sln ├── FacadeDesignPattern │ ├── Class1.cs │ └── FacadeDesignPattern.csproj ├── FactoryDesignPattern │ ├── Class1.cs │ └── FactoryDesignPattern.csproj ├── IOC_DI_Unity │ ├── Class1.cs │ └── IOC_DI_Unity.csproj ├── IteratorDesignPattern │ ├── Class1.cs │ └── IteratorDesignPattern.csproj ├── LazyLoading │ ├── Class1.cs │ └── LazyLoading.csproj ├── MementoDesignPattern │ ├── Class1.cs │ └── MementoDesignPattern.csproj ├── ObserverDesignPattern │ ├── Class1.cs │ └── ObserverDesignPattern.csproj ├── PrototypeDesignPattern │ ├── Class1.cs │ └── PrototypeDesignPattern.csproj ├── ProxyDesignPattern │ ├── Class1.cs │ └── ProxyDesignPattern.csproj ├── ReplaceIfPolymorphismDesignPattern │ ├── Class1.cs │ └── ReplaceIfPolymorphismDesignPattern.csproj ├── RepositoryDesignPattern │ ├── Entity.cs │ ├── GenericRepository.cs │ ├── Migrations │ │ ├── 20200519155833_init.Designer.cs │ │ ├── 20200519155833_init.cs │ │ └── EmployeeDBContextModelSnapshot.cs │ ├── NonGenericRepository.cs │ └── RepositoryDesignPattern.csproj ├── SingletonDesignPattern │ ├── Class1.cs │ └── SingletonDesignPattern.csproj ├── StateDesignPattern │ ├── Class1.cs │ └── StateDesignPattern.csproj ├── StrategyDesignPattern │ ├── Class1.cs │ └── StrategyDesignPattern.csproj ├── TemplateDesignPattern │ ├── Class1.cs │ └── TemplateDesignPattern.csproj └── UOWDesignPattern │ ├── UOW.cs │ ├── UOWDesignPattern.csproj │ └── UOW_Generic.cs ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # JustCode is a .NET coding add-in 131 | .JustCode 132 | 133 | # TeamCity is a build add-in 134 | _TeamCity* 135 | 136 | # DotCover is a Code Coverage Tool 137 | *.dotCover 138 | 139 | # AxoCover is a Code Coverage Tool 140 | .axoCover/* 141 | !.axoCover/settings.json 142 | 143 | # Visual Studio code coverage results 144 | *.coverage 145 | *.coveragexml 146 | 147 | # NCrunch 148 | _NCrunch_* 149 | .*crunch*.local.xml 150 | nCrunchTemp_* 151 | 152 | # MightyMoose 153 | *.mm.* 154 | AutoTest.Net/ 155 | 156 | # Web workbench (sass) 157 | .sass-cache/ 158 | 159 | # Installshield output folder 160 | [Ee]xpress/ 161 | 162 | # DocProject is a documentation generator add-in 163 | DocProject/buildhelp/ 164 | DocProject/Help/*.HxT 165 | DocProject/Help/*.HxC 166 | DocProject/Help/*.hhc 167 | DocProject/Help/*.hhk 168 | DocProject/Help/*.hhp 169 | DocProject/Help/Html2 170 | DocProject/Help/html 171 | 172 | # Click-Once directory 173 | publish/ 174 | 175 | # Publish Web Output 176 | *.[Pp]ublish.xml 177 | *.azurePubxml 178 | # Note: Comment the next line if you want to checkin your web deploy settings, 179 | # but database connection strings (with potential passwords) will be unencrypted 180 | *.pubxml 181 | *.publishproj 182 | 183 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 184 | # checkin your Azure Web App publish settings, but sensitive information contained 185 | # in these scripts will be unencrypted 186 | PublishScripts/ 187 | 188 | # NuGet Packages 189 | *.nupkg 190 | # NuGet Symbol Packages 191 | *.snupkg 192 | # The packages folder can be ignored because of Package Restore 193 | **/[Pp]ackages/* 194 | # except build/, which is used as an MSBuild target. 195 | !**/[Pp]ackages/build/ 196 | # Uncomment if necessary however generally it will be regenerated when needed 197 | #!**/[Pp]ackages/repositories.config 198 | # NuGet v3's project.json files produces more ignorable files 199 | *.nuget.props 200 | *.nuget.targets 201 | 202 | # Microsoft Azure Build Output 203 | csx/ 204 | *.build.csdef 205 | 206 | # Microsoft Azure Emulator 207 | ecf/ 208 | rcf/ 209 | 210 | # Windows Store app package directories and files 211 | AppPackages/ 212 | BundleArtifacts/ 213 | Package.StoreAssociation.xml 214 | _pkginfo.txt 215 | *.appx 216 | *.appxbundle 217 | *.appxupload 218 | 219 | # Visual Studio cache files 220 | # files ending in .cache can be ignored 221 | *.[Cc]ache 222 | # but keep track of directories ending in .cache 223 | !?*.[Cc]ache/ 224 | 225 | # Others 226 | ClientBin/ 227 | ~$* 228 | *~ 229 | *.dbmdl 230 | *.dbproj.schemaview 231 | *.jfm 232 | *.pfx 233 | *.publishsettings 234 | orleans.codegen.cs 235 | 236 | # Including strong name files can present a security risk 237 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 238 | #*.snk 239 | 240 | # Since there are multiple workflows, uncomment next line to ignore bower_components 241 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 242 | #bower_components/ 243 | 244 | # RIA/Silverlight projects 245 | Generated_Code/ 246 | 247 | # Backup & report files from converting an old project file 248 | # to a newer Visual Studio version. Backup files are not needed, 249 | # because we have git ;-) 250 | _UpgradeReport_Files/ 251 | Backup*/ 252 | UpgradeLog*.XML 253 | UpgradeLog*.htm 254 | ServiceFabricBackup/ 255 | *.rptproj.bak 256 | 257 | # SQL Server files 258 | *.mdf 259 | *.ldf 260 | *.ndf 261 | 262 | # Business Intelligence projects 263 | *.rdl.data 264 | *.bim.layout 265 | *.bim_*.settings 266 | *.rptproj.rsuser 267 | *- [Bb]ackup.rdl 268 | *- [Bb]ackup ([0-9]).rdl 269 | *- [Bb]ackup ([0-9][0-9]).rdl 270 | 271 | # Microsoft Fakes 272 | FakesAssemblies/ 273 | 274 | # GhostDoc plugin setting file 275 | *.GhostDoc.xml 276 | 277 | # Node.js Tools for Visual Studio 278 | .ntvs_analysis.dat 279 | node_modules/ 280 | 281 | # Visual Studio 6 build log 282 | *.plg 283 | 284 | # Visual Studio 6 workspace options file 285 | *.opt 286 | 287 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 288 | *.vbw 289 | 290 | # Visual Studio LightSwitch build output 291 | **/*.HTMLClient/GeneratedArtifacts 292 | **/*.DesktopClient/GeneratedArtifacts 293 | **/*.DesktopClient/ModelManifest.xml 294 | **/*.Server/GeneratedArtifacts 295 | **/*.Server/ModelManifest.xml 296 | _Pvt_Extensions 297 | 298 | # Paket dependency manager 299 | .paket/paket.exe 300 | paket-files/ 301 | 302 | # FAKE - F# Make 303 | .fake/ 304 | 305 | # CodeRush personal settings 306 | .cr/personal 307 | 308 | # Python Tools for Visual Studio (PTVS) 309 | __pycache__/ 310 | *.pyc 311 | 312 | # Cake - Uncomment if you are using it 313 | # tools/** 314 | # !tools/packages.config 315 | 316 | # Tabs Studio 317 | *.tss 318 | 319 | # Telerik's JustMock configuration file 320 | *.jmconfig 321 | 322 | # BizTalk build output 323 | *.btp.cs 324 | *.btm.cs 325 | *.odx.cs 326 | *.xsd.cs 327 | 328 | # OpenCover UI analysis results 329 | OpenCover/ 330 | 331 | # Azure Stream Analytics local run output 332 | ASALocalRun/ 333 | 334 | # MSBuild Binary and Structured Log 335 | *.binlog 336 | 337 | # NVidia Nsight GPU debugger configuration file 338 | *.nvuser 339 | 340 | # MFractors (Xamarin productivity tool) working folder 341 | .mfractor/ 342 | 343 | # Local History for Visual Studio 344 | .localhistory/ 345 | 346 | # BeatPulse healthcheck temp database 347 | healthchecksdb 348 | 349 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 350 | MigrationBackup/ 351 | 352 | # Ionide (cross platform F# VS Code tools) working folder 353 | .ionide/ 354 | -------------------------------------------------------------------------------- /DesignPattern/AbstractFactory/AbstractFactoryDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | AbstractFactoryDesignPattern 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /DesignPattern/AbstractFactory/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AbstractFactoryDesignPattern 4 | { 5 | // Creating Abstract product 6 | public interface Investment 7 | { 8 | string FD(double amount); 9 | string MF(double amount); 10 | } 11 | 12 | // Creating Concrete Product 13 | public class SBI : Investment 14 | { 15 | public string FD(double amount) 16 | { 17 | return "Fix Deposit of INR " + amount + " is done in SBI"; 18 | } 19 | 20 | public string MF(double amount) 21 | { 22 | return "Mutual fund of INR " + amount + " is done in SBI"; 23 | } 24 | } 25 | 26 | // Creating Concrete Product 27 | public class ICICI : Investment 28 | { 29 | public string FD(double amount) 30 | { 31 | return "Fix Deposit of INR " + amount + " is done in ICICI"; 32 | } 33 | 34 | public string MF(double amount) 35 | { 36 | return "Mutual fund of INR " + amount + " is done in ICICI"; 37 | } 38 | } 39 | 40 | public class GovtFund : Investment 41 | { 42 | public string FD(double amount) 43 | { 44 | return "Fix Deposit of INR " + amount + " is done in GovtFund"; 45 | } 46 | 47 | public string MF(double amount) 48 | { 49 | return "Mutual fund of INR " + amount + " is done in GovtFund"; 50 | } 51 | } 52 | 53 | public class RelianceFund : Investment 54 | { 55 | public string FD(double amount) 56 | { 57 | return "Fix Deposit of INR " + amount + " is done in RelianceFund"; 58 | } 59 | 60 | public string MF(double amount) 61 | { 62 | return "Mutual fund of INR " + amount + " is done in RelianceFund"; 63 | } 64 | } 65 | 66 | 67 | 68 | //Creating the Abstract Factory 69 | public abstract class InvestmentFactory 70 | { 71 | public abstract Investment GetProduct(string InvestType); 72 | public static InvestmentFactory CreateFactory(string InvestType) 73 | { 74 | if (InvestType.Equals("Private")) 75 | return new PrivateSectorFactory(); 76 | else 77 | return new PublicSectorFactory(); 78 | } 79 | } 80 | 81 | 82 | //Creating Concrete Factory 83 | public class PrivateSectorFactory : InvestmentFactory 84 | { 85 | public override Investment GetProduct(string productType) 86 | { 87 | if (productType.Equals("ICICI")) 88 | { 89 | return new ICICI(); 90 | } 91 | else if (productType.Equals("RelianceFund")) 92 | { 93 | return new RelianceFund(); 94 | } 95 | else 96 | return null; 97 | } 98 | } 99 | 100 | public class PublicSectorFactory : InvestmentFactory 101 | { 102 | public override Investment GetProduct(string productType) 103 | { 104 | if (productType.Equals("SBI")) 105 | { 106 | return new SBI(); 107 | } 108 | else if (productType.Equals("RelianceFund")) 109 | { 110 | return new GovtFund(); 111 | } 112 | else 113 | return null; 114 | } 115 | } 116 | 117 | 118 | 119 | } 120 | -------------------------------------------------------------------------------- /DesignPattern/AdapterDesignPattern/AdapterDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/AdapterDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace AdapterDesignPattern 4 | { 5 | 6 | public interface IExport 7 | { 8 | void Export(); 9 | } 10 | public class PDF_Class : IExport 11 | { 12 | public void Export() 13 | { 14 | Console.WriteLine("Export PDF"); 15 | } 16 | 17 | } 18 | 19 | public class TXT_Class : IExport 20 | { 21 | public void Export() 22 | { 23 | Console.WriteLine("Export TXT"); 24 | } 25 | 26 | 27 | } 28 | 29 | // which we don't have control of code 30 | public class XLS_ClassThirdParty 31 | { 32 | public void ExportThirdParty() 33 | { 34 | Console.WriteLine("Export XLS"); 35 | } 36 | 37 | } 38 | 39 | 40 | // Objects based Adapter design pattern 41 | public class XLS_Class_ObjectAdapter : IExport 42 | { 43 | XLS_ClassThirdParty thirdParty = new XLS_ClassThirdParty(); 44 | 45 | public void Export() 46 | { 47 | thirdParty.ExportThirdParty(); 48 | } 49 | } 50 | 51 | // Class based Adapter design pattern 52 | public class XLS_Class_ClassAdapter : XLS_ClassThirdParty, IExport 53 | { 54 | public void Export() 55 | { 56 | ExportThirdParty(); 57 | } 58 | 59 | } 60 | 61 | } -------------------------------------------------------------------------------- /DesignPattern/AggregateRootDesignPattern/AggregateRootDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/AggregateRootDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace AggregateRootDesignPattern 5 | { 6 | // Aggregate root is aggregation relationship contained referenced via the root and not directly 7 | // Aggregate root is complicated so beware of your root logic becoming complex 8 | 9 | 10 | 11 | public class Customer 12 | { 13 | 14 | private List
addresses = new List
(); 15 | 16 | // Any references from outside the aggregate should only go to the aggreate root 17 | public void Add(Address o) 18 | { 19 | foreach (var x in addresses) 20 | { 21 | if (o.Type == x.Type) 22 | { 23 | throw new Exception("Type can not be duplicates"); 24 | } 25 | } 26 | addresses.Add(o); 27 | } 28 | } 29 | 30 | public class Address 31 | { 32 | public int Type { get; set; } 33 | public string Street { get; set; } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DesignPattern/BridgeDesignPattern/BridgeDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/BridgeDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BridgeDesignPattern 4 | { 5 | // Implementor interface 6 | public interface IPaymentSystem 7 | { 8 | void ProcessPayment(string paymentSystem); 9 | } 10 | 11 | //ConcreteImplementor 12 | public class CitiPaymentSystem : IPaymentSystem 13 | { 14 | public void ProcessPayment(string paymentSystem) 15 | { 16 | Console.WriteLine("Using CitiBank gateway for " + paymentSystem); 17 | } 18 | } 19 | 20 | public class ICICIPaymentSystem : IPaymentSystem 21 | { 22 | public void ProcessPayment(string paymentSystem) 23 | { 24 | Console.WriteLine("Using IDBIBank gateway for " + paymentSystem); 25 | } 26 | } 27 | 28 | // Abstraction 29 | public abstract class Payment 30 | { 31 | public IPaymentSystem IPaymentSystem; 32 | public abstract void MakePayment(); 33 | } 34 | 35 | //RefinedAbstraction 36 | public class CardPayment : Payment 37 | { 38 | public override void MakePayment() 39 | { 40 | IPaymentSystem.ProcessPayment("Card Payment"); 41 | } 42 | } 43 | 44 | public class NetBankingPayment : Payment 45 | { 46 | public override void MakePayment() 47 | { 48 | IPaymentSystem.ProcessPayment("NetBanking Payment"); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /DesignPattern/BuilderDesignPattern/BuilderDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/BuilderDesignPattern/Class1 - Copy.cs: -------------------------------------------------------------------------------- 1 | //using System; 2 | 3 | //namespace BuilderDesignPattern 4 | //{ 5 | // public class Report 6 | // { 7 | // public string ReportType { get; set; } 8 | // public string ReportHeader { get; set; } 9 | // public string ReportFooter { get; set; } 10 | // public string ReportContent { get; set; } 11 | // public void DisplayReport() 12 | // { 13 | // Console.WriteLine("Report Type :" + ReportType); 14 | // Console.WriteLine("Header :" + ReportHeader); 15 | // Console.WriteLine("Content :" + ReportContent); 16 | // Console.WriteLine("Footer :" + ReportFooter); 17 | // } 18 | // } 19 | 20 | // public abstract class ReportBuilder 21 | // { 22 | // protected Report reportObject; 23 | // public abstract void SetReportType(); 24 | // public abstract void SetReportHeader(); 25 | // public abstract void SetReportContent(); 26 | // public abstract void SetReportFooter(); 27 | // public void CreateNewReport() 28 | // { 29 | // reportObject = new Report(); 30 | // } 31 | // public Report GetReport() 32 | // { 33 | // return reportObject; 34 | // } 35 | // } 36 | 37 | // public class ExcelReport : ReportBuilder 38 | // { 39 | // public override void SetReportContent() 40 | // { 41 | // reportObject.ReportContent = "Excel Content Section"; 42 | // } 43 | // public override void SetReportFooter() 44 | // { 45 | // reportObject.ReportFooter = "Excel Footer"; 46 | // } 47 | // public override void SetReportHeader() 48 | // { 49 | // reportObject.ReportHeader = "Excel Header"; 50 | // } 51 | // public override void SetReportType() 52 | // { 53 | // reportObject.ReportType = "Excel"; 54 | // } 55 | // } 56 | 57 | // public class ReportDirector 58 | // { 59 | // public Report MakeReport(ReportBuilder reportBuilder) 60 | // { 61 | // reportBuilder.CreateNewReport(); 62 | // reportBuilder.SetReportType(); 63 | // reportBuilder.SetReportHeader(); 64 | // reportBuilder.SetReportContent(); 65 | // reportBuilder.SetReportFooter(); 66 | // return reportBuilder.GetReport(); 67 | // } 68 | // } 69 | 70 | 71 | 72 | 73 | //} 74 | -------------------------------------------------------------------------------- /DesignPattern/BuilderDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace BuilderDesignPattern 4 | { 5 | public class Report 6 | { 7 | public string ReportType { get; set; } 8 | public string ReportHeader { get; set; } 9 | public string ReportFooter { get; set; } 10 | public string ReportContent { get; set; } 11 | public void DisplayReport() 12 | { 13 | Console.WriteLine("Report Type :" + ReportType); 14 | Console.WriteLine("Header :" + ReportHeader); 15 | Console.WriteLine("Content :" + ReportContent); 16 | Console.WriteLine("Footer :" + ReportFooter); 17 | } 18 | } 19 | 20 | public abstract class ReportBuilder 21 | { 22 | protected Report reportObject; 23 | 24 | public void CreateNewReport() 25 | { 26 | reportObject = new Report(); 27 | } 28 | public abstract void SetReportType(); 29 | public abstract void SetReportHeader(); 30 | public abstract void SetReportContent(); 31 | public abstract void SetReportFooter(); 32 | 33 | public Report GetReport() 34 | { 35 | return reportObject; 36 | } 37 | } 38 | 39 | public class ExcelReport : ReportBuilder 40 | { 41 | public override void SetReportContent() 42 | { 43 | reportObject.ReportContent = "Excel Content Section"; 44 | } 45 | public override void SetReportFooter() 46 | { 47 | reportObject.ReportFooter = "Excel Footer"; 48 | } 49 | public override void SetReportHeader() 50 | { 51 | reportObject.ReportHeader = "Excel Header"; 52 | } 53 | public override void SetReportType() 54 | { 55 | reportObject.ReportType = "Excel"; 56 | } 57 | } 58 | 59 | public class ReportDirector 60 | { 61 | public Report MakeReport(ReportBuilder reportBuilder) 62 | { 63 | reportBuilder.CreateNewReport(); 64 | reportBuilder.SetReportType(); 65 | reportBuilder.SetReportHeader(); 66 | reportBuilder.SetReportContent(); 67 | reportBuilder.SetReportFooter(); 68 | return reportBuilder.GetReport(); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /DesignPattern/ChainOfResponsibilityDesignPattern/ChainOfResponsibilityDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/ChainOfResponsibilityDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | //using System; 2 | 3 | //namespace ChainOfResponsibilityDesignPattern 4 | //{ 5 | // public abstract class NewVehicle 6 | // { 7 | // protected NewVehicle process; 8 | 9 | // public void SetProcess(NewVehicle process) 10 | // { 11 | // this.process = process; 12 | // } 13 | 14 | // public abstract void Proceed(string request); 15 | // } 16 | 17 | // public class SelectVehicle : NewVehicle 18 | // { 19 | // public override void Proceed(string request) 20 | // { 21 | // try 22 | // { 23 | // Console.WriteLine("Vehicle Selection Process Started"); 24 | 25 | // if (process != null) 26 | // { 27 | // process.Proceed(request); 28 | // } 29 | // } 30 | // catch (Exception err) 31 | // { 32 | // Console.WriteLine("Error while selecting vehicle."); 33 | // process = null; 34 | // } 35 | // } 36 | // } 37 | 38 | // public class MakePayment : NewVehicle 39 | // { 40 | // public override void Proceed(string request) 41 | // { 42 | // try 43 | // { 44 | // Console.WriteLine("Payment Process Started"); 45 | 46 | // if (process != null) 47 | // { 48 | // process.Proceed(request); 49 | // } 50 | // } 51 | // catch (Exception err) 52 | // { 53 | // Console.WriteLine("Error while doing payment."); 54 | // process = null; 55 | // } 56 | // } 57 | // } 58 | 59 | // public class GenerateServiceBook : NewVehicle 60 | // { 61 | // public override void Proceed(string request) 62 | // { 63 | // try 64 | // { 65 | // Console.WriteLine("Generating Service Book Process Started"); 66 | 67 | // if (process != null) 68 | // { 69 | // process.Proceed(request); 70 | // } 71 | // } 72 | // catch (Exception err) 73 | // { 74 | // Console.WriteLine("Error while generating service book."); 75 | // process = null; 76 | // } 77 | // } 78 | // } 79 | 80 | // public class Insurance : NewVehicle 81 | // { 82 | // public override void Proceed(string request) 83 | // { 84 | // try 85 | // { 86 | // Console.WriteLine("Insurance Process Started"); 87 | 88 | // if (process != null) 89 | // { 90 | // process.Proceed(request); 91 | // } 92 | // } 93 | // catch (Exception err) 94 | // { 95 | // Console.WriteLine("Error while doing insurance."); 96 | // process = null; 97 | // } 98 | // } 99 | // } 100 | 101 | // public class Delivery : NewVehicle 102 | // { 103 | // public override void Proceed(string request) 104 | // { 105 | // try 106 | // { 107 | // Console.WriteLine("Delivery Process Started"); 108 | 109 | // if (process != null) 110 | // { 111 | // process.Proceed(request); 112 | // } 113 | // } 114 | // catch (Exception err) 115 | // { 116 | // Console.WriteLine("Error while delivering vehicle."); 117 | // process = null; 118 | // } 119 | // } 120 | // } 121 | //} 122 | -------------------------------------------------------------------------------- /DesignPattern/ChainOfResponsibilityDesignPattern/IHandler.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 ChainOfResponsibilityDesignPattern 8 | { 9 | public interface IHandler 10 | { 11 | void Handle(); 12 | IHandler SetNext(IHandler handler); 13 | int ExecuteQuery(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DesignPattern/ChainOfResponsibilityDesignPattern/Method1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ChainOfResponsibilityDesignPattern 4 | { 5 | public class Method1 : IHandler 6 | { 7 | private IHandler _next; 8 | 9 | public bool HasValue { get; set; } 10 | 11 | public int ExecuteQuery() 12 | { 13 | return 1; 14 | } 15 | 16 | public void Handle() 17 | { 18 | if (ExecuteQuery() > 0) 19 | { 20 | Console.WriteLine("Method 1"); 21 | _next.Handle(); 22 | } 23 | else 24 | { 25 | this.HasValue = true; 26 | 27 | } 28 | } 29 | 30 | public IHandler SetNext(IHandler handler) 31 | { 32 | _next = handler; 33 | return _next; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /DesignPattern/ChainOfResponsibilityDesignPattern/Method2.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ChainOfResponsibilityDesignPattern 4 | { 5 | public class Method2 : IHandler 6 | { 7 | private IHandler _next; 8 | 9 | public bool HasValue { get; set; } 10 | 11 | public int ExecuteQuery() 12 | { 13 | return 1; 14 | } 15 | 16 | public void Handle() 17 | { 18 | if (ExecuteQuery() > 0) 19 | { 20 | Console.WriteLine("Method 2"); 21 | _next.Handle(); 22 | } 23 | else 24 | { 25 | this.HasValue = true; 26 | 27 | } 28 | } 29 | 30 | public IHandler SetNext(IHandler handler) 31 | { 32 | _next = handler; 33 | return _next; 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /DesignPattern/ChainOfResponsibilityDesignPattern/Method3.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ChainOfResponsibilityDesignPattern 4 | { 5 | public class Method3 : IHandler 6 | { 7 | private IHandler _next; 8 | 9 | public bool HasValue { get; set; } 10 | 11 | public int ExecuteQuery() 12 | { 13 | return 0; 14 | } 15 | 16 | public void Handle() 17 | { 18 | if (ExecuteQuery() > 0) 19 | { 20 | Console.WriteLine("Method 3"); 21 | } 22 | else 23 | { 24 | this.HasValue = true; 25 | 26 | } 27 | } 28 | 29 | public IHandler SetNext(IHandler handler) 30 | { 31 | _next = handler; 32 | return _next; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /DesignPattern/CleanCode/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace CleanCode 5 | { 6 | public class Class1 7 | { 8 | decimal CleanCodeNamingMatters() 9 | { 10 | List p = new List() { 5.50m, 1.48m }; 11 | decimal t = 0; 12 | foreach (var i in p) 13 | { 14 | t += i; 15 | } 16 | return t; 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /DesignPattern/CleanCode/CleanCode.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/CompositeDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace CompositeDesignPattern 5 | { 6 | public interface IEmployeeCDP 7 | { 8 | void GetDetails(int indentation); 9 | } 10 | 11 | public class EmployeeCDP : IEmployeeCDP 12 | { 13 | public EmployeeCDP(string name, string dept) 14 | { 15 | this.Name = name; 16 | this.Department = dept; 17 | } 18 | 19 | public string Name { get; set; } 20 | public string Department { get; set; } 21 | 22 | public void GetDetails(int indentation) 23 | { 24 | Console.WriteLine(string.Format("{0}- Name:{1}, Dept:{2} (Leaf) ", 25 | new String('-', indentation), this.Name.ToString(), 26 | this.Department)); 27 | 28 | } 29 | } 30 | 31 | public class ManagerCDP : IEmployeeCDP 32 | { 33 | public List SubOrdinates; 34 | public ManagerCDP(string name, string dept) 35 | { 36 | this.Name = name; 37 | this.Department = dept; 38 | SubOrdinates = new List(); 39 | } 40 | public string Name { get; set; } 41 | public string Department { get; set; } 42 | public void GetDetails(int indentation) 43 | { 44 | Console.WriteLine(); 45 | Console.WriteLine(string.Format("{0}+ Name:{1}, " + 46 | "Dept:{2} - Manager(Composite)", 47 | new String('-', indentation), this.Name.ToString(), 48 | this.Department)); 49 | foreach (IEmployeeCDP component in SubOrdinates) 50 | { 51 | component.GetDetails(indentation + 1); 52 | } 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /DesignPattern/CompositeDesignPattern/CompositeDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/ConsoleApp/ConsoleApp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | 7 | 8 | 9 | 10 | all 11 | runtime; build; native; contentfiles; analyzers; buildtransitive 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /DesignPattern/ConsoleApp/Program.cs: -------------------------------------------------------------------------------- 1 | //using AdapterDesignPattern; 2 | //using BuilderDesignPattern; 3 | //using FactoryDesignPattern; 4 | //using DecoratorDesignPattern; 5 | //using PrototypeDesignPattern; 6 | //using MementoDesignPattern; 7 | //using AggregateRootDesignPattern; 8 | //using IteratorDesignPattern; 9 | using AbstractFactoryDesignPattern; 10 | using BridgeDesignPattern; 11 | using ChainOfResponsibilityDesignPattern; 12 | using CompositeDesignPattern; 13 | using DecoratorDesignPattern; 14 | using FacadeDesignPattern; 15 | using IOC_DI_Unity; 16 | using LazyLoading; 17 | using Microsoft.Extensions.Caching.Memory; 18 | using ObserverDesignPattern; 19 | using ProxyDesignPattern; 20 | using ReplaceIfPolymorphismDesignPattern; 21 | using RepositoryDesignPattern; 22 | using SingletonDesignPattern; 23 | using StateDesignPattern; 24 | using StrategyDesignPattern; 25 | using System; 26 | using TemplateDesignPattern; 27 | using Unity; 28 | using UOWDesignPattern; 29 | 30 | namespace ConsoleApp 31 | { 32 | class Program 33 | { 34 | static void Main(string[] args) 35 | { 36 | Console.WriteLine("Hello World!"); 37 | 38 | 39 | 40 | 41 | //#region Factory design pattern 42 | //ConsoleColorMethod("Factory design pattern"); 43 | 44 | 45 | //IFactory factory = CreateFactory.GetObject(ObjectType.Customer); 46 | //Console.WriteLine("Get customer object : " + factory.getName()); 47 | 48 | //factory = CreateFactory.GetObject(ObjectType.Supplier); 49 | //Console.WriteLine("Get supplier object : " + factory.getName()); 50 | //#endregion 51 | 52 | 53 | //#region Adapter design pattern 54 | //IExport target = new PDF_Class(); 55 | //target.Export(); 56 | 57 | //target = new XLS_Class_ObjectAdapter(); 58 | //target.Export(); 59 | 60 | //target = new XLS_Class_ClassAdapter(); 61 | //target.Export(); 62 | //#endregion 63 | 64 | //#region Builder design pattern 65 | 66 | //// Client Code 67 | 68 | //ReportDirector reportDirector = new ReportDirector(); 69 | 70 | //ExcelReport excelReport = new ExcelReport(); 71 | //Report report = reportDirector.MakeReport(excelReport); 72 | ////report.DisplayReport(); 73 | 74 | //#endregion 75 | 76 | #region Decorator design pattern 77 | IMemoryCache _memoryCache = new MemoryCache(new MemoryCacheOptions()); 78 | IWeatherService innerService = new WeatherService(); 79 | IWeatherService withCachingDecorator = new WeatherServiceCachingDecorator(innerService, _memoryCache); 80 | IWeatherService withLoggingDecorator = new WeatherServiceLoggingDecorator(withCachingDecorator); 81 | 82 | // with no cache - first time 83 | Console.WriteLine(withCachingDecorator.GetCurrentWeather("Bangalore")); 84 | // with cache - second time 85 | Console.WriteLine(withCachingDecorator.GetCurrentWeather("Bangalore")); 86 | 87 | 88 | // with no cache - first time 89 | Console.WriteLine(withLoggingDecorator.GetCurrentWeather("Bangalore")); 90 | // with cache - second time 91 | Console.WriteLine(withLoggingDecorator.GetCurrentWeather("Bangalore")); 92 | 93 | 94 | Console.ReadLine(); 95 | 96 | 97 | 98 | #endregion 99 | 100 | #region Prototype Design Pattern 101 | 102 | //Employee emp1 = new Employee(); 103 | //emp1.Name = "Amit Naik"; 104 | //emp1.Department = "IT"; 105 | //Employee emp2 = emp1.GetClone(); 106 | //emp2.Name = "Shwetha"; 107 | 108 | //Console.WriteLine("Emplpyee 1: "); 109 | //Console.WriteLine("Name: " + emp1.Name + ", Department: " + emp1.Department); 110 | //Console.WriteLine("Emplpyee 2: "); 111 | //Console.WriteLine("Name: " + emp2.Name + ", Department: " + emp2.Department); 112 | 113 | 114 | #endregion 115 | 116 | 117 | #region Memento Design Pattern 118 | 119 | //Employee emp1 = new Employee(); 120 | //emp1.Name = "Amit Naik"; 121 | //emp1.Department = "IT"; 122 | //Employee emp2 = emp1.GetClone(); 123 | //emp2.Name = "Shwetha"; 124 | 125 | 126 | 127 | //Console.WriteLine("Emplpyee 1: "); 128 | //Console.WriteLine("Name: " + emp1.Name + ", Department: " + emp1.Department); 129 | ////Console.WriteLine("Emplpyee 2: "); 130 | ////Console.WriteLine("Name: " + emp2.Name + ", Department: " + emp2.Department); 131 | 132 | //Employee emp3 = emp1.Revert(); 133 | //Console.WriteLine("Reverting "); 134 | //Console.WriteLine("Name: " + emp3.Name + ", Department: " + emp3.Department); 135 | 136 | #endregion 137 | 138 | 139 | #region Aggregate root design pattern 140 | 141 | //Customer customer = new Customer(); 142 | 143 | //customer.Add(new Address { Type = 1 }); 144 | //customer.Add(new Address { Type = 1 }); 145 | 146 | #endregion 147 | 148 | 149 | #region Iterator design pattern 150 | 151 | //Customer customer = new Customer(); 152 | 153 | //customer.Add(new Address { Type = 1 }); 154 | //customer.Add(new Address { Type = 2 }); 155 | 156 | //// we can avoid manipulating methods like below in Iterator design pattern 157 | ////customer.addresses.Add(new Address { Type = 3 }); 158 | 159 | 160 | //// Implementing via Ienumerable 161 | //foreach (var item in customer.GetAddresses()) 162 | //{ 163 | // Console.WriteLine($"customer address type : {item.Type}"); 164 | //} 165 | 166 | //// Implementing via IEnumerator 167 | //foreach (var item in customer) 168 | //{ 169 | // Console.WriteLine($"customer address type : {item.Type}"); 170 | 171 | //} 172 | ////var x = customer.GetEnumerator(); 173 | 174 | 175 | #endregion 176 | 177 | 178 | #region Generic Repository Design Pattern 179 | 180 | 181 | ConsoleColorMethod("Generic Repository design pattern"); 182 | 183 | // Generic 184 | IGenericRepository repository = null; 185 | repository = new GenericRepository(); 186 | 187 | try 188 | { 189 | var employee = new Employee() { Name = "Amit", Salary = 60000, Gender = "Male", Dept = "IT" }; 190 | repository.Add(employee); 191 | repository.Save(); 192 | 193 | repository = new GenericRepository(); 194 | var id = repository.GetById(1); 195 | 196 | repository = new GenericRepository(); 197 | var all = repository.GetAll(); 198 | 199 | repository = new GenericRepository(); 200 | var id1 = repository.GetById(1); 201 | 202 | if (id1 != null) 203 | { 204 | id1.Name = "Amit Naik"; 205 | repository.Update(id1); 206 | repository.Save(); 207 | 208 | repository = new GenericRepository(); 209 | repository.Delete(1); 210 | repository.Save(); 211 | } 212 | else 213 | { 214 | 215 | Console.ForegroundColor = ConsoleColor.Red; 216 | Console.WriteLine($"Error : ID not found"); 217 | Console.ForegroundColor = ConsoleColor.White; 218 | } 219 | 220 | } 221 | catch (Exception ex) 222 | { 223 | Console.ForegroundColor = ConsoleColor.Red; 224 | Console.WriteLine($"Error :{ex.Message}"); 225 | Console.ForegroundColor = ConsoleColor.White; 226 | 227 | } 228 | #endregion 229 | 230 | #region Non Generic Repository Design Pattern 231 | 232 | ConsoleColorMethod("Non Generic Repository design pattern"); 233 | 234 | // Generic 235 | EmployeeRepository employeeRepository = null; 236 | employeeRepository = new EmployeeRepository(); 237 | 238 | try 239 | { 240 | var employee = new Employee() { Name = "Shweta", Salary = 600000, Gender = "Female", Dept = "IT" }; 241 | employeeRepository.Add(employee); 242 | employeeRepository.Save(); 243 | 244 | employeeRepository = new EmployeeRepository(); 245 | var id = employeeRepository.GetById(1); 246 | 247 | employeeRepository = new EmployeeRepository(); 248 | var all = employeeRepository.GetAll(); 249 | 250 | employeeRepository = new EmployeeRepository(); 251 | var id1 = employeeRepository.GetById(2); 252 | 253 | if (id1 != null) 254 | { 255 | id1.Name = "Shweta Naik"; 256 | employeeRepository.Update(id1); 257 | employeeRepository.Save(); 258 | 259 | employeeRepository = new EmployeeRepository(); 260 | employeeRepository.Delete(1); 261 | employeeRepository.Save(); 262 | } 263 | else 264 | { 265 | Console.ForegroundColor = ConsoleColor.Red; 266 | Console.WriteLine($"Error : ID not found"); 267 | Console.ForegroundColor = ConsoleColor.White; 268 | } 269 | } 270 | catch (Exception ex) 271 | { 272 | Console.ForegroundColor = ConsoleColor.Red; 273 | Console.WriteLine($"Error :{ex.Message}"); 274 | Console.ForegroundColor = ConsoleColor.White; 275 | 276 | } 277 | #endregion 278 | 279 | #region UOW design pattern for Generic Repository 280 | 281 | ConsoleColorMethod("UOW design pattern for Generic Repository"); 282 | 283 | UOW_Generic uowG = new UOW_Generic(new EmployeeDBContext()); 284 | 285 | try 286 | { 287 | var employee = new Employee() { Name = "Shweta", Salary = 600000, Gender = "Female", Dept = "IT UOW generic" }; 288 | 289 | uowG.employeeRepository.Add(employee); 290 | uowG.Complete(); 291 | } 292 | catch (Exception ex) 293 | { 294 | 295 | uowG.Dispose(); 296 | 297 | Console.ForegroundColor = ConsoleColor.Red; 298 | Console.WriteLine($"Error : {ex.Message}"); 299 | Console.ForegroundColor = ConsoleColor.White; 300 | 301 | } 302 | #endregion 303 | 304 | #region UOW design pattern for Non Generic Repository 305 | 306 | ConsoleColorMethod("UOW design pattern for Non Generic Repository"); 307 | 308 | UnitOfWork uow = new UnitOfWork(new EmployeeDBContext()); 309 | 310 | try 311 | { 312 | var employee = new Employee() { Name = "Shweta", Salary = 600000, Gender = "Female", Dept = "IT UOW non generic" }; 313 | 314 | uow.employeeRepository.Add(employee); 315 | uow.Complete(); 316 | } 317 | catch (Exception ex) 318 | { 319 | 320 | uow.Dispose(); 321 | 322 | Console.ForegroundColor = ConsoleColor.Red; 323 | Console.WriteLine($"Error : {ex.Message}"); 324 | Console.ForegroundColor = ConsoleColor.White; 325 | 326 | } 327 | #endregion 328 | 329 | #region Template design pattern 330 | ConsoleColorMethod("Template design pattern"); 331 | 332 | TemplateHiringProcess hiringProcess = new CSDepartment(); 333 | 334 | Console.WriteLine("*** Hiring CS students"); 335 | hiringProcess.HireFreshers(); 336 | 337 | Console.WriteLine(Environment.NewLine); 338 | 339 | hiringProcess = new EEEDepartment(); 340 | Console.ForegroundColor = ConsoleColor.Yellow; 341 | Console.WriteLine("*** Hiring EEE students"); 342 | hiringProcess.HireFreshers(); 343 | 344 | #endregion 345 | 346 | #region Singleton Design Pattern 347 | ConsoleColorMethod("Singleton design pattern"); 348 | 349 | Singleton s1 = Singleton.getInstance(); 350 | Singleton s2 = Singleton.getInstance(); 351 | 352 | s1.myMethod(); 353 | 354 | // Test for same instance 355 | //if (s1 == s2) 356 | //{ 357 | // Console.WriteLine("Objects are the same instance"); 358 | //} 359 | 360 | #endregion 361 | 362 | #region Replace IF with Polyphormism 363 | 364 | ConsoleColorMethod("Replace IF with Polyphormism"); 365 | 366 | Console.WriteLine("Enter your skill set for job opening (like JavaScript,c#,Net)"); 367 | 368 | //string knowledge = Console.ReadLine(); 369 | string knowledge = "javascript"; 370 | 371 | Console.WriteLine(SimpleFactoryRIP.Create(knowledge.ToLower())); 372 | #endregion 373 | 374 | #region Abstract Factory design pattern 375 | 376 | ConsoleColorMethod("Abstract Factory design pattern"); 377 | 378 | var investmentPrivateFactory = InvestmentFactory.CreateFactory("Private"); 379 | string productType = investmentPrivateFactory.GetProduct("ICICI").FD(1000); 380 | 381 | Console.WriteLine(productType); 382 | 383 | Console.WriteLine(InvestmentFactory.CreateFactory("Public").GetProduct("SBI").MF(50000)); 384 | #endregion 385 | 386 | #region IOC DI Unity 387 | 388 | ConsoleColorMethod("IOC DI Unity"); 389 | 390 | IUnityContainer container = new UnityContainer(); 391 | container.RegisterType("JIO"); 392 | container.RegisterType("Vodafone"); 393 | 394 | IRechargeHandler recharge = container.Resolve("JIO"); 395 | recharge.DoRecharge(); 396 | 397 | #endregion 398 | 399 | #region Lazy loading 400 | 401 | ConsoleColorMethod("Lazy loading"); 402 | Console.WriteLine("Enter your skill set for job opening (like JavaScript,c#,Net)"); 403 | 404 | string knowledgeLazyLoading = "Javascript"; 405 | 406 | Console.WriteLine(LazyLoadingFactory.Create(knowledgeLazyLoading.ToLower())); 407 | 408 | // When we call second time, it doesn't add to temp Dictionary in LazyLoadingFactory class file 409 | Console.WriteLine(LazyLoadingFactory.Create(knowledgeLazyLoading.ToLower())); 410 | 411 | #endregion 412 | 413 | 414 | 415 | #region Bridge design pattern 416 | 417 | ConsoleColorMethod("Bridge design pattern"); 418 | Payment order = new CardPayment(); 419 | order.IPaymentSystem = new CitiPaymentSystem(); 420 | order.MakePayment(); 421 | 422 | order.IPaymentSystem = new ICICIPaymentSystem(); 423 | order.MakePayment(); 424 | 425 | order = new NetBankingPayment(); 426 | order.IPaymentSystem = new CitiPaymentSystem(); 427 | order.MakePayment(); 428 | #endregion 429 | 430 | 431 | #region Composite design pattern 432 | ConsoleColorMethod("Composite design pattern"); 433 | IEmployeeCDP John = new EmployeeCDP("John", "IT"); 434 | IEmployeeCDP Mike = new EmployeeCDP("Mike", "IT"); 435 | IEmployeeCDP Jason = new EmployeeCDP("Jason", "HR"); 436 | IEmployeeCDP Eric = new EmployeeCDP("Eric", "HR"); 437 | IEmployeeCDP Henry = new EmployeeCDP("Henry", "HR"); 438 | 439 | IEmployeeCDP James = new ManagerCDP("James", "IT") 440 | { SubOrdinates = { John, Mike } }; 441 | IEmployeeCDP Philip = new ManagerCDP("Philip", "HR") 442 | { SubOrdinates = { Jason, Eric, Henry } }; 443 | 444 | IEmployeeCDP Bob = new ManagerCDP("Bob", "Head") 445 | { SubOrdinates = { James, Philip } }; 446 | James.GetDetails(1); 447 | #endregion 448 | 449 | 450 | #region Facade design pattern 451 | ConsoleColorMethod("Facade design pattern"); 452 | 453 | AadharFacade aadharFacade = new AadharFacade(); 454 | aadharFacade.CreateAadhar(); 455 | #endregion 456 | 457 | #region Chain of Responsibility design pattern 458 | ConsoleColorMethod("Chain of Responsibility design pattern"); 459 | 460 | var m1 = new Method1(); 461 | m1.SetNext(new Method2()).SetNext(new Method3()); 462 | 463 | m1.Handle(); 464 | 465 | 466 | 467 | 468 | //NewVehicle selection = new SelectVehicle(); 469 | //NewVehicle payment = new MakePayment(); 470 | //NewVehicle serviceBook = new GenerateServiceBook(); 471 | //NewVehicle insurance = new Insurance(); 472 | //NewVehicle delivery = new Delivery(); 473 | 474 | //selection.SetProcess(payment); 475 | //payment.SetProcess(serviceBook); 476 | //serviceBook.SetProcess(insurance); 477 | //insurance.SetProcess(delivery); 478 | 479 | //selection.Proceed("Bajaj Pulsar"); 480 | 481 | 482 | #endregion 483 | 484 | 485 | #region Strategy design pattern 486 | ConsoleColorMethod("Strategy design pattern"); 487 | HashingContext context; 488 | 489 | context = new HashingContext(new MD5Hash()); 490 | string strSHA1 = context.HashPassword("Amit Naik"); 491 | Console.WriteLine("Amit Naik - " + strSHA1); 492 | 493 | context = new HashingContext(new SHA384Hash()); 494 | string StrSHA384 = context.HashPassword("Shwetha Naik"); 495 | Console.WriteLine("Shwetha Naik - " + StrSHA384); 496 | 497 | #endregion 498 | 499 | 500 | #region Strategy design pattern 501 | ConsoleColorMethod("Strategy design pattern"); 502 | IImage Image1 = new ProxyImage("Tiger Image"); 503 | 504 | Console.WriteLine("Image1 calling DisplayImage first time :"); 505 | Image1.DisplayImage(); // loading necessary 506 | Console.WriteLine("Image1 calling DisplayImage second time :"); 507 | Image1.DisplayImage(); // loading unnecessary 508 | Console.WriteLine("Image1 calling DisplayImage third time :"); 509 | Image1.DisplayImage(); // loading unnecessary 510 | Console.WriteLine(); 511 | IImage Image2 = new ProxyImage("Lion Image"); 512 | Console.WriteLine("Image2 calling DisplayImage first time :"); 513 | Image2.DisplayImage(); // loading necessary 514 | Console.WriteLine("Image2 calling DisplayImage second time :"); 515 | Image2.DisplayImage(); // loading unnecessary 516 | 517 | #endregion 518 | 519 | #region Strategy design pattern 520 | ConsoleColorMethod("Strategy design pattern"); 521 | 522 | //Create a Product with Out Of Stock Status 523 | Subject IPhone = new Subject("IPhone Mobile", 10000, "Out Of Stock"); 524 | //User Anurag will be created and user1 object will be registered to the subject 525 | Observer user1 = new Observer("Amit", IPhone); 526 | //User Pranaya will be created and user1 object will be registered to the subject 527 | Observer user2 = new Observer("Shweta", IPhone); 528 | //User Priyanka will be created and user3 object will be registered to the subject 529 | Observer user3 = new Observer("Krishna", IPhone); 530 | 531 | Console.WriteLine("IPhone Mobile current state : " + IPhone.getAvailability()); 532 | Console.WriteLine(); 533 | // Now product is available 534 | IPhone.setAvailability("Available"); 535 | #endregion 536 | 537 | 538 | #region State design pattern 539 | ConsoleColorMethod("State design pattern"); 540 | 541 | // Initially Vending Machine will be 'noMoneyState' 542 | VendingMachine vendingMachine = new VendingMachine(); 543 | Console.WriteLine("Current VendingMachine State : " 544 | + vendingMachine.vendingMachineState.GetType().Name + "\n"); 545 | vendingMachine.DispenseProduct(); 546 | vendingMachine.SelectProductAndInsertMoney(50, "Pepsi"); 547 | // Money has been inserted so vending Machine internal state 548 | // changed to 'hasMoneyState' 549 | Console.WriteLine("\nCurrent VendingMachine State : " 550 | + vendingMachine.vendingMachineState.GetType().Name + "\n"); 551 | vendingMachine.SelectProductAndInsertMoney(50, "Fanta"); 552 | vendingMachine.DispenseProduct(); 553 | // Product has been dispensed so vending Machine internal state 554 | // changed to 'NoMoneyState' 555 | Console.WriteLine("\nCurrent VendingMachine State : " 556 | + vendingMachine.vendingMachineState.GetType().Name); 557 | #endregion 558 | 559 | } 560 | 561 | 562 | static void ConsoleColorMethod(string designPattern) 563 | { 564 | Console.ForegroundColor = ConsoleColor.Green; 565 | Console.WriteLine(Environment.NewLine + designPattern + Environment.NewLine); 566 | Console.ForegroundColor = ConsoleColor.White; 567 | 568 | } 569 | 570 | } 571 | } 572 | -------------------------------------------------------------------------------- /DesignPattern/DecoratorDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Caching.Memory; 2 | using System; 3 | 4 | namespace DecoratorDesignPattern 5 | { 6 | public interface IWeatherService 7 | { 8 | string GetCurrentWeather(String location); 9 | string GetForecast(String location); 10 | } 11 | 12 | 13 | public class WeatherService : IWeatherService 14 | { 15 | public string GetCurrentWeather(string location) 16 | { 17 | return "24 degree"; 18 | } 19 | 20 | public string GetForecast(string location) 21 | { 22 | return "partly cloudy"; 23 | } 24 | } 25 | 26 | public class WeatherServiceCachingDecorator : IWeatherService 27 | { 28 | private readonly IMemoryCache _cache; 29 | private readonly IWeatherService _innerWeatehrService; 30 | public WeatherServiceCachingDecorator(IWeatherService weatherService, IMemoryCache cache) 31 | { 32 | _innerWeatehrService = weatherService; 33 | _cache = cache; 34 | } 35 | 36 | public string GetCurrentWeather(string location) 37 | { 38 | string cacheKey = $"WeatherConditions::{location}"; 39 | if (_cache.TryGetValue(cacheKey, out var currentWeather)) 40 | { 41 | return currentWeather; 42 | } 43 | else 44 | { 45 | var currentConditions = _innerWeatehrService.GetCurrentWeather(location); 46 | _cache.Set(cacheKey, currentConditions, TimeSpan.FromMinutes(30)); 47 | return currentConditions; 48 | } 49 | } 50 | 51 | public string GetForecast(string location) 52 | { 53 | string cacheKey = $"WeatherForecast::{location}"; 54 | if (_cache.TryGetValue(cacheKey, out var forecast)) 55 | { 56 | return forecast; 57 | } 58 | else 59 | { 60 | var locationForecast = _innerWeatehrService.GetForecast(location); 61 | _cache.Set(cacheKey, locationForecast, TimeSpan.FromMinutes(30)); 62 | return locationForecast; 63 | } 64 | } 65 | 66 | } 67 | 68 | 69 | public class WeatherServiceLoggingDecorator : IWeatherService 70 | { 71 | private readonly IWeatherService _innerWeatherService; 72 | public WeatherServiceLoggingDecorator(IWeatherService weatherService) 73 | { 74 | _innerWeatherService = weatherService; 75 | } 76 | 77 | public string GetCurrentWeather(string location) 78 | { 79 | var currentConditions = _innerWeatherService.GetCurrentWeather(location); 80 | Console.WriteLine("GetCurrentWeather logging"); 81 | return currentConditions; 82 | } 83 | 84 | public string GetForecast(string location) 85 | { 86 | var locationForecast = _innerWeatherService.GetForecast(location); 87 | Console.WriteLine("GetForecast logging"); 88 | return locationForecast; 89 | } 90 | } 91 | } -------------------------------------------------------------------------------- /DesignPattern/DecoratorDesignPattern/DecoratorDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /DesignPattern/DesignPattern.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30002.166 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{102E898E-D49D-491C-8C00-C982F6827E24}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryDesignPattern", "FactoryDesignPattern\FactoryDesignPattern.csproj", "{38E44C0C-355F-4223-BEEC-C66FBBB76CDA}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdapterDesignPattern", "AdapterDesignPattern\AdapterDesignPattern.csproj", "{E9A5C47B-A83B-47A2-B4EF-1A238A1850E2}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuilderDesignPattern", "BuilderDesignPattern\BuilderDesignPattern.csproj", "{7DFB2281-10D1-42B1-8187-31E83BD18419}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DecoratorDesignPattern", "DecoratorDesignPattern\DecoratorDesignPattern.csproj", "{4A2B38C9-F890-4543-A04D-7F5086F62592}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PrototypeDesignPattern", "PrototypeDesignPattern\PrototypeDesignPattern.csproj", "{EEC298B6-E034-410A-8234-B7FEA8C2E150}" 17 | EndProject 18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IteratorDesignPattern", "IteratorDesignPattern\IteratorDesignPattern.csproj", "{8B9E0C84-2F96-414D-A739-90518F108692}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AggregateRootDesignPattern", "AggregateRootDesignPattern\AggregateRootDesignPattern.csproj", "{2F572FA5-EBCA-4713-8632-4360070392AC}" 21 | EndProject 22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MementoDesignPattern", "MementoDesignPattern\MementoDesignPattern.csproj", "{56F894D6-2BA0-41CB-A15C-8449CD63940B}" 23 | EndProject 24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RepositoryDesignPattern", "RepositoryDesignPattern\RepositoryDesignPattern.csproj", "{24B4C0C3-E366-46D0-B6AB-394356BEAD80}" 25 | EndProject 26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SingletonDesignPattern", "SingletonDesignPattern\SingletonDesignPattern.csproj", "{098132FD-7305-4BE3-B135-1ABD517A30EB}" 27 | EndProject 28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TemplateDesignPattern", "TemplateDesignPattern\TemplateDesignPattern.csproj", "{661BC7B9-F97A-404E-BEEE-9CFFF23E881F}" 29 | EndProject 30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReplaceIfPolymorphismDesignPattern", "ReplaceIfPolymorphismDesignPattern\ReplaceIfPolymorphismDesignPattern.csproj", "{D453469F-E1F4-444E-9112-864847B46BE1}" 31 | EndProject 32 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CleanCode", "CleanCode\CleanCode.csproj", "{29ABFDE5-0125-4A45-A355-48F32262B497}" 33 | EndProject 34 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFactoryDesignPattern", "AbstractFactory\AbstractFactoryDesignPattern.csproj", "{832727C2-2BBB-448A-8C5C-34C53E656070}" 35 | EndProject 36 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IOC_DI_Unity", "IOC_DI_Unity\IOC_DI_Unity.csproj", "{1DF15BC9-524D-4684-9280-BB9658C46F5F}" 37 | EndProject 38 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LazyLoading", "LazyLoading\LazyLoading.csproj", "{BF468425-9C2D-4B13-9F0F-F259ED78F454}" 39 | EndProject 40 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UOWDesignPattern", "UOWDesignPattern\UOWDesignPattern.csproj", "{6B54C92B-761C-41A3-A8EA-16C72B0DA3BA}" 41 | EndProject 42 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BridgeDesignPattern", "BridgeDesignPattern\BridgeDesignPattern.csproj", "{4DBC3B24-4A12-4713-8EEF-ECC9CA96A4BF}" 43 | EndProject 44 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompositeDesignPattern", "CompositeDesignPattern\CompositeDesignPattern.csproj", "{16EDABBA-CB2A-42DE-8ACE-AC920F7A009C}" 45 | EndProject 46 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FacadeDesignPattern", "FacadeDesignPattern\FacadeDesignPattern.csproj", "{E4053B2D-16C9-4E19-BA5A-573A57BA2E10}" 47 | EndProject 48 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibilityDesignPattern", "ChainOfResponsibilityDesignPattern\ChainOfResponsibilityDesignPattern.csproj", "{99F2F5F3-9EFD-4477-9F02-144C0CB93868}" 49 | EndProject 50 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrategyDesignPattern", "StrategyDesignPattern\StrategyDesignPattern.csproj", "{326DEE46-3CEE-4831-A2CD-B8E71F25FC41}" 51 | EndProject 52 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ProxyDesignPattern", "ProxyDesignPattern\ProxyDesignPattern.csproj", "{05961BA1-4BBE-40C9-AFEE-AF61AA170A4D}" 53 | EndProject 54 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObserverDesignPattern", "ObserverDesignPattern\ObserverDesignPattern.csproj", "{84933CF0-08B2-40AB-9D35-7329BD5D6B0C}" 55 | EndProject 56 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StateDesignPattern", "StateDesignPattern\StateDesignPattern.csproj", "{55B80C22-E7AB-4C79-8FB9-AE2D5134B901}" 57 | EndProject 58 | Global 59 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 60 | Debug|Any CPU = Debug|Any CPU 61 | Release|Any CPU = Release|Any CPU 62 | EndGlobalSection 63 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 64 | {102E898E-D49D-491C-8C00-C982F6827E24}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 65 | {102E898E-D49D-491C-8C00-C982F6827E24}.Debug|Any CPU.Build.0 = Debug|Any CPU 66 | {102E898E-D49D-491C-8C00-C982F6827E24}.Release|Any CPU.ActiveCfg = Release|Any CPU 67 | {102E898E-D49D-491C-8C00-C982F6827E24}.Release|Any CPU.Build.0 = Release|Any CPU 68 | {38E44C0C-355F-4223-BEEC-C66FBBB76CDA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 69 | {38E44C0C-355F-4223-BEEC-C66FBBB76CDA}.Debug|Any CPU.Build.0 = Debug|Any CPU 70 | {38E44C0C-355F-4223-BEEC-C66FBBB76CDA}.Release|Any CPU.ActiveCfg = Release|Any CPU 71 | {38E44C0C-355F-4223-BEEC-C66FBBB76CDA}.Release|Any CPU.Build.0 = Release|Any CPU 72 | {E9A5C47B-A83B-47A2-B4EF-1A238A1850E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 73 | {E9A5C47B-A83B-47A2-B4EF-1A238A1850E2}.Debug|Any CPU.Build.0 = Debug|Any CPU 74 | {E9A5C47B-A83B-47A2-B4EF-1A238A1850E2}.Release|Any CPU.ActiveCfg = Release|Any CPU 75 | {E9A5C47B-A83B-47A2-B4EF-1A238A1850E2}.Release|Any CPU.Build.0 = Release|Any CPU 76 | {7DFB2281-10D1-42B1-8187-31E83BD18419}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 77 | {7DFB2281-10D1-42B1-8187-31E83BD18419}.Debug|Any CPU.Build.0 = Debug|Any CPU 78 | {7DFB2281-10D1-42B1-8187-31E83BD18419}.Release|Any CPU.ActiveCfg = Release|Any CPU 79 | {7DFB2281-10D1-42B1-8187-31E83BD18419}.Release|Any CPU.Build.0 = Release|Any CPU 80 | {4A2B38C9-F890-4543-A04D-7F5086F62592}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 81 | {4A2B38C9-F890-4543-A04D-7F5086F62592}.Debug|Any CPU.Build.0 = Debug|Any CPU 82 | {4A2B38C9-F890-4543-A04D-7F5086F62592}.Release|Any CPU.ActiveCfg = Release|Any CPU 83 | {4A2B38C9-F890-4543-A04D-7F5086F62592}.Release|Any CPU.Build.0 = Release|Any CPU 84 | {EEC298B6-E034-410A-8234-B7FEA8C2E150}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 85 | {EEC298B6-E034-410A-8234-B7FEA8C2E150}.Debug|Any CPU.Build.0 = Debug|Any CPU 86 | {EEC298B6-E034-410A-8234-B7FEA8C2E150}.Release|Any CPU.ActiveCfg = Release|Any CPU 87 | {EEC298B6-E034-410A-8234-B7FEA8C2E150}.Release|Any CPU.Build.0 = Release|Any CPU 88 | {8B9E0C84-2F96-414D-A739-90518F108692}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 89 | {8B9E0C84-2F96-414D-A739-90518F108692}.Debug|Any CPU.Build.0 = Debug|Any CPU 90 | {8B9E0C84-2F96-414D-A739-90518F108692}.Release|Any CPU.ActiveCfg = Release|Any CPU 91 | {8B9E0C84-2F96-414D-A739-90518F108692}.Release|Any CPU.Build.0 = Release|Any CPU 92 | {2F572FA5-EBCA-4713-8632-4360070392AC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 93 | {2F572FA5-EBCA-4713-8632-4360070392AC}.Debug|Any CPU.Build.0 = Debug|Any CPU 94 | {2F572FA5-EBCA-4713-8632-4360070392AC}.Release|Any CPU.ActiveCfg = Release|Any CPU 95 | {2F572FA5-EBCA-4713-8632-4360070392AC}.Release|Any CPU.Build.0 = Release|Any CPU 96 | {56F894D6-2BA0-41CB-A15C-8449CD63940B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 97 | {56F894D6-2BA0-41CB-A15C-8449CD63940B}.Debug|Any CPU.Build.0 = Debug|Any CPU 98 | {56F894D6-2BA0-41CB-A15C-8449CD63940B}.Release|Any CPU.ActiveCfg = Release|Any CPU 99 | {56F894D6-2BA0-41CB-A15C-8449CD63940B}.Release|Any CPU.Build.0 = Release|Any CPU 100 | {24B4C0C3-E366-46D0-B6AB-394356BEAD80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 101 | {24B4C0C3-E366-46D0-B6AB-394356BEAD80}.Debug|Any CPU.Build.0 = Debug|Any CPU 102 | {24B4C0C3-E366-46D0-B6AB-394356BEAD80}.Release|Any CPU.ActiveCfg = Release|Any CPU 103 | {24B4C0C3-E366-46D0-B6AB-394356BEAD80}.Release|Any CPU.Build.0 = Release|Any CPU 104 | {098132FD-7305-4BE3-B135-1ABD517A30EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 105 | {098132FD-7305-4BE3-B135-1ABD517A30EB}.Debug|Any CPU.Build.0 = Debug|Any CPU 106 | {098132FD-7305-4BE3-B135-1ABD517A30EB}.Release|Any CPU.ActiveCfg = Release|Any CPU 107 | {098132FD-7305-4BE3-B135-1ABD517A30EB}.Release|Any CPU.Build.0 = Release|Any CPU 108 | {661BC7B9-F97A-404E-BEEE-9CFFF23E881F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 109 | {661BC7B9-F97A-404E-BEEE-9CFFF23E881F}.Debug|Any CPU.Build.0 = Debug|Any CPU 110 | {661BC7B9-F97A-404E-BEEE-9CFFF23E881F}.Release|Any CPU.ActiveCfg = Release|Any CPU 111 | {661BC7B9-F97A-404E-BEEE-9CFFF23E881F}.Release|Any CPU.Build.0 = Release|Any CPU 112 | {D453469F-E1F4-444E-9112-864847B46BE1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 113 | {D453469F-E1F4-444E-9112-864847B46BE1}.Debug|Any CPU.Build.0 = Debug|Any CPU 114 | {D453469F-E1F4-444E-9112-864847B46BE1}.Release|Any CPU.ActiveCfg = Release|Any CPU 115 | {D453469F-E1F4-444E-9112-864847B46BE1}.Release|Any CPU.Build.0 = Release|Any CPU 116 | {29ABFDE5-0125-4A45-A355-48F32262B497}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 117 | {29ABFDE5-0125-4A45-A355-48F32262B497}.Debug|Any CPU.Build.0 = Debug|Any CPU 118 | {29ABFDE5-0125-4A45-A355-48F32262B497}.Release|Any CPU.ActiveCfg = Release|Any CPU 119 | {29ABFDE5-0125-4A45-A355-48F32262B497}.Release|Any CPU.Build.0 = Release|Any CPU 120 | {832727C2-2BBB-448A-8C5C-34C53E656070}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 121 | {832727C2-2BBB-448A-8C5C-34C53E656070}.Debug|Any CPU.Build.0 = Debug|Any CPU 122 | {832727C2-2BBB-448A-8C5C-34C53E656070}.Release|Any CPU.ActiveCfg = Release|Any CPU 123 | {832727C2-2BBB-448A-8C5C-34C53E656070}.Release|Any CPU.Build.0 = Release|Any CPU 124 | {1DF15BC9-524D-4684-9280-BB9658C46F5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 125 | {1DF15BC9-524D-4684-9280-BB9658C46F5F}.Debug|Any CPU.Build.0 = Debug|Any CPU 126 | {1DF15BC9-524D-4684-9280-BB9658C46F5F}.Release|Any CPU.ActiveCfg = Release|Any CPU 127 | {1DF15BC9-524D-4684-9280-BB9658C46F5F}.Release|Any CPU.Build.0 = Release|Any CPU 128 | {BF468425-9C2D-4B13-9F0F-F259ED78F454}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 129 | {BF468425-9C2D-4B13-9F0F-F259ED78F454}.Debug|Any CPU.Build.0 = Debug|Any CPU 130 | {BF468425-9C2D-4B13-9F0F-F259ED78F454}.Release|Any CPU.ActiveCfg = Release|Any CPU 131 | {BF468425-9C2D-4B13-9F0F-F259ED78F454}.Release|Any CPU.Build.0 = Release|Any CPU 132 | {6B54C92B-761C-41A3-A8EA-16C72B0DA3BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 133 | {6B54C92B-761C-41A3-A8EA-16C72B0DA3BA}.Debug|Any CPU.Build.0 = Debug|Any CPU 134 | {6B54C92B-761C-41A3-A8EA-16C72B0DA3BA}.Release|Any CPU.ActiveCfg = Release|Any CPU 135 | {6B54C92B-761C-41A3-A8EA-16C72B0DA3BA}.Release|Any CPU.Build.0 = Release|Any CPU 136 | {4DBC3B24-4A12-4713-8EEF-ECC9CA96A4BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 137 | {4DBC3B24-4A12-4713-8EEF-ECC9CA96A4BF}.Debug|Any CPU.Build.0 = Debug|Any CPU 138 | {4DBC3B24-4A12-4713-8EEF-ECC9CA96A4BF}.Release|Any CPU.ActiveCfg = Release|Any CPU 139 | {4DBC3B24-4A12-4713-8EEF-ECC9CA96A4BF}.Release|Any CPU.Build.0 = Release|Any CPU 140 | {16EDABBA-CB2A-42DE-8ACE-AC920F7A009C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 141 | {16EDABBA-CB2A-42DE-8ACE-AC920F7A009C}.Debug|Any CPU.Build.0 = Debug|Any CPU 142 | {16EDABBA-CB2A-42DE-8ACE-AC920F7A009C}.Release|Any CPU.ActiveCfg = Release|Any CPU 143 | {16EDABBA-CB2A-42DE-8ACE-AC920F7A009C}.Release|Any CPU.Build.0 = Release|Any CPU 144 | {E4053B2D-16C9-4E19-BA5A-573A57BA2E10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 145 | {E4053B2D-16C9-4E19-BA5A-573A57BA2E10}.Debug|Any CPU.Build.0 = Debug|Any CPU 146 | {E4053B2D-16C9-4E19-BA5A-573A57BA2E10}.Release|Any CPU.ActiveCfg = Release|Any CPU 147 | {E4053B2D-16C9-4E19-BA5A-573A57BA2E10}.Release|Any CPU.Build.0 = Release|Any CPU 148 | {99F2F5F3-9EFD-4477-9F02-144C0CB93868}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 149 | {99F2F5F3-9EFD-4477-9F02-144C0CB93868}.Debug|Any CPU.Build.0 = Debug|Any CPU 150 | {99F2F5F3-9EFD-4477-9F02-144C0CB93868}.Release|Any CPU.ActiveCfg = Release|Any CPU 151 | {99F2F5F3-9EFD-4477-9F02-144C0CB93868}.Release|Any CPU.Build.0 = Release|Any CPU 152 | {326DEE46-3CEE-4831-A2CD-B8E71F25FC41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 153 | {326DEE46-3CEE-4831-A2CD-B8E71F25FC41}.Debug|Any CPU.Build.0 = Debug|Any CPU 154 | {326DEE46-3CEE-4831-A2CD-B8E71F25FC41}.Release|Any CPU.ActiveCfg = Release|Any CPU 155 | {326DEE46-3CEE-4831-A2CD-B8E71F25FC41}.Release|Any CPU.Build.0 = Release|Any CPU 156 | {05961BA1-4BBE-40C9-AFEE-AF61AA170A4D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 157 | {05961BA1-4BBE-40C9-AFEE-AF61AA170A4D}.Debug|Any CPU.Build.0 = Debug|Any CPU 158 | {05961BA1-4BBE-40C9-AFEE-AF61AA170A4D}.Release|Any CPU.ActiveCfg = Release|Any CPU 159 | {05961BA1-4BBE-40C9-AFEE-AF61AA170A4D}.Release|Any CPU.Build.0 = Release|Any CPU 160 | {84933CF0-08B2-40AB-9D35-7329BD5D6B0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 161 | {84933CF0-08B2-40AB-9D35-7329BD5D6B0C}.Debug|Any CPU.Build.0 = Debug|Any CPU 162 | {84933CF0-08B2-40AB-9D35-7329BD5D6B0C}.Release|Any CPU.ActiveCfg = Release|Any CPU 163 | {84933CF0-08B2-40AB-9D35-7329BD5D6B0C}.Release|Any CPU.Build.0 = Release|Any CPU 164 | {55B80C22-E7AB-4C79-8FB9-AE2D5134B901}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 165 | {55B80C22-E7AB-4C79-8FB9-AE2D5134B901}.Debug|Any CPU.Build.0 = Debug|Any CPU 166 | {55B80C22-E7AB-4C79-8FB9-AE2D5134B901}.Release|Any CPU.ActiveCfg = Release|Any CPU 167 | {55B80C22-E7AB-4C79-8FB9-AE2D5134B901}.Release|Any CPU.Build.0 = Release|Any CPU 168 | EndGlobalSection 169 | GlobalSection(SolutionProperties) = preSolution 170 | HideSolutionNode = FALSE 171 | EndGlobalSection 172 | GlobalSection(ExtensibilityGlobals) = postSolution 173 | SolutionGuid = {E8D0593B-E448-4939-8A9F-AD068F799469} 174 | EndGlobalSection 175 | EndGlobal 176 | -------------------------------------------------------------------------------- /DesignPattern/FacadeDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace FacadeDesignPattern 5 | { 6 | interface IAadhar 7 | { 8 | void Capture(); 9 | void Validate(); 10 | } 11 | 12 | class BiometricInfo : IAadhar 13 | { 14 | public void Capture() 15 | { 16 | Console.WriteLine("Biometric Info is captured."); 17 | } 18 | 19 | public void Validate() 20 | { 21 | Console.WriteLine("Biometric Info is verified."); 22 | } 23 | } 24 | 25 | class DemographicInfo : IAadhar 26 | { 27 | public void Capture() 28 | { 29 | Console.WriteLine("Demographic Info is captured."); 30 | } 31 | 32 | public void Validate() 33 | { 34 | Console.WriteLine("Demographic Info is verified."); 35 | } 36 | } 37 | 38 | public class AadharFacade 39 | { 40 | DemographicInfo demographicInfo; 41 | BiometricInfo biometricInfo; 42 | 43 | public AadharFacade() 44 | { 45 | demographicInfo = new DemographicInfo(); 46 | biometricInfo = new BiometricInfo(); 47 | } 48 | 49 | public void CreateAadhar() 50 | { 51 | Console.WriteLine("*****Creating Aadhar*****"); 52 | 53 | Thread.Sleep(2000); 54 | demographicInfo.Capture(); 55 | 56 | Thread.Sleep(2000); 57 | demographicInfo.Validate(); 58 | 59 | Thread.Sleep(2000); 60 | biometricInfo.Capture(); 61 | 62 | Thread.Sleep(2000); 63 | biometricInfo.Validate(); 64 | 65 | Thread.Sleep(2000); 66 | Console.WriteLine("*****Aadhar Creation Done*****"); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /DesignPattern/FacadeDesignPattern/FacadeDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/FactoryDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace FactoryDesignPattern 4 | { 5 | 6 | public enum ObjectType 7 | { 8 | Customer, 9 | Supplier 10 | } 11 | 12 | public class CreateFactory 13 | { 14 | public static IFactory GetObject(ObjectType type) 15 | { 16 | IFactory factory = null; 17 | 18 | switch (type) 19 | { 20 | case ObjectType.Customer: 21 | factory = new CustomerFactory(); 22 | break; 23 | 24 | case ObjectType.Supplier: 25 | factory = new SupplierFactory(); 26 | break; 27 | 28 | default: 29 | throw new NotSupportedException(); 30 | 31 | } 32 | 33 | return factory; 34 | } 35 | } 36 | 37 | public interface IFactory 38 | { 39 | string getName(); 40 | } 41 | 42 | public class CustomerFactory : IFactory 43 | { 44 | 45 | public string getName() 46 | { 47 | return "Customer name"; 48 | } 49 | } 50 | 51 | public class SupplierFactory : IFactory 52 | { 53 | 54 | 55 | public string getName() 56 | { 57 | return "Supplier name"; 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /DesignPattern/FactoryDesignPattern/FactoryDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/IOC_DI_Unity/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IOC_DI_Unity 4 | { 5 | public interface IRechargeHandler 6 | { 7 | void DoRecharge(); 8 | 9 | } 10 | public class RechargeJIO : IRechargeHandler 11 | { 12 | public void DoRecharge() 13 | { 14 | Console.WriteLine("Recharge for JIO is done successfully."); 15 | } 16 | } 17 | 18 | public class RechargeVodafone : IRechargeHandler 19 | { 20 | public void DoRecharge() 21 | { 22 | Console.WriteLine("Recharge for Vodafone is done successfully."); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /DesignPattern/IOC_DI_Unity/IOC_DI_Unity.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/IteratorDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace IteratorDesignPattern 6 | { 7 | // Iterator pattern helps you loop through a collection in Aggregate root condition 8 | public class Customer 9 | { 10 | // if we keep private then we can achieve aggregate root design pattern 11 | // if we keep pubilc then we are isolating aggregate root design pattern 12 | private List
addresses = new List
(); 13 | 14 | // Too loop addresses in outside the class then we will implement IEnumerable (Iterator design pattern) 15 | 16 | // IEnumerable is statless iteration and IEnumerator is statefull iteration 17 | public IEnumerable
GetAddresses() 18 | { 19 | // Tolist to create new reference type 20 | return addresses.ToList
(); 21 | } 22 | 23 | public IEnumerator
GetEnumerator() 24 | { 25 | foreach (var val in addresses) 26 | { 27 | //Console.WriteLine($"customer address type : {val.Type}"); 28 | yield return val; 29 | } 30 | } 31 | 32 | 33 | // Any references from outside the aggregate should only go to the aggreate root 34 | public void Add(Address o) 35 | { 36 | foreach (var x in addresses) 37 | { 38 | if (o.Type == x.Type) 39 | { 40 | throw new Exception("Type can not be duplicates"); 41 | } 42 | } 43 | addresses.Add(o); 44 | } 45 | } 46 | 47 | public class Address 48 | { 49 | public int Type { get; set; } 50 | public string Street { get; set; } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /DesignPattern/IteratorDesignPattern/IteratorDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/LazyLoading/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace LazyLoading 5 | { 6 | public static class LazyLoadingFactory 7 | { 8 | private static Lazy> skill = null; 9 | 10 | static LazyLoadingFactory() 11 | { 12 | skill = new Lazy>(() => LoadCustomer()); 13 | } 14 | 15 | private static Dictionary LoadCustomer() 16 | { 17 | Dictionary temp = new Dictionary(); 18 | 19 | temp.Add("javascript", "Requirement matches"); 20 | temp.Add("c#", "Requirement matches"); 21 | 22 | return temp; 23 | } 24 | 25 | public static string Create(string skillType) 26 | { 27 | // Design Pattern : RIP Pattern 28 | return skill.Value[skillType]; 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DesignPattern/LazyLoading/LazyLoading.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/MementoDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using System; 3 | 4 | namespace MementoDesignPattern 5 | { 6 | 7 | public interface IEmployee 8 | { 9 | 10 | string Name { get; set; } 11 | string Department { get; set; } 12 | Employee GetClone(); 13 | Employee Revert(); 14 | 15 | } 16 | 17 | 18 | public interface IMemento 19 | { 20 | Employee GetClone(); 21 | Employee Revert(); 22 | 23 | } 24 | 25 | public class Employee : IEmployee, IMemento 26 | { 27 | private IEmployee employee = null; 28 | public string Name { get; set; } 29 | public string Department { get; set; } 30 | public Employee GetClone() 31 | { 32 | // Memberwise clone creates a fresh object rather than point BYREF 33 | employee = (IEmployee)this.MemberwiseClone(); 34 | return (Employee)employee; 35 | } 36 | 37 | public Employee Revert() 38 | { 39 | // Can implement auto mapper 40 | this.Name = employee.Name; 41 | this.Department = employee.Department; 42 | return this; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DesignPattern/MementoDesignPattern/MementoDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /DesignPattern/ObserverDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace ObserverDesignPattern 5 | { 6 | public interface IObserver 7 | { 8 | void update(string availability); 9 | } 10 | 11 | public interface ISubject 12 | { 13 | void RegisterObserver(IObserver observer); 14 | void RemoveObserver(IObserver observer); 15 | void NotifyObservers(); 16 | } 17 | 18 | public class Observer : IObserver 19 | { 20 | public string UserName { get; set; } 21 | public Observer(string userName, ISubject subject) 22 | { 23 | UserName = userName; 24 | subject.RegisterObserver(this); 25 | } 26 | 27 | public void update(string availabiliy) 28 | { 29 | Console.WriteLine("Hello " + UserName + ", Product is now " + availabiliy + " on Amazon"); 30 | } 31 | } 32 | 33 | public class Subject : ISubject 34 | { 35 | private List observers = new List(); 36 | private string ProductName { get; set; } 37 | private int ProductPrice { get; set; } 38 | private string Availability { get; set; } 39 | public Subject(string productName, int productPrice, string availability) 40 | { 41 | ProductName = productName; 42 | ProductPrice = productPrice; 43 | Availability = availability; 44 | } 45 | 46 | public string getAvailability() 47 | { 48 | return Availability; 49 | } 50 | public void setAvailability(string availability) 51 | { 52 | this.Availability = availability; 53 | Console.WriteLine("Availability changed from Out of Stock to Available."); 54 | NotifyObservers(); 55 | } 56 | public void RegisterObserver(IObserver observer) 57 | { 58 | Console.WriteLine("Observer Added : " + ((Observer)observer).UserName); 59 | observers.Add(observer); 60 | } 61 | public void AddObservers(IObserver observer) 62 | { 63 | observers.Add(observer); 64 | } 65 | public void RemoveObserver(IObserver observer) 66 | { 67 | observers.Remove(observer); 68 | } 69 | public void NotifyObservers() 70 | { 71 | Console.WriteLine("Product Name :" 72 | + ProductName + ", product Price : " 73 | + ProductPrice + " is Now available. So notifying all Registered users "); 74 | Console.WriteLine(); 75 | foreach (IObserver observer in observers) 76 | { 77 | observer.update(Availability); 78 | } 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /DesignPattern/ObserverDesignPattern/ObserverDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/PrototypeDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace PrototypeDesignPattern 4 | { 5 | public class Employee 6 | { 7 | public string Name { get; set; } 8 | public string Department { get; set; } 9 | public Employee GetClone() 10 | { 11 | // Memberwise clone creates a fresh object rather than point BYREF 12 | return (Employee)this.MemberwiseClone(); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /DesignPattern/PrototypeDesignPattern/PrototypeDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/ProxyDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace ProxyDesignPattern 4 | { 5 | public interface IImage 6 | { 7 | void DisplayImage(); 8 | } 9 | 10 | public class RealImage : IImage 11 | { 12 | private string Filename { get; set; } 13 | public RealImage(string filename) 14 | { 15 | Filename = filename; 16 | LoadImageFromDisk(); 17 | } 18 | public void LoadImageFromDisk() 19 | { 20 | Console.WriteLine("Loading Image : " + Filename); 21 | } 22 | public void DisplayImage() 23 | { 24 | Console.WriteLine("Displaying Image : " + Filename); 25 | } 26 | } 27 | 28 | public class ProxyImage : IImage 29 | { 30 | private RealImage realImage = null; 31 | private string Filename { get; set; } 32 | public ProxyImage(string filename) 33 | { 34 | Filename = filename; 35 | } 36 | public void DisplayImage() 37 | { 38 | if (realImage == null) 39 | { 40 | realImage = new RealImage(Filename); 41 | } 42 | realImage.DisplayImage(); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /DesignPattern/ProxyDesignPattern/ProxyDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/ReplaceIfPolymorphismDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace ReplaceIfPolymorphismDesignPattern 5 | { 6 | public static class SimpleFactoryRIP 7 | { 8 | private static Lazy> skill = null; 9 | 10 | static SimpleFactoryRIP() 11 | { 12 | skill = new Lazy>(() => LoadCustomer()); 13 | } 14 | 15 | private static Dictionary LoadCustomer() 16 | { 17 | Dictionary temp = new Dictionary(); 18 | 19 | temp.Add("javascript", "Requirement matches"); 20 | temp.Add("c#", "Requirement matches"); 21 | 22 | return temp; 23 | } 24 | 25 | public static string Create(string skillType) 26 | { 27 | 28 | // Design Pattern : RIP Pattern 29 | return skill.Value[skillType]; 30 | } 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /DesignPattern/ReplaceIfPolymorphismDesignPattern/ReplaceIfPolymorphismDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/RepositoryDesignPattern/Entity.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace RepositoryDesignPattern 9 | { 10 | public class Employee 11 | { 12 | public int EmployeeID { get; set; } 13 | public string Name { get; set; } 14 | public string Gender { get; set; } 15 | public Nullable Salary { get; set; } 16 | public string Dept { get; set; } 17 | } 18 | 19 | public class EmployeeDBContext : DbContext 20 | { 21 | public DbSet Employees { get; set; } 22 | 23 | protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 24 | { 25 | optionsBuilder 26 | .UseSqlServer("Data Source=(local)\\SQLexpress;Initial Catalog=EmployeeDP;Integrated Security=True"); 27 | } 28 | 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /DesignPattern/RepositoryDesignPattern/GenericRepository.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace RepositoryDesignPattern 9 | { 10 | public interface IGenericRepository where T : class 11 | { 12 | IEnumerable GetAll(); 13 | T GetById(object id); 14 | void Add(T obj); 15 | void Update(T obj); 16 | void Delete(object id); 17 | void Save(); 18 | } 19 | 20 | 21 | public class GenericRepository : IGenericRepository where T : class 22 | { 23 | private EmployeeDBContext _context = null; 24 | private DbSet table = null; 25 | public GenericRepository() 26 | { 27 | _context = new EmployeeDBContext(); 28 | table = _context.Set(); 29 | } 30 | public GenericRepository(EmployeeDBContext context) 31 | { 32 | _context = context; 33 | table = _context.Set(); 34 | } 35 | public IEnumerable GetAll() 36 | { 37 | return table.ToList(); 38 | } 39 | public T GetById(object id) 40 | { 41 | return table.Find(id); 42 | } 43 | public void Add(T obj) 44 | { 45 | table.Add(obj); 46 | } 47 | public void Update(T obj) 48 | { 49 | table.Update(obj); 50 | } 51 | public void Delete(object id) 52 | { 53 | T existing = table.Find(id); 54 | table.Remove(existing); 55 | } 56 | public void Save() 57 | { 58 | _context.SaveChanges(); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /DesignPattern/RepositoryDesignPattern/Migrations/20200519155833_init.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Migrations; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | using RepositoryDesignPattern; 9 | 10 | namespace RepositoryDesignPattern.Migrations 11 | { 12 | [DbContext(typeof(EmployeeDBContext))] 13 | [Migration("20200519155833_init")] 14 | partial class init 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.1.4") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("RepositoryDesignPattern.Employee", b => 25 | { 26 | b.Property("EmployeeID") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnType("int") 29 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 30 | 31 | b.Property("Dept") 32 | .HasColumnType("nvarchar(max)"); 33 | 34 | b.Property("Gender") 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("Name") 38 | .HasColumnType("nvarchar(max)"); 39 | 40 | b.Property("Salary") 41 | .HasColumnType("int"); 42 | 43 | b.HasKey("EmployeeID"); 44 | 45 | b.ToTable("Employees"); 46 | }); 47 | #pragma warning restore 612, 618 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /DesignPattern/RepositoryDesignPattern/Migrations/20200519155833_init.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore.Migrations; 2 | 3 | namespace RepositoryDesignPattern.Migrations 4 | { 5 | public partial class init : Migration 6 | { 7 | protected override void Up(MigrationBuilder migrationBuilder) 8 | { 9 | migrationBuilder.CreateTable( 10 | name: "Employees", 11 | columns: table => new 12 | { 13 | EmployeeID = table.Column(nullable: false) 14 | .Annotation("SqlServer:Identity", "1, 1"), 15 | Name = table.Column(nullable: true), 16 | Gender = table.Column(nullable: true), 17 | Salary = table.Column(nullable: true), 18 | Dept = table.Column(nullable: true) 19 | }, 20 | constraints: table => 21 | { 22 | table.PrimaryKey("PK_Employees", x => x.EmployeeID); 23 | }); 24 | } 25 | 26 | protected override void Down(MigrationBuilder migrationBuilder) 27 | { 28 | migrationBuilder.DropTable( 29 | name: "Employees"); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /DesignPattern/RepositoryDesignPattern/Migrations/EmployeeDBContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | using RepositoryDesignPattern; 8 | 9 | namespace RepositoryDesignPattern.Migrations 10 | { 11 | [DbContext(typeof(EmployeeDBContext))] 12 | partial class EmployeeDBContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .HasAnnotation("ProductVersion", "3.1.4") 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 21 | 22 | modelBuilder.Entity("RepositoryDesignPattern.Employee", b => 23 | { 24 | b.Property("EmployeeID") 25 | .ValueGeneratedOnAdd() 26 | .HasColumnType("int") 27 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 28 | 29 | b.Property("Dept") 30 | .HasColumnType("nvarchar(max)"); 31 | 32 | b.Property("Gender") 33 | .HasColumnType("nvarchar(max)"); 34 | 35 | b.Property("Name") 36 | .HasColumnType("nvarchar(max)"); 37 | 38 | b.Property("Salary") 39 | .HasColumnType("int"); 40 | 41 | b.HasKey("EmployeeID"); 42 | 43 | b.ToTable("Employees"); 44 | }); 45 | #pragma warning restore 612, 618 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /DesignPattern/RepositoryDesignPattern/NonGenericRepository.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace RepositoryDesignPattern 9 | { 10 | 11 | public interface IEmployeeRepository 12 | { 13 | IEnumerable GetAll(); 14 | Employee GetById(int EmployeeID); 15 | void Add(Employee employee); 16 | void Update(Employee employee); 17 | void Delete(int EmployeeID); 18 | void Save(); 19 | } 20 | 21 | public class EmployeeRepository : IEmployeeRepository 22 | { 23 | private readonly EmployeeDBContext _context; 24 | public EmployeeRepository() 25 | { 26 | _context = new EmployeeDBContext(); 27 | } 28 | public EmployeeRepository(EmployeeDBContext context) 29 | { 30 | _context = context; 31 | } 32 | public IEnumerable GetAll() 33 | { 34 | return _context.Employees.ToList(); 35 | } 36 | public Employee GetById(int EmployeeID) 37 | { 38 | return _context.Employees.Find(EmployeeID); 39 | } 40 | public void Add(Employee employee) 41 | { 42 | _context.Employees.Add(employee); 43 | } 44 | public void Update(Employee employee) 45 | { 46 | _context.Entry(employee).State = EntityState.Modified; 47 | } 48 | public void Delete(int EmployeeID) 49 | { 50 | Employee employee = _context.Employees.Find(EmployeeID); 51 | _context.Employees.Remove(employee); 52 | } 53 | public void Save() 54 | { 55 | _context.SaveChanges(); 56 | } 57 | 58 | 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /DesignPattern/RepositoryDesignPattern/RepositoryDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | all 12 | runtime; build; native; contentfiles; analyzers; buildtransitive 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /DesignPattern/SingletonDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace SingletonDesignPattern 4 | { 5 | public sealed class Singleton 6 | { 7 | private static readonly Singleton instance = null; 8 | 9 | private Singleton() 10 | { 11 | 12 | } 13 | 14 | 15 | public static Singleton getInstance() 16 | { 17 | 18 | if (instance == null) 19 | { 20 | return new Singleton(); 21 | } 22 | return instance; 23 | 24 | } 25 | 26 | 27 | public void myMethod() 28 | { 29 | Console.WriteLine("This is my singleton method"); 30 | } 31 | 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /DesignPattern/SingletonDesignPattern/SingletonDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/StateDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace StateDesignPattern 4 | { 5 | public interface IVendingMachineState 6 | { 7 | void SelectProductAndInsertMoney(int amount, String productName); 8 | void DispenseProduct(); 9 | } 10 | 11 | public class NoMoneyState : IVendingMachineState 12 | { 13 | public void DispenseProduct() 14 | { 15 | Console.WriteLine("Vending Machine cannot dispense product because money is not inserted and product is not selected"); 16 | } 17 | public void SelectProductAndInsertMoney(int amount, string productName) 18 | { 19 | Console.WriteLine(amount + "Rs has been inserted and " + productName + " has been selected"); 20 | } 21 | } 22 | 23 | public class HasMoneyState : IVendingMachineState 24 | { 25 | public void DispenseProduct() 26 | { 27 | Console.WriteLine("Vending Machine dispensed the product "); 28 | } 29 | public void SelectProductAndInsertMoney(int amount, string productName) 30 | { 31 | Console.WriteLine("Already Vending machine has money and product selected, So wait till it finish the current dispensing process"); 32 | } 33 | } 34 | 35 | public class VendingMachine : IVendingMachineState 36 | { 37 | //Createing a variable to maintain the internal state 38 | public IVendingMachineState vendingMachineState { get; set; } 39 | //Initially the vending machine has NoMoneyState 40 | public VendingMachine() 41 | { 42 | vendingMachineState = new NoMoneyState(); 43 | } 44 | 45 | public void SelectProductAndInsertMoney(int amount, string productName) 46 | { 47 | vendingMachineState.SelectProductAndInsertMoney(amount, productName); 48 | // Money has been inserted so vending Machine internal state 49 | // changed to 'hasMoneyState' 50 | if (vendingMachineState is NoMoneyState) 51 | { 52 | vendingMachineState = new HasMoneyState(); 53 | Console.WriteLine("VendingMachine internal state has been moved to : " 54 | + vendingMachineState.GetType().Name); 55 | } 56 | } 57 | public void DispenseProduct() 58 | { 59 | vendingMachineState.DispenseProduct(); 60 | // Product has been dispensed so vending Machine changed the 61 | // internal state to 'NoMoneyState' 62 | if (vendingMachineState is HasMoneyState) 63 | { 64 | vendingMachineState = new NoMoneyState(); 65 | Console.WriteLine("VendingMachine internal state has been moved to : " 66 | + vendingMachineState.GetType().Name); 67 | } 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /DesignPattern/StateDesignPattern/StateDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/StrategyDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Security.Cryptography; 3 | using System.Text; 4 | 5 | namespace StrategyDesignPattern 6 | { 7 | public abstract class HashingStrategy 8 | { 9 | public abstract string GenerateHash(string text); 10 | } 11 | 12 | public class HashingContext 13 | { 14 | private HashingStrategy _hashingStrategy; 15 | 16 | public HashingContext(HashingStrategy hashingStrategy) 17 | { 18 | _hashingStrategy = hashingStrategy; 19 | } 20 | 21 | public string HashPassword(string text) 22 | { 23 | return _hashingStrategy.GenerateHash(text); 24 | } 25 | } 26 | 27 | public class MD5Hash : HashingStrategy 28 | { 29 | public override string GenerateHash(string text) 30 | { 31 | MD5 md5 = new MD5CryptoServiceProvider(); 32 | 33 | Byte[] bytes; 34 | bytes = ASCIIEncoding.Default.GetBytes(text); 35 | 36 | Byte[] encodedBytes; 37 | encodedBytes = md5.ComputeHash(bytes); 38 | 39 | return BitConverter.ToString(encodedBytes); 40 | } 41 | } 42 | 43 | 44 | public class SHA1Hash : HashingStrategy 45 | { 46 | public override string GenerateHash(string text) 47 | { 48 | SHA1 sha1 = new SHA1CryptoServiceProvider(); 49 | 50 | Byte[] bytes; 51 | bytes = ASCIIEncoding.Default.GetBytes(text); 52 | 53 | Byte[] encodedBytes; 54 | encodedBytes = sha1.ComputeHash(bytes); 55 | 56 | return BitConverter.ToString(encodedBytes); 57 | } 58 | } 59 | 60 | public class SHA256Hash : HashingStrategy 61 | { 62 | public override string GenerateHash(string text) 63 | { 64 | SHA256 sha256 = new SHA256CryptoServiceProvider(); 65 | 66 | Byte[] bytes; 67 | bytes = ASCIIEncoding.Default.GetBytes(text); 68 | 69 | Byte[] encodedBytes; 70 | encodedBytes = sha256.ComputeHash(bytes); 71 | 72 | return BitConverter.ToString(encodedBytes); 73 | } 74 | } 75 | 76 | public class SHA384Hash : HashingStrategy 77 | { 78 | public override string GenerateHash(string text) 79 | { 80 | SHA384 sha384 = new SHA384CryptoServiceProvider(); 81 | 82 | Byte[] bytes; 83 | bytes = ASCIIEncoding.Default.GetBytes(text); 84 | 85 | Byte[] encodedBytes; 86 | encodedBytes = sha384.ComputeHash(bytes); 87 | 88 | return BitConverter.ToString(encodedBytes); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /DesignPattern/StrategyDesignPattern/StrategyDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/TemplateDesignPattern/Class1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace TemplateDesignPattern 4 | { 5 | public abstract class TemplateHiringProcess 6 | { 7 | public void HireFreshers() 8 | { 9 | FirstRoundTest(); 10 | GroupDiscussion(); 11 | TechnicalInterview(); 12 | HR_Interview(); 13 | } 14 | 15 | public abstract void FirstRoundTest(); 16 | 17 | private void GroupDiscussion() 18 | { 19 | Console.WriteLine("Conduct group discussion"); 20 | } 21 | public abstract void TechnicalInterview(); 22 | private void HR_Interview() 23 | { 24 | Console.WriteLine("Conduct HR Interviews"); 25 | } 26 | 27 | } 28 | 29 | public class CSDepartment : TemplateHiringProcess 30 | { 31 | public override void FirstRoundTest() 32 | { 33 | Console.WriteLine("Conduct CS First round test"); 34 | } 35 | 36 | public override void TechnicalInterview() 37 | { 38 | Console.WriteLine("Conduct CS Technical round test"); 39 | } 40 | } 41 | 42 | 43 | public class EEEDepartment : TemplateHiringProcess 44 | { 45 | public override void FirstRoundTest() 46 | { 47 | Console.WriteLine("Conduct EEE First round test"); 48 | } 49 | 50 | public override void TechnicalInterview() 51 | { 52 | Console.WriteLine("Conduct EEE Technical round test"); 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /DesignPattern/TemplateDesignPattern/TemplateDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /DesignPattern/UOWDesignPattern/UOW.cs: -------------------------------------------------------------------------------- 1 | using RepositoryDesignPattern; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace UOWDesignPattern 9 | { 10 | public interface IUnitOfWork 11 | { 12 | IEmployeeRepository employeeRepository { get; } 13 | Task CompleteAsync(); 14 | int Complete(); 15 | } 16 | 17 | 18 | public class UnitOfWork : IUnitOfWork 19 | { 20 | private readonly EmployeeDBContext dbContext; 21 | public UnitOfWork(EmployeeDBContext dbContext) 22 | { 23 | this.dbContext = dbContext; 24 | } 25 | private IEmployeeRepository _employeeRepository; 26 | public IEmployeeRepository employeeRepository 27 | { 28 | get 29 | { 30 | if (this._employeeRepository == null) 31 | { 32 | this._employeeRepository = new EmployeeRepository(dbContext); 33 | } 34 | return this._employeeRepository; 35 | } 36 | } 37 | 38 | public async Task CompleteAsync() 39 | { 40 | return await dbContext.SaveChangesAsync(); 41 | } 42 | public int Complete() 43 | { 44 | return dbContext.SaveChanges(); 45 | } 46 | public void Dispose() => dbContext.Dispose(); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /DesignPattern/UOWDesignPattern/UOWDesignPattern.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp3.1 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /DesignPattern/UOWDesignPattern/UOW_Generic.cs: -------------------------------------------------------------------------------- 1 | using RepositoryDesignPattern; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace UOWDesignPattern 9 | { 10 | public interface IUnitOfWork_Generic where T : class 11 | { 12 | GenericRepository employeeRepository { get; } 13 | Task CompleteAsync(); 14 | int Complete(); 15 | } 16 | 17 | 18 | public class UOW_Generic : IUnitOfWork_Generic where T : class 19 | { 20 | private readonly EmployeeDBContext dbContext; 21 | public UOW_Generic(EmployeeDBContext dbContext) 22 | { 23 | this.dbContext = dbContext; 24 | } 25 | private GenericRepository _employeeRepository; 26 | public GenericRepository employeeRepository 27 | { 28 | get 29 | { 30 | if (this._employeeRepository == null) 31 | { 32 | this._employeeRepository = new GenericRepository(dbContext); 33 | } 34 | return this._employeeRepository; 35 | } 36 | } 37 | 38 | public async Task CompleteAsync() 39 | { 40 | return await dbContext.SaveChangesAsync(); 41 | } 42 | public int Complete() 43 | { 44 | return dbContext.SaveChanges(); 45 | } 46 | public void Dispose() => dbContext.Dispose(); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Amit P Naik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DesignPattern 2 | 3 | Few important design pattern 4 | 5 | ## What are design patterns? 6 | 7 | Design patterns are documented tried and tested solutions for recurring problems in a given context 8 | 9 | ## Main categories of design patterns 10 | 11 | | Creational | Strucural | Behavioral | Other design pattern | 12 | | --- | --- | --- | --- | 13 | | [Abstract Factory](#abstract-factory) | [Adapter](#adapter) | [Chain of Resposibility](#ChainofResposibility) | [Repository design pattern](Repository-design-pattern) | 14 | | [Builder](Builder) | [Bridge](Bridge) | Command | UOW | 15 | | [Factory](#factory-pattern) | [Composite](Composite) | Interpreter | CQRS | 16 | | [Prototype](Prototype) | [Decorator](Decorator) | [Iterator](Iterator) | [Inversion Of Control and Dependency Injection](IOC-DI) | 17 | | [Singleton](#singleton) | [Facade](Facade) | [Mediator](Mediator) | [Lazy Loading](lazy-loading) | 18 | | - | Flyweight | [Memento](Memento) | 19 | | - | [Proxy](#Proxy) | [Observer](#Observer) | 20 | | - | - | [State](#State) | 21 | | - | - | [Strategy](#Strategy) | 22 | | - | - | [Template method](Templatemethod) | 23 | | - | - | Visitor | 24 | 25 | ## Sending Feedback 26 | 27 | For feedback can drop mail to my email address amit.naik8103@gmail.com or you can create [issue](https://github.com/Amitpnk/DesignPattern/issues/new) 28 | 29 | ## Design Principle 30 | 31 | 1. SOLID 32 | 2. YAGNI 33 | 3. KIS 34 | 4. DRY 35 | 36 | ## Design Pattern 37 | 38 | ### Creational Patterns 39 | * Abstract Factory: Creates an instance of several families of classes 40 | * Builder: Separates object construction from its representation 41 | * Factory Method: Creates an instance of several derived classes 42 | * Prototype: A fully initialized instance to be copied or cloned 43 | * Singleton: A class in which only a single instance can exist 44 | 45 | ### Structural Patterns 46 | * Adapter: Match interfaces of different classes 47 | * Bridge: Separates an object’s abstraction from its implementation 48 | * Composite: A tree structure of simple and composite objects 49 | * Decorator: Add responsibilities to objects dynamically 50 | * Façade: A single class that represents an entire subsystem 51 | * Flyweight: A fine-grained instance used for efficient sharing 52 | * Proxy: An object representing another object 53 | 54 | ### Behavioral Patterns 55 | * Mediator: Defines simplified communication between classes 56 | * Memento: Capture and restore an object's internal state 57 | * Interpreter: A way to include language elements in a program 58 | * Iterator: Sequentially access the elements of a collection 59 | * Chain of Resp: A way of passing a request between a chain of objects 60 | * Command: Encapsulate a command request as an object 61 | * State: Alter an object's behavior when its state changes 62 | * Strategy: Encapsulates an algorithm inside a class 63 | * Observer: A way of notifying change to a number of classes 64 | * Template Method: Defer the exact steps of an algorithm to a subclass 65 | * Visitor: Defines a new operation to a class without change 66 | 67 | 68 | ### Singleton 69 | 70 | In this pattern, only one single instance can be created throughout the lifetime of the application. From the starting point of the application to the end of the application execution, only one instance takes care of achieving the functionalities. 71 | 72 | Example: 73 | 74 | 1. Configuration Management 75 | 76 | In case if you need to do configuration at starting of application and throughtout life of application. 77 | 78 | 2. Logging 79 | 80 | Logging is one of the common requirements of the application, where the single logger instance would be available at the start of the application. When we use Log4Net or NLog for logging, a single logger instance will take care of logging 81 | 82 | ### Factory pattern 83 | 84 | In Factory pattern, the factory class wants to decide which subclass will be used to create the objects .This object creation is determined dynamically depending on the situation or condition. 85 | 86 | 1. Different types of reports 87 | 88 | In the application, we have to create different types of reports, like excel, pdf, crystal report etc. Based on the user selection report type, we can create the instance of different report class type. 89 | 90 | 2. Different types of accounts in a banking system 91 | 92 | The banking system has different types of accounts for customers, like Checking account, Saving account, or Loan account. Based on the different types of accounts, being passed from the customer, the specific account instance will be created and the required action will be called from that object 93 | 94 | ### Abstract Factory 95 | 96 | Abstract factory design pattern creates a factory of related or group of objects without explicitly specifying their classes 97 | 98 | ### Adapter 99 | 100 | Adapter pattern is a pattern which is used to match two incompatible interfaces. When we have two different interfaces which do not match, the adapter classes work as a wrapper to match it. 101 | 102 | Example 103 | 104 | Consider we have a third party tool which does not have proper interface or code to fit into our application. To match it, we need to extend the third party code as a wrapper 105 | 106 | ### Observer 107 | 108 | It is the pattern which communicates to the subscribed objects when there is a change in the subject. 109 | 110 | Example 111 | 112 | Customers get notified by email, message in the mobile, hard copy of letter in postal address when the loan application is approved by the bank. 113 | 114 | ### Facade 115 | 116 | When we design a system, we call different services, external service calls, database calls which altogether makes the system complex. Actually, exposing internal calls is not important to the clients. Only client needs to call some code that internally can communicate to the different systems or different services involved in it. 117 | 118 | Façade design pattern is used when a system is very complex or difficult to understand how it is working internally. 119 | 120 | Example 121 | 122 | Different Bank ATM cards Working process in a single ATM Machine. When withdrawing money from an ATM we are not aware of how the transaction happens internally from the same bank or a different bank 123 | 124 | ### Template 125 | 126 | The same business rule applies for the entire client with some variation. The method skeleton can be put in an abstract class and different client implementation can be put in the derived class. 127 | 128 | Example 129 | 130 | In a report generation module, customer requirement is to generate different type of reports either HTMLReport, PDF report , excel report or in a flat file report where the report header , logo and footer keep constant 131 | 132 | ### Chain of Responsibility 133 | 134 | In this pattern we have chains of objects which keep on passing on different chains. 135 | 136 | Loan or Leave approval process where a request passes between multiple stages. We apply for a loan it goes to validation, verification, approval process. In each process there should be a set of rules where they can approve a certain stage or pass the request chain to next immediate chain flow. 137 | 138 | Another example to understand the chain of responsibility is the clarification on the requirement in an agile team. 139 | Development team gets a defect which has a complex algorithm 140 | 141 | Development team reached to QA team, QA team tried to answer the questions but is unable to answer. QA team emailed the questions to product owner. The product owner finally passed the question to stakeholder. The Email chain keeps increasing from developer to QA then Product owner and finally the stakeholder. If any of the chain can know the perfect answer the chain stops there 142 | 143 | ### Decorator 144 | 145 | Decorator pattern attaches additional responsibilities to an object dynamically and transparently. Decorators provide a flexible alternative to sub classing for extending functionality. 146 | 147 | In any organization we see lot of projects, many variety of skill sets depending upon the customer projects. 148 | 149 | 150 | In a MS.NET team different teams are there, but the base skill sets are ASP.NET, C#, SQL Server. 151 | 152 | The new requirement 1 will keep on asking give me candidates with additional skill sets of WCF, WEB API as a top up skills 153 | 154 | The new requirement 2 will keep on asking give me candidates with additional skill sets of WCF, ANGULAR JS as a top up skills 155 | 156 | The new requirement 3 will keep on asking give me candidates with additional skill sets of WEB API, ANGULAR, WCF and ASP.NET MVC top up skills 157 | 158 | On this example, the Base Skill set object gets appended to different skill sets dynamically, based on the project need. 159 | 160 | 161 | ## Credits 162 | 163 | * [C-sharpcorner GOF](https://www.c-sharpcorner.com/article/understanding-gof-design-pattern-with-simple-example/) 164 | --------------------------------------------------------------------------------