Design Patterns
-
Upload
ashely-burks -
Category
Documents
-
view
23 -
download
0
description
Transcript of Design Patterns
Design Patterns
Now you are ready for Design Patterns
• Design patterns are recurring solutions to software design problems you find again and again in real-world application development. Patterns are about design and interaction of objects, as well as providing a communication platform concerning elegant, reusable solutions to commonly encountered programming challenges.
• The Gang of Four (GoF) patterns are generally considered the foundation for all other patterns. They are categorized in three groups: Creational, Structural, and Behavioral.
Observer pattern(behavioral)
• Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
• http://www.dofactory.com/Patterns/PatternObserver.aspx#_self2
This is the class diagram from the Observer Pattern Example
When would I use the Observer Pattern?
• When you want to notify Investor objects of a change in a Stock object (say the price changed)
• When you want to notify Avatar objects of a change in a Boss object (say the boss is loosing health and you want to make sure all the avatars trying to kill him are notified)
Singleton Pattern(creational)
• Ensure a class has only one instance and provide a global point of access to it.
• http://www.dofactory.com/Patterns/PatternSingleton.aspx#_self2
Singleton Pattern Example Class Diagram
When would l use the Singleton Pattern?
• LoadBalancing object• Only a single instance (the
singleton) of the class can be created because servers may dynamically come on- or off-line
• every request must go through the one object that has knowledge about the state of the (web) farm.
• A game service is a game component that uses an interface to enforce a singleton pattern and make a single instance of a game component accessible to all other components
• A game service is a built in mechanism that allows a game to create and manage a single instance of a component and allows other components access to the service.
• Good candidates for a game service are. – InputHandler – GameConsole – Profiling Service – Scoreboard
• Gamer Services in the XNA Framework– http://msdn.microsoft.com/en-us/
library/microsoft.xna.framework.gamerservices.aspx
Factory Method Pattern(creational)
• Creates an instance of several derived classes• Define an interface for creating an object, but
let subclasses decide which class to instantiate.
• Factory Method lets a class defer instantiation to subclasses.
Here is the class diagram
When would I use the factory pattern?
Website factory• For a myspace or facebook
type of application
Enemy factory• So that you could have
several different enemies in a game
How would I make levels in my game?
The answer is a pattern
• The abstract factory pattern– Creates an instance of several families of classes– Provide an interface for creating families of related
or dependent objects without specifying their concrete classes.
Class Diagram for the Abstract Factory
Homework
• Start working on your Design Patterns Presentation (this is due at the end of the semester)
• Study for the midterm (next week)• Finish(work on) your game. (this is due after
spring break)• Read DesignPatterns-
TheIndieStoneCommunity.pdf