design patterns summer course 2010-2011 - session#2
TRANSCRIPT
![Page 1: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/1.jpg)
Session #6Muhamad Hesham
Day in Programmer`s Life
![Page 2: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/2.jpg)
Agenda
State vs Strategy Day in Programmer Life GOF visit Project Description
![Page 3: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/3.jpg)
Pattern’s Challenge Are they Twin patterns?
What are the differences between State and Strategy Pattern?
![Page 4: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/4.jpg)
Our Target Game
Learning through Game Programming
Why Game Programming ?
2. Resource intensive software
3. Interactions till death
1.Fun & Interesting
4. Coding non-stopOOD & Design patterns in action
![Page 5: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/5.jpg)
Our Target Game Platformer Skeleton
Main Actors • Level• Player• Enemy• Gem
![Page 6: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/6.jpg)
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 7: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/7.jpg)
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 8: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/8.jpg)
1. We expose private members to the public.
2. We are dependant on certain members.
3. Each time we need modify the level code.
Without correct pattern Our code smells bad!
![Page 9: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/9.jpg)
Call for new requirement
Once the player die I want: Enemies speed is reset. Other gems change its color
to black.
![Page 10: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/10.jpg)
Call for new requirement
Each level can be in one of two styles: Pharonic Aztec
First level is Aztec. Second and third levels are
Pharonic.
![Page 11: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/11.jpg)
First try for implementation
1. We need find where we decide which image we load.
2. Categorize images to 2 sets, Pharonic an Aztec.
![Page 12: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/12.jpg)
Each time we add a new theme
we add piles of if-else statements.
Our class do more than its job
(SCSR).
Without correct pattern Our code smells bad!
![Page 13: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/13.jpg)
Call for new requirement
My nephew has another opinion First level is Pharonic. Second level is Aztec Third level is random.
![Page 14: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/14.jpg)
Call for new requirement
I want the enemy turn crazy when you collect a gem.
If you touch the enemy while it is crazy, you die and it turns idle again.
Increase crazy enemy speed by 4 times.
![Page 15: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/15.jpg)
First try for implementation
What do you think we need do this time ?
![Page 16: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/16.jpg)
Violates Open Closed Principle.
Further additions will cause bugs.
State transitions are buried between if-else.
Without correct pattern Our code smells bad!
![Page 17: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/17.jpg)
Original Enemy State Machine
Running Idle
reach cliff
after a while
![Page 18: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/18.jpg)
Updated Enemy State Machine
Running Idle
reach cliff
after a while
Crazy
gem collectedgem
collected
player died
![Page 19: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/19.jpg)
Call for new requirement
I want …
Go to HELL !!!
![Page 20: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/20.jpg)
GOF Visit: Authors
![Page 21: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/21.jpg)
GOF Visit: Organization
Let`s ask the
authors
![Page 22: Design Patterns Summer Course 2010-2011 - Session#2](https://reader036.vdocument.in/reader036/viewer/2022070318/5577dbd8d8b42a7b7b8b477c/html5/thumbnails/22.jpg)
It is Project Time!