1/23/2018 Design Patterns David Talby.

Slides:



Advertisements
Similar presentations
Chain of Responsibility Pattern Gof pp Yuyang Chen.
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.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
Matt Klein 7/2/2009.  Intent  Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Iterators T.J. Niglio Computer & Systems Engineering Fall 2003 Software Design & Documentation Object Behavioral.
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 Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
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.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VI Composite, Iterator, and Visitor Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Proxy Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
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.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
More Design Patterns In Delphi Jim Cooper Falafel Software Session Code: D3.03 Track: Delphi.
Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.
Chapter 26 GoF Design Patterns. The Adapter Design Pattern.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural 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.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Proxy.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
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 SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Examples (D. Schmidt et al)
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Abstract Factory Pattern
Abstract Data Types and Encapsulation Concepts
7. Inheritance and Polymorphism
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
Java Primer 1: Types, Classes and Operators
Behavioral Design Patterns
Creational Pattern: Prototype
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
object oriented Principles of software design
Abstract Factory Pattern
How to be a Good Developer
Intent (Thanks to Jim Fawcett for the slides)
GoF Design Patterns (Ch. 26). GoF Design Patterns Adapter Factory Singleton Strategy Composite Façade Observer (Publish-Subscribe)
Design Patterns Satya Puvvada Satya Puvvada.
Software Engineering Lecture 7 - Design Patterns
Frameworks And Patterns
GoF Design Patterns (Ch. 26)
Introduction to Data Structure
Overview of C++ Polymorphism
Java Programming Language
Informatics 122 Software Design II
Object Oriented Design
Software Design Lecture : 38.
GoF Patterns Ch. 26.
Presentation transcript:

1/23/2018 Design Patterns David Talby

Object Oriented Design Course 1/23/2018 This Lecture Re-Routing Method Calls Proxy, Chain of Responsibility Working with external libraries Adapter, Façade Coding partial algorithms Template Method The Singleton Pattern Patterns Summary 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Proxy Provide a placeholder for another object, to control access to it For example, we’d like to defer loading the images of a document until we must display it 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Requirements Only load images when required Client code must not know whether lazy load is used or not Images may be loaded from a file, a database or a network Such code should be encapsulated Should be easy to add variations, such as security and compression 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution Define a new graphic ImageProxy, which holds an image’s file name Holds an uninitialized Image object When its draw() method is called: draw() { if (image == NULL) image = load(filename); image->draw(); } 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution II Many ways to implement load: Read from a file or database Use a complex network protocol Use encryption, compression, … Compute the returned object Any such complex logic is well encapsulated in the proxy The proxy can hold part of Image’s data for efficiency 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The UML 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Fine Print The Proxy vocabulary Virtual Proxy – creates expensive objects on demand Remote Proxy – a local representative of an object in another address space Protection Proxy – controls access to the original object Smart Pointers – overload regular pointers with additional actions 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Fine Print II Uses of smart pointers Reference counting Synchronization (lock management) Profiling and statistics Copy-on-write Cache coherence Pooling Smart pointers are easy in C++ thanks to overloading = and –> 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Fine Print III Proxy is very much like Decorator Decorator = functional addition Proxy = technical addition 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Known Uses Every programming language Every middleware package Every database package 1/23/2018 Object Oriented Design Course

Chain of Responsibility 1/23/2018 Chain of Responsibility Decouple the sender and receiver of a message, and give more than one receiver a chance to handle it For example, a context-sensitive help system returns help on the object currently in focus Or its parent if it has no help Recursively 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Requirements Allow calling for context-sensitive help from any graphical object If the object can’t handle the request (it doesn’t include help), it knows where to forward it The set of possible handlers is defined and changed dynamically 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution Define a HelpHandler base class: class HelpHandler { handleHelp() { if (successor != NULL) successor->handleHelp(); } HelpHandler* successor = NULL; 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution II Class Graphic inherits HelpHandler Graphic descendants that have help to show redefine handleHelp: handleHelp() { ShowMessage(“Buy upgrade”); } Either the root Graphic object or HelpHandler itself can redefine handleHelp to show a default 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The UML 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Fine Print Receipt isn’t guaranteed Usually parents initialize the successor of an item upon creation To themselves or their successor The kind of request doesn’t have to be hard-coded: class Handler { handle(Request* request) { // rest as before 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Known Uses Context-sensitive help Messages in a multi-protocol network service Handling user events in a user interface framework Updating contained objects/queries in a displayed document 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Adapter Convert the interface of a class into another that clients expect For example, We’d like to use advanced Text and SpellCheck component that we bought But Text doesn’t inherit Graphic or supply iterators, and SpellCheck doesn’t inherit Visitor We don’t have their source code 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Requirements Convert the interface of a class into a more convenient one Without the class’s source code No compilation dependencies The class may be a module in a non-object oriented language 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution If you can’t reuse by inheritance, reuse by composition: class TextGraphic : public Graphic { public: void draw() { text->paint(); } // other methods adapted... private: BoughtTextComponent *text; } 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Requirements II Stacks and queues are kinds of lists, but they provide less functionality LinkedQueue is a linked list implementation of interface Queue We’d like to reuse LinkedList for it Inheritance can’t be used if children offer less than their parents 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution II Object Adapter Class LinkedQueue will hold a reference to a LinkedList and delegate requests to it Class Adapter Class LinkedQueue will inherit from both Queue and LinkedList Method signatures in both classes must match In C++ class adapters are safer thanks to private inheritance 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The UML Object Adapter: 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The UML II Class Adapter: 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Known Uses Using external libraries Reusing non O-O code Limiting access to classes 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Facade Provide a unified interface to a set of interfaces of subsystems For example, a compiler is divided into many parts Scanner, parser, syntax tree data structure, optimizers, generation, … Most clients just compile files, and don’t need to access inner parts 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Requirements Provide a simple, easy to use and remember interface for compilation Keep the flexibility to tweak inner parts when needed 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution Define a façade Compiler class as the entry point to the system 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The UML 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Fine Print Advantages of a façade: Most users will use a very simple interface for the complex system Clients are decoupled from the system Makes it easier to replace the entire system with another Packages (Java) and namespaces (C++) are ways to define “systems” of classes and decide which classes are visible to the system’s clients 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Known Uses A Compiler or XML Parser Browsing objects at runtime The Choices O-O operating system The File and Memory systems 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Template Method Define the skeleton of an algorithm and let subclasses complete it For example, a generic binary tree class or sort algorithm cannot be fully implemented until a comparison operator is defined How do we implement everything except the missing part? 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Requirements Code once all parts of an algorithm that can be reused Let clients fill in the gaps 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution Code the skeleton in a class where only the missing parts are abstract: class BinaryTree<G> { void add(G* item) { if (compare(item, root)) // usual logic } int compare(G* g1, G* g2) = 0; 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution II Useful for defining comparable objects in general: class Comparable { operator <(Comparable x) = 0; operator >=(Comparable x) { return !(this < x); } operator >(Comparable x) { return !(this < x) && !(this == x); 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution III A very common pattern: class HelpHandler { handleHelp() { if (successor != NULL) successor->handleHelp(); } HelpHandler* successor = NULL; 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The UML 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Fine Print The template method is public, but the ones it calls should be protected The called methods can be declared with an empty implementation if this is a common default This template can be replaced by passing the missing function as a template parameter 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Known Uses So fundamental that it can be found almost anywhere Factory Method is a kind of template method specialized for creation 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Singleton Ensure that only one instance of a class exists, and provide a global access point to it For example, ensure that there’s one WindowManager, FileManager or PrintSpooler object in the system Desirable to encapsulate the instance and responsibility for its creation in the class 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution O-O languages support methods shared by all objects of a class static in C++ and Java class methods in SmallTalk, Delphi The singleton class has a reference to its single instance The instance has a getter method which initializes it on the first request The class’s constructor is protected to prevent creating other instances 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Solution class Spooler { public: static Spooler* instance() { if (_instance == NULL) _instance = new Spooler(); return _instance; } protected: Spooler() { ... } private: static Spooler* _instance = 0; 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The UML 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 The Fine Print Passing arguments for creation can be done with a create(...) method Making the constructor public makes it possible to create other instance except the “main” one Not a recommended style instance() can manage concurrent access or manage a list of instances Access to singletons is often a bottleneck in concurrent systems 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Known Uses Every system has singletons! WindowManager, PrinterManager, FileManager, SecurityManager, ... Class Application in a framework Log and error reporting classes With other design patterns 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Bridge Separate an abstraction from its implementations For example, a program must run on several platforms An Entire Hierarchy of Interfaces must be supported on each platform Using Abstract Factory alone would result in a class per platform per interface – too many classes! 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Interpreter Given a language, define a data structure for representing sentences along with an interpreter for it For example, a program must interpret code or form layout, or support search with regular expression and logical criteria Not covered here 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Momento Without violating encapsulation, store an object’s internal state so that it can be restored later For example, a program must store a simulation’s data structures before a random or approximation action, and undo must be supported Not covered here 1/23/2018 Object Oriented Design Course

Object Oriented Design Course 1/23/2018 Patterns Summary O-O concepts are simple Objects, Classes, Interfaces Inheritance vs. Composition Open-Closed Principle Single Choice Principle Pattern of patterns 1/23/2018 Object Oriented Design Course

The Benefits of Patterns 1/23/2018 The Benefits of Patterns Finding the right classes Finding them faster Common design jargon Consistent format Coded infrastructures and above all: Pattern = Documented Experience 1/23/2018 Object Oriented Design Course