35
Session # 5 Muhamad Hesham Know me well to get your goals!

Design Patterns Summer Course 2010-2011 - Session#1

Embed Size (px)

Citation preview

Page 1: Design Patterns Summer Course 2010-2011 - Session#1

Session # 5Muhamad Hesham

Know me well to get your goals!

Page 2: Design Patterns Summer Course 2010-2011 - Session#1

Agenda

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

Page 3: Design Patterns Summer Course 2010-2011 - Session#1

Where are we?

1. Make sure your software does what

customer wants it to do

Great Software

Page 4: Design Patterns Summer Course 2010-2011 - Session#1

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

Page 5: Design Patterns Summer Course 2010-2011 - Session#1

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

Page 6: Design Patterns Summer Course 2010-2011 - Session#1

Guess what?

“The one constant in software development”

Page 7: Design Patterns Summer Course 2010-2011 - Session#1

Guess what?

“The one constant in software development”

CHANGE

Page 8: Design Patterns Summer Course 2010-2011 - Session#1

Why Design Patterns? Job Requirements

Page 9: Design Patterns Summer Course 2010-2011 - Session#1

Pocket Money

You: Dad, I need pocket money.

Page 10: Design Patterns Summer Course 2010-2011 - Session#1

Pocket Money

Angry Dad

Page 11: Design Patterns Summer Course 2010-2011 - Session#1

Pocket Money

Troubled Dad

Page 12: Design Patterns Summer Course 2010-2011 - Session#1

Pocket Money

Happy Dad

Page 13: Design Patterns Summer Course 2010-2011 - Session#1

Pepsi Machine

Page 14: Design Patterns Summer Course 2010-2011 - Session#1

Out of

Pepsi

Pepsi Sold

No Mone

y

Has Mone

y

insert money

eject money

pepsi > 0

pepsi = 0

order pepsi

Pepsi State Machine

Page 15: Design Patterns Summer Course 2010-2011 - Session#1

Platformer Game

Page 16: Design Patterns Summer Course 2010-2011 - Session#1

Enemy State Machine

Running Idle

reach cliff

after a while

Page 17: Design Patterns Summer Course 2010-2011 - Session#1

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

}}

Page 18: Design Patterns Summer Course 2010-2011 - Session#1

Messenger

Page 19: Design Patterns Summer Course 2010-2011 - Session#1

Messenger State Machine

Away

Available

Busy

busy

sign inOfflin

e

away

away

available

busy

available

All states are connected to offline

via sign out

Page 20: Design Patterns Summer Course 2010-2011 - Session#1

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) {

}...…

}

Page 21: Design Patterns Summer Course 2010-2011 - Session#1

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) {

}...…

}

Page 22: Design Patterns Summer Course 2010-2011 - Session#1

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) {

}...…

}

Page 23: Design Patterns Summer Course 2010-2011 - Session#1

Our Code Smells Bad

Violates Open Closed Principle.

Further additions may cause bugs.

State transitions are buried between if-else.

Page 24: Design Patterns Summer Course 2010-2011 - Session#1

Exploring the pattern Let’s Google it!

Page 25: Design Patterns Summer Course 2010-2011 - Session#1

Explaining the pattern Pattern class diagram

Page 26: Design Patterns Summer Course 2010-2011 - Session#1

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.

Page 27: Design Patterns Summer Course 2010-2011 - Session#1

Explaining the pattern Example

Page 28: Design Patterns Summer Course 2010-2011 - Session#1

How they interact?

ContextCurrent-

State

Context

ContextSet Initial State

Foo Foo( )

Page 29: Design Patterns Summer Course 2010-2011 - Session#1

Applicability

What are the symptoms should I take care of ?

Page 30: Design Patterns Summer Course 2010-2011 - Session#1

Applicability

But take care! OVER DESIGN nightmare

Page 31: Design Patterns Summer Course 2010-2011 - Session#1

Design Challenges

Who defines the state transitions? (Classes Dependency)

Page 32: Design Patterns Summer Course 2010-2011 - Session#1

Design Challenges

Creating and destroying State objects (Memory Management)

Page 33: Design Patterns Summer Course 2010-2011 - Session#1

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

Page 34: Design Patterns Summer Course 2010-2011 - Session#1

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?

Page 35: Design Patterns Summer Course 2010-2011 - Session#1

Thank you ...

[email protected]