150 likes | 273 Vues
Design patterns are essential solutions to recurring software design problems found in real-world application development. They focus on the design and interaction of objects while providing a platform for elegant and reusable programming solutions. Key patterns include the foundational Gang of Four designs, categorized into Creational, Structural, and Behavioral patterns. Examples like the Observer, Singleton, and Factory Method patterns illustrate practical applications in scenarios such as notifying objects of state changes or creating single instances of classes, streamlining development processes across various applications.
E N D
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
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
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.
When would I use the factory pattern? Website factory Enemy factory So that you could have several different enemies in a game • For a myspace or facebook type of application
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.
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