L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.

Slides:



Advertisements
Similar presentations
Design Patterns.
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.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
C15: Design Patterns Gamma,Helm,Johnson,Vlissides (GOF)
Plab – Tirgul 12 Design Patterns
Design Patterns for Object Oriented systems CSC 515 Ashwin Dandwate.
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor 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.
Builder A Creational Design Pattern A Presentation by Alex Bluhm And.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Design Patterns.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
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.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Software Components Creational Patterns.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Computing IV Singleton Pattern Xinwen Fu.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
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.
Patterns Composite Pattern. Patterns All designers use patterns. Patterns in solutions come from patterns in problems. "A pattern is a solution to a problem.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Session 30 Final Review. Final Details Wednesday, December 14 at 8 AM Wright 5 (same classroom) Final will be comprehensive Open book Open notes Test.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Examples (D. Schmidt et al)
Design Patterns: MORE Examples
Design Patterns: Brief Examples
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
object oriented Principles of software design
Presented by Igor Ivković
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
Informatics 122 Software Design II
Presented by Igor Ivković
Presentation transcript:

L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation Issues)‏ Template Method (L6: Implementation Issues)‏ Abstract Factory (L7: Polymorphism1)‏ Singleton (L8: Polymorphism2)‏ Prototype (L9 : Design by Contract)‏ Adapter (L10: Exceptions)‏ Facade (L10: Exceptions)‏ Proxy (L10: Exceptions)‏

Design Patterns Definition: “Each pattern describes a problem which occurs over and over again in the environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice.”

Classic Reference Chapter 24 of Budd.  “Design Patterns: Elements of Reusable Object-Oriented Software”, by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides.

COSC346 - Pattern of the Day - Lecture 10 4 Iterator Problem: How to provide a way to access elements of an aggregate object sequentially without exposing the underlying representation. Solution: Provide an iterator object for the sole purpose of sequential access.

COSC346 - Pattern of the Day - Lecture 10 5 Example vector vec; vector ::iterator iter; // add stuff to vec … // now traverse vec for (iter=vec.begin(); iter!=vec.end(); iter++) { cout << *iter << endl; }

COSC346 - Pattern of the Day - Lecture 10 6 Consequences Makes changing the order of iteration easy Simplifies the aggregate’s interface Can traverse an aggregate more than once at the same time

COSC346 - Pattern of the Day - Lecture 10 7 Iterators and Algorithms #include vector vec; sort(vec.begin(), vec.end()); random_shuffle(vec.begin(), vec.end()); int mx = max(vec.begin(), vec.end());

Factory Method Problem: You have a method that returns a newly created object, but you want subclasses to have the ability to return different types of objects. Solution: Allow the subclass to override the creation method and return a different type of object.

Example class BoardGame { private: Board gameBoard; public: BoardGame() { gameBoard = makeGameBoard(); pieces = makeInitialPieces(); gameBoard.add(pieces); }; virtual Board *makeGameBoard() = 0; virtual Vector makeInitialPieces() = 0; } class OthelloBoardGame : public BoardGame { public: virtual Board *makeGameBoard() { return new OthelloBoard();}; }

Consequences Eliminates the need to bind application- specific classes into code. May cause a large class hierarchy.

Strategy Problem: How do you allow the algorithm that is used to solve a particular problem be easily and dynamically changed by the client? Solution: Define a family of algorithms with a similar interface.

Example class SortableVector: public vector { private: Sorter _sorter; public: void sort() { _sorter.sort(this); }} class Sorter { public: virtual void sort(SortableVector tosort)=0; } class QuickSorter : public Sorter { public: void sort(SortableVector tosort) {... }}

Consequences Families of related algorithms. Alternative to subclassing. Eliminates conditional statements. Clients must be aware of different strategies. Increases the number of objects.

Composite Scope: Object Problem: How do you permit the creation of complex objects using only simple parts? Solution: Provide a small collection of simple components but allow these components to be nested arbitrarily

Example

Consequences Makes the client simple Makes it easy to add new kinds of components Can make the design overly general

Template Method Scope: Class Problem: How do we avoid code duplication when subclasses share similar parts of an algorithm? Solution: Provide a template method that implements the invariant part of the algorithm and makes calls to subclass specific methods.

Example class GamePlayer { private: Player p1, p2; public: void playGame() { do { p1.makeMove(); if (!checkWin())‏ p2.makeMove(); } while (!checkWin()); } virtual void makeMove()=0; virtual boolean checkWin()=0; }

Consequences Fundamental for code reuse Important for class libraries Factors out common behaviours Parent class calls the operations of a subclass

Abstract Factory Problem: How to provide a mechanism for creating instances of families of related objects without specifying their concrete representations? Solution: Provide a method that returns a new value that is characterised only by an interface or parent class.

class ChessPieceFactory { public: virtual Pawn *makePawn()=0; virtual Rook *makeRook()=0; }; class ChessPieceFactory2D: public ChessPieceFactory { public: Pawn *makePawn() { // make a 2d pawn } Rook *makeRook() { // make a 2d rook } }; void addPiecesToBoard() { Position pos1, pos2, pos3; addPiece(factory->makePawn(), pos1); addPiece(factory->makePawn(), pos2); addPiece(factory->makeRook(), pos3); }

Consequences Isolates concrete classes Makes exchanging product families easy Promotes consistency among products Supporting new kinds of products is difficult

23 Singleton Problem: How do we ensure that there will never be more than one instance of a class created? Solution: Create a class that only allows one instance of itself.

24 Example class Singleton { public: static Singleton &theOne(){ return *the_one;}; private: Singleton(){}; static Singleton *the_one; };... Singleton *Singleton::the_one = new Singleton();

COSC346 - Pattern of the Day - Lecture 7 25 Prototype Problem:How to make new objects when we don’t know the type of object at compile time. Solution: Pass in a prototype variable to the appropriate method (using a parent class) and clone that prototype.

COSC346 - Pattern of the Day - Lecture 7 26 Example class Building { private: Wall *wallPrototype; map walls; public: public Building(Wall *_wallPrototype) { wallPrototype = _wallPrototype;}; void addWall(Location *l) { walls[l] = wallPrototype.clone();}; }

COSC346 - Pattern of the Day - Lecture 7 27 Example Building *house = new Building(new BrickWall()); house->addWall(East); house->addWall(West); Building glassHouse = new Building(new GlassWall()); glassHouse->addWall(East); Building shed = new Building(new CorrugatedIronWall());

COSC346 - Pattern of the Day - Lecture 7 28 Example Modified class Building { public: Building(Wall *wallPrototype) { addWall(East, wallPrototype.clone()); addWall(West, wallPrototype.clone()); addWall(North, wallPrototype.clone()); addWall(South, wallPrototype.clone()); }

COSC346 - Pattern of the Day - Lecture 7 29 Consequences Reduced subclassing. Dynamic composition. Requires a clone method.

Adapter/Facade/Proxy Three very similar design patterns that use intermediary classes between client and workers. Adapter: converts an interface to a subsystem interface a client requires. Facade: defines an interface (with several workers) that makes the subsystem easier to use. Proxy: acts as a placeholder for another object to control access to it.

Adapter Client Adapter Adaptee

Facade Client Worker 1 Worker 2 Worker 3

Proxy Client Proxy Server