ALTERNATE UNIVERSE DEV

CodingBlocks

Episode 16 – Design Patterns Part 2 – Oh behave!

This week we're following up on our episode about talking about Creational Design Patterns a few of our favorite behavioral patterns: Template. Strategy, and Null Object. Also, pumpkin spice lattes, Mario's pants, and a billion dollar mistake. Points of Interest Pumpkin Spice Latte is back! Great feedback from NDepend, great things coming in V6! LG Electronics UM65 34UM65 34-Inch Screen LED-Lit Monitor Thank you for all the awesome reviews! - Special thanks to @hyperspacial! Check out Allen's videos on SQL joins Goodbye YUI Events Build Guild Atlanta Atlanta Code Camp SingalR and the Internet of Things! If no-one has already coined the terms strinteger, strool, stratetime and my favourite strecimal then let me be the first — Lee Englestone (@LeeEnglestone) September 2, 2014 Template Pattern (Hollywood Principle) Uses an abstract class with implementation of application logic but uses subclasses overridden method calls to actually execute the program Game example: Abstract game class with playOneGame() initializeGame(), makeMove(), isEndOfGame, printWinner() Examples in .NET ASP Page life cycle Aspectacular Powershell Commandlets Strategy Pattern (aka Policy Pattern) Encapsulates a family of algorithms, so put the algorithm in a class, and pass that class as an argument at run timeThese classes must meet a common interface, or have a common parent (yuck) Comparisons in ColorMine The alternative is switch statements, harder to extend - gets messy w/ different initialization parameters Examples in .NET Encryption Linq - Order By Dependency Injection Null Object Pattern "I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years." - Sir Charles Antony Richard Hoare When to use? Abstracting null from client Strategy pattern implementation (Algorithms can be selected on the fly) Command pattern implementation (Command objects encapsulate an action and its parameters) Also considered a special case of: Strategy pattern Command pattern State pattern (A clean way for an object to partially change its type at runtime) Benefits Clean code - who needs a null check? no if else required Less if's means lower complexity -- Remember NDepend's cyclomatic complexity score? Who care what was returned, just do stuff! Interfaces make for good Null Objects, because of comparisons and memory savings! Resources We Like Design Patterns Library - Pluralsight Gang of Four Book Tips / Tricks Go listen to the MSDev Show! ObjectQuery.Include Method SELECT * INTO newTableName from tableName WHERE 1=0 Follow @sqlkris!

Episode source