Design Patterns Summer Course 2010-2011 - Session#1

Preview:

Citation preview

Session # 5Muhamad Hesham

Know me well to get your goals!

Agenda

Why design patterns? Dynamic behaviors everywhere Our pattern exposed Applicability Design Changes Why all of this?

Where are we?

1. Make sure your software does what

customer wants it to do

Great Software

Where are we?

1. Make sure your software does what

customer wants it to do

Great Software

2. Apply basic OO princibles to add

more flexibility

Where are we?

1. Make sure your software does what

customer wants it to do

Great Software

2. Apply basic OO princibles to add

more flexibility

3. Strive for maintainability and

reusable design

Guess what?

“The one constant in software development”

Guess what?

“The one constant in software development”

CHANGE

Why Design Patterns? Job Requirements

Pocket Money

You: Dad, I need pocket money.

Pocket Money

Angry Dad

Pocket Money

Troubled Dad

Pocket Money

Happy Dad

Pepsi Machine

Out of

Pepsi

Pepsi Sold

No Mone

y

Has Mone

y

insert money

eject money

pepsi > 0

pepsi = 0

order pepsi

Pepsi State Machine

Platformer Game

Enemy State Machine

Running Idle

reach cliff

after a while

Coding by Sense®

/// <summary>/// Paces back and forth along a platform, waiting at either end./// </summary>public void Update(GameTime gameTime){ if (State == EnemyState.Idle)

{// If time-out go-to state Running// Else wait for some amount of time

} else if (State == EnemyState.Running) {

// Update enemy position // If reach cliff go-to state Idle

}}

Messenger

Messenger State Machine

Away

Available

Busy

busy

sign inOfflin

e

away

away

available

busy

available

All states are connected to offline

via sign out

Coding by Sense®

/// <summary>/// Occurs when a remote client sends a message/// </summary>public void OnMessageSent(Message msg){ if (State == MessengerState.Aways)

{ } else if (State == MessengerState.Busy) {

}...…

}

Coding by Sense®

/// <summary>/// Occurs when a remote client sends a message/// </summary>public void OnContactSignIn(Message msg){ if (State == MessengerState.Aways)

{ } else if (State == MessengerState.Busy) {

}...…

}

Coding by Sense®

/// <summary>/// Occurs when a remote client sends a message/// </summary>public void OnNudge(Message msg){ if (State == MessengerState.Aways)

{ } else if (State == MessengerState.Busy) {

}...…

}

Our Code Smells Bad

Violates Open Closed Principle.

Further additions may cause bugs.

State transitions are buried between if-else.

Exploring the pattern Let’s Google it!

Explaining the pattern Pattern class diagram

Explaining the pattern Participants

Context defines the interface of interest to clients. maintains an instance of a ConcreteState subclass

that defines the current state. State

defines an interface for encapsulating the behavior associated with a particular state of the Context.

ConcreteState subclasses each subclass implements a behavior associated with

a state of the Context.

Explaining the pattern Example

How they interact?

ContextCurrent-

State

Context

ContextSet Initial State

Foo Foo( )

Applicability

What are the symptoms should I take care of ?

Applicability

But take care! OVER DESIGN nightmare

Design Challenges

Who defines the state transitions? (Classes Dependency)

Design Challenges

Creating and destroying State objects (Memory Management)

What’s Next

State Pattern: Chapter 10 in Head First Strategy Pattern: Chapter 1 in Head First GoF: Behavioral Pattern Section Section: Enhance way of drawing shapes

Further Reading

Finally

Where are our scientists? HF & DS authors. Taha & Elmasri. Previous:▪ Algorithm, Algebra, 1,2,3, Camera, NASA.

Hospital. Nueclar scientist. Stat.

What about efforts? Thomas Edison. Reading books. Ebn Makhld. Quotes.

“No pain, no gain”Anonymous.

براحة “ العلم =ستطاع ُي ال”الجسم

كثير بن ُيحى

“My inventions comes from 2% inspiration and 98% of efforts”Thomas Edison.

“Science is what we understand well enough to explain to a computer. Art is everything else we do”Donald Knuth.

Why all of this?

Thank you ...

Questions?mhesham.fcis@gmail.com