DEV Community

CodingBlocks

Episode 11 – Design Patterns Part 1 – You Create Me!

This week we're tackling the first section of seminal Design Patterns book: Creational Patterns. We discuss factories of factories, "bullet hell" games, pathological liars, and Allen's lack of voice. Big thanks to @rajsotweet for calling us out and getting us motivated to record! Points of Interest .NET Framework 4.5.2 is out! ASP vNext Awesome stats from Kaspersky! Entity Framework 7 PowerShell Module Design Rules (and When to Bend Them) NuGet package that integrates PowerShell into MSBuild as a first-class citizen Strongly Named Assemblies in Nuget Fun Stuff! Rogue Code Influx Watch_Dogs   Why study design patterns? Famous "Gang of Four" book published in 1994, still in first edition! "They help make a system independent of how its objects are created, composed, and represented" Promote composition over inheritance (smaller, more focused classes that can be recomposed into different functionality - Do one thing well philosophy) Patterns divided Into 3 Parts Creational Patterns Structural Patterns Behavioral Patterns Main Creational Patterns Factory Methods Factories Builders Prototypes Singletons Factories and Factory Methods Patterns "Nobody really buys hammers anymore." Centralize object creation Decouples class creation from logic When do you know that you need a factory? Real world example: AWS SDK Reflection of Control Builder Pattern Great way to build complex objects Real world examples: StringBuilder, UriBuilder Prototype Pattern Cloning at runtime! Prototype Example   Real world examples: ICloneable and JavaScript Singleton Pattern One object to coordinate actions across the system Globally scoped, often to avoid dependency passing/injecting Mandate their own creation/lifecycle/persistance Single instance of a class (restricts the instantiation of a class to one object) Different implementations: Static Classes, Inner Objects / Double Check Locking, Lazy Any chance your constructor will fail? Don't use static constructors! Singletons are Pathological Liars Outlaw vs Skeet, static vs double checked locking singletons Additional Resources Gang of Four Book Exploring the Factory Design Pattern C# in Depth, 3rd Edition by Jon Skeet Design Patterns in the .Net Framework Design Patterns Library Tips / Tricks Bookmarks in Visual Studio Debugging Object Initializers (again, oops!) Click a word in your Visual Studio editor window, once the word highlights, use Ctrl + F3 to find the next instance of the word in the file Clean Code recommended using static methods instead of constructor overloads: static methods instead of constructor overloads Color.FromHex(“#ffff00”) instead of Color(“#ffff00”) Thanks for listening!

Episode source