Introduction to Design Patterns

Slides:



Advertisements
Similar presentations
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Advertisements

Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Plab – Tirgul 12 Design Patterns
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
. Plab – Tirgul 12 Design Patterns. Design Patterns u The De-Facto Book on Design Patterns:
Spring 2010CS 2251 Design Patterns. Spring 2010CS 2252 What is a Design Pattern? "a general reusable solution to a commonly occurring problem in software.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
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.
Unified Modeling Language
Introduction to software design patterns For CSE 3902 By: Matt Boggus.
Introduction To System Analysis and design
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
Design Patterns.
02 - Behavioral Design Patterns – 2 Moshe Fresko Bar-Ilan University תשס"ח 2008.
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.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Observer Behavioral Pattern. Intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified.
CS3773 Software Engineering Lecture 04 UML Class Diagram.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
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.
ECE450S – Software Engineering II
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IV Structural Patterns.
CSC 480 Software Engineering Design With Patterns.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
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.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 31. Review Creational Design Patterns – Singleton Pattern – Builder Pattern.
CSC 480 Software Engineering Design With Patterns.
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.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
Duke CPS Programming Heuristics l Identify the aspects of your application that vary and separate them from what stays the same ä Take what varies.
Overview of Behavioral Patterns ©SoftMoore ConsultingSlide 1.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Examples (D. Schmidt et al)
GRASP – Designing Objects with Responsibilities
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Strategy Design Pattern
Chapter 7 – Object-Oriented Design
Chapter 10 Design Patterns.
Software Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
Presented by Igor Ivković
Software Engineering Lecture 7 - Design Patterns
Adapter Pattern 1.
Object Oriented Design Patterns - Structural Patterns
Object Oriented Design Patterns - Behavioral Patterns
CSC 480 Software Engineering
An Introduction to Software Architecture
Informatics 122 Software Design II
CSC 480 Software Engineering
Object Oriented System Design Class Diagrams
Presented by Igor Ivković
CSC 480 Software Engineering
Presentation transcript:

Introduction to Design Patterns Slides adapted from Craig Zilles

Design Pattern “Each pattern describes a problem which occurs over and over again in our 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.” -- Christopher Alexander Each pattern has 4 essential elements: A name The problem it solves The solution The consequences

Let’s start with some “Micro-Patterns” (1) Name: Most-wanted holder Problem: Want to find the “most wanted” element of a collection. Solution: Initialize most-wanted holder to first element. Compare every other element to value in most-wanted holder, replace if the new value is better. Thing mostWanted = things[0]; for (int i = 1 ; i < things.length ; i ++) { if (thing[i].isBetterThan(mostWanted)) { mostWanted = thing[i]; }

Let’s start with some “Micro-Patterns” (2) Name: One-way flag Problem: Want to know if a property is true/false for every element of a collection. Solution: Initialize a boolean to one value. Traverse the whole collection, setting the boolean to the other value if an element violates the property. boolean allValid = true; for (Thing thing : things) { if (!thing.isValid()) { allValid = false; break; }

Let’s start with some “Micro-Patterns” (3) Name: Follower Problem: Want to compare adjacent elements of collection. Solution: As you iterate through a collection, set the value of the follower variable to the current element as the last step. boolean collectionInOrder = true; Thing follower = null; for (Thing thing : things) { if (follower != null && thing.isBiggerThan(follower)) { collectionInOrder = false; } follower = thing;

Other ”Micro-Patterns”

“Design Patterns” focus on object-level Relate to relationships between classes & objects IsA (inheritance) and HasA (containment) relationships Many of these seem obvious (in hind sight) The power is giving these names, codifying a best practice solution, and understanding their strengths/limitations.

UML Class Diagrams Unified Modeling Language (UML) A standard for diagrammatic representations in software engineering. The Class Diagram is the main building block for object-oriented modeling; it shows: the system's classes their attributes and operations (or methods), and the relationships among objects

Class/Object Notation Class definitions Abstract in italics Methods have parentheses Variables do not Types are optional; included when useful

Class/Object Notation (cont.) Class relationships Diamond = Has A collection of Solid dot = multiple Triangle = Inheritance (Is A) Solid line = Has A (containment) Dashed line = creates

Class/Object Notation (cont.) Object instances Objects have rounded corners

Strategy Intent: define a family of algorithms, encapsulate each one, and make them interchangable. Strategy lets the algorithm vary independently from clients that use it. Use the strategy pattern when: Many related classes differ only in their behavior. You need different variants of an algorithm (e.g., trade-offs) An algorithm uses data that clients shouldn’t know about E.g., encapsulate the algorithm data from client A class defines multiple behaviors and these are implemented using conditionals.

Strategy Pattern Solution Strategy abstract base class exposes algorithm interface. Context object HasA Concrete Strategy object. Context object invokes algorithm interface from strategy.

Problem: Social media updates You have your InstaTwitInYouFaceTrest app open and a friend makes a post / updates their status. How do you get the info before the next time you (manually) refresh your app?

The Observer Pattern (a.k.a. Publish/Subscribe) Problem: Keep a group of objects “in sync” in the presence of asynchronous updates, while minimizing the amount of coupling. Intent: Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. Use the Observer pattern when: When changes to one object requires changes to other and you don’t know which and/or how many. When an object should be able to notify other objects without making assumptions about who these other objects are (i.e., you don’t want these objects tightly coupled).

Observer Pattern A) Classes B) Objects

Observer Pattern A) HasA (containment) B) IsA (inheritance)

Observer Pattern Solution: Observers can “attach” to a Subject. When Subject is updated, it calls Update() on all Observers Observers can query Subject for updated state.

Class/Object Notation (cont.) Interaction Diagram Solid vertical line = existed before/after interaction Dashed vertical line = didn’t exist Time passing Dashed horizontal line = creation Box = period active during interaction Solid horizontal line = invocation

Observer Pattern Interaction Example aConcreteObserver modifies a ConcreteSubject

Adapter Pattern Intent: Convert the interface of a class into another interface that a client expects. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces. Use the Adapter pattern when: You want to use an existing class and interface doesn’t match the one that you need You want to create a reusable class that cooperates with unrelated and unforeseen classes (non-compatible interfaces) You need to use several existing subclasses, but it’s impractical to adapt their interface by subclassing every one.

Adapter Pattern Solution: Adapter class IsA derived class of Target type Adapter class HasA Adaptee class Adapter class delegates requests to Adaptee class

Scrabble

Scrabble word score Sum of the letter values

Scrabble word score, continued public static int wordScore(String word) { int score = 0; for (int i = 0 ; i < word.length() ; i++) { char letter = word.charAt(i); score += letterScore(letter); } return score;

Control-flow based public static int letterScore(char c) { char upperC = Character.toUpperCase(c); switch (upperC) { case 'A': case 'E': case 'I': case 'L': case 'N': case 'O': case 'R': case 'S': case 'T': case 'U': return 1; case 'D': case 'G': return 2; case 'B': case 'C': case 'M': case 'P': return 3; case 'F': case 'H': case 'V': case 'W': case 'Y': return 4; case 'K': return 5; case 'J': case 'X': return 8; case 'Q': case 'Z': return 10; default: // handle error } // should never reach here return 0;

Table-based Solution private static final int [] scoresByChar = {/* A */ 1, /* B */ 3, /* C */ 3, /* D */ 2, /* E */ 1, /* F */ 4, /* G */ 2, /* H */ 4, /* I */ 1, /* J */ 8, /* K */ 5, /* L */ 1, /* M */ 3, /* N */ 1, /* O */ 1, /* P */ 3, /* Q */ 10, /* R */ 1, /* S */ 1, /* T */ 1, /* U */ 1, /* V */ 4, /* W */ 4, /* X */ 8, /* Y */ 4, /* Z */ 10}; public static int letterScore2(char c) { char cAsUppercase = Character.toUpperCase(c); int index = cAsUppercase - 'A'; if (index < 0 || index >= 26) { // handle error } return scoresByChar[index];