Design Patterns A brief introduction to what they are, why they are useful, and some examples of those that are commonly used.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
Plab – Tirgul 12 Design Patterns
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
(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.
5/08 What is a Design Pattern „Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the.
Nov, 1, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and its.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
Oct, 16, Design Patterns PROBLEM CONTEXT SOLUTION A design pattern documents a proven solution to a recurring problem in a specific context and.
DESIGN PATTERNS Redesigning Applications And
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
(c) 2010 University of California, Irvine – André van der Hoek1June 29, 2015 – 08:55:05 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Design Patterns Alan Shalloway, James Trott, Design Patterns Explained, Addison-Wesley, Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements.
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.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
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.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
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.
DESIGN PATTERNS Sanjeeb Kumar Nanda 30-Aug What is a pattern? Pattern is a recurring solution to a standard problem Each Pattern describes a problem.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
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)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
CS 5150 Software Engineering Lecture 16 Program Design 3.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory pattern Intent Provide an interface for creating families of related or dependent objects without specifying their concrete classes.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns: MORE Examples
Abstract Factory Pattern
Design Patterns: Brief Examples
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
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Factory Patterns 1.
Introduction to Design Patterns
object oriented Principles of software design
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Design Patterns Satya Puvvada Satya Puvvada.
Design Patterns - A few examples
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
Informatics 122 Software Design II
Chapter 8, Design Patterns Introduction
Presentation transcript:

Design Patterns A brief introduction to what they are, why they are useful, and some examples of those that are commonly used

What are Design Patterns? They capture Design expertise and are abstracted from existing Design examples Using Design Patterns is reuse of Design Expertise Each pattern describes a problem which occurs over and over again in the software development design environment, and then describes the core of the solution to that problem in such a way that the solution can be used again and again, without ever doing it the same way twice

What are Design Patterns (cont’d) They are class- and method-level solutions to common problems in object-oriented design Should have a good working knowledge of OO principles (inheritance, polymorphism, abstraction, interfaces, etc.), an OO language (such as Java), and UML to use them productively If you want to go from being a good Java developer to a great one, study Design Patterns

Important Texts Definitive text: Gamma, Helm, Johnson, and Vlissides (the “Gang of Four”) – Design Patterns, Elements of Reusable Object-Oriented Software This book solidified thinking about patterns and became the seminal Design Patterns text Design Patterns in Java by Metsker and Wake is also a good text

Elements of Design Patterns – 4 Essential Parts Name A name for a pattern adds to the design vocabulary It allows design at a higher level of abstraction Problem Description of a problem and its context Sometimes includes enumeration of typical design flaws Solution Elements that make up the design and their relationships Includes responsibilities and collaborations Consequences Time and space trade-offs Possibly language and implementation concerns

Classification of Design Patterns – 3 Categories Creational Patterns (typically involved with object construction) Factory, Abstract Factory, Builder, Prototype, Singleton Structural Patterns Adapter, Bridge, Composite, Decorator, Façade, Flyweight, Proxy Behavioral Patterns Interpreter, Template, Chain of Responsibility, Command, Iterator, Mediator, Memento, Observer, State, Strategy, Visitor

Where do Design Patterns sit in Software Architecture Hierarchy? Between Classes and Objects (the innermost layer) and Frameworks (Java API, .NET Framework, etc.) Layers from outermost to innermost are Global Enterprise System Application: Often a collection of Frameworks and subsystems Macro: Frameworks Micro: Design Patterns live here Objects

Designing for Change – Causes for Redesign (I) Creating an object by specifying a class explicitly Commits to a particular implementation instead of an interface Can complicate future changes Create objects indirectly Patterns: Abstract Factory, Factory Method, Prototype Dependence on specific operations Commits to one way of satisfying a request Compile-time and runtime modifications to request handling can be simplified by avoiding hard-coded requests Patterns: Chain of Responsibility, Command

Causes for Redesign (II) Dependence on hardware and software platform External OS-APIs vary Design system to limit platform dependencies Patterns: Abstract Factory, Bridge Dependence on object representations or implementations Clients that know how an object is represented, stored, located, or implemented might need to be changed when object changes Hide information from clients to avoid cascading changes Patterns: Abstract factory, Bridge, Memento, Proxy

Causes for Redesign (III) Algorithmic dependencies Algorithms are often extended, optimized, and replaced during development and reuses Algorithms that are likely to change should be isolated Patterns: Builder, Iterator, Strategy, Template Method, Visitor Tight coupling Leads to monolithic systems Tightly coupled classes are hard to reuse in isolation Patterns: Abstract Factory, Bridge, Chain of Responsibility, Command, Facade, Mediator, Observer

Causes for Redesign (IV) Extending functionality by subclassing Requires in-depth understanding of the parent class Overriding one operation might require overriding another Can lead to an explosion of classes (for simple extensions) Patterns: Bridge, Chain of Responsibility, Composite, Decorator, Observer, Strategy Inability to alter classes conveniently Sources not available Change might require modifying lots of existing classes Patterns: Adapter, Decorator, Visitor

