Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.

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.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
CS590L - Lecture 6 1 CS590L Distributed Component Architecture References: - E. Gamma, R. Helm, R. Johnson, J. Vlissides, Design Patterns: Elements of.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Patterns Reusable solutions to common object-oriented programming problems When given a programming problem, re-use an existing solution. Gang of Four.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
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.
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
DESIGN PATTERNS Redesigning Applications And
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.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
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.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Powerpoint Templates Page 1 Powerpoint Templates What is Design Patterns ? by Indriati Teknik Informatika – UB.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
DESIGN PATTERNS CSC532 Adv. Topics in Software Engineering Shirin A. Lakhani.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
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 Gang Qian Department of Computer Science University of Central Oklahoma.
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.
CSE 403, Spring 2008, Alverson Software Design “There are two ways of constructing a software design: one way is to make it so simple that there are obviously.
ECE450S – Software Engineering II
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Design Patterns Yonglei Tao. Design Patterns  A design pattern describes a recurring design problem, a solution, and the context in which that solution.
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.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
CS616: Software Engineering Spring 2009 Design Patterns Sami Taha.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Stephenson College DP 98 1 Design Patterns by Derek Peacock.
Design Patterns Introduction
Using Software Design Patterns Bill Anderson. About me Fox developer since 1987 Fox developer since 1987 Program Director, Los Angeles Visual Foxpro Developers.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
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:
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns Spring 2017.
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
Design Pattern Catalogues
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
object oriented Principles of software design
Software Engineering Lecture 7 - Design Patterns
Design Patterns in Game Design
Informatics 122 Software Design II
Presentation transcript:

Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park

Design Patterns Descriptions of reusable solutions to common software design problems Captures the experience of experts Rationale for design Tradeoffs Codifies design in reusable form Example Iterator pattern

Goals Solve common programming challenges Improve reliability of solution Aid rapid software development Useful for real-world applications

Observations Design patterns are like recipes – generic solutions to expected situations Design patterns are language independent Recognizing when and where to use design patterns requires familiarity & experience Design pattern libraries serve as a glossary of idioms for understanding common, but complex solutions

Observations (cont.) Many design patterns may need to fit together Design Patterns (by Gamma et al., a.k.a. Gang of Four, or GOF) list 23 design patterns Around 250 common OO design patterns Design patterns are used throughout the Java Class Libraries

Documentation Format 1. Motivation or context for pattern 2. Prerequisites for using a pattern 3. Description of program structure 4. List of participants (classes & objects) 5. Collaborations (interactions) between participants 6. Consequences of using pattern (good & bad) 7. Implementation techniques & issues 8. Example codes 9. Known uses 10. Related patterns

Types of Design Patterns Creational Deal with the best way to create objects Structural Ways to bring together groups of objects Behavioral Ways for objects to communicate & interact

Creational Patterns 1. Abstract Factory- Creates an instance of several families of classes 2. Builder - Separates object construction from its representation 3. Factory Method - Creates an instance of several derived classes 4. Prototype - A fully initialized instance to be copied or cloned 5. Singleton - A class of which only a single instance can exist

Structural Patterns 6. Adapter - Match interfaces of different classes 7. Bridge - Separates an object’s interface from its implementation 8. Composite - A tree structure of simple and composite objects 9. Decorator - Add responsibilities to objects dynamically 10. Façade - Single class that represents an entire subsystem 11. Flyweight - Fine-grained instance used for efficient sharing 12. Proxy - Object representing another object

Behavioral Patterns 13. Chain of Responsibility - A way of passing a request between a chain of objects 14. Command - Encapsulate a command request as an object 15. Interpreter - A way to include language elements in a program 16. Iterator - Sequentially access the elements of a collection 17. Mediator - Defines simplified communication between classes 18. Memento - Capture and restore an object's internal state

Behavioral Patterns (cont.) 19. Observer - A way of notifying change to a number of classes 20. State - Alter an object's behavior when its state changes 21. Strategy - Encapsulates an algorithm inside a class 22. Template Method - Defer the exact steps of an algorithm to a subclass 23. Visitor - Defines a new operation to a class without changing class

Iterator Pattern Definition Move through list of objects without knowing its internal representation Where to use & benefits Use a standard interface to represent data objects Uses standard iterator built in each standard collection, like List, Sort, or Map Need to distinguish variations in the traversal of an aggregate

Iterator Pattern Example Iterator for collection Original Examine elements of collection directly Using pattern Collection provides Iterator class for examining elements in collection

Iterator Example public interface Iterator { Bool hasNext(); Object next(); } Iterator it = myCollection.iterator(); while ( it.hasNext() ) { MyObj x = (MyObj) it.next(); // finds all objects …// in collection }

Singleton Pattern Definition One instance of a class or value accessible globally Where to use & benefits Ensure unique instance by defining class final Access to the instance only via methods provided

Singleton Example public class Employee { public static final int ID = 1234;// ID is a singleton } public final class MySingleton { // declare the unique instance of the class private static MySingleton uniq = new MySingleton(); // private constructor only accessed from this class private MySingleton() { … } // return reference to unique instance of class public static MySingleton getInstance() { return uniq; }

Adapter Pattern Definition Convert existing interfaces to new interface Where to use & benefits Help match an interface Make unrelated classes work together Increase transparency of classes

Adapter Pattern Example Adapter from integer Set to integer Priority Queue Original Integer set does not support Priority Queue Using pattern Adapter provides interface for using Set as Priority Queue Add needed functionality in Adapter methods

Adapter Example public interface PriorityQueue {// Priority Queue void add(Object o); int size(); Object removeSmallest(); }

Adapter Example public class PriorityQueueAdapter implements PriorityQueue { Set s; PriorityQueueAdapter(Set s) { this.s = s; } public void add(Object o) { s.add(o); } int size() { return s.size(); } public Integer removeSmallest() { Integer smallest = Integer.MAX_VALUE; Iterator it = s.iterator(); while ( it.hasNext() ) { Integer i = it.next(); if (i.compareTo(smallest) < 0) smallest = i; } s.remove(smallest); return smallest; }

Factory Pattern Definition Provides an abstraction for deciding which class should be instantiated based on parameters given Where to use & benefits A class cannot anticipate which subclasses must be created Separate a family of objects using shared interface Hide concrete classes from the client

Factory Pattern Example Car Factory produces different Car objects Original Different classes implement Car interface Directly instantiate car objects Need to modify client to change cars Using pattern Use carFactory class to produce car objects Can change cars by changing carFactory

Factory Example class 350Z implements Car;// fast car class Ram implements Car;// truck class Accord implements Car;// family car Car fast = new 350Z();// returns fast car public class carFactory { public static Car create(String type) { if (type.equals("fast"))return new 350Z(); if (type.equals("truck"))return new Ram(); else if (type.equals(“family”)return new Accord(); } Car fast = carFactory.create("fast"); // returns fast car

Decorator Pattern Definition Attach additional responsibilities or functions to an object dynamically or statically Where to use & benefits Provide flexible alternative to subclassing Add new function to an object without affecting other objects Make responsibilities easily added and removed dynamically & transparently to the object

Decorator Pattern Example Pizza Decorator adds toppings to Pizza Original Pizza subclasses Combinatorial explosion in # of subclasses Using pattern Pizza decorator classes add toppings to Pizza objects dynamically Can create different combinations of toppings without modifying Pizza class

Decorator Example public interface Pizza { int cost(); } public class SmallPizza extends Pizza { int cost(){ return 8; } } public class LargePizza extends Pizza { int cost(){ return 12; } } public class PizzaDecorator implements Pizza { Pizza p; PizzaDecorator (Pizza p) { this.p = p; } int cost(){ return p.cost(); } }

Decorator Example public class withOlive extends PizzaDecorator { int cost() { return p.cost() + 2; } } public class withHam extends PizzaDecorator { int cost() { return p.cost() + 3; } } Pizza HamOlivePizza = new withHam ( new withOlive ( new LargePizza() ) ); … = HamOlivePizza.cost();// returns Pizza DoubleHamPizza = new withHam ( new withHam ( new SmallPizza() ) ); … = DoubleHamPizza.cost();// returns 8+3+3

Decorator Pattern Examples from Java I/O Interface InputStream Concrete subclasses FileInputStream, ByteArrayInputStream Decorators BufferedInputStream, DataInputStream Code InputStream s = new DataInputStream( new BufferedInputStream (new FileInputStream()));