Design Patterns David Talby. This Lecture n What is it all about? n Abstract Factory n Composite n Strategy.

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Design Patterns David Talby. This Lecture Representing other objects Proxy, Adapter, Façade Re-routing method calls Chain of Responsibility Coding partial.
Plab – Tirgul 12 Design Patterns
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
Design Patterns David Talby. This Lecture n Patterns for a User Interface u Representing a Document F Composite, Flyweight, Decorator u Writing Portable.
Design Patterns David Talby. This Lecture n The Creational Patterns u Builder u Prototype u Factory Method u (Abstract Factory) u Singleton n Choosing.
Design Patterns David Talby. This Lecture n The Creational Patterns u Abstract Factory u Builder u Prototype u Factory Method n Choosing Between Them.
Design Patterns David Talby. This Lecture Handle Synchronization & Events Observer Simplify Complex Interactions Mediator Change Behavior Dynamically.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
The Composite Pattern.. Composite Pattern Intent –Compose objects into tree structures to represent part-whole hierarchies. –Composite lets clients treat.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Marcelo Santos – OOAD-CDT309, Spring 2008, IDE-MdH 1 Object-Oriented Analysis and Design - CDT309 Period 4, Spring 2008 Design Patterns: someone has already.
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Chapter 3.4 Programming Fundamentals. 2 Data Structures Arrays – Elements are adjacent in memory (great cache consistency) – They never grow or get reallocated.
Design Patterns David Talby. This Lecture n The rest of the pack u Working over a network F Proxy, State, Chain of Responsibility u Working with external.
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.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
Design Patterns David Talby. This Lecture n Re-Routing Method Calls u Proxy, Chain of Responsibility n Working with external libraries u Adapter, Façade.
Implementing Design Patterns Using Java St. Louis Java Special Interest Group Eric M. Burke Object Computing, Inc. Presented on July 9, 1998 (updated July.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
1 GoF Template Method (pp ) GoF Strategy (pp ) PH Single User Protection (pp ) Presentation by Julie Betlach 6/08/2009.
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.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Strategy Design Patterns CS 590L - Sushil Puradkar.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
GoF: Document Editor Example Rebecca Miller-Webster.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Object Oriented Design David Talby. Welcome! n Introduction n UML u Use Case Diagrams u Interaction Diagrams u Class Diagrams n Design Patterns u Composite.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Strategy Pattern.
OO Methodology Elaboration Iteration 2 - Design Patterns -
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Design Patterns David Talby. This Lecture Re-routing method calls Chain of Responsibility Coding partial algorithms Template Method The Singleton Pattern.
Design Patterns Introduction
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
Design Patterns. Outline Purpose Purpose Useful Definitions Useful Definitions Pattern Overview Pattern Overview.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
March 10, 2007 Design Patterns David Talby.
Design Patterns David Talby. 11/20/2016Object Oriented Design Course2 Contents Representing Data Structures Composite Flyweight Decorator Traversing Data.
1/23/2018 Design Patterns David Talby.
4/16/2018 Design Patterns David Talby.
Design Patterns: MORE Examples
Strategy: A Behavioral Design Pattern
Design Patterns: Brief Examples
Design Patterns Lecture part 2.
Introduction to Design Patterns
Jim Fawcett CSE776 – Design Patterns Summer 2003
Informatics 122 Software Design II
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Introduction to Data Structure
Informatics 122 Software Design II
Object Oriented Design
Software Design Lecture : 28.
Presentation transcript:

Design Patterns David Talby

This Lecture n What is it all about? n Abstract Factory n Composite n Strategy

Introduction n O-O Design is Hard n Errors are expensive n Reuse experts’ designs n Pattern = Documented experience

Expected Benefits n Finding the right classes n Finding them faster n Common design jargon n Consistent format n Coded infrastructures

O-O Programming n An interface is a contract to clients. n A class implements interface(s). n Objects are instances of classes. n Objects are only accessed through their public interfaces. n Only two relations between classes: Inheritance and composition

Object Relationships n Inheritance: Static and efficient, but exposes and couples modules n Composition: Hides more from client and can change dynamically n Gang of Four: “Favor composition over inheritance” n Dijkstra: “Most problems in computer science can be solved by another level of indirection”.

Designing for Change n The Open-Closed Principle n Non-clairvoyance n Key Issue: Prepare for change! n Well, prepare for what?

Causes of Redesign n Dependence on hardware or software platform n Dependence on representation or implementation n Specifying a class upon creation n Algorithmic dependence n Tight coupling n Overuse of inheritance n Inability to alter classes easily

Pattern Categories n Creational - Replace explicit creation problems, prevent platform dependencies n Structural - Handle unchangeable classes, lower coupling and offer alternatives to inheritance n Behavioral - Hide implementation, hides algorithms, allows easy and dynamic configuration of objects

Pattern of Patterns n Encapsulate the varying aspect n Interfaces n Inheritance describes variants n Composition allows a dynamic choice between variants Criteria for success: Open-Closed Principle Single Choice Principle

1. Abstract Factory n A program must be able to choose one of several families of classes n For example, a program’s GUI should run on several platforms n Each platform comes with its own set of GUI classes: WinButton, WinScrollBar, WinWindow MotifButton, MotifScrollBar, MotifWindow pmButton, pmScrollBar, pmWindow

The Requirements n Uniform treatment of every button, window, etc. in the code u Easy - Define their interfaces: n Uniform object creation n Easy to switch between families n Easy to add a family

The Solution n Define a Factory - a class that creates objects: class WidgetFactory { Button* makeButton(args) = 0; Window* makeWindow(args) = 0; // other widgets… }

The Solution II n Define a concrete factory for each of the families: class WinWidgetFactory { Button* makeButton(args) { return new WinButton(args); } Window* makeWindow(args) { return new WinWindow(args); }

The Solution III n Select once which family to use: WidgetFactory* wf = new WinWidgetFactory(); n When creating objects in the code, don’t use ‘new’ but call: Button* b = wf->makeButton(args); n Switch families - once in the code! n Add a family - one new factory, no effect on existing code!

The Big (UML) Picture

The Fine Print n The factory doesn’t have to be abstract, if we expect a remote possibility of having another family n Usually one factory per application, a perfect example of a singleton n Not easy to extend the abstract factory’s interface

Known Uses n Different operating systems (could be Button, could be File) n Different look-and-feel standards n Different communication protocols

Pattern of Patterns n Encapsulate the varying aspect n Interfaces n Inheritance describes variants n Composition allows a dynamic choice between variants Criteria for success: Open-Closed Principle Single Choice Principle

2. Composite n A program must treat simple and complex objects uniformly n For example, a painting program has simple objects (lines, circles and texts) as well as composite ones (wheel = circle + six lines).

The Requirements n Treat simple and complex objects uniformly in code - move, erase, rotate and set color work on all n Some composite objects are defined statically (wheels), while others dynamically (user selection) n Composite objects can be made of other composite objects n We need a smart data structure

The Solution n All simple objects inherit from a common interface, say Graphic: class Graphic { void move(int x, int y) = 0; void setColor(Color c) = 0; void rotate( double angle ) = 0; } n The classes Line, Circle and others inherit Graphic and add specific features (radius, length, etc.)

The Solution II n This new class inherits it as well: class CompositeGraphic : public Graphic, public list { void rotate(double angle) { for (int i=0; i<count(); i++) item(i)->rotate(); }

The Solution III n Since a CompositeGraphic is a list, it had add(), remove() and count() methods n Since it is also a Graphic, it has rotate(), move() and setColor() too n Such operations on a composite object work using a ‘forall’ loop n Works even when a composite holds other composites - results in a tree-like data structure

The Solution IV n Example of creating a composite: CompositeGraphic *cg; cg = new CompositeGraphic(); cg->add(new Line(0,0,100,100)); cg->add(new Circle(50,50,100)); cg->add(t); // dynamic text graphic cg->remove(2); n Can keep order of inserted items if the program needs it

The GoF UML n Single Inheritance n Root has add(), remove() methods

The Fine Print n Sometimes useful to let objects hold a pointer to their parent n A composite may cache data about its children (count is an example) n Make composites responsible for deleting their children n Beware of circles in the graph! n Any data structure to hold children will do (list, array, hashtable, etc.)

Known Uses n In almost all O-O systems n Document editing programs n GUI (a form is a composite widget) n Compiler parse trees (a function is composed of simpler statements or function calls, same for modules) n Financial assets can be simple (stocks, options) or a composite portfolio

Pattern of Patterns n Encapsulate the varying aspect n Interfaces n Inheritance describes variants n Composition allows a dynamic choice between variants Criteria for success: Open-Closed Principle Single Choice Principle

3. Strategy n A program must switch between complex algorithms dynamically n For example, a document editor has several rendering algorithms, with different time/beauty tradeoffs n Word is a common example

The Requirements n Algorithms are complex, would be havoc to have them inside the one Document class n Switch algorithms dynamically n Easy to add new algorithms

The Solution n Define an abstract class that represents an algorithm: class Renderer { void render(Document *d) = 0; } n Each specific algorithm will be a descendant class FastRenderer, TexRenderer, …

The Solution II n The document itself chooses the rendering algorithm: class Document { render() { renderer->render(this); } setFastRendering() { renderer = new FastRenderer(); } private: Renderer *renderer; }

The GoF UML

The Fine Print n Inheriting a strategy would deny a dynamic switch n Some strategies may not use all information passed from Context n Strategies can be stateless, and then they can be shared n In some cases strategy objects are optional

Known Uses n Document rendering programs n Compiler code optimizations n Different heuristic algorithms (games, portfolio selection) n Different memory management schemes (Booch components) n Validation of dialog boxes (optional strategies, Borland ObjectWindows )

Pattern of Patterns n Encapsulate the varying aspect n Interfaces n Inheritance describes variants n Composition allows a dynamic choice between variants Criteria for success: Open-Closed Principle Single Choice Principle

Summary n Experience is required to see these patterns when they occur in your application n It’s worth looking for them - someone else has already done a major part of your job! n Patterns are practical example of the O-O approach’s usefulness n “Catalog” of 23 patterns on Intranet