How Design Patterns Solve Design Problems Finding Appropriate Objects Decomposing a system into objects is the hard part OO-designs often end up with classes with no counterparts in real world (low-level classes like arrays) Strict modeling of the real world leads to a system that reflects today’s realities but not necessarily tomorrows Design patterns identify less-obvious abstractions Determining Object Granularity Objects can vary tremendously in size and number Facade pattern describes how to represent subsystems as objects Flyweight pattern describes how to support huge numbers of objects

Specifying Object Interfaces Set of all signatures defined by an object’s operations Any request matching a signature in the objects interface may be sent to the object Interfaces may contain other interfaces as subsets Type: Denotes a particular interfaces An object may have many types Widely different object may share a type Objects of the same type need only share parts of their interfaces A subtype contains the interface of its supertype Dynamic binding, polymorphism

Program to an interface, not an implementation Manipulate objects solely in terms of interfaces defined by abstract classes! Benefits: Clients remain unaware of the specific types of objects they use. Clients remain unaware of the classes that implement the objects. Clients only know about abstract class(es) defining the interfaces Do not declare variables to be instances of particular concrete classes Use creational patterns to create actual objects.

Favor object composition over class inheritance White-box reuse: Reuse by subclassing (class inheritance) Internals of parent classes are often visible to subclasses works statically, compile-time approach Inheritance breaks encapsulation Black-box reuse: Reuse by object composition Requires objects to have well-defined interfaces No internal details of objects are visible

return rectangle->Area() Delegation Makes composition as powerful for reuse as inheritance Two objects involved in handling requests Explicit object references, no this-pointer Extreme example of object composition to achieve code reuse Drawback: dynamic, hard to understand, run-time inefficiencies rectangle Area() Area() width height return rectangle->Area() return width * height

Singleton Pattern Use the Singleton pattern when Singleton classes there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point. when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code. Singleton classes Define a method that delivers one instance (checks to see if reference is null, if so, it creates the instance) Are themselves responsible for creating that instance We have seen an example of the Singleton Pattern in our introduction to Fitnesse Other examples where you want only one instance in a class: print spooler, file system, window manager

Model / View / Controller Not listed as a pattern by Gang of Four, but recognized as one Model: implements algorithms and is independent of environment View: communicates with environment and implements I/O interface for model Controller: Controls data exchange (notification protocol) between model and view

MVC (cont’d) MVC decouples views from models – more general: Decoupling objects so that changes to one can affect any number of others without requiring the object to know details of the others Observer pattern solves the more general problem MVC allows view to be nested: CompositeView objects act just as View objects Composite pattern describes the more general problem of grouping primitive and composite objects into new objects with identical interfaces MVC controls appearance of view by controller: Example of the more general Strategy pattern MVC uses Factory and Decorator patterns as well

MVC (cont’d) A view of MVC Model US $ -> EUR Model EUR -> US $ View character-based Controller Model US $ -> EUR View GUI, Document 1 View GUI, Document 2 Model EUR -> US $

ABSTRACT FACTORY (Object Creational) Intent: Provide an interface for creating families of related or dependent objects without specifying their concrete classes Motivation: User interface toolkit supports multiple look-and-feel standards (Motif, Presentation Manager) Different appearances and behaviors for UI widgets App should not hard-code its widgets Solution: Abstract WidgetFactory class Interfaces for creating each basic kind of widget Abstract class for each kind of widgets, Concrete classes implement specific look-and-feel

Applicability Use the Abstract Factory pattern when A system should be independent of how its products are created, composed, and represented A system should be configured with one of multiple families of produces A family of related product objects is designed to be used together, and you need to enforce this constraint You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations

ABSTRACT FACTORY Structure View Abstract Factory Client CreateProductA() CreateProductB() AbstractProductA ProductA2 ProductA1 ConcreteFactory1 ConcreteFactory2 CreateProductA() CreateProductB() CreateProductA() CreateProductB() AbstractProductB ProductB2 ProductB1

ABSTRACT FACTORY Participants AbtractFactory Declares interface for operations that create abstract product objects ConcreteFactory Implements operations to create concrete product objects AbstractProduct Declares an interface for a type of product object ConcreteProduct Defines a product object to be created by concrete factory Implements the abstract product interface Client Uses only interfaces declared by AbstractFactory and AbstractProduct classes

Facade Provide unified interface to interfaces within a subsystem Shield clients from subsystem components Promote weak coupling between client and subsystem components Client Facade

Strategy Make algorithms interchangeable---”changing the guts” Alternative to subclassing Choice of implementation at run-time Increases run-time complexity Context Strategy ContextInterface() Operation() ConcreteStrategy1 ConcreteStrategy2 Operation() Operation()

Chain of Responsibility Decouple sender of a request from receiver Give more than one object a chance to handle Flexibility in assigning responsibility Often applied with Composite successor Client Handler ContextInterface() handleRequest() ConcreteHandler1 ConcreteHandler2 handleRequest() handleRequest()

Closing Remarks Learning to apply patterns is an ongoing process, just like learning to apply recursion The better you understand Object Oriented principles, the more able you will be to apply design patterns You are well served to educate yourself at least nominally on the concept of patterns – it could well be a topic of discussion at a job interview