CSC207H: Software Design Lecture 6

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

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

SDC Design patterns GoF

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

Design Patterns. An introduction

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

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

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

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

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

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

OO Design Principles

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

Design Patterns Reid Holmes

Object-Oriented Oriented Programming

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

administrivia today UML start design patterns Tuesday, September 28, 2010

A few important patterns and their connections

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

Topics in Object-Oriented Design Patterns

TDDB84. Lecture 2. fredag 6 september 13

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Template Method Pattern. George Blankenship

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

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

Design patterns. Jef De Smedt Beta VZW

TDDB84: Lecture 6. Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command. fredag 4 oktober 13

CPSC 310 Software Engineering. Lecture 11. Design Patterns

SYLLABUS CHAPTER - 1 [SOFTWARE REUSE SUCCESS FACTORS] Reuse Driven Software Engineering is a Business

A Rapid Overview of UML

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

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

What is Design Patterns?

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

Introduction to Software Engineering: Object Design I Reuse & Patterns

Object-oriented Software Design Patterns

Topics. Software Process. Agile. Requirements. Basic Design. Modular Design. Design Patterns. Testing. Quality. Refactoring.

UNIT I Introduction to Design Patterns

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

INTERNAL ASSESSMENT TEST III Answer Schema

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

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

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

LECTURE NOTES ON DESIGN PATTERNS MCA III YEAR, V SEMESTER (JNTUA-R09)

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

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

Object Oriented Paradigm

CS/CE 2336 Computer Science II

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

Design Patterns: Structural and Behavioural

3 Product Management Anti-Patterns by Thomas Schranz

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

UNIT I Introduction to Design Patterns

Writing your own Java I/O Decorator p. 102 Tools for your Design Toolbox p. 105 Exercise Solutions p. 106 The Factory Pattern Baking with OO

1 Software Architecture

Software Eningeering. Lecture 9 Design Patterns 2

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

Design Patterns Lecture 2

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

Design Patterns! Acknowledgements!

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

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Design patterns. OOD Lecture 6

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

DESIGN PATTERN - INTERVIEW QUESTIONS

A Reconnaissance on Design Patterns

S T R U C T U R A L M O D E L I N G ( M O D E L I N G A S Y S T E M ' S L O G I C A L S T R U C T U R E U S I N G C L A S S E S A N D C L A S S D I A

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

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

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

Using Design Patterns in Java Application Development

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

Introduction and History

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

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

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

Applying Design Patterns to SCA Implementations

CS 247: Software Engineering Principles. Design Patterns

Design Patterns. (and anti-patterns)

SOFTWARE PATTERNS. Joseph Bonello

Design Patterns Reid Holmes

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

Application Architectures, Design Patterns

The GoF Design Patterns Reference

Tuesday, October 4. Announcements

Lecture 20: Design Patterns II

INSTITUTE OF AERONAUTICAL ENGINEERING

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

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

EINDHOVEN UNIVERSITY OF TECHNOLOGY

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

Development and Implementation of Workshop Management System Application to Explore Combing Multiple Design Patterns

SWEN425 DESIGN PATTERNS

What is Design Patterns?

CSE870: Advanced Software Engineering (Cheng) 1

Information systems modelling UML and service description languages


Object-Oriented Design

UP Requirements. Software Design - Dr Eitan Hadar (c) Activities of greater emphasis in this book. UP Workflows. Business Modeling.

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Transcription:

CSC207H: Software Design Lecture 6 Wael Aboelsaadat wael@cs.toronto.edu http://ccnet.utoronto.ca/20075/csc207h1y/ Office: BA 4261 Office hours: R 5-7 Acknowledgement: These slides are based on material by Prof. Karen Reid

Software house: what happens inside? Understand the requirements Design the software Write the program Test the program Write Documentation Package/Sell/Market

How to produce a design? Identify classes Identify relations between classes

How to identify a class nouns class verbs methods adjectives attribute/member-variables

How to identify relations? Owns/has it? Uses it? Is type of?

How good is a class definition? Completeness class should captures all meaningful characteristics of an abstraction Sufficiency Class provides enough characteristics of an abstraction to allow meaningful and efficient interaction Coupling If an individual class is hard to understand, correct or change without referring to other classes, it said to be strongly coupled to another class, which is BAAAAD...

How good is a class definition? Cohesion Class methods work together to provide well-defined behaviour, no unrelated elements or "coincidental cohesion" Primitiveness Class should only provide primitive operations (clean+tidy or KISS)

Design Example

How to represent a design? We need a standard notation Unified Modelling Language (UML)

UML Unified Modeling Language A way to draw information about program design The pictures we show here come from http://www.dofactory.com/patterns/patterns.aspx

UML: class diagram Types Class Interface What is a valid class? Type Propertiers Methods Visibility (public, protected, private)

UML: class diagram - relations Association Aggregation is a "part of" relationship Lifetime responsibility Contains-a Composition No lifetime responsibility Has-a

UML: class diagram - relations Association Aggregation is a "part of" relationship Lifetime responsibility Contains-a Car Radio Composition No lifetime responsibility Has-a Car Wheel

UML: class diagram - relations Inheritance Why? Is-A relationship & exchangeable types Types of inheritance Single inheritance vs. multiple Inheritance Single level vs. multi-level Examples Apple is a fruit Door & Window? What about manager, secretary, programmer & executive classes?

UML: class diagram - relations Inheritance Why? Is-A relationship & exchangeable types Types of inheritance Single inheritance vs. multiple Inheritance Single level vs. multi-level Examples Apple is a fruit Door & Window? What about manager, secretary, programmer & executive classes?

UML Notation Construct Description class a description of a set of objects that share the same attributes, operations, methods, relationships and semantics. interface a named set of operations that characterize the behavior of an element. component a modular, replaceable and significant part of a system that packages implementation and exposes a set of interfaces. node a run-time physical object that represents a computational resource. Syntax «interface»

UML Notation Construct Description Syntax association a relationship between two or more classifiers that involves connections among their instances. aggregation A special form of association that specifies a whole-part relationship between the aggregate (whole) and the component part. generalization a taxonomic relationship between a more general and a more specific element. dependency a relationship between two modeling elements, in which a change to one modeling element (the independent element) will affect the other modeling element (the dependent element).

UML Notation Construct Description Syntax realization a relationship between a specification and its implementation.

Tools in a Software House Programming Languages Scripting Languages Integrated Development Environment (IDE) App Profiling Tools Version Control App Quality Assurance Framework Software Build Management Framework Requirements/Feature Tracking App Variance Tracking App Architecture Tools

Design & Architecture Tool Violet UML modelling app http://horstmann.com/violet/

Design Patterns

What is a 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

Pattern Name Elements of Design Patterns Increases design vocabulary, higher level of abstraction Problem When to apply the pattern Problem and context, conditions for applicability of pattern Solution Relationships, responsibilities, and collaborations of design elements Not any concrete design or implementation, rather a template Consequences Results and trade-offs of applying the pattern Space and time trade-offs, reusability, extensibility, portability

What is a Design Pattern (II) Description of communicating objects and classes that are customized to solve a general design problem in a particular context. Each pattern focuses in a particular objectoriented design problem or issue Patterns describe the shape of code rather than the details

Design Pattern Space Creational Structural Behavioral Factory Method Abstract Factory Builder Prototype Singleton Adapter Bridge Composite Decorator Flyweight Facade Proxy Interpreter Template Method Chain of Responsibility Command Iterator Mediator Memento Observer State Strategy Visitor

Pattern: Command objects that represent actions

Common UI commands it is common in a GUI to have several ways to activate the same behavior example: toolbar "Cut" button and "Edit / Cut" menu this is good ; it makes the program flexible for the user we'd like to make sure the code implementing these common commands is not duplicated

Command pattern Command: an object that represents an action sometimes called a "functor" to represent an object whose sole goal is to encapsulate one function

Command pattern - example

Pattern: Singleton At max One Instance of a class!

Singleton Pattern Used to ensure that a class has only one instance. For example, one printer spooler object, one file system, one window manager, etc. Instead the class itself is made responsible for keeping track of its instance. It can thus ensure that no more than one instance is created. This is the singleton pattern.

Singleton example code } public class MySingletonClass { private static MySingletonClass instance = new MySingletonClass(); public static MySingletonClass getinstance() { return instance; } /** There can be only one. */ private MySingletonClass() {}

Pattern: Observer

Observer Pattern A B C D X 15 35 35 15 Y 10 40 30 20 Z 10 40 30 20 Relative Percentages A B C D D C A B Change notification A=10% B=40% C=30% D=20% Data Source (file, website etc)

Observer Pattern Subject attach (Observer) detach (Observer) Notify () Concrete Subject GetState() SetState() subjectstate observers For all x in observers{ x Update(); } subject Observer Update() Concrete Observer Update() observerstate observerstate= subject getstate();

Observer Pattern Need to separate presentational aspects with the data, i.e. separate views and data. Classes defining application data and presentation can be reused. Change in one view automatically reflected in other views. Also, change in the application data is reflected in all views. Defines one-to-many dependency amongst objects so that when one object changes its state, all its dependents are notified.

Observer Pattern GUI programming example

Pattern: Template Method

What s Wrong With This? public class PizzaMaker { public void cookpizzas(list pizzas) { for (int i=0; i<pizzas.size(); ++i) { Object pizza = pizzas.get(i); if (pizza instanceof ThinCrustPizza) { ((ThinCrustPizza)pizza).cookInWoodFireOven(); } else if (pizza instanceof PanPizza) { ((PanPizza)pizza).cookInGreasyPan(); } else { } } } }

The Open-Closed Principle Classes should be open for extension, but closed for modification I.e., you should be able to extend a system without modifying the existing code The type-switch in the example violates this Have to edit the code every time the marketing department comes up with a new kind of pizza

Abstraction is the Solution Solve the problem by creating a Pizza interface with a cook method Or an abstract base class whose cook method must be overridden by every child Simple, right?

How Open Should You Be? public abstract class Pizza { public final void cook() { placeoncookingsurface(); placeincookingdevice(); int cooktime = getcooktime(); letitcook(cooktime); removefromcookingdevice(); } protected abstract void placeoncookingsurface(); protected abstract void placeincookingdevice(); protected abstract int getcooktime(); protected abstract void letitcook(int min); protected abstract void removefromcookingdevice(); }

Template Method Design Pattern The Template Method design pattern is used to set up the skeleton of an algorithm Details then filled in by concrete subclasses But what if someone wants to do something you didn t anticipate? E.g., wants to add a PancakePizza that has to be flipped over halfway through the cooking process

Override the Template Method? public final void cook() { placeoncookingsurface(); placeincookingdevice(); int cooktime = getcooktime(); letitcook(cooktime/2); flip(); letitcook(cooktime/2); removefromcookingdevice(); } But cook was final And it s storing up trouble for the future

Squeeze It Somewhere Else? protected void removefromcookingdevice() { flip(); letitcook(cooktime); remove from skillet } removefromcookingdevice shouldn t be doing other things Think about the documentation And once again, we re storing up trouble for the future

Leave Space for Future Growth? public final void cook() { beforeplacingoncookingsurface(); placeoncookingsurface(); beforeplacingincookingdevice(); placeincookingdevice(); beforecooking(); for (int i=0; i<getcookingphases(); i++) { letitcook(getcooktime(i)); aftercookingphase(i); } beforeremovingfromcookingdevice(); removefromcookingdevice(); afterremovingfromcookingdevice(); }

Template Method Pattern

Design Patterns: discussion Not just about object-oriented design User interface patterns Business patterns Anti-patterns (things to avoid) Be careful, not every coding problem is a design pattern

Serves Two Purposes Communication: a concise way for designers to communicate with each other And argue out exactly what they mean Often without worrying about specific implementation details Education: gives them a way to communicate what they know to newcomers Don't expect to connect them all to your own experience the first time But keep them in mind as you work on other courses "Hey, I know how to do this!

Are We Winning? You can t tell if your designs are any good until you can tell good from bad Presume you know how to tell good code from bad Indentation, variable naming, documentation, unit tests, etc. From here on in, you can only lose marks for doing it badly