5 Classic Patterns Presented by: Michael Woo d In Everyday Code [email protected] Strategic Data System Centerville, OH ttp://mvwood.com @mikewo on Twitter ArcTrax
Dec 22, 2014
5 Classic PatternsPresented by:
Michael WoodIn Everyday Code
Strategic Data SystemsCenterville, OH
http://mvwood.com@mikewo on Twitter
ArcTrax
What are Patterns?
Now for something COMPLETELY different!
Design Principles
SOLID
ingle Responsibility Principle
pen / Close Principle
iskov Substitution Principle
nterface Segregation Principle
ependency Inversion Principle
Now back to your regularly scheduled presentation.
Types of Patterns
Creational
Structural
Behavioral
Adapter Pattern
Adapter PatternClient IHuman
HumanWookieAdapter
Wookie
.Growl()
.Speak()
.Speak()
Object Adapter
Adapter Pattern
Decorator Pattern
Decorator PatternIceCreamIngredient
.Cost
VanillaIce
CreamBananasChocolate
Fudge
.Cost = $3.50
.50 ¢.50 ¢
public override decimal Cost(){ return _internalIngredient.Cost() + .50M;}
Decorator PatternWait, isn’t this just wrapping
objects like the adapter pattern?
Decorator PatternWhat are some downsides with this pattern?
Factory Patterns
Simple Factory PatternLaptop laptop;switch (brand){ case “dell”: laptop = new DellLaptop(); break; case “trashiba”: laptop = new ToshibaLaptop(); break;}
laptop.BlueScreen();LaptopFactory laptopFactory = new LaptopFactory();Laptop laptop = laptopFactory.BuildLaptop(brand);Laptop.BlueScreen();
Public class LaptopFactory { public Laptop BuildLaptop(string brand) { Laptop laptop; switch (brand) { case “dell”: return new DellLaptop(); case “trashiba”: return new ToshibaLaptop(); } } }
Hold up! Didn’t you just move the problem to another class?
Factory Method PatternShipyard .CreateShip()
UsNavyShipyard CorellianShipyard
Okay, so what what’s the difference between the simple factory and an instance of one of the
derived factory classes? The both seem to do the
same thing.
Abstract Factory Pattern
Universe
.CreateHero()
.CreateVillian()
.CreateCity()
DcUniverse MarvelUniverse
BatmanJokerGotham
WolverineMagnetoNew York
Factory Patterns
Command Pattern
Should it matter?
Command Pattern
RemoteOffCommand
Command
.Execute()
GarageDoorOpenCommand.Execute()
GarageDoor
.Open()
.Close()
This reminds me of the adapter pattern. We wrapped an object and
pointed to a method.
Command Pattern
Design Patterns provide general, reusable solutions to a commonly occurring problem in software design
Remember
Strategic Data SystemsCenterville, OH
Michael [email protected]
http://mvwood.com
@mikewo on Twitter
ArcTrax
ResourcesPresentation Materials
http://shrinkster.com/15z6
Pattern Resource – Do Factory http://dofactory.com
Patterns in Enterprise Software (Martin Fowler)http://shrinkster.com/15z8
Books:Design Patterns (Gang of Four book)Head First Design Patterns by Freeman & FreemanRefactoring to Patterns by Kerievsky
More Resources can be found in the presentation materials.