5. Design Patterns

Transcription

SENG 454Game Design PatternsAssoc.Prof.Dr. Tansel Dökeroğlu

Books2

Books3

Syllabus of the lecture4

Definition of a design pattern A general, reusable solution to a commonly occurringproblem within a given context in software design. A description or template for how to solve a problem thatcan be used in many different situations. Formalized best practices that the programmer can use tosolve common problems when designing an application orsystem.5

Definition of a design pattern Not a finished design that can be transformed directly intosource or machine code. Design patterns show relationships and interactions betweenclasses or objects, without specifying the final applicationclasses or objects that are involved. Only suitable for object-oriented languages.6

Applications of design patterns Speed up the development process by providing tested,proven development paradigms. Improves code readability for coders and architects familiarwith the patterns. Design patterns provide general solutions, not specific tosome problems. Allows developers to communicate using well-known, wellunderstood names for software interactions.7

Good for solving software structural problems AbstractionEncapsulationInformation hidingSeparation of concernsCoupling and cohesionSeparation of interface and implementationSingle point of referenceDivide and conquer8

Solving non-functional software problems TestabilityReusabilityMaintenance

Patterns, Architectures & FrameworksFrameworks are partially completed software systems thatmay be targeted at a particular type of application.These are tailored by completing the unfinished components.Architectures model software structure at the highestpossible level, and give the overall system view.An architecture can use many different patterns in differentcomponentsSoftware Design Patterns are more like small-scale or localarchitectures for architectural components or sub-components10

Categorization of Classical Design Patterns Creational: Creating an object in a flexible way.Separate creation from operation/use. Structural: Compose objects to obtain newfunctionality. Behavioral: Related with communication betweenobjects.11

Creational Design PatternsFactory : providing an interface for creating objects in a superclass,but allows subclasses to alter the type of objects that will becreated.Builder: constructing complex objects step by step.The pattern allows you to produce different types andrepresentations of an object using the same construction code.12

Creational Design PatternsPrototype: copying existing objects without making your codedependent on their classes. Some of the object’s fields may beprivate and not visible from outside of the object itself.Singleton: Ensuring that a class has only one instance, whileproviding a global access point to this instance. some of the object’sfields may be private and not visible from outside of the object itself.13

Structural Design PatternsAdapter: allows objects with incompatible interfaces to collaborate.Bridge: splitting a large class or a set of closely related classes intotwo separate hierarchies—abstraction and implementation—whichcan be developed independently of each other.Composite: composing objects into tree structures and then workwith these structures as if they were individual objects.14

Structural Design PatternsDecorator: attaching new behaviors to objects by placing theseobjects inside special wrapper objects that contain the behaviors.Facade: providing a simplified interface to a library, a framework, orany other complex set of classes.Flyweight: fitting more objects into the available amount of RAM bysharing common parts of state between multiple objects instead ofkeeping all of the data in each object.15

Behavioral Design PatternsCommand: turning a request into a stand-alone object thatcontains all information about the request. This transformationlets you parameterize methods with different requests, delayor queue a request’s execution, and supportundoable operations.16

Behavioral Design PatternsIterator: traversing elements of a collection without exposing itsunderlying representation (list, stack, tree, etc.).Mediator: reducing chaotic dependencies between objects.The pattern restricts direct communications between the objectsand forces them to collaborate only via a mediator object.17

Behavioral Design PatternsObserver: defining a subscription mechanism to notify multipleobjects about any events that happen to the objectthey’re observing.Template: defining the skeleton of an algorithm in the superclassbut lets subclasses override specific steps of the algorithm withoutchanging its structure.Visitor: separating algorithms from the objects on whichthey operate.18

Behavioral Design PatternsState: letting an object alter its behavior when its internal statechanges. It appears as if the object changed its class. This pattern canbe observed in a vending machine. Vending machines have statesbased on the inventory, amount of currency deposited, the ability tomake change, the item selected, etc.Strategy: defining a family of algorithms, put each of them into aseparate class, and make their objects interchangeable.19

Game Design Patterns Sequencing Patterns: time — the artificial world lives andbreathes at its own rthyme. We must invent time and craft thegears that drive our game’s clock. Behavioral (game): quickly define and refine a large quantity ofmaintainable behavior. Decoupling Patterns : we mean a change in one usually doesn’trequire a change in the other. When you change some feature inyour game, the fewer places in code you have to touch, the easierit is. Optimization Patterns: Optimizing for performance is a deep artthat touches all aspects of software.20

Sequencing Patterns Double Buffer Game Loop Update MethodDecoupling Patterns Component Event Queue Service LocatorBehavioral Patterns Bytecode Subclass Sandbox Type ObjectOptimization Patterns Data Locality Dirty Flag Object Pool Spatial Partition

Game Design Patterns Sequencing Patterns: time —the artificial world lives and breathes at its own rthyme. We must invent time and craft the gears that drive our game'sclock. Behavioral (game): quickly define and refine a large quantity of maintainable behavior. Decoupling Patterns : we mean a change in one usually doesn't