24
Lecture # 2 Ahmed Safwat Abdelrahman Z. Al-Ogail Muhamad Hesham

Design Patterns Summer Course 2009-2010 - Session#2

Embed Size (px)

Citation preview

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

Lecture # 2Ahmed Safwat

Abdelrahman Z. Al-OgailMuhamad Hesham

Page 2: Design Patterns Summer Course 2009-2010 - Session#2

Call for a new requirement. First try for implementation. Bad smell code. Meet observer pattern. Pattern class diagram. Second try for implementation.(developing

pattern in C#). Common uses. What’s next? (Further reading).

Page 3: Design Patterns Summer Course 2009-2010 - Session#2

Call for new requirement

Once the player collects a gem I want:

Enemy increases its speed.

Other gems change its color to a random color.

Page 4: Design Patterns Summer Course 2009-2010 - Session#2

First try for implementation

1. We need find the place in code where

the gem is tested for being collected.

2. We need modify enemy speed.

3. We need modify gem color.

Page 5: Design Patterns Summer Course 2009-2010 - Session#2

What bad things we did ?

1. We expose private

members to the public.

2. We are dependant on

certain members.

3. Each time we need modifythe level code.

Our code smells bad!

Page 6: Design Patterns Summer Course 2009-2010 - Session#2

Real life example

Do you know how news paper subscribtion work ?

Page 7: Design Patterns Summer Course 2009-2010 - Session#2

SubscribersNot notified

Publisher

Publisher + Subscriber

Page 8: Design Patterns Summer Course 2009-2010 - Session#2

Subscribers

Subscribe

Publisher

Publisher + Subscriber

Page 9: Design Patterns Summer Course 2009-2010 - Session#2

Subscribers

Publisher

Publisher + Subscriber

Page 10: Design Patterns Summer Course 2009-2010 - Session#2

Subscribers

I am not interested anymore

Publisher

Publisher + Subscriber

Page 11: Design Patterns Summer Course 2009-2010 - Session#2

SubscribersNot notified

Publisher

Publisher + Subscriber

Page 12: Design Patterns Summer Course 2009-2010 - Session#2

Discovering Pattern Let’s Google it!

Page 13: Design Patterns Summer Course 2009-2010 - Session#2

Explaining the Pattern See how it’s easy!

Page 14: Design Patterns Summer Course 2009-2010 - Session#2

Class Diagram Source: GoF’s Book

Page 15: Design Patterns Summer Course 2009-2010 - Session#2

Implementation Developing the pattern in C#

See how we develop a pattern!

Page 16: Design Patterns Summer Course 2009-2010 - Session#2

Call for new requirement

Once the player die I want:

Enemies speed is reset.

Other gems change its color to black.

Page 17: Design Patterns Summer Course 2009-2010 - Session#2

Just pattern it

1. We need find the place in code where

the gem is player is tested for being dead.

2. We need reset enemy speed.

3. We need modify color gem with

black.

Page 18: Design Patterns Summer Course 2009-2010 - Session#2

Common Uses Source: GoF’s Book

Spreadsheets, Bar Graph and Pie Graph presentations of the same data.

Chat Windows in IMs.

RSS and News-feed.

In Smalltalk programming language MVC

Model: subject

View: observers

Page 19: Design Patterns Summer Course 2009-2010 - Session#2

What’s Next? Wait a minute! What about the rest

Patterns explanation in GoF

Pattern Name and Classification: A descriptive and unique name that helps in identifying and referring to the pattern

Intent: A description of the goal behind the pattern and the reason for using it.

Also Known As: Other names for the pattern

Motivation (Forces): A scenario consisting of a problem and a context in which this pattern can be used.

Page 20: Design Patterns Summer Course 2009-2010 - Session#2

What’s Next? Wait a minute! What about the rest

Patterns explanation in GoF

Consequences: A description of the results, side effects, and trade offs caused by using the pattern.

Sample Code: An illustration of how the pattern can be used in a programming language

Known Uses: Examples of real usages of the pattern.

Related Patterns: Other patterns that have some relationship with the pattern; discussion of the differences between the pattern and similar patterns

Page 21: Design Patterns Summer Course 2009-2010 - Session#2

What’s Next? Not Covered points

Design Advantages of using this pattern:

Objects are loosely coupled.

Adding new observers at any time without effort.

Changes in subject or observer don’t effect the other

Page 22: Design Patterns Summer Course 2009-2010 - Session#2

How they attended Lessons? How they managed their time?

Reading while eating & walking

Eating Ka`ak

Results:

Bagdad Library

Saheb Abn Abbad`s Library

▪ Lon Gorand talking about his books 400 Hajri.

It is about times

Page 23: Design Patterns Summer Course 2009-2010 - Session#2

What’s Next Books to read

Head First – Design Patterns: Chapter # 2

GoF – Behavioral Patterns Section