Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution.

Slides:



Advertisements
Similar presentations
Bad code smells Refactoring Example Game Design Experience Professor Jim Whitehead January 26, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
Advertisements

Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Ch 12: Object-Oriented Analysis
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
Decorator & Chain of Responsibility Patterns Game Design Experience Professor Jim Whitehead February 2, 2009 Creative Commons Attribution 3.0 (Except for.
Computer Science – Game DesignUC Santa Cruz Announcements Website – Readings and sample code updated – Last year’s mid-term exam with answer key – Mid-term.
Plab – Tirgul 12 Design Patterns
Computer Science – Game DesignUC Santa Cruz Today Publish/Subscribe Design Pattern Delegates Sprite movement Particles.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
Computer Science – Game DesignUC Santa Cruz Game Jam Two teams from CMPS 20 – Less Than Royal – Colon Trey.
Computer Science – Game DesignUC Santa Cruz CMPS 20: Game Design Experience Code Organization UML Patterns Code Smells Feb Arnav Jhala.
Chapter 6: Using Design Patterns
Procedural vs Object Oriented Design Bad code smells Game Design Experience Professor Jim Whitehead January 23, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
What is the Chain? It’s a behavioral design pattern. It deals with how objects make requests and how they are handled.
CSE 115 / 503 Introduction to Computer Science For Majors I Carl Alphonce 219 Bell Hall.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Delegates & Events Observer and Strategy Patterns Game Design Experience Professor Jim Whitehead January 30, 2009 Creative Commons Attribution 3.0 creativecommons.org/licenses/by/3.0.
Particle Systems Final Exam Study Guide Game Design Experience Professor Jim Whitehead March 16, 2009 Creative Commons Attribution 3.0 (Except copyrighted.
Inheritance, Method Overloading, Interfaces, Abstract Classes Game Design Experience Professor Jim Whitehead January 28, 2009 Creative Commons Attribution.
Space Partitioning for Broad Sweep Collision Detection Part 1 - Grids Game Design Experience Professor Jim Whitehead February 11, 2009 Creative Commons.
CSE 115 / 503 Introduction to Computer Science For Majors I Carl Alphonce 219 Bell Hall.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
The Decorator Design Pattern (also known as the Wrapper) By Gordon Friedman Software Design and Documentation September 22, 2003.
CSE 115 / 503 Introduction to Computer Science I
Inheritance and Polymorphism CS351 – Programming Paradigms.
CSCI-383 Object-Oriented Programming & Design Lecture 15.
CS1101: Programming Methodology Aaron Tan.
Design Patterns.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
Polymorphism, Inheritance Pt. 1 COMP 401, Fall 2014 Lecture 7 9/9/2014.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Abstract Factory Design Pattern making abstract things.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Programming in Java CSCI-2220 Object Oriented Programming.
Object Oriented Software Development
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
CSC 313 – Advanced Programming Topics. What Is the Factory Method?  Creation details hidden by AbstractCreator  Does effective job of limiting concrete.
CSCI-383 Object-Oriented Programming & Design Lecture 10.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Object Oriented Analysis & Design Game Patterns. Contents  What patterns are  Delegation  Game Loop  Scene Graph  Double Buffering  Component 
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CSCE 240 – Intro to Software Engineering Lecture 3.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
XuanTung Hoang 1 Something to discuss Feedbacks on Midterm Exam Final exam and term project  Final exam requires solid knowledge/skills in Java  Be more.
CSCI 383 Object-Oriented Programming & Design Lecture 15 Martin van Bommel.
CompSci 108 Midterm Review. Some exams are about challenging the way you think… But not this one.
Where are we ? Setup Sprites Input Collision Drawing Sprites
Behavioral Design Patterns
Object-Orientated Programming
Software Engineering Lecture 7 - Design Patterns
Decorator Pattern Richard Gesick.
Presentation transcript:

Exam Questions Chain of Responsibility & Singleton Patterns Game Design Experience Professor Jim Whitehead February 4, 2009 Creative Commons Attribution 3.0 (Except for copyrighted images) creativecommons.org/licenses/by/3.0

Announcements Homework #1, Revisited (Improved Design) ► Due next Monday, February 9 ► Make sure you ask in section this week whether your design is procedural, object-oriented, or other

Midterm Exam The midterm exam is this Friday ► Will cover material up to and including Monday’s class (does not include today’s lecture) Will give list of potential questions on following slides Mostly short answer questions

Potential Exam Topics As Univ. of California students, you are expected to be able to assess complex material and make judgments concerning its relative importance. That said, it can be helpful to have some input from the Professor to help focus studying activity. The following are questions/topics that are likely, but not guaranteed to appear on the exam. Anything covered in class or in the assigned readings may appear, even if not explicitly mentioned today.

Potential Exam Questions C# language ► Basic syntax ► Foreach ► Properties ► Delegates ► Lists ► Arrays ► Visibility rules ► Enumerations ► Interfaces and Abstract classes Know how to use these, and difference between these Abstract methods ► Be prepared to read and understand C# code examples, and to write short segments of C# code

Potential Exam Questions Object-oriented design ► Know the difference between procedural and object- oriented design Know the indicators of a pure procedural design ► Know the nouns/verbs rule of thumb for determining classes and methods from a problem description Refactoring ► What is a bad code smell? How is it related to refactoring? ► Know the bad code smells covered in class Duplicate code, long method, long class, switch, feature envy Be able to define these, and identify these in a code sample Know how to refactor these to remove them

Potential Exam Questions Be able to define classification ► Know how classification relates to the is-a relationship ► Know how abstraction relates to classification UML structure diagrams ► Know how to represent a class in UML ► Know how to represent inheritance (is-a) and aggregation (contains) relationships among classes ► Given some C# code, be able to draw a UML diagram for it

Potential Exam Questions XNA ► What is a spritebatch? ► What is a clock tick? How long is the default clock tick in XNA? ► What is the difference between Update() and Draw() methods? ► Be able to understand code that receives input from an Xbox controller Design patterns ► Know UML diagrams for each of the design patterns covered in class Strategy, Observer, Decorator Need to memorize these, and be able to reproduce them Need to understand how they work Given an example of one these patterns, be able to identify the pattern ► Know what these mean: Favor composition over inheritance Write to an interface, not an implementation

Problem: Magic system with variable effects Consider a game that has castles, fighters, wizards, and magic cars ► Fighters can be inside castles ► Wizards have spells: Some only affect the castle, weakening its walls (lightening bolt) Other, more powerful spells, weaken the walls and do damage to the people inside (fireball explosion) If the people inside the castle are inside a magic car, the car absorbs some, but not all, of the damage Need some way to have the applied damage vary depending on who is inside what ► E.g., damage depends on containment

An approach: Chain of Spell Handlers Create objects that ► Represent a cast spell ► Represent castles, fighters, cars Create a chain of object references that represent containment ► Castle –references-> Car –references-> Fighter Pass each spell object along the containment chain ► Castle handles spell first, absorbs some damage, reduces power of spell, and passes it along to Car ► Car handles spell, reduces power of spell (no damage – it’s magic), and passes along to Fighter ► Fighter handles spell last, applying damage

Chain of Spell Handlers Chain of object references looks like Decorator chain ► Containment of castle(cars(fighters)) Separate object represents request (spell) fighter car castle fireball power = 100 HP = 500 HP = 100 HP = 50 Fireball spell handed to castle for processing first

Chain of Spell Handlers Chain of object references looks like Decorator chain ► Containment of castle(cars(fighters)) Separate object represents request (spell) fighter car castle fireball power = 25 HP = 425 HP = 100 HP = 50 Fireball spell handed to castle for processing first Castle reduces its HP, and reduces power of fireball

Chain of Spell Handlers Chain of object references looks like Decorator chain ► Containment of castle(cars(fighters)) Separate object represents request (spell) fighter car castle fireball power = 10 HP = 425 HP = 100 HP = 50 Castle hands Fireball spell off to car (next in chain), which reduces spell power

Chain of Spell Handlers Chain of object references looks like Decorator chain ► Containment of castle(cars(fighters)) Separate object represents request (spell) fighter car castle fireball power = 0 HP = 425 HP = 100 HP = 40 Car hands Fireball spell off to fighter (next in chain), which applies remaining spell power as damage. Fighter is end of chain, so no further handoffs.

Chain of Responsibility Pattern IHandler interface ► Successor: reference to next object in the chain ► Request(): method that receives request object (e.g. spell), and takes action on it Handler1, Handler2 ► Specific handler implementations (e.g., castle, car, fighter) ► Each handles requests in a different way Demonstration of Chain of Responsibility in Visual C#

Implementation details Request is often represented as an object instance ► In this case, need to add IRequest interface and Request1…RequestN implementations to UML diagram +Request(IRequest r) Request1Request2 // Request state > IRequest

Chain of Responsibility: Pros and Cons Benefits ► Reduces coupling between requests, and objects that handle requests Would be very easy to add a new character class (say, a thief) and have it handle spells in a class-specific way, without modifying the spell class ► Dynamic modification of request handling Can change the chain of request handlers at runtime For example, as fighters enter/exit cars, castles, etc. Drawback ► Handling isn’t guaranteed Since it is up to each object in the chain whether to take action, there is no global guarantee that anyone will handle a request

Problem: Representing Game State In most computer games, there is some state associated with the entire game ► Current level number ► Pointer to level data ► Time elapsed while playing ► High score during this play session Want one and only one instance of the object holding this state Also want to easily gain access to this state anywhere in the game ► Avoid need for long data passing chains

Approach: Singleton Pattern Singleton pattern ensures only one instance is ever made of a class ► Wait.. How is this possible? Can’t I have my code just call “new” as many times as I want? ► Not if the constructor is private! Ensures that only code within the class can call the constructor ► OK, so now I can’t call new, which means there is no way to ever create an instance, right? ► Wrong – a private class variable (“instance”) in the Singleton is initialized by calling the Singleton’s constructor ► Great, but how do I get access to “instance” if it’s private? ► Use a public static property (“UniqueInstance”). You can always call this since it’s static.

Singleton Implementation Constructor is private by default In C#, the first call on any methods, properties or constructors of a class causes its class variables to be set up. ► First access to the UniqueInstance property causes variable “instance” to be initialized by calling constructor ► Hence, first access to UniqueInstance causes new instance to be created, then returns a reference to it ► Clever! Sealed keyword means no subclasses are possible public sealed class Singleton { // Private Constructor Singleton() { } // Private object initialized by calling private // constructor static readonly Singleton instance = new Singleton(); // Public static property is used to retrieve // reference to the Singleton instance public static Singleton UniqueInstance { get { return instance; } } Code from C# 3.0 Design Patterns, p Originally from:

Singleton Advantages Pro: ► Ensures only one instance of class is ever created ► Can get access to data in this class from anywhere Con: ► Acts like global variables ► Can cause methods to have unintended side-effects, due to data sharing via state in the singleton

Homework Read in C# 3.0 Design Patterns ► pages (Factory Method and Singleton patterns)