Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

43
Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    216
  • download

    0

Transcript of Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Page 1: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Design Patterns in Action

Finding and Using Patterns in Software

David KaplinNYU Masters Student

Page 2: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Overview

● Problem Domain (the Games!)– Voronoi– Nanomunchers

● Introduction to Design Patterns● Patterns in Action (Heurgame Framework)

Page 3: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

The Voronoi Game

● Each player has N stones to place on a grid● Once a stone is placed it cannot be removed

as long as the owner is in the game● a Voronoi diagram is a tessellation of a plane

into polygons– Every stone is in the interior of one polygon– For every point x in the polygon P associated

with stone s, x is closer to s than it is to any other stone s'.

– Distance is based on Euclidean distance.● Play is turn based● Largest area wins

Page 4: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

All Players placed their first stones

Page 5: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Blue moved

Page 6: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Dark Green Moved

Page 7: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Dark Orange Moved

Page 8: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

An advanced two player game...

Page 9: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Adversarial Nanomunchers● You and your opponents each have 2

nanomunchers.● Nanomunchers eat nodes. Nodes are

connected via at most 4 edges.● Nanomunchers move in a predetermined

order {Up, Down, Right, Left}.● If it is not possible to move in one direction

the next one in its list is used.● If there is no available node, a nanomuncher

dies of starvation.● If two nanomunchers advance on the same

node the one with a better direction wins

Page 10: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Nanomunchers

● At the start of the game all the information you have is how the nodes are connected

● You know nothing of your adversaries● The only decisions you can make are

– Where to place each nanomuncher– What program to run on each nanomuncher

Page 11: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.
Page 12: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.
Page 13: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.
Page 14: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.
Page 15: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.
Page 16: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.
Page 17: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.
Page 18: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Room For Variation

● What if alternating turns in Voronoi was not fair?

● What if you had three nanomunchers?● What would change if you had eight

directions instead of four in nanomunchers?

Abstraction minimizes these worries!

Page 19: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Patterns in ActionThe Heurgame Framework

● What is Heurgame?A set of abstractions and utilities that should allow greater flexibility in building game software.

● What Heurgame isn'tHeurgame is not a Game Engine!

Page 20: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Heurgame Framework Overview

Game Logic

Event System

NetworkSystem

UserInterface

Logging System

Page 21: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

A Warning

There is no silver bullet in building software.

There is no single “right” way.

Page 22: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

What is a Design Pattern?

“A Design Pattern is a description of communicating objects and classes that are customized to solve a general design problem in a particular context” - Design Patterns, GoF

Design Patterns allow a commonly used shorthand for existing object-oriented structures.

Page 23: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Reading Patterns

● Unified Modeling Language (UML)– Visual representation of OO Systems– Class Diagram– Collaboration Diagram– and many others...

● Problem Domain● Patterns associated● Pros● Cons

Page 24: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Common Design Patternsand where to find them

● Standard Library Patterns– Iterator – Observer – Factory

● User Interface Libraries– Command– State– Memento– Flyweight

• Compilers● Visitor● Composite● Builder

Page 25: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Networking System

● Designed to be protocol agnostic● GameServer● Referee

– Deals with the Players via a PlayerProxy● PlayerProxy

– Uses the Proxy Pattern to allow both Socket Connected Players and Players directly interfacing with the server to be treated equally

RefereePlayerProxy “Bob”

PlayerProxy “Alice”

Pythonsocket

Human

Rest ofArchitect

ure

Page 26: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Proxy Pattern

● Use to restrict or control access to objects● Relies on Delegation and Composition● Also used where simple references are not

enough

Page 27: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Game Logic

● Game – Provides the SystemAnalyzer● SystemAnalyzer – Global Analysis● MoveAnalyzer – Local Analysis● Referee – Relies on the Move Analyzer● Close to the Mediator Pattern

Game SystemAnalyzer

MoveAnalyzer

Referee

Could be many classes

Page 28: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Mediator Pattern

● Encapsulates how objects interact– One Mediator– Many Colleagues

● Emphasis on loose coupling

Page 29: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Using Mediator

● Referee obtains a reference to the SystemAnalyzer

● For each turn a TurnBasedReferee uses the MoveAnalyzer to see if the move is valid(out of bounds)

● The MoveAnalyzer in turn will query the SystemAnalyzer for wider conditions (duplicate move, illegal move)

● The only established relationship is that the System Analyzer has at least one Move Analyzer(who has a back reference)

Page 30: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Using Mediator

● There are no restrictions on the Number of System Analyzers or Move Analyzers

● More complex games may call for entire systems of classes to be involved.– Nanomunchers

SystemAnalyzer knows a graph, the graph knows nodesa BotWrangler, that knows the botsthe bots know where they can movethe MoveAnalyzer talks to the SystemAnalyzer

Page 31: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Event System

● Anywhere there are events you will find the Observer Pattern

● Two Pieces– Subject – knows the Observers

aka Broadcaster– Observer – Acts on the notification

aka Listener

Page 32: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Observer Pattern

● Also known as Publish and Subscribe● Very loose coupling is desired● Observer is a very common pattern

Page 33: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Event System II

● TurnIterator generates a sequence of turn events in a particular order.

● This is a direct combination of the Subject of the Observer Pattern being crossed with the Iterator Pattern.

Page 34: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Hybrid Iterator-Observer

● Traditionally, Iterator spans an existing set of items

● Imagine a “Lazy Iterator” ... a Generator

Page 35: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

The TurnIterator

● Isolates the Turn mechanism of the game● Allows greater flexibility than a for loop

– RandomTurn Generators– RoundRobinTurnIterator

The standard way of iterating turns– SecondChanceRRTurnIterator

In Voronoi the Second player is at a disadvantage, it can be proved that the first player can always win under strict alternation.

Solution: Give the second player two first turns.

Page 36: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

User Interface

● Primarily on the Listener/Observer side of the Event System

● Examples:● ScoreBoard - Updates on PlayerEvents and

TurnEvents● EstimatedTime – Updates on TimeEvents● Complex UI will use the Builder Pattern

(in development)– Three Parts: Director, Builder, Product

Page 37: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Builder Pattern

● Encapsulate the construction process while removing coupling to the actual product

Page 38: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Proposed Builder Pattern

● Abstract away mundane aspects of layout and certain repeated tasks

Page 39: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Logging System

● Loosely Coupled● LogReaders, LogWriters, Log, DefaultLog● Too much duplicated Code!● Facade Pattern to the Rescue: LogBox

LogBox----------

AddEntryAddUrgent

LogWriter

LogReader

LogGame

Interface

Page 40: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Facade

● Simple interface to complex system

Page 41: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

Facade in Action

Page 42: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

More Information

● Design Patterns : Elements of Resusable Object-Oriented Software– Authors “Gang of Four” (GoF)– Erich Gamma, Richard Helm, Ralph Johnson,

John Vlissides– http://hillside.net/patterns/DPBook/DPBook.html

● UML http://www.uml.org● UML for Patterns

http://www.tml.hut.fi/~pnr/Tik-76.278/gof/html/

● Heurgame Framework [BETA]– http://www.cs.nyu.edu/~dbk1/heurgame/

Page 43: Design Patterns in Action Finding and Using Patterns in Software David Kaplin NYU Masters Student.

The Games

● Heurgame Implementations● Voronoi

– http://www.cs.nyu.edu/~dbk1/voronoi/● Nanomunchers

– http://www.cs.nyu.edu/~dbk1/nanomunchers/