240 likes | 542 Views
xkcd.com. Applied Object-Oriented Design Principles. Jay Hill jayhill@jayhill.net @jittery codequota.com speakerrate.com/ jayhill. Objectives. touch on useful, less prominent design patterns demonstrate implementation and sample use cases leverage language/compiler features
E N D
Applied Object-OrientedDesign Principles Jay Hill jayhill@jayhill.net @jittery codequota.com speakerrate.com/jayhill
Objectives • touch on useful, less prominent design patterns • demonstrate implementation and sample use cases • leverage language/compiler features • draw connections – SOLID & patterns
A Word On Patterns • useful • elegant • extensible • capacity for evil
Single Responsibility Principle (SRP) • Open-Closed Principle (OCP) • Liskov Substitution Principle (LSP) • Interface Segregation Principle (ISP) • Dependency Inversion (DI) LM G/B TFY
Null Object Pattern • Specialization of Special Case Pattern • Useful when consumers of your object/API expect always to get an object
Null Object Pattern • Enumerator • Factory • Fluent interface
Enums Enough public enum ItStarts { OutSimply, Enough, WeJustWant, AListOf, NamedConstants, ThatAreHandy, ToCallAndUse, NotMagicNumbers, ButThen, WeWantMore, AndMoreStill }
Enums Enough [Description(“Slippery Rise-over-Run”)] #dadjoke
Intervention • Admit you have a problem • Step away from the enum • Embrace the power of object • Join a 12-step program
3ish-Step Enum Recovery 1. static members 2. static constructor 3… Everything else • Interface implementations • Instance members • Equality overrides • Inheritance
An enum is more performant by virtue of living on the stack as a value type, rather than a reference type that gets piled on the heap • Bitmask operations (flags) are free with the purchase of any enum
Nested Classes • Are they necessary? • Are they useful? public class Foo { private class Bar { } }
Nested Classes • Ad here to SRP while reducing “class explosion” • Control access & inhertance
Composite Pattern • Intelligent multiplicity, masquerading as a single object • Relies on abstractions • Can avoid cascading refactorings / API changes • Plays nice with factories, like Decorator and Proxy Patterns
Recapitulation • Pattern usage • Null Object Pattern • Nested classes • Leveraging IDisposable + using • enum abuse & how to get help • Composite Pattern
Image Sources • http://blogs.msdn.com/b/expression/archive/2008/03/19/mix08-session-xaml-ready-agency-with-expression-blend.aspx • http://designinformer.com/wallpaper-week-solid/ • http://touchingspiritbearsummerreading.blogspot.com/2009/07/after-reading-july-1st.html • others… whoops
Applied Object-OrientedDesign Principles Jay Hill jayhill@jayhill.net @jittery codequota.com speakerrate.com/jayhill