Design Patterns. What is wrong with this picture? Ball double gravity 32.1 Private: Enemy double gravity 32.1 Private: Don’t repeat yourself!

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

CS 350 – Software Design The Bridge Pattern – Chapter 10 Most powerful pattern so far. Gang of Four Definition: Decouple an abstraction from its implementation.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Figures – Chapter 7.
Design Patterns Pepper. Find Patterns Gang of Four created 23 Siemens published another good set x
Chapter 7 – Object-Oriented Design
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Design Patterns Today: Intro to Topic designpatterns.f12.ppt CS 121 “Ordering Chaos” “Mike” Michael A. Erlinger.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Chapter 22 Object-Oriented Design
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
More OOP Design Patterns
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Design Patterns Ric Holt & Sarah Nadi U Waterloo, March 2010.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns Discussion of pages: xi-11 Sections: Preface, Forward, Chapter
Design Patterns.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
Chapter 7 – Design and Implementation Lecture 2 1Chapter 7 Design and implementation.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
CSSE 374: Introduction to Gang of Four Design Patterns
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Design Patterns CSCI 5801: Software Engineering. 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.
Design Patterns. Properties of good design Minimize complexity Maintainable Loose coupling Extensibility Reusability High fan in Low to medium fan out.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 5150 Software Engineering Lecture 16 Program Design 3.
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.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Software Design Refinement Using Design Patterns
Design Patterns: Brief Examples
Chapter 5 – Design and Implementation
Chapter 7 – Object-Oriented Design
GoF Patterns (GoF) popo.
MPCS – Advanced java Programming
Behavioral Design Patterns
Observer Design Pattern
object oriented Principles of software design
Presented by Igor Ivković
Introduction to Design Patterns
Informatics 122 Software Design II
Presented by Igor Ivković
Presentation transcript:

Design Patterns

What is wrong with this picture? Ball double gravity 32.1 Private: Enemy double gravity 32.1 Private: Don’t repeat yourself!

How about this? Global double gravity 3.21 We’re not using a constant because we want to support different worlds with different gravitational forces.

Why not use globals? A.They make code hard to debug. B.They make code hard to change. C.Profs O’Neill and Kuenning will haunt your dreams if you do. Answer: All of the above This is a Design Pattern....

Design Patterns A.Description of the problem. B.Essence of a solution C.Description of accumulated knowledge. D.Book: Design Patterns, by gang of 4. E.Web page devoted to software Design Patterns:

Elements of a Design Pattern Name that is meaningful Description of the problem area that explains when the pattern may be applied A solution description of the parts of the design solution, their relationships, and their responsibilities. –not a concrete design description, but a template –many times expressed graphically - UML Statement of the consequences of applying the pattern –results and trade-offs –used to indicate applicability

The Observer pattern Pattern nameObserver DescriptionSeparates the display of the state of an object from the object itself and allows alternative displays to be provided. When the object state changes, all displays are automatically notified and updated to reflect the change. Problem description In many situations, you have to provide multiple displays of state information, such as a graphical display and a tabular display. Not all of these may be known when the information is specified. All alternative presentations should support interaction and, when the state is changed, all displays must be updated. This pattern may be used in all situations where more than one display format for state information is required and where it is not necessary for the object that maintains the state information to know about the specific display formats used. Solution description This involves two abstract objects, Subject and Observer, and two concrete objects, ConcreteSubject and ConcreteObject, which inherit the attributes of the related abstract objects. The abstract objects include general operations that are applicable in all situations. The state to be displayed is maintained in ConcreteSubject, which inherits operations from Subject allowing it to add and remove Observers (each observer corresponds to a display) and to issue a notification when the state has changed. The ConcreteObserver maintains a copy of the state of ConcreteSubject and implements the Update() interface of Observer that allows these copies to be kept in step. The ConcreteObserver automatically displays the state and reflects changes whenever the state is updated. The UML model of the pattern is shown in Figure ConsequencesThe subject only knows the abstract Observer and does not know details of the concrete class. Therefore there is minimal coupling between these objects. Because of this lack of knowledge, optimizations that enhance display performance are impractical. Changes to the subject may cause a set of linked updates to observers to be generated, some of which may not be necessary.

Observer Pattern: Multiple displays

UML model of the Observer pattern

Using Design Patterns Is a design process –develop a design –experiencing a problem –recognizing that an existing pattern can be used –most difficult step is the need for a taxonomy of Design Patterns

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

Singleton Gravity Class class Gravity { public: static Gravity* getInstance(); double getGravity(); private: static Gravity* theGravityInstance; Gravity() {}; ~Gravity) {}; Gravity(const Gravity& toCopy) {}; Gravity& operator=(const Gravity& toCopy) {}; }; Gravity::Gravity* theGravityInstance = NULL; users can ask for a pointer to THE instance and then get the value for gravity the constructor is private

Implementation Gravity* getInstance() { if (theGravityInstance == NULL) theGravityInstance = new Gravity; return theGravityInstance; } int getGravity() { return double(3.21) } Singletons provide the functionality of global variables without the problems

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

façade A facade is an object that provides a simplified interface to a larger body of code, such as a class library. A facade can make a software lib easier to use, understand and test. A facade can make code more readable because it has convenient methods for common tasks A facade can reduce dependencies of outside code on library inner workings. A facade is a wrapper

Triangle2D Triangle3D tuple3D vertices[3] triangle() triangle(tuple3D v[3] ~triangle() set color(r, g, b) rotate(vector, angle) translate(dx, dy, dz) scale(sx, sy, sz) draw() set z=0 2d rotate about origin, use 3d rotate about etc. Triangle 2D is special case of Triangle 3D

facade: Triangle2D Triangle3D tuple3D vertices[3] triangle() triangle(tuple3D v[3]) ~triangle() set color(r, g, b) rotate(vector, angle) translate(dx, dy, dz) scale(sx, sy, sz) draw() Triangle2D Triangle3D* myTriangle triangle() triangle(tuple2D v[3]) ~triangle() set color(r, g, b) rotate(angle) translate(dx, dy) scale(sx, sy) draw() MORE INTUITIVE, Hides the 3D interface.

Strategy design pattern Problem: Want to be able to swap the algorithm used in an application at runtime Solution: Strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable Strategy DP lets the algorithm vary independently from the clients that use it.

cPhysicsEngine cPhysicsFast I want to support two (or more) different collision detection algorithms. cPhysicsSlow In the future I may want to use a super slow algorithm. Encapsulate change

UML of Strategy DP

cPhysicsEnginecDetectCollision cDetectCollisionFast Strategy Design Pattern cDetectCollisionSlow supports several design principles: encapsulate change open-closed principle single responsibility principles favor composition over inheritance

Bridge Pattern Problem: Want to decouple implementation from abstraction so they can change independently A teacher (Communicator object) can talk to any kid (talkable object), and a kid should also allow any teacher (Communicator object) to start talking to him/her as well. Just like how printers work with computers. If we have a USB cable (bridge), then we can connect any printer to any computer to start printing. It really doesn't matter if it's a laser printer or a color printer, either Windows PC or Mac. Because we know all the printers will allow the computers to print, makes sense?

Bridge Shape draw() Graphics package DirectX adapter OpenGL adapter OpenGLDirectX Circle draw() Rectangle draw() Decouple abstraction from implementation abstracts interface wrappers

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

PaintProgram processKey processMouse Mode processKey processMouse Eraser Spray Paint Fill State Design Pattern 1 supports open-closed, encapsulate change, single responsibility principles

PaintProgram processKey processMouse Mode processKey processMouse Eraser Spray Paint Fill State Design Pattern 1 ModeManager processKey processMouse Mode mgr. returns pointer to correct mode, i.e., Key or Mouse handling function

Command Design Pattern Encapsulate a request as an object to permit logging, queuing, un-doing etc. an object is used to represent and encapsulate all the info needed to call a method at a later time. Includes method name, object that owns that method and values for any parms,.

Command MouseKeyMenu Command Design Pattern

MVC Design Pattern Model-view-Controller The pattern isolates “domain logic” (application logic for the user) from the user interface permitting independent development, testing and maintenance of each (separation of concerns) user interacts with user interface, e.g., button controller converts into understandable action for the model controller tells model of user action (model state may change) view queries model to generate user interface; view gets its date from model. view may render itself or is notified by model of changes in state that require screen update

MVC supports high cohesion and low coupling

Other design patterns wikipedia books