What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003

Similar documents
Design Patterns. Lecture 10: OOP, autumn 2003

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

Creational Design Patterns

Design Patterns! Acknowledgements!

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Tuesday, October 4. Announcements

The Strategy Pattern Design Principle: Design Principle: Design Principle:

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

SDC Design patterns GoF

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

Design Patterns. Software Engineering. Sergio Feo-Arenis slides by: Matthias Keil

CSE870: Advanced Software Engineering (Cheng) 1

families of related or dependent objects

Design Patterns. Softwaretechnik. Matthias Keil. Albert-Ludwigs-Universität Freiburg

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

COSC 3351 Software Design. Design Patterns Structural Patterns (II) Edgar Gabriel. Spring Decorator

Object Oriented Paradigm

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Composite Pattern. IV.4 Structural Pattern

DESIGN PATTERNS Course 4

Introduction and History

CPSC 310 Software Engineering. Lecture 11. Design Patterns

Top Down Design vs. Modularization

Introduction to Software Engineering: Object Design I Reuse & Patterns

Lecture 20: Design Patterns II

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Design Pattern and Software Architecture: IV. Design Pattern

Object-Oriented Oriented Programming

Software Design COSC 4353/6353 D R. R A J S I N G H

COURSE 4 DESIGN PATTERNS

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Object-oriented Software Design Patterns

The 'Gang of Four' Companion

COURSE 2 DESIGN PATTERNS

An Introduction to Patterns

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Topics in Object-Oriented Design Patterns

6.3 Patterns. Definition: Design Patterns

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Design patterns. Jef De Smedt Beta VZW

Object-Oriented Oriented Programming Factory Method Pattern Abstract Factory Pattern. CSIE Department, NTUT Woei-Kae Chen

INTERNAL ASSESSMENT TEST III Answer Schema

Design Patterns. An introduction

Design Patterns Reid Holmes

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

A Reconnaissance on Design Patterns

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Design Patterns Reid Holmes

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

INSTITUTE OF AERONAUTICAL ENGINEERING

Design Patterns: Structural and Behavioural

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

In this Lecture you will Learn: Design Patterns. Patterns vs. Frameworks. Patterns vs. Frameworks

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

Design Patterns. Gunnar Gotshalks A4-1

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

CS 2720 Practical Software Development University of Lethbridge. Design Patterns

Introduction to Design Patterns

EECS 4314 Advanced Software Engineering. Topic 04: Design Pattern Review Zhen Ming (Jack) Jiang

Design for change. You should avoid

Overview CS Kinds of Patterns. Design Pattern. Factory Pattern Rationale. Kinds of Factory Patterns

Software Eningeering. Lecture 9 Design Patterns 2

JAVA MOCK TEST JAVA MOCK TEST II

Design Patterns Lecture 2

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

CS 520/620 Advanced Software Engineering Fall September 27, 2016

What is Design Patterns?

Summary of the course lectures

UNIT I Introduction to Design Patterns

DESIGN PATTERN - INTERVIEW QUESTIONS

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

Interfaces, Polymorphism and Iteration. Lecture 7a: OOP, autumn 2003

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

CS 520/620 Advanced Software Engineering Spring February 11, 2016

David Talby March 21, 2006

CSE 70 Final Exam Fall 2009

Chapter 8, Design Patterns Visitor

Using Design Patterns in Java Application Development

Object-Oriented Design

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Exam in TDDB84: Design Patterns,

CS/CE 2336 Computer Science II

The GoF Design Patterns Reference

TDDB84. Lecture 2. fredag 6 september 13

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Software Design Patterns. Aliaksei Syrel

design patterns FOR B.tech (jntu - hyderabad & kakinada) (IV/I - CSE AND IV/II - IT) CONTENTS 1.1 INTRODUCTION TO DESIGN PATTERNS TTERNS... TTERN?...

2.1 Design Patterns and Architecture (continued)

CSE 401/M501 Compilers

Transcription:

What are patterns? Design Patterns Lecture 10: OOP, autumn 2003 Many recurring problems are solved in similar ways This wisdom is collected into patterns design patterns - about software design Other kinds - analysis, organization, etc. patterns General definitions: An abstraction from a concrete form which keeps recurring in specific, non-arbitrary contexts. A recurring solution to a common problem in a given context and system of forces. A successfully recurring "best practice" that has proven itself in the "trenches". A literary format for capturing the wisdom and experience of expert designers, and communicating it to novices Antipatterns (http://www.antipatterns.com/) - negative experience what doesn t work and why Already familiar with: iterators, template methods 2 Design patterns Based on OO principles - inheritance & the substitution principle Goals Improve performance, code quality Provide a common vocabulary for developers Danger: can increase complexity if misused Kinds of patterns Creational - ways to create objects Structural - ways to combine classes & objects into structures Behavioral - communication between objects Pattern structure (simplified) Problem - statement of the problem and intent of the solution Forces - the kinds of criteria that justify designs and implementations (correctness, performance, space usage, modularity, extensibility, maintainability) Solution - static structure & dynamic behavior Creational patterns Factory method Abstract factory In Liskov: factory classes and factory objects Singleton 3 4 The factory pattern Factory pattern structure Dependencies on constructors => class dependencies Need to hide actual object class e.g. abstract collections vs. implementation use factory methods that produce the right objects Example: iterators create generator objects hide actual class of generators (assume only Iterator interface) Using code becomes simpler independent of actual class => more modular and maintainable 5 6

Factory pattern example The abstract factory pattern public class Set { private Object[] contents; public Iterator iterator() { return new SetIterator(); private class SetIterator implements Iterator { long currentindex = 0; public boolean hasnext() { public Object next() { public class Client { public somemethod(set s) { Iterator it = s.iterator(); while(it.hasnext()) { // do something Client Iterator next() hasnext() <<implements>> SetIterator (from Set) <<creates>> Set iterator() 7 need to produce objects from a set of compatible classes Examples: Different GUI look&feel (Motif vs. Windows) Different database storage (memory vs. disk) Different communication protocols provide one abstract factory class with many factory methods factory subclasses provide specific implementations client code uses specific factory objects factory subclasses produce objects of consistent classes Benefits: simplify using code independence from actual object classes avoid errors when creating objects 8 Abstract factory pattern structure Abstract factory pattern example abstract class Window { class MotifWindow extends Window { class AWTWindow extends Window { abstract class Scrollbar { class MotifScrollBar extends Scrollbar { class AWTScrollBar extends Scrollbar { abstract class WidgetFactory { public static WidgetFactory getwidgetfactory(); Window createwindow( ); ScrollBar createscrollbar( ); class MotifWidgetFactory implements WidgetFactory { class AWTWidgetFactory implements WidgetFactory { 9 10 When to use factories The singleton pattern NOT when just reimplementing a type When multiple implementations of a type When sets of classes provide different versions of the same code Use abstract factories to reduce dependency on factory classes 11 Problem at most one object per type class objects are not convenient Examples Single database connection Single root window Benefits Improve performance Reduce errors Potential mistakes Use directly the static method Implement by static class // Example public class DBConnection { private static DBConnection conn; private DBConnection(); public getdbconnection( ) { if (conn == null) { conn = new DBConnection(); return conn; 12

Structural patterns Flyweights Proxy Adapter Composite The flyweight pattern Many instances of identical objects Constructors always produce new objects Need to reduce storage Example: many copies of the same string (e.g. in a document) many font objects Flyweights must be immutable Enough shared objects to justify overhead Use one flyweight object to represent all identical instances Use a factory (method/class) to produce flyweights 13 14 Flyweight pattern structure The proxy pattern FlyweightFactory creates and manages flyweight objects ConcreteFlyweight implements the Flyweight interface and adds storage for the intrinsic state, if any Intrinsic data makes the instance unique Extrinsic data is the information passed in as arguments in method calls The Client references the Flyweight object Provides a surrogate for the real object Useful when the real object needs protection (e.g. limited access user access to a proxy) is a remote object (e.g. RMI) lacks some good-to-have actions ( smart reference, e.g. load at first access) The proxy knows about the real object and implements the same interface 15 16 Adaptor Converts the interface of one class into another interface Lets classes work together that couldn't otherwise because of incompatible interfaces The composite pattern Problem Nested structure All objects belong to the same type hierarchy Component and leaf nodes Examples User interfaces - GUI elements Compilers - parse trees 17 18

Composite pattern structure Behavioral patterns For traversal of composites Interpreter Visitor Control abstraction Observer 19 20 The interpreter pattern Interpreter pattern structure How to traverse a composite Apply some computation over the tree Solution Each node has a method per computation Each method calls children methods Easy to add new nodes Hard to add new computations over the whole composite 21 22 The visitor pattern Visitor pattern structure How to traverse a composite Apply some computation over the tree Composite nodes know they structure Visitors implement various algorithms Easy to add nodes Easy to add new computations More complex 23 24

The observer pattern Observer structure One change affects one or many objects. Many object behaviors depends on one object state. Need broadcast communication Decouple classes into observers and subjects 25 26