Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Design Patterns Today: Intro to Topic designpatterns.f12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns. What is wrong with this picture? Ball double gravity 32.1 Private: Enemy double gravity 32.1 Private: Don’t repeat yourself!
Design Patterns.
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.
CSSE 374: Introduction to Gang of Four Design Patterns
ECE 452 / CS 446 / SE464 Design Patterns: Part 2 - Answers A Tutorial By Peter Kim Partially based on the tutorial by Michał Antkiewicz.
Software Engineering 1 Object-oriented Analysis and Design Applying UML and Patterns An Introduction to Object-oriented Analysis and Design and Iterative.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Computing IV Singleton Pattern Xinwen Fu.
GoF: Document Editor Example Rebecca Miller-Webster.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
Design Patterns By Mareck Kortylevitch and Piotreck Ratchinsky.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS616: Software Engineering Spring 2009 Design Patterns Sami Taha.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Gang of Four Patterns 23 total 15 useful How are they different from GRASP Patterns?
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 Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Five design principles
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
SOLID Design Principles
StarBuzz Coffee Recipe Boil some water Brew coffee in boiling water Pour coffee in cup Add sugar and milk Tea Recipe Boil some water Steep tea in boiling.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Design I. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out Portability.
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Design Patterns C++ Java C#.
Introduction to Design Patterns
Behavioral Design Patterns
Design Patterns C++ Java C#.
object oriented Principles of software design
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
How to be a Good Developer
Presented by Igor Ivković
Software Engineering Lecture 7 - Design Patterns
Design Patterns in Game Design
CS 350 – Software Design Principles and Strategies – Chapter 14
Presented by Igor Ivković
GoF Patterns Ch. 26.
Presentation transcript:

Design Patterns

Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out Portability Leanness Stratification Standard techniques

Some principles of good design Open-Close Principle Dependency Inversion Principle Interface Segregation Principle Single Responsibility Principle Liskov’s Substitution Principle No forgery principle (keep data in a single place) One rule one place (don’t duplicate code)

What is wrong with this picture? Ball double gravity 32.1 Private: Enemy double gravity 32.1 Private: No forgery!

How about this? Global double gravity 3.21

Why not use globals? A.They make code hard to understand. B.They make code hard to debug. C.They make code hard to modify.

Why not use globals? D.Profs O’Neill and Kuenning with haunt your dreams if you do.

Answer All of the above.

Singleton Pattern Problem: Ensure a class has only one instance and provide a global point of access to that instance.

Singleton Class class Singleton { public: static Singleton* getInstance(); private: static Singleton* theSingletonInstance; Singleton() {}; ~Singleton() {}; Singleton(const Singleton& toCopy) {}; Singleton& operator=(const Singleton& toCopy) {}; }; Singleton::Singleton* theSingletonInstance = NULL;

Instance Implementation Singleton* getInstance() { if (theSingletonInstance == NULL) theSingletonInstance = new Singleton; return theSingletonInstance; }

Example class Ball { public: static Ball* theBall(); tuple getPosition(); void setPosition(tuple newPosition); private: SpheretheSphere; Ball() {}; ~Ball() {}; tuple position; }; Ball::Ball* theBall = NULL; use: Ball* myBall = Ball::getInstance();

what I want I need a 2D graphics library that supports the following functions for triangles: –set color to r,g,b –translate vertices by dx, dy –rotate  degrees about the origin –draw

what I have I have a 3D graphics library with a triangle class with the following interface –triangle() –triangle(v1x, v1y, v1z, v2x, v2y, v2z, v3x, v3y, v3z) –~triangle() –set color(r, g, b) –rotate(vector, angle) –translate(dx, dy, dz) –scale(sx, sy, sz) –draw() –flip(planeA, planeB, planeC, planeD) –texture(textureMap) –standardize()

just use the 3d class Constructor: triangle t(v1x, v1y, 0, v2x, v2y, 0, v3x, v3y, 0) Rotate: t.rotate(,alpha) Interface Segregation Principle

Solution Triangle2DTriangle3D implements the 2d triangle interface

façade Problem: You need to use a subset of a complex system or you need to interact with the system in a particular way.

what I want I want a physics engine that (among other things) detects collisions: cCollision cPhysicsEngine::detectCollision(cPath p, cTriangles t) I have a fast collision detection algorithm and a slower, more robust algorithm.

cPhysicsEngine cPhysicsFast How about this? cPhysicsSlow In the future I may want to use a super slow algorithm.

cPhysicsEnginecDetectCollision cDetectCollisionFast Strategy Design Pattern cDetectCollisionSlow supports open-close and single responsibility principles

Strategy design pattern Problem: Want to be able to swap the algorithm used in an application.

Bridge vs. Strategy Shape Drawer Low Res Hi Res cPhysicsEnginecDetectCollision cDetectCollisionFastcDetectCollisionSlow Different intents: bridge allows implementation to vary and includes adapters strategy allows algorithms (behavior) to vary DP hi res

what I want I am building a drawing program. The user enters keystrokes to change modes (Add, Delete, Move) and mouse input that is interpreted based on the current mode.

what I have global int mode; drawer.processMouse(key,position) { if mode==add processMouseAddMode(key,position) else if mode==delete processMouseDeleteMode(key,position) else if mode==move processMouseMoveMode(key,position) }

Drawer processKey processMouse DeleteDrawerAddDrawerMoveDrawer How about this

Drawer processKey processMouse Mode processMouse DeleteAddMove State Design Pattern 1 supports open-close and single responsibility principles

Drawer processKey processMouse Mode processMouse DeleteAddMove State Design Pattern 1 ModeManager processKey Mode mgr. returns pointer to correct mode

State Design Pattern Problem: want to allow an object to alter its behavior when its internal state changes

State vs. Strategy Drawer processKey processMouse Mode processMouse DeleteAddMove 1 ModeManager processKey cPhysicsEnginecDetectCollision cDetectCollisionFastcDetectCollisionSlow Different intents: state allows behaviors to vary dynamically strategy typically used when algorithm is selected at start

Problem continued I also want to support “Undo” Help!

Command MouseKeyMenu Command Design Pattern

Encapsulate a request as an object to permit logging, queuing, un-doing etc.

what I want I want a 2D drawing program that supports triangle and lines I want to be able to add, delete, draw, and move primitives. I want to be also want to be able to group primitives into a “widget” and treat the widget as a primitive. I want to be able to add and delete primitives from a widget

solution 1 WidgetShape * TriangleLine What is the difference between a triangle and a widget holding a triangle?

Composite Design Pattern Widget Shape * TriangleLine

Abstract factory

Observer Design Pattern

Other design patterns wikipedia!