Design Patterns Satya Puvvada Satya Puvvada.

Slides:



Advertisements
Similar presentations
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Advertisements

1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
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.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
Design Pattern Course Builder Pattern 1 Mahdieh Monzavi AmirKabir University of Technology, Department of Computer Engineering & Information Technology.
Design Patterns CS is not simply about programming
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Behavioral Patterns C h a p t e r 5 – P a g e 128 BehavioralPatterns Design patterns that identify and realize common interactions between objects Chain.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
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.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
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 Patterns CSCI 5801: Software Engineering. Design Patterns.
Facade Introduction. Intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the.
Computing IV Singleton Pattern Xinwen Fu.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
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.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
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.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns Introduction
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
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.
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.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
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.
Examples (D. Schmidt et al)
Jim Fawcett CSE776 – Design Patterns Summer 2006
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Presented by FACADE PATTERN
Abstract Factory Pattern
Design Patterns A brief introduction to what they are, why they are useful, and some examples of those that are commonly used.
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
Jim Fawcett CSE776 – Design Patterns Fall 2016
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
Behavioral Design Patterns
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
object oriented Principles of software design
Abstract Factory Pattern
Decorator Design Pattern
Intent (Thanks to Jim Fawcett for the slides)
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
Object Oriented Design Patterns - Structural Patterns
Behavioral Design Pattern
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Structural Patterns: Adapter and Bridge
Informatics 122 Software Design II
Chapter 8, Design Patterns Introduction
Presentation transcript:

Design Patterns Satya Puvvada Satya Puvvada

Objectives Gain an understanding of using design patterns to improve design and implementation Learn to develop robust, efficient and reusable C++ programs using design patterns Satya Puvvada

Contents – Day 1 Introduction to design patterns Introduction to UML notation Patterns Singleton Factory method Abstract Factory Observer Strategy Adapter Visitor Satya Puvvada

Contents – Day 2 Patterns Builder Bridge Facade Proxy Composite Chain of responsibility Command Satya Puvvada

Contents – Day 3 Patterns Case Study References and conclusions Flyweight Memento State Decorator Prototype Mediator Case Study References and conclusions Satya Puvvada

Builder Separate the construction of a complex object from its representation so that the same construction process can create different representations. Satya Puvvada

Builder Satya Puvvada

Builder Structure… Satya Puvvada

Builder Satya Puvvada

Builder Discussion Sometimes creational patterns are complementory: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations. Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately. Builder is to creation as Strategy is to algorithm. Satya Puvvada

Builder Discussion Builder often builds a Composite. Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed. Satya Puvvada

Builder Consequences It lets you vary a product's internal representation It isolates code for construction and representation It gives you finer control over the construction process Satya Puvvada

Bridge Decouple an abstraction from its implementation so that the two can vary independently. Satya Puvvada

Bridge Every new widget needs two implementations If you add a third windowing toolkit you need to have 3 implementations per widget Satya Puvvada

Bridge - Motivation Inheritance binds an implementation to the abstraction permanently, which makes it difficult to modify, extend and reuse abstractions and implementations independently. Satya Puvvada

Bridge - Motivation (Cont.) It is inconvenient to extend the window abstraction to cover different kinds of windows or new platform. It makes the client-code platform dependent. Satya Puvvada

Bridge Satya Puvvada

Bridge Structure… Satya Puvvada

Bridge Decoupling interface and implementation Improved extensibility Hiding implementation details from clients Satya Puvvada

Bridge - Applicability To avoid a permanent binding between an abstraction and its implementation. Specific implementation can be selected at run-time To independently extend abstraction and implementation by subclassing. Different abstractions can be combined with different implementations. Changes in the implementation of an abstraction should have no impact on the clients. Satya Puvvada

Bridge - Applicability (Cont.) To avoid proliferation of classes as shown in the Motivation section. To share an implementation among multiple objects. e.g. Handle/Body from Coplien. Satya Puvvada

Bridge - Consequences Decoupling interface and implementation. Improved extensibility of both abstraction and implementation class hierarchies. Hiding implementation detail from client. Satya Puvvada

Bridge - Related Patterns Similar structure to Adapter but different intent. Abstract Factory pattern can be used with the Bridge for creating objects. Satya Puvvada

Bridge – Example Image viewer application designed to view BMP files on Windows operating systems. However, it can easily be extended to view other image formats like JPEG on Windows or view BMP images on other operating systems like OS/2. example uses two-class hierarchies viz., CImage and CImageImp. CImage class hierarchy defines the abstraction for the clients and CImageImp class hierarchy provides implementation for the specified abstraction. Satya Puvvada

Bridge – Another Example CImage and its derived classes are responsible for handling different image formats such as BMP, JPEG, PCX etc., and CImageImp classes are responsible for the representation of the images on different operating systems like Windows, OS/2. The CImage object provides basic services for loading and showing images and it is configured with a CImageImp object. Services that are dependent on a particular implementation (like show) are forwarded to CImageImp class (say to PaintImage). Satya Puvvada

Bridge – Another Example In this way, new image formats can be added to the CImage class hierarchy without affecting the CImageImp and CImageImp can be extended to provide implementation for a new operating system without affecting CImage. In short, the goal of the Bridge Pattern is achieved, that is, to vary abstraction and implementation independently. Satya Puvvada

Bridge – Another Example Satya Puvvada

Bridge – Another Example class CImage { // Method declarations public : virtual INT Load( LPCSTR, CRuntimeClass * ) = 0; virtual INT Show( CWnd *, WPARAM ); // Data members protected : CImageImp * m_pImageImp; }; class CBmpImage : public CImage virtual INT Load( LPCSTR, CRuntimeClass * ); Satya Puvvada

Bridge – Another Example class CImageImp : public CObject { // Method declarations public : virtual INT InitImageInfo( LPSTR ) = 0; virtual BOOL PaintImage( CWnd *, CRect * ) = 0; // Attributes LPBYTE m_pImage; LONG m_lNormalWidth; LONG m_lNormalHeight; }; Satya Puvvada

Bridge – Another Example class CWinImp : public CImageImp { // Method declarations public : INT InitImageInfo( LPSTR ); BOOL PaintImage( CWnd *, CRect * ); // Attributes protected : BYTE * m_pBmi; CPalette * m_pPalette; }; INT CImage::Show( CWnd * pWnd, WPARAM wParam ) // Step 1 - Check and delegate this method to m_pImageImp ASSERT( m_pImageImp != NULL ); return m_pImageImp->PaintImage( pWnd, ( CRect * ) wParam ); } Satya Puvvada

Bridge – Another Example INT CBmpImage::Load( LPCSTR lpszFileName, CRuntimeClass * pRuntimeClass ) { // Some initialization code before creating image implementation object … // Initialize image information, after creating image implementation object m_pImageImp = ( CImageImp * ) pRuntimeClass->CreateObject(); if( m_pImageImp == NULL ) return FAILURE; } m_pImageImp->InitImageInfo(..); return SUCCESS; Satya Puvvada

Facade - Intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher level interface that makes the subsystem easier to use. Satya Puvvada

Facade - Motivation To reduce complexity of large systems, we need to structure it into subsystems. A common goal is to reduce dependencies between subsystems. This can be done using Facade, which provides a single well-defined interface for the more general facilities of the subsystem. Satya Puvvada

Facade- Motivation (cont.) client classes Facade subsystem classes Satya Puvvada

Facade -Example Facade Client MemoryManager MemmoryAllocator PageManagement PageDirectory PageTable HeapAllocator LocalAllocator MemorySharingTechnique Satya Puvvada

Facade - Structure Facade Satya Puvvada

Facade - Applicability To provide simple interface to a complex subsystem, which is useful for most clients. To reduce the dependencies between the client and the subsystem, or dependencies between various subsystems. To simplify the dependencies between the layers of a subsystem by making them communicate solely through their facades. Satya Puvvada

Facade - Consequences It shields the clients from subsystem components, thereby making the subsystem easier to use. It promotes weak coupling between subsystem and its clients. Components in a subsystems can change without affecting the clients. Porting of subsystems is easier. Simplified interface of the Facade may not be adequate for all clients. Satya Puvvada

Facade - Consequences Source code Satya Puvvada

Proxy Pattern Provide a surrogate or placeholder for another object to control access to it. Satya Puvvada

Proxy Example Satya Puvvada

Proxy Structure Satya Puvvada

Proxy benefits remote proxy can hide the fact that an object resides in a different address space. A virtual proxy can perform optimizations such as creating an object on demand. Both protection proxies and smart references allow additional housekeeping tasks when an object is accessed. Satya Puvvada

Proxy source code Memory overhead Protection proxy Satya Puvvada

Composite Pattern Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. Satya Puvvada

Composite Example Satya Puvvada

Composite Example Satya Puvvada

Composite Structure Satya Puvvada

Composite Source code Satya Puvvada

Chain of responsibility Avoid coupling the sender of a request to its receiver by giving more than one object a chance to handle the request. Chain the receiving objects and pass the request along the chain until an object handles it. Satya Puvvada

Satya Puvvada

Chain of Responsability Satya Puvvada

Chain of responsability Satya Puvvada

Participants Handler (HelpHandler) defines an interface for handling requests. (optional) implements the successor link. ConcreteHandler (PrintButton, PrintDialog) handles requests it is responsible for. can access its successor. if the ConcreteHandler can handle the request, it does so; otherwise it forwards the request to its successor. Client initiates the request to a ConcreteHandler object on the chain. Satya Puvvada

Applying Command Pattern… Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. Satya Puvvada

Command Example Satya Puvvada

Command Example Satya Puvvada

Command Structure Satya Puvvada

Command Structure Satya Puvvada

Command Consequences Command decouples the object that invokes the operation from the one that knows how to perform it. Commands are first-class objects. They can be manipulated and extended like any other object. It's easy to add new Commands, because you don't have to change existing classes. Satya Puvvada

Applying Composite on Command Satya Puvvada