Design Patterns

15
Design Patterns

description

Design Patterns. Now you are ready for Design Patterns. - PowerPoint PPT Presentation

Transcript of Design Patterns

Page 1: Design  Patterns

Design Patterns

Page 2: 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.

Page 3: Design  Patterns

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

Page 4: Design  Patterns

This is the class diagram from the Observer Pattern Example

Page 5: Design  Patterns

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)

Page 6: Design  Patterns

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

Page 7: Design  Patterns

Singleton Pattern Example Class Diagram

Page 8: Design  Patterns

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

Page 9: Design  Patterns

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.

Page 10: Design  Patterns

Here is the class diagram

Page 11: Design  Patterns

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

Page 12: Design  Patterns

How would I make levels in my game?

Page 13: Design  Patterns

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.

Page 14: Design  Patterns

Class Diagram for the Abstract Factory

Page 15: Design  Patterns

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