Design Patterns. V22.0474-001 Software Engineering Lecture 8. Adapted from Prof. Necula CS 169, Berkeley 1



Similar documents
Patterns in. Lecture 2 GoF Design Patterns Creational. Sharif University of Technology. Department of Computer Engineering

Gadget: A Tool for Extracting the Dynamic Structure of Java Programs

How To Use The Command Pattern In Java.Com (Programming) To Create A Program That Is Atomic And Is Not A Command Pattern (Programmer)

Génie Logiciel et Gestion de Projets. Patterns

Structural Design Patterns Used in Data Structures Implementation

CMSC 132: Object-Oriented Programming II. Design Patterns I. Department of Computer Science University of Maryland, College Park

Scratch Primary Lesson 4

Content Author's Reference and Cookbook

ARIS Design Platform Getting Started with BPM

Fundamentals of Java Programming

Rapid Application Development of a Decision Support System using Object Oriented Programming

Quickstart for Desktop Version

I ntroduction. Accessing Microsoft PowerPoint. Anatomy of a PowerPoint Window

Name of pattern types 1 Process control patterns 2 Logic architectural patterns 3 Organizational patterns 4 Analytic patterns 5 Design patterns 6

Java Application Developer Certificate Program Competencies

WYSIWYG Tips and FAQ

Computing Concepts with Java Essentials

Design Patterns. Design patterns are known solutions for common problems. Design patterns give us a system of names and ideas for common problems.

core. Volume I - Fundamentals Seventh Edition Sun Microsystems Press A Prentice Hall Title ULB Darmstadt

A First Set of Design Patterns for Algorithm Animation

Assignment # 2: Design Patterns and GUIs

A QUICK OVERVIEW OF THE OMNeT++ IDE

Graphic Design for Beginners

BELEN JESUIT PREPARATORY SCHOOL Computer Science Department COURSE DESCRIPTIONS. And OBJECTIVES

How to create labels using a Microsoft Access data file?

Content Author's Reference and Cookbook

Creating Online Surveys with Qualtrics Survey Tool

Design principles of the Drupal CSC website

Software Development Methodologies

Excerpts from Chapter 4, Architectural Modeling -- UML for Mere Mortals by Eric J. Naiburg and Robert A. Maksimchuk

Domains and Competencies

Object Oriented Design

Glossary of Object Oriented Terms

Microsoft Access 2010 handout

GenericServ, a Generic Server for Web Application Development

Encapsulating Crosscutting Concerns in System Software

Browsing and working with your files and folder is easy with Windows 7 s new look Windows Explorer.

USER CONVERSION P3, SURETRAK AND MICROSOFT PROJECT ASTA POWERPROJECT PAUL E HARRIS EASTWOOD HARRIS

Content Management System

Flash Tutorial Part I

Types of UML Diagram. UML Diagrams OOAD. Computer Engineering Sem -IV

Modeling the User Interface of Web Applications with UML

Guide to SAS/AF Applications Development

ABSTRACT FACTORY AND SINGLETON DESIGN PATTERNS TO CREATE DECORATOR PATTERN OBJECTS IN WEB APPLICATION

Publisher 2010 Cheat Sheet

Software Refactoring using New Architecture of Java Design Patterns

Johannes Sametinger. C. Doppler Laboratory for Software Engineering Johannes Kepler University of Linz A-4040 Linz, Austria

Ohio University Computer Services Center August, 2002 Crystal Reports Introduction Quick Reference Guide

Getting Started with Excel Table of Contents

From Systems to Services

Chapter 3 Chapter 3 Service-Oriented Computing and SOA Lecture Note

Creating Carbon Menus. (Legacy)

Microsoft Word 2013 Tutorial

Architecture, Design and Implementation of a Mathematical Derivation Editor. Johannes Eriksson

Triggers & Actions 10

Implementação. Interfaces Pessoa Máquina 2010/ Salvador Abreu baseado em material Alan Dix. Thursday, June 2, 2011

game development documentation game development documentation: concept document

Creating Comic Life Images Using Microsoft Word, Clipart, and Irfanview.

Word basics. Before you begin. What you'll learn. Requirements. Estimated time to complete:

ADOBE MUSE. Building your first website

AP Computer Science AB Syllabus 1

MiniDraw Introducing a framework... and a few patterns

Design Patterns. Advanced Software Paradigms (A. Bellaachia) 1

Using the Query Analyzer

HIT THE GROUND RUNNING MS WORD INTRODUCTION

Microsoft Word 2010 Tutorial

Microsoft Word 2010 Tutorial

Designing portal site structure and page layout using IBM Rational Application Developer V7 Part of a series on portal and portlet development

A Graphical User Interface Testing Methodology

Top 10 Oracle SQL Developer Tips and Tricks

SAP Business Intelligence (BI) Reporting Training for MM. General Navigation. Rick Heckman PASSHE 1/31/2012

This document covers version of BPMN2 Modeler, published November 15, 2013.

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Compiling Object Oriented Languages. What is an Object-Oriented Programming Language? Implementation: Dynamic Binding

A Pattern Language for Developing Web based Multi Source Data Acquisition Application 1

PowerPoint 2013 Basics of Creating a PowerPoint Presentation

Beginning Word. Objectives: You will-

Software Engineering Transfer Degree

ARCHITECTURAL DESIGN OF MODERN WEB APPLICATIONS

CHAPTER 1: CLIENT/SERVER INTEGRATED DEVELOPMENT ENVIRONMENT (C/SIDE)

Contents. Introduction and System Engineering 1. Introduction 2. Software Process and Methodology 16. System Engineering 53

SiteBuilder 2.1 Manual

Design document Goal Technology Description

An Automatic Reversible Transformation from Composite to Visitor in Java

USER GUIDE. Unit 4: Schoolwires Editor. Chapter 1: Editor

Composing Concerns with a Framework Approach

User Guide QAD Customer Relationship Management. Introduction Sales Management Marketing Management Customer Service Reference

Baseline Code Analysis Using McCabe IQ

CREATING FORMAL REPORT. using MICROSOFT WORD. and EXCEL

In this session, we will explain some of the basics of word processing. 1. Start Microsoft Word 11. Edit the Document cut & move

TEACHING COMPUTER PROGRAMMING WITH PROGRAM ANIMATION

What s New V 11. Preferences: Parameters: Layout/ Modifications: Reverse mouse scroll wheel zoom direction

SignalDraw: GUI Tool For Generating Pulse Sequences

Flowcharting, pseudocoding, and process design

Transcription:

Design Patterns V22.0474-001 Software Engineering Lecture 8 1

How Do We Design Software? We all understand Algorithms Data structures Classes When describing a design, algorithms/data structures/classes form the vocabulary But there are higher levels of design 2

Design Patterns: History Christopher Alexander An architect A professor The father of design patterns As applied to architecture Pattern Languages (1977) Design Patterns in Software Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides Application of design patterns to object-oriented programming Book: Design Patterns: Elements of Reusable Object- Oriented Software 3

What are Design Patterns? A pattern describes a problem that occurs often, along with a tried solution to the problem - Christopher Alexander, 1977 Descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context Not individual classes or libraries Such as lists, hash tables Not full designs 4

Elements of a Design Pattern 1. Pattern name Useful part of design vocabulary 2. Problem solved and applicability When to apply the pattern 3. Solution Participants and their relationships 4. Consequences Costs of applying the pattern, space and time trade-offs 5

Improved Communication One of the main benefits of design patterns is that they name common (and successful) ways of building software. 6

More Specifically Teaching and learning It is much easier to learn architecture from descriptions of design patterns than from reading code Teamwork Members of a team have a way to name and discuss the elements of their design 7

Example: A Text Editor Describe a text editor using patterns A running example Introduces several important patterns Gives an overall flavor of pattern culture Note: This example is from the Design Patterns book. 8

Text Editor Requirements A WYSIWYG editor Text and graphics can be freely mixed Graphical user interface Toolbars, scrollbars, etc. Multiple windowing systems Traversal operations: spell-checking, hyphenation 9

The Game I describe a design problem for the editor I ask What is your design? This is audience participation time I give you the wise and insightful pattern 10

Problem: Document Structure A document is represented by its physical structure: Primitive glyphs characters, rectangles, circles, pictures,... Lines A sequence of glyphs Columns A sequence of lines Pages A sequence of columns Documents A sequence of pages What is your design? 11

Alternative Designs Classes for Character, Circle, Line, Column, Page, Not so good A lot of code duplication One (abstract) class of Glyph Each element realized by a subclass of Glyph All elements present the same interface How to draw Compute bounding rectangle Mouse hit detection Makes extending the class easy Treats all elements uniformly 12

Example of Hierarchical Composition character glyph A c a r picture glyph line glyph (composite) column glyph (composite) 13

Diagram Glyph Draw(Window) Intersects(Point p) n children Character Picture Line Draw(Window) Intersects(Point p) Draw(Window) Intersects(Point p) Draw(Window) Intersects(Point p) 14

Notes Drawing Each primitive element draws itself At its assigned location Each compound element recursively calls draw on its elements But doesn t care what those elements are Line::Draw(Window w) { for each c in children do c->draw(w); } 15

Composites This is the composite pattern Goes by many other names Recursive composition, structural induction, tree walk, Predates design patterns Applies to any hierarchical structure Leaves and internal nodes have same functionality Composite implements the same interface as the contained elements 16

Problem: Formatting A particular physical structure for a document Decisions about layout Must deal with e.g., line breaking Design issues Layout is complicated No best algorithm Many alternatives, simple to complex What is your design? 17

Not So Good Add a format method to each Glyph class Problems Can t modify the algorithm without modifying Glyph Can t easily add new formatting algorithms 18

The Core Issue Formatting is complex We don t want that complexity to pollute Glyph We may want to change the formatting method Encapsulate formatting behind an interface Each formatting algorithm an instance Glyph only deals with the interface 19

Diagram Glyph Draw(Window) Intersects(Point p) Insert(Glyph) Document Draw(Window) Intersects(Point p) Insert(Glyph g) 1 Formatter Format() formatter FormatWord Format() FormatTex Format() 20

Strategies This is the strategy pattern Isolates variations in algorithms we might use Formatter is the strategy, Compositor is context General principle encapsulate variation In OO languages, this means defining abstract classes for things that are likely to change 21

Problem: Enhancing the User Interface We will want to decorate elements of the UI Add borders Scrollbars Etc. How do we incorporate this into the physical structure? What is your design? 22

Not So Good Object behavior can be extended using inheritance Major drawback: inheritance structure is static Subclass elements of Glyph BorderedComposition ScrolledComposition BorderedAndScrolledComposition ScrolledAndBorderedComposition Leads to an explosion of classes 23

Decorators Want to have a number of decorations (e.g., Border, ScrollBar, Menu) that we can mix independently x = new ScrollBar(new Border(new Character)) We have n decorators and 2 n combinations 24

Transparent Enclosure Define Decorator Implements Glyph Has one member Glyph decorated Border, ScrollBar, Menu extend Decorator Border::Draw(Window w) { decorated->draw(w); drawborder(decorated->bounds()); } 25

Diagram Glyph Draw(Win) Border Draw(Win) decorated->draw(w) DrawBorder(w) decorated decorated->draw(w) DrawScrollBar(w) Decorator SetDecorated(Glyph g) Draw(Win) ScrollBar Draw(Win) 26

Decorators This is the decorator pattern A way of adding responsibilities to an object Commonly extending a composite As in this example 27

Problem: Supporting Look-and-Feel Standards Different look-and-feel standards Appearance of scrollbars, menus, etc. We want the editor to support them all What do we write in code like ScrollBar scr = new? What is your design? 28

The Not-so-Good Strawmen Terrible ScrollBar scr = new MotifScrollBar Little better ScrollBar scr; if (style == MOTIF) then scr = new MotifScrollBar else if (style == ) then - will have similar conditionals for menus, borders, etc. 29

Abstract Object Creation Encapsulate what varies in a class Here object creation varies Want to create different menu, scrollbar, etc Depending on current look-and-feel Define a GUIFactory class One method to create each look-and-feel dependent object One GUIFactory object for each look-and-feel Created itself using conditionals 30

Diagram GuiFactory CreateScrollBar() CreateMenu() MotifFactory CreateScrollBar() { return new MotifScrollBar();} CreateMenu() { return new MotifMenu();} MacFactory CreateScrollBar() { return new MacScrollBar()} CreateMenu() { return new MacMenu()} 31

Diagram 2: Abstract Products Glyph ScrollBar scrollto(int); MotifScrollBar scrollto(int); MacScrollBar scrollto(int); 32

Factories This is the abstract factory pattern A class which Abstracts the creation of a family of objects Different instances provide alternative implementations of that family Note The current factory is still a global variable The factory can be changed even at runtime 33

Problem: Supporting Multiple Window Systems We want to run on multiple window systems Problem: Wide variation in standards Big interfaces Can t afford to implement our own windowing system Different models of window operations Resizing, drawing, raising, etc. Different functionality What is your design? 34

A First Cut Take the intersection of all functionality A feature is in our window model if it is in every real-world windowing system we want to support Define an abstract factory to hide variation Create windowing objects for current window system using the factory Problem: intersection of functionality may not be large enough 35

Second Cut Define our own abstract window hierarchy All operations we need represented Model is tuned to our application Define a parallel hierarchy Abstracts concrete window systems Has all functionality we need I.e., could be more than the intersection of functions Requires writing methods for systems missing functionality 36

Diagram wimpl->drawline; wimpl->drawline; wimpl->drawline; wimpl->drawline; wimpl Window DrawRect() WindowImp DrawLine() AppWindow IconWindow MacWindowImp DrawLine() XWindowImp DrawLine() 37

Bridges This is the bridge pattern Note we have two hierarchies Logical The view of our application, tuned to our needs Implementation The interface to the outside world Abstract base class, with multiple implementations Logical, implementational views can evolve independently, So long as the bridge is maintained 38

User Commands User has a vocabulary of operations E.g., jump to a particular page Operations can be invoked multiple ways By a menu By clicking an icon By keyboard shortcut Want undo/redo/command line option/menu option How do we represent user commands? What is your design? 39

A Good Design Define a class of user operations Abstract class Presents interface common to all operations E.g., undo/redo Each operation is a subclass Jump to a page, cut, paste, 40

Diagram Command Execute() Undo() CutCommand Execute() Undo() SaveCommand Execute() Undo() 41

Commands This is the command pattern Note the user has a small programming language The abstraction makes this explicit In this case the language is finite Class structure can represent all possibilities explicitly Other patterns for richer languages E.g., the Interpreter Pattern 42

Problem: Spell Checking Considerations Spell-checking requires traversing the document Need to see every glyph, in order Information we need is scattered all over the document There may be other analyses we want to perform E.g., grammar analysis What is your design? 43

One Possibility Iterators Hide the structure of a container from clients A method for pointing to the first element advancing to the next element getting the current element testing for termination iterator i = CreateIterator(composition); for(i = i->first();!(i->isdone()); i = i->next()) { do something with Glyph i->current() ; } 44

Diagram Iterator First() Next() PreorderIterator First() Next() PostorderIterator First() Next() 45

Notes Iterators work well if we don t need to know the type of the elements being iterated over E.g., send kill message to all processes in a queue Not a good fit for spell-checking for(i = i->first();!(i->isdone()); i = i->next()) { do something with Glyph i->current() ; } Must cast i->current() to spell-check it... if(i instanceof Char) { } else { } 46

Visitors The visitor pattern is more general Iterators provide traversal of containers Visitors allow The idea Traversal And type-specific actions Separate traversal from the action Have a do it method for each element type Can be overridden in a particular traversal 47

Diagram Glyph Draw(Window) Scan(Visitor) Visitor visitchar(character) visitpicture(picture) visitline(column) Character Draw(Window) Scan(Visitor v) { v->visitchar(this); } Picture Draw(Window) Scan(Visitor v) { v->visitpicture(this); } Line Draw(Window) Scan(Visitor v) { v->visitline(this); for each c in children c->scan(v) } 48

Visitor Comments The dynamic dispatch on Glyph::Scan achieves type-safe casting dynamic dispatch to Char::Scan, Picture::Scan, Each of the Glyph::Scan calls the visitor-specific action (e.g., Visitor:visitChar) implements the search (e.g., in Line::Scan) Have a visitor for each action (e.g., spellcheck, search-and-replace) 49

Design Patterns A good idea Simple Describe useful micro-architectures Capture common organizations of classes/objects Give us a richer vocabulary of design Relatively few patterns of real generality Watch out for the hype... 50