5 Classic Patterns In Everyday Code

Preview:

DESCRIPTION

Patterns are an important tool to use as architects and developers. They provide a common vocabulary for us to design with, as well as a common approach to a common problem. Come learn about the five most useful patterns, and how to use them in your everyday code.

Citation preview

5 Classic PatternsPresented by:

Michael WoodIn Everyday Code

mike.wood@sds-consulting.com

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

Strategic Data SystemsCenterville, OH

Michael Woodmike.wood@sds-consulting.com

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.

mike.wood@sds-consulting.com

Recommended