Top Banner
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
35

5 Classic Patterns In Everyday Code

Dec 22, 2014

Download

Technology

Michael Wood

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.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 5 Classic Patterns In Everyday Code

5 Classic PatternsPresented by:

Michael WoodIn Everyday Code

[email protected]

Strategic Data SystemsCenterville, OH

http://mvwood.com@mikewo on Twitter

ArcTrax

Page 2: 5 Classic Patterns In Everyday Code

What are Patterns?

Page 3: 5 Classic Patterns In Everyday Code
Page 4: 5 Classic Patterns In Everyday Code
Page 5: 5 Classic Patterns In Everyday Code

Now for something COMPLETELY different!

Page 6: 5 Classic Patterns In Everyday Code

Design Principles

Page 7: 5 Classic Patterns In Everyday Code

SOLID

ingle Responsibility Principle

pen / Close Principle

iskov Substitution Principle

nterface Segregation Principle

ependency Inversion Principle

Page 8: 5 Classic Patterns In Everyday Code

Now back to your regularly scheduled presentation.

Page 9: 5 Classic Patterns In Everyday Code

Types of Patterns

Creational

Structural

Behavioral

Page 10: 5 Classic Patterns In Everyday Code

Adapter Pattern

Page 11: 5 Classic Patterns In Everyday Code

Adapter PatternClient IHuman

HumanWookieAdapter

Wookie

.Growl()

.Speak()

.Speak()

Object Adapter

Page 12: 5 Classic Patterns In Everyday Code

Adapter Pattern

Page 13: 5 Classic Patterns In Everyday Code

Decorator Pattern

Page 14: 5 Classic Patterns In Everyday Code

Decorator PatternIceCreamIngredient

.Cost

VanillaIce

CreamBananasChocolate

Fudge

.Cost = $3.50

.50 ¢.50 ¢

public override decimal Cost(){ return _internalIngredient.Cost() + .50M;}

Page 15: 5 Classic Patterns In Everyday Code

Decorator PatternWait, isn’t this just wrapping

objects like the adapter pattern?

Page 16: 5 Classic Patterns In Everyday Code

Decorator PatternWhat are some downsides with this pattern?

Page 17: 5 Classic Patterns In Everyday Code

Factory Patterns

Page 18: 5 Classic Patterns In Everyday Code

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(); } } }

Page 19: 5 Classic Patterns In Everyday Code

Hold up! Didn’t you just move the problem to another class?

Page 20: 5 Classic Patterns In Everyday Code

Factory Method PatternShipyard .CreateShip()

UsNavyShipyard CorellianShipyard

Page 21: 5 Classic Patterns In Everyday Code

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.

Page 22: 5 Classic Patterns In Everyday Code

Abstract Factory Pattern

Universe

.CreateHero()

.CreateVillian()

.CreateCity()

DcUniverse MarvelUniverse

BatmanJokerGotham

WolverineMagnetoNew York

Page 23: 5 Classic Patterns In Everyday Code

Factory Patterns

Page 24: 5 Classic Patterns In Everyday Code

Command Pattern

Page 25: 5 Classic Patterns In Everyday Code
Page 26: 5 Classic Patterns In Everyday Code
Page 27: 5 Classic Patterns In Everyday Code

Should it matter?

Page 28: 5 Classic Patterns In Everyday Code

Command Pattern

RemoteOffCommand

Command

.Execute()

GarageDoorOpenCommand.Execute()

GarageDoor

.Open()

.Close()

Page 29: 5 Classic Patterns In Everyday Code

This reminds me of the adapter pattern. We wrapped an object and

pointed to a method.

Page 30: 5 Classic Patterns In Everyday Code

Command Pattern

Page 32: 5 Classic Patterns In Everyday Code
Page 33: 5 Classic Patterns In Everyday Code
Page 34: 5 Classic Patterns In Everyday Code

Strategic Data SystemsCenterville, OH

Michael [email protected]

http://mvwood.com

@mikewo on Twitter

ArcTrax

Page 35: 5 Classic Patterns In Everyday Code

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.

[email protected]