08/15/09 Design Patterns James Brucker.

Slides:



Advertisements
Similar presentations
Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
Advertisements

James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
Design Patterns Yes, they are important Robert Cotton April 23, 2009.
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.
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.
James Tam Introduction To Design Patterns You will learn about design techniques that have been successfully applied to different scenarios.
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 Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson,Ralph Johnson and John Vlissides (The Gang of.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
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.
Object Oriented Software Development
Design Patterns Alan Shalloway, James Trott, Design Patterns Explained, Addison-Wesley, Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements.
Design Patterns.
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.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
Design Patterns in Java Chapter 1 Introduction Summary prepared by Kirk Scott 1.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
GRASP: Designing Objects with Responsibilities
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Testing Extensible Design Patterns in OO Frameworks through Scenario Templates D.S. Sanders Software Verification & Validation.
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.
05/26/2004www.indyjug.net1 Indy Java User’s Group May Knowledge Services, Inc.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Patterns in programming
Introduction To Design Patterns
GRASP – Designing Objects with Responsibilities
Design Patterns: MORE Examples
Software Design Refinement Using Design Patterns
COMP2110 Software Design in lecture 14 Patterns(1) /Detailed Design
Abstract Factory Pattern
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns Lecture part 2.
Design Patterns.
CMPE 135: Object-Oriented Analysis and Design October 24 Class Meeting
Abstract Factory Pattern
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Intent (Thanks to Jim Fawcett for the slides)
Designing with Interfaces
Advanced Programming Behnam Hatami Fall 2017.
Informatics 122 Software Design II
Singleton Pattern Pattern Name: Singleton Pattern Context
Collections James Brucker.
DESIGN PATTERNS : Introduction
Interator and Iterable
CMPE 135 Object-Oriented Analysis and Design March 21 Class Meeting
Informatics 122 Software Design II
Chapter 8, Design Patterns Introduction
Software Design Lecture : 39.
Presentation transcript:

08/15/09 Design Patterns James Brucker

Reusable Ideas Developers reuse knowledge, experience, & code 08/15/09 Reusable Ideas Developers reuse knowledge, experience, & code Application Level reuse a project design & code of a similar project Design Level apply known design principles and design patterns Logic Level apply known algorithms to implement some behavior Method Implementation Level use programming idioms for common tasks

A Programming Idiom Problem: process every element of an array... 08/15/09 A Programming Idiom Problem: process every element of an array... Idiom: 1. initialize result 2. loop over the array 3. process each element of the array // add the values of all the coupons we have sold... Coupon [ ] coupons = ... double total = 0; // initialize for( int k=0; k<coupons.length; k++ ) { // loop total += coupons[k].getTotal(); // process each one }

08/15/09 An Algorithm Problem: find the shortest path from nodes A to B in a graph Solution: apply Dykstra's Shortest Path algorithm

Reusable Code Requirement: 08/15/09 Reusable Code Requirement: sort a List of Persons by last name. Ignore case. Solution: Write a Comparator and use Collections.sort List<Person> people = manager.getPeople( ); Comparator<Person> compByName = new Comparator<>() { public int compare(Person a, Person b) { return a.getLastname().compareToIgnoreCase( b.getLastname()); } }; java.util.Collections.sort( people, compByName );

Reusable Code Requirement: 08/15/09 Reusable Code Requirement: record activity & events of our program in a file, so we have a record of what the program has done and any problems that occur. Solution: Use the open-source Log4J or slf4j framework. public class Purse { private static final Logger log = Logger.getLogger(Purse.class); public boolean insert(Money m) { if (m == null) log.error("argument is null"); else log.info("inserting " + m);

Logger Output Log File: 08/15/09 Logger Output Log File: You control where logging is output, and how much detail is recorded. Config file: log4j.properties. Example: 6:02:27 Purse insert INFO inserting 10 Baht 6:03:00 Purse insert INFO inserting 20 Baht 6:03:10 Purse insert ERROR argument is null 6:03:14 Purse withdraw INFO withdraw 10 Baht message Class and Method Severity

What is a Design Pattern? 08/15/09 What is a Design Pattern? A situation that occurs over and over, along with a reusable design of a solution

Format for Describing a Pattern 08/15/09 Format for Describing a Pattern Pattern Name: Iterator Context We need to access elements of a collection. Motivation (Forces) We want to access elements of a collection without the need to know the underlying structure of the collection. Solution Each collection provides an iterator with methods to get the next element and check for more elements. Consequences Application is not coupled to the collection. Collection type can be changed w/o changing the application.

<<interface>> 08/15/09 Diagram for Iterator <<interface>> Iterator<T> hasNext( ): bool next( ): T ConcreteIterator hasNext( ): bool next( ): T

08/15/09 Examples of Iterator What Iterators have you used?

How do you Get an Iterator? 08/15/09 How do you Get an Iterator? Context: We want to create an Iterator without knowing the class of the group of objects. Forces: We don't want the code to be coupled to a particular collection. We want to always create iterators in the same way, regardless of the group of objects it refers to or how the Iterator is actually created. Collection<String> stuff = Foo.makeCollection( ); Iterator<String> iterator = stuff.iterator( );

Solution: Define a Factory Method 08/15/09 Solution: Define a Factory Method A factory method is a method that creates other objects. <<interface>> Iterable<T> iterator( ): Iterator<T> <<interface>> Iterator<T> hasNext( ): bool next( ): T creates Collection iterator() ConcreteIterator creates

Structure of Iterator Pattern 08/15/09 Structure of Iterator Pattern access elements of Collection iterator( ): Iterator Application Iterator hasNext( ): bool next( ): Element creates ConcreteIterator cursor hasNext( ): bool next( ): Element Data Source

Example List<String> list = new ArrayList<>( ); 08/15/09 Example List<String> list = new ArrayList<>( ); list.add( "apple" ); . . . // add more elements Iterator<String> iter = list.iterator( ); while( iter.hasNext( ) ) { System.out.println( iter.next() ); }

Design Patterns - Gang of Four book 08/15/09 Design Patterns - Gang of Four book The "Gang of Four" The first book to popularize the idea of software patterns: Gamma, Helm, Johnson, Vlissides Design Patterns: Elements of Reusable Object- Oriented Software. (1995)

Good Design Patterns Books 08/15/09 Good Design Patterns Books Good for Java programmers Design Patterns Explained, 2E (2004) by Allan Shallow & James Trott also wrote: Pattern Oriented Design. Head First Design Patterns (2004) by Eric & Elizabeth Freeman Visual & memorable examples, code is too simple.

Structure of Patterns in Gang of Four book 08/15/09 Name of Pattern Intent what the pattern does. Motivation Why this pattern. When to apply this pattern Structure Logical structure of the pattern. UML diagrams. Participants and Collaborators What are the elements of the pattern? What do they do? Consequences The benefits and disadvantages of using the pattern.

Iterator Pattern Pattern Name: Iterator Context 08/15/09 Iterator Pattern Pattern Name: Iterator Context We need to access elements of a collection. Motivation (Forces) We want to use or view elements of a collection without the need to know the underlying structure of the collection. Solution Each collection provides an iterator with methods to check for more elements and get the next element.

Design Patterns To Know 08/15/09 Design Patterns To Know 1. Iterator 2. Adapter 3. Factory Method 4. Decorator 5. Singleton 6. Strategy - Layout Manager, used in a Container 7. State 8. Command 9. Observer 10. Facade

SKE Favorite Design Patterns 08/15/09 SKE Favorite Design Patterns The SKE12 Software Spec & Design class were asked: "What patterns are most instructve or most useful?"

SKE12 Patterns Votes Pattern Votes MVC 18 State 17 Factory Method 16 08/15/09 SKE12 Patterns Votes Pattern Votes MVC 18 State 17 Factory Method 16 Command 15 Strategy 15 Facade 12 Singleton 12 Iterator 11 Observer 11 Adapter 8 Decorator 4 Template Method 3

Categories of Patterns 08/15/09 Categories of Patterns Creational - how to create objects Structural - relationships between objects Behavioral - how to implement some behavior

Situations (Context) not Patterns 08/15/09 Situations (Context) not Patterns Learn the situation and the motivation (forces) that motivate the solution. Pay attention to Applicability for details of context where the pattern applies. (Avoid applying the wrong pattern.)

Adding New Behavior Situation: 08/15/09 Adding New Behavior Situation: we want to add some new behavior to an existing class Forces: 1. don't want to add more responsibility to the class 2. the behavior may apply to similar classes, too Example: Scrollbars

Changing the Interface 08/15/09 Changing the Interface Situation: we want to use a class in an application that requires interface A. But the class doesn't implement A. Forces: 1. not appropriate to modify the existing class for the new application 2. we may have many classes we need to modify Example: change an Enumeration to look like an Iterator

Convenient Implementation 08/15/09 Convenient Implementation Situation: some interfaces require implementing a lot of methods. But most of the methods aren't usually required. Forces: 1. how can we make it easier to implement interface? 2. how to supply default implementations for methods? Example: MouseListener (6 methods), List (24 methods)

A Group of Objects act as One 08/15/09 A Group of Objects act as One Situation: we want to be able to use a Group of objects in an application, and the application can treat the whole group like a single object. Forces: There are many objects that behave similarly. To avoid complex code we'd like to treat as one object. Example: KeyPad in a mobile phone app.

Creating Objects without Knowing Type 08/15/09 Situation: we are using a framework like OCSF. the framework needs to create objects. how can we change the type of object that the framework creates? Forces: 1. want the framework to be extensible. 2. using "new" means coupling between the class and the framework. Example: JDBC (Java Database Connection) creates connections for different kinds of databases.

Do Something Later Situation: 08/15/09 Do Something Later Situation: we want to run a task at a given time (in the future) Forces: we don't want our "task" to be responsible for the schedule of when it gets run. This situation occurs a lot, so we need a reusable solution. Example: We're writing a digital clock. We want an alarm to sound at a specified time.

Situations (Context) not Patterns 08/15/09 Situations (Context) not Patterns Don't memorize pattern names. Learn the situation and the goals (forces) that motivate the solution.