Architecture Patterns Architecture: the Big Picture

Architecture Patterns Architecture: the Big Picture

the gamedesigninitiative at cornell university Lecture 11 Architecture Patterns Architecture: The Big Picture the gamedesigninitiative 2 Architecture Patterns at cornell university Architecture: The Big Picture the gamedesigninitiative 3 Architecture Patterns at cornell university Utilizing Software Patterns Pattern: reusable solution to a problem Typically a template, not a code library Tells you how to design your code Made by someone who ran into problem first In many cases, pattern gives you the interface List of headers for non-hidden methods 2110 all Specification for non-hidden methods over again Only thing missing is the implementation the gamedesigninitiative 4 Architecture Patterns at cornell university Example: Singletons Goal: Want to limit class to a single instance Do not want to allow users to construct new objects But do want them to access the single object Application: Writing to the console/terminal Want a unique output stream to write to console Many output streams would conflict w/ each other Given by a unique object in Java (System.out) A class with static methods in C# (not a singleton) the gamedesigninitiative 5 Architecture Patterns at cornell university Creating a Singleton in Java public class Singleton { public static final Singleton instance = new Singleton(); private Singleton() { // Initialize all fields for instance } public static Singleton getInstance() { return instance; } } the gamedesigninitiative 6 Architecture Patterns at cornell university Creating a Singleton in Java public class Singleton { public static final Singleton instance = new Singleton(); private Singleton() { Provide as an immutable constant // InitializeKeep userall fields from for instance } instantiating public static Singleton getInstance() { return instance; Static method is an } alternative to providing } access with a constant the gamedesigninitiative 7 Architecture Patterns at cornell university Architecture Patterns Essentially same idea as software pattern Template showing how to organize code But does not contain any code itself Only difference is scope Software pattern: simple functionality Architecture pattern: complete program Classic pattern: Model-View-Controller (MVC) Most popular pattern in single client applications the gamedesigninitiative 8 Architecture Patterns at cornell university Model-View-Controller Pattern Controller Calls the • Updates model in methods of response to events • Updates view with model changes Model View • Defines/manages • Displays model the program data to the user/player • Responds to the • Provides interface controller requests for the controller the gamedesigninitiative 9 Architecture Patterns at cornell university Example: Temperature Converter Model: (TemperatureModel.java) Stores one value: fahrenheit But the methods present two values View: (TemperatureView.java) Constructor creates GUI components Recieves user input but does not “do anything” Controller: (TemperatureConverter.java) Main class: instantiates all of the objects “Communicates” between model and view the gamedesigninitiative 10 Architecture Patterns at cornell university TemperatureConverter Example View Controller TemperatureConverter @105dc TemperatureModel fahrenheit 32.0 double Model getFahrenheit() setFahrenheit(double) getCentigrade() setCentrigrade(double) the gamedesigninitiative 11 Architecture Patterns at cornell university The Game Loop and MVC Model: The game state Value of game resources Location of game objects Update View: The draw phase Focus of upcoming lectures Draw Controller: The update phase Alters the game state Topic of previous lecture the gamedesigninitiative 12 Architecture Patterns at cornell university Model-Controller Separation (Standard) Model Controller Store/retrieve object data Process user input Limit access (getter/setter) Determine action for input Preserve any invariants Example: mouse, gamepad Only affects this object Call action in the model Implements object logic Complex actions on model Traditional controllers May affect multiple models are “lightweight” Example: attack, collide the gamedesigninitiative 13 Architecture Patterns at cornell university Classic Software Problem: Extensibility Given: Class with some base functionality Might be provided in the language API Might be provided in 3rd party software Goal: Object with additional functionality Classic solution is to subclass original class first Example: Extending GUI widgets (e.g. Swing) But subclassing does not always work… How do you extend a Singleton object? the gamedesigninitiative 14 Architecture Patterns at cornell university Problem with Subclassing Games have lots of classes Each game entity is different NPC Needs its own functionality (e.g. object methods) Human Orc Want to avoid redundancies Makes code hard to change Common source of bugs Human Human Orc Orc Warrior Archer Warrior Archer Might be tempted to subclass Common behavior in parents Redundant Behavior Specific behavior in children the gamedesigninitiative 15 Architecture Patterns at cornell university Problem with Subclassing Games have lots of classes Each game entity is different NPC Needs its own functionality (e.g. object methods) Warrior Archer Want to avoid redundancies Makes code hard to change Common source of bugs Human Orc Human Orc Warrior Warrior Archer Archer Might be tempted to subclass Common behavior in parents Redundant Behavior Specific behavior in children the gamedesigninitiative 16 Architecture Patterns at cornell university Model-Controller Separation (Standard) Model NPC Store/retrieve object data Limit access (getter/setter) Preserve any invariants Human Orc Only affects this object Implements object logic Human Human Orc Orc Warrior Archer Warrior Archer Complex actions on model May affect multiple models Example: attack, collide Redundant Behavior the gamedesigninitiative 17 Architecture Patterns at cornell university Model-Controller Separation (Alternate) Model Controller Store/retrieve object data Process game actions Limit access (getter/setter) Determine from input or AI Preserve any invariants Find all objects effected Only affects this object Apply action to objects Process interactions In this case, models Look at current game state are lightweight Look for “triggering” event Apply interaction outcome the gamedesigninitiative 18 Architecture Patterns at cornell university Does Not Completely Solve Problem Code correctness a concern Can I flee? Methods have specifications Must use according to spec Check correctness via typing Find methods in object class Example: orc.flee() Check type of parameters Example: force_to_flee(orc) Logical association with type Even if not part of class the gamedesigninitiative 19 Architecture Patterns at cornell university Issues with the OO Paradigm Object-oriented programming is very noun-centric All code must be organized into classes Polymorphism determines capability via type OO became popular with traditional MVC pattern Widget libraries are nouns implementing view Data structures (e.g. CS 2110) are all nouns Controllers are not necessarily nouns, but lightweight Games, interactive media break this paradigm View is animation (process) oriented, not widget oriented Actions/capabilities only loosely connected to entities the gamedesigninitiative 20 Architecture Patterns at cornell university Programming and Parts of Speech Classes/Types are Nouns Actions are Verbs Methods have verb names Capability of a game object Method calls are sentences Often just a simple function subject.verb(object) damage(object) subject.verb() collide(object1,object1) Classes related by is-a Relates to objects via can-it Indicates class a subclass of Example: Orc can-it flee Example: String is-a Object Not necessarily tied to class Example: swapping items Objects are class instances the gamedesigninitiative 21 Architecture Patterns at cornell university Possible Solution: Decorator Pattern New Functionality OriginalReference to Original Request Decorator Object Functionalitybase object Object the gamedesigninitiative 22 Architecture Patterns at cornell university Java I/O Example InputStream input = System.in; Built-in console input Reader reader = new InputStreamReader(input); Make characters easy to read BufferedReader buffer = new BufferedReader(reader); Read whole line at a time Most of java.io " works this way the gamedesigninitiative 23 Architecture Patterns at cornell university Alternate Solution: Delegation Pattern Original Reference to Request Delegate Object delegate Object 1 Forward Request Inversion of the Decorator Pattern the gamedesigninitiative 24 Architecture Patterns at cornell university Alternate Solution: Delegation Pattern Original Reference to Request Delegate Object delegate Object 12 Forward Request Inversion of the Decorator Pattern the gamedesigninitiative 25 Architecture Patterns at cornell university Example: Sort Algorithms public class SortableArray extends ArrayList{! private Sorter sorter = new MergeSorter();! new QuickSorter(); public void setSorter(Sorter s) { sorter = s; }! public void sort() {! Object[] list = toArray();! public interface Sorter { sorter.sort(list);! public void sort(Object[] list); clear(); ! } for (o:list) { add(o); }! }! }! the gamedesigninitiative 26 Architecture Patterns at cornell university Comparison of Approaches Decoration Delegation Pattern applies to decorator Applies to original object Given the

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    35 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us