Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design.

Slides:



Advertisements
Similar presentations
What Are Design Patterns and Why Do I Need Them? Software professionals may be familiar with the term "Design Patterns," but many have no idea of where.
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.
Lecture 9 Design Patterns CSCI – 3350 Software Engineering II Fall 2014 Bill Pine.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
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.
(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.
Dept. of Computer Engineering, Amirkabir University of Tech. 1 Design Patterns Dr. Noorhosseini Introduction.
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.
Visual Basic: An Object Oriented Approach 11 – Patterns in object oriented programming.
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 William A. Hoffman NYU OOP Class.
(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.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns Alan Shalloway, James Trott, Design Patterns Explained, Addison-Wesley, Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements.
Design Patterns Trends and Case Study John Hurst June 2005.
Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design.
樣式導向設計 (Pattern-Oriented Design) 課程簡介 Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering.
Patterns 1 Patterns in OO - history 77 - Work of the architect Christopher Alexander 80s - Kent Beck playing with Smalltalk 91 - Gamma’s PhD thesis on.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
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.
January 12, Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable.
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.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
Behavioral Design Patterns Morteza Yousefi University Of Science & Technology Of Mazandaran 1of 27Behavioral Design Patterns.
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.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
How Would You Solve This? l Suppose that you want to have one and only one instance of a class – Can you think of ever only wanting one instance of a class?
Testing Extensible Design Patterns in OO Frameworks through Scenario Templates D.S. Sanders Software Verification & Validation.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
How Would You Solve This?
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
樣式導向設計 (Pattern-Oriented Design) 課程簡介
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns Lecture part 2.
Introduction to Design Patterns
Design Patterns Introduction
object oriented Principles of software design
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
CSE 403 Software Design.
DESIGN PATTERNS : Introduction
Informatics 122 Software Design II
Chapter 8, Design Patterns Introduction
Presentation transcript:

Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design

Outline Overview of Design Patterns Two Design Patterns –Iterator, one you have used –Strategy, from Heads First Chapter 1

The Beginning of Patterns Christopher Alexander, architect –A Pattern Language--Towns, Buildings, Construction –Timeless Way of Building (1979) –“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.” Other patterns: novels (tragic, romantic, crime), movies genres (drama, comedy, documentary)

“Gang of Four” (GoF) Book Design Patterns: Elements of Reusable Object-Oriented Software, Addison-Wesley Publishing Company, 1994 Written by this "gang of four" –Dr. Erich Gamma, then Software Engineer, Taligent, Inc. –Dr. Richard Helm, then Senior Technology Consultant, DMR Group –Dr. Ralph Johnson, then and now at University of Illinois, Computer Science Department –Dr. John Vlissides, then a researcher at IBM Thomas J. Watson Research Center See John's WikiWiki tribute page

Object-Oriented Design Patterns This book defined 23 patterns in three categories – Creational patterns deal with the process of object creation – Structural patterns, deal primarily with the static composition and structure of classes and objects – Behavioral patterns, which deal primarily with dynamic interaction among classes and objects

Documenting Discovered Patterns Many other patterns have been introduced documented –For example, the book Data Access Patterns by Clifton Nock introduces 4 decoupling patterns, 5 resource patterns, 5 I/O patterns, 7 cache patterns, and 4 concurrency patterns. –Other pattern languages include telecommunications patterns, pedagogical patterns, analysis patterns –Patterns are mined at places like Patterns ConferencesPatterns Conferences

ChiliPLoP A few patterns work-shopped at ChiliPLoP, Wickenburg and Carefree Arizona (books now)ChiliPLoP –Patterns of Enterprise Application Architecture Martin FowlerPatterns of Enterprise Application Architecture –Patterns of Fault Tolerant Software, Bob HamnerPatterns of Fault Tolerant Software –Patterns of Adopting Agile Development Practices Amr ElssamadisyPatterns of Adopting Agile Development Practices –2010: Patterns of Parallel Programming, Ralph Johnson 16 patterns and one Pattern Language

GoF Patterns – Creational Patterns Abstract Factory Builder Factory Method Prototype Singleton – Structural Patterns Adapter Bridge Composite Decorator Façade Flyweight Proxy – Behavioral Patterns Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor

Why Study Patterns? Reuse tried, proven solutions –Provides a head start –Avoids gotchas later (unanticipated things) –No need to reinvent the wheel Establish common terminology –Design patterns provide a common point of reference –Easier to say, “We could use Strategy here.” Provide a higher level prospective –Frees us from dealing with the details too early

Other advantages Most design patterns make software more modifiable, less brittle –we are using time tested solutions Using design patterns makes software systems easier to change—more maintainable Helps increase the understanding of basic object- oriented design principles –encapsulation, inheritance, interfaces, polymorphism

Style for Describing Patterns We will use this structure to describe them: – Pattern name – Recurring problem: what problem the pattern addresses – Solution: the general approach of the pattern – Unified Modeling Language (UML) view of the pattern Participants: a description as a class diagram – Usages: examples of this pattern

A few OO Design Patterns Coming up: – Iterator access the elements of an aggregate object sequentially without exposing its underlying representation – Strategy A means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable

Iterator

Pattern: Iterator Name: Iterator (a.k.a Enumeration) Recurring Problem: How can you loop over all objects in any collection. You don’t want to change client code when the collection changes. Want the same methods Solution: 1) Have each class implement an interface, and 2) Have an interface that works with all collections Consequences: Can change collection class details without changing code to traverse the collection

GoF Version of Iterator page 257 ListIterator First() Next() IsDone() CurrentItem() // A C++ Implementation ListIterator itr = list.iterator(); for(itr.First(); !itr.IsDone(); itr.Next()) { cout << itr.CurrentItem().toString();

Java’s version of Iterator public interface Iterator { boolean hasNext(); E next(); void remove(); //optional } hasNext returns true if the iteration has more elements next returns the next element in the iteration remove removes the last element that was returned by next from the underlying Collection, call only once per next

Java’s Iterator interface // The Client code // The Client code List bank = new ArrayList (); List bank = new ArrayList (); bank.add(new BankAccount(""First", 0.01) ); bank.add(new BankAccount(""First", 0.01) ); //... //... bank.add(new BankAccount("Last", )); bank.add(new BankAccount("Last", )); String ID = "Last"; String ID = "Last"; Iterator itr = bank.iterator(); Iterator itr = bank.iterator(); while(itr.hasNext()) { while(itr.hasNext()) { if(itr.next().getID().equals(searchAcct.getID())) if(itr.next().getID().equals(searchAcct.getID())) System.out.println("Balance " + getBalance()); System.out.println("Balance " + getBalance()); }

UML Diagram of Java's Iterator with a few Collections > Iterator hasNext() next() > List iterator(): Iterator … Client Vector iterator() Iterator hasNext() next() LinkedList iterator() ArrayList iterator()

Strategy Design Pattern Strategy

Pattern: Strategy Name : Strategy (a.k.a Policy) Problem : You want to encapsulate a family of algorithms and make them interchangeable. Strategy lets the algorithm vary independently from the clients that use it (GoF) Solution : Create an abstract strategy class (or interface) and extend (or implement) it in numerous ways. Each subclass defines the same method names in different ways

General Form with extends

Example Usage with implements

Design Pattern: Strategy Consequences: –Allows families of algorithms Known uses from the Gang of 4 book: –ET++ and Interviews use different line breaking strategies –RTL System for compiler code optimization, strategies defining different register allocation schemes and instruction set scheduling policies –SwapsManager calculation engine framework computes prices for different financial instruments –RApp use strategies to lay out route wires to connect subsytems on the circuits Rapp produces

Design Pattern: Strategy –Saving files in different formats, Word, ODT, RTF, HTML plain text. –Compress files using different compression algorithms. –Capture video using different video compression algorithms. –Plot the same data using different formats (points, line chart, bar chart, etc.) –Display calendars, with different holidays for different countries (Strategy classes are USAHoliday, CanadaHoliday… –A store may have various pricing strategies (10% off everything, $10 off when the total exceeds $200, etc.), and these strategies may be used at different times.

Design Pattern: Strategy Known uses more locally –Layout managers in Java –Different Poker Strategies in a 335 Project –Different PacMan ghost chasing strategies in a 335 Project –Different Strategies for taking over the world in a 335 Project

Code Demo Play tic tac toe against two different AIs