Nat. Thomas Software Engineering Telephonics Inc. Long Island IEEE Lecture Series

Similar documents
Applying the Observer Design Pattern

Design Patterns. CSCE Lecture 17-10/28/2015 (Partially adapted from Head First Design Patterns by Freeman, Bates, Sierra, and Robson)

Using Design Patterns in Java Application Development

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

What is Design Patterns?

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

Design Patterns. An introduction

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

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

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

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

Tuesday, October 4. Announcements

SDC Design patterns GoF

What is Design Patterns?

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

CS427a: Object-Oriented Programming Design Patterns for Flexible and Reusable design

Applying the Decorator Design Pattern

DESIGN PATTERN - INTERVIEW QUESTIONS

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

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

The Observer Pattern

Lectures 24 and 25 Introduction to Architectural Styles and 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

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

What is Design Patterns?

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

11/12/12. Objectives DESIGN PATTERNS. Design Pattern. Defined Design Patterns. Applying Design Patterns. Motivating Example

Introduction to Software Engineering: Object Design I Reuse & Patterns

Object Oriented Paradigm

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

Object-Oriented Design

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

Applying the Factory Method Design Pattern

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

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

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

CS427a: Object-Oriented Programming Design Patterns for Flexible and Reusable design

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

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

A Primer on Design Patterns

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

Object-oriented Software Design Patterns

Design Patterns Reid Holmes

The GoF Design Patterns Reference

Programmazione. Prof. Marco Bertini

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

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

An Introduction to Patterns

Object-Oriented Design

Soluzioni Generali a Problemi Ricorren6 nello Sviluppo di So:ware ad Ogge=: Design Pa*ern. Luca Sabatucci ICAR-CNR. Martedì 24 Maggio 2016

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

CS251 Software Engineering Lectures 18: Intro to DP

CS 247: Software Engineering Principles. Design Patterns

CS/CE 2336 Computer Science II

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

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

Review Software Engineering October, 7, Adrian Iftene

3 Product Management Anti-Patterns by Thomas Schranz

Design patterns. OOD Lecture 6

Object Oriented Programming. Michał Bereta

Extensibility Design Patterns From The Initial Stage of Application Life-Cycle

Design Patterns. Gunnar Gotshalks A4-1

1 Software Architecture

A Rapid Overview of UML

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

An Introduction to Patterns

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

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

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

Overview of Patterns: Introduction

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

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

Lecture 20: Design Patterns II

TDDB84. Lecture 2. fredag 6 september 13

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

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

Design patterns. Jef De Smedt Beta VZW

Application Architectures, Design Patterns

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

CS342: Software Design. Oct 3, 2017

Foundations of Software Engineering Design Patterns -- Introduction

As a programmer, you know how easy it can be to get lost in the details

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

Topics in Object-Oriented Design Patterns

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

Improve Your SystemVerilog OOP Skills

Inheritance and Polymorphism in Java

Design Patterns. (and anti-patterns)

Design Patterns: Structural and Behavioural

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

TDDB84: Lecture 09. SOLID, Language design, Summary. fredag 11 oktober 13

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

2.1 Design Patterns and Architecture (continued)

2.1 Design Patterns and Architecture (continued)

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

SOFTWARE ENGINEERING SOFTWARE DESIGN. Saulius Ragaišis.

Software Engineering I (02161)

Laboratorio di Tecnologie dell'informazione

Transcription:

Nat. Thomas Software Engineering Telephonics Inc. Long Island IEEE Lecture Series 10/19/05 Software Design Patterns 1

Software Design Patterns Agenda What are they? Why are they important? Pattern Taxonomy GoF Gang of Four The foundation List of original patterns Brief Unified Modeling Language (UML) overview Strategy example Singleton example Observer example Reference Material (books, web sites) 10/19/05 Software Design Patterns 2

What Are They? Definitions GoF They are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context. Head First Design Patterns someone has already solved your problem Bruce Eckel states (from Thinking in Patterns ) - Probably the most important step forward in objectoriented design - a pattern embodies a complete idea within a program, and thus it can sometimes appear at the analysis phase or high-level design phase. 10/19/05 Software Design Patterns 3

Why are they Important? You re exploiting the wisdom and knowledge of lessons learned by other developers and designers who have been down the path you are about to travel. Accommodates change software will always be changing so the design should expect and deal with it. Change - the one constant in software development. No Silver Bullet refer to article No Silver Bullet: Essence and Accidents of Software Engineering by Frederick P. Brooks, Jr. 10/19/05 Software Design Patterns 4

Pattern Taxonomy Bruce Eckel says watch out for pollution of the term because of it being associated with good OOD. Defines 4 categories: Idiom - how we write code in a particular language to do a particular type of thing. This could be something as common as the way you code the process of stepping through an array in C (and not running off the end). Specific Design: the solution that we come up with to solve a particular problem. This might be a clever design, but it makes no attempt to be general. Standard Design: a way to solve this kind of problem. A design that has become more general, typically through reuse. Design Pattern: how to solve an entire class of similar problem. This usually only appears after applying a standard design a number of times, and then seeing a common pattern throughout these applications. 10/19/05 Software Design Patterns 5

Pattern Taxonomy Con t Each category is fine for the problem being solved. Don t waste time trying to solve every problem with a design pattern. 10/19/05 Software Design Patterns 6

Gang of Four Book Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides Released 1995 Describes 23 patterns in 3 categories (Creational, Structural & Behavioral) 10/19/05 Software Design Patterns 7

Design Pattern Space Purpose Creational Structural Behavioral Scope Class Factory Method Adaptor(class) Interpreter Template method Object Abstract Factory Adaptor(object) Chain of Responsibility Builder Bridge Command Prototype Composite Iterator Singleton Decorator Mediator Façade Memento Flyweight Observer Proxy State Strategy Visitor 10/19/05 Software Design Patterns 8

Unified Modeling Language All examples are illustrated using UML notation. GoF referred to using something called OMT (Object Modeling Technique) which heavily influenced the present day UML. 10/19/05 Software Design Patterns 9

Simple UML example UML - Unified Modeling Language notation class Printer Printer -ErrorCode:int -TonerStatus:int +Reset() +Test():int +Print() Private attributes in class Public methods 10/19/05 Software Design Patterns 10

Duck Pond Simulation Design - 1 Joe works for a company that makes a highly successful duck pond simulation game. The initial designers used OO techniques and created a Duck superclass from which all other ducks inherited. 10/19/05 Software Design Patterns 11

Duck Pond Simulation Design - 2 Abstract class Duck +swim():void display() is pure virtual requiring the subclasses to implement there own display behavior. MallardDuck RubberDuck 12

Duck Pond Simulation Design - 3 Over the year the company has been under increasing pressure from competitors. After a week-long off-site brainstorming session over golf, the company executives think it s time for major innovation. They want something impressive for the vendors show in Maui next week. They want flying ducks!!! 10/19/05 Software Design Patterns 13

Duck Pond Simulation Design - 4 Joe s manager told them it will be no problem for Joe to whip up something in a week. After all, said Joe s boss he s an OO programmer how hard can it be? Joe adds a fly method to the Duck super class. 10/19/05 Software Design Patterns 14

Duck Pond Simulation Design - 5 Duck +swim():void +quack():void +fly():void RubberDucks can't quack or fly so Joe overrides the base class's quack and fly methods.. MallardDuck RedheadDuck RubberDuck +quack():void +fly():void 15

Duck Pond Simulation Design - 6 Joe gets a call from an upset executive in Maui. There are flying rubber duckies all over the screen. What happened? By adding the fly capability to the superclass, he gave flying behavior to all ducks, even those that can t fly. 10/19/05 Software Design Patterns 16

Duck Pond Simulation Design - 7 Duck +swim():void +quack():void +fly():void RubberDucks can't quack or fly so Joe overrides the base class's quack and fly methods.. MallardDuck RedheadDuck RubberDuck +quack():void +fly():void 17

Duck Pond Simulation Design - 8 Joe thinks about inheritance He could override the fly method and the quack methods in the RubberDuck class. What happens if a wood decoy duck is added? Override the fly and quack methods again. Joe has to do this overriding stuff for every new Duck subclass added in the future. 10/19/05 Software Design Patterns 18

Duck Pond Simulation Design - 9 Duck +swim():void +quack():void +fly():void RubberDucks can't quack or fly so Joe overrides the base class's quack and fly methods.. MallardDuck RedheadDuck RubberDuck +quack():void +fly():void 19

Duck Pond Simulation Design - 10 Joe thinks again and removes the fly and quack methods. He places the methods in new interface classes Flyable and Quackable. Now only classes that need to fly or quack inherit from them. He believes this is good. Seems reasonable. 10/19/05 Software Design Patterns 20

Duck Pond Simulation Design - 11 +fly():void Flyable Quackable +quack():void Duck +swim():void Now the problem becomes a maintenance nightmare because the fly behavior may have to change. That means modifying the code in each of the classes that inherit the fly interface. MallardDuck RedheadDuck RubberDuck +fly():void +q uack():void +fly():void +quack():void +quack():void 21

Duck Pond Simulation Design - 12 What happens if you have to make a change to flying behavior? Not all ducks exhibit the same flying behavior. Code reuse is blown away. You might need a design pattern to save the day. 10/19/05 Software Design Patterns 22

Duck Pond Simulation Design - 13 Design Principle Identify the aspects of your application that vary and separate them from what stays the same. Put another way - Take what varies and encapsulate it so it won t effect the rest of your system. 10/19/05 Software Design Patterns 23

Duck Pond Simulation Design - 14 FlyBehavior +fly():void Flyable's subclasses represent all the possible flying behaviors and likewise for QuackBehavior. QuackBehavior +quack():void FlyWithWings FlyNoWings FlyRocketPowered Quack Squeak NoQuack +fly():void +fly():void +fly():void +quack():void +quack():void +quack():void 24

Duck Pond Simulation Design - 15 Now other objects can reuse our fly and quack behaviors because they are no longer hidden away in our Duck subclasses. We can add new behaviors without modifying our existing behavior classes or touching any of the Duck subclasses that use flying or quack behaviors. 10/19/05 Software Design Patterns 25

Duck Pond Simulation Design - 16 Design Principle Program to an interface, not an implementation 10/19/05 Software Design Patterns 26

Duck Pond Simulation Design - 17 Programming to an implementation Dog* D = new Dog(); D->makesound(); // dog barking Programming to an interface Animal* A = new Dog(); A->makesound(); Even better Animal* A = getanimal(); A->makesound(); 10/19/05 Software Design Patterns 27

Duck Pond Simulation Design - 18 Fly and Quack methods are pulled out of the Duck superclass. FlyBehavior and QuackBehavior classes encapsulate all possible fly and quack behaviors. Include behavior setter methods in the Duck class. 10/19/05 Software Design Patterns 28

Duck Pond Simulation Design - 19 NewDuckSuperclass -flybehavior : FlyBehavior -quackbehavior : QuackBehavior +performquack():void +performfly():void +setflybehavior(flybehavior fb):void +setquackbehavior(quackbehavior qb):void +swim():void MallardDuck Now ModelDuck's behav ior can be set at runtime v ia the set methods. The constructor could set default behav iors: FlyBehav ior* flybehav ior = new FlyWithWings; quackbehavior* quackbehavior = new Squeak; 29

Duck Pond Simulation Design - 20 The NewDuckSuperclass delegates flying and quacking behavior instead of having fly and quack methods In previous solutions we were locked into using the implementation defined in the Duck class or in the Duck subclass with no ability to change the behavior at runtime. 10/19/05 Software Design Patterns 30

Duck Pond Simulation Design - 21 class NewDuckSuperclass { FlyBehavior* flybehavior; QuackBehavior* quackbehavior; public: void performfly() { flybehavior->fly(); } void performquack() { quackbehavior->quack(); } } 10/19/05 Software Design Patterns 31

Duck Pond Simulation Design - 22 Class MallardDuck : public NewDuckSuperclass { Public: } MallardDuck() { } quackbehavior = new Quack(); flybehavior = new FlyWithWings(); The constructor initializes the MallardDuck to specific behaviors. We can make our design more flexible by adding methods to NewDuckSuperclass to allow changing the behavior at runtime 10/19/05 Software Design Patterns 32

Duck Pond Simulation Design - 23 Void main() { NewDuckSuperclass* mallard = new MallardDuck; mallard->performquack(); mallard->performfly(); mallard->setfylbehavior(new FlyRocketPowered); mallard->performfly(); } 10/19/05 Software Design Patterns 33

Duck Pond Simulation Design - 24 Congratulations you have just completed the Strategy Pattern!!! The Strategy Pattern encapsulates a family of algorithms and makes them interchangeable. 10/19/05 Software Design Patterns 34

Singleton Pattern -1 Your ticket to creating one and only one object. The simplest of all the design patterns. 10/19/05 Software Design Patterns 35

Singleton Pattern -2 Singleton -instance : Singleton* -item : int 36

Singleton Pattern - 3 class Singleton { Singleton* getinstance(); int getitem(); private: static Singleton* instance; Singleton(); //constructor private int Item; } Singleton::instance = (0); //set new Singleton() here for thread safety Singleton* Singleton::getInstance() { // called lazy instantiation and my not work in a multithreaded situation // or use double-checked locking here for thread safety if (instance == NULL) { instance = new Singleton(); } return instance; } //usage Singleton::getInstance()->getItem(); 10/19/05 Software Design Patterns 37

Observer Pattern - 1 Keep your objects in the know. Don t miss out when something interesting happens. Define a one to many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 10/19/05 Software Design Patterns 38

Observer Pattern - 2 Statement of work states that you have just received a contract to build the next generation internet based weather station. The station shall be responsible for monitoring humidity, temperature and pressure and will update three displays with these parameters. The three displays shall show current conditions, weather stats and forecast. 10/19/05 Software Design Patterns 39

Observer Pattern - 3 Humidity Sensor Temperature Sensor Weather Station WeatherData object Display Pressure sensor 40

Observer Pattern - 4 WeatherData -temperature : float -humidity : float -pressure : float +measurementschange +gettemperature():float +gethumidity():float +getpressure():float CurrentConditionsDisplay ForecastDispl ay StatisticsDisplay 41

Observer Pattern - 5 class WeatherData { public : WeatherData(); ~WeatherData(); void measurementschanged(); private : float gethumidity(); float getpressure(); float gettemperature(); //// Attributes //// protected : }; float humidity; float pressure; float temperature; 10/19/05 Software Design Patterns 42

Observer Pattern - 7 You know how a newspaper works. You want the paper, you subscribe. You don t want the paper anymore, you unsubscribe. Publishers + Subscribers = Observer Pattern Publishers are called Subjects and Subscribers are called Observers 10/19/05 Software Design Patterns 43

Observer Pattern - 8 Subject +registerobserver(observer* ob):void +removeobserver(observer* ob):void +notifyobserver():void * observers Observer +update(subject* changedsubject):void All the methods in the Subject are virtual. Observer classes update is pure virtual so when you inherit you are forced to implement it. <<Interface>> <<Interface>> WeatherData ConcreteObserver The concrete classes are your classes. You just inherit from the interface classes. +measurementschanged():void +gettemperature():float +gethumidity():float +getpressure():float +update(subject* changedsubject):void +someothermethods():void Only the Subject contains the state we are interested in. 44

Observer Pattern - 9 Subject +registerobserver(observer* ob):void +removeobserver(observer* ob):void +notifyobserver():void <<Interface>> * observers Observer +update(subject* changedsubject):voi <<Interface>> CurrentConditionsDisplay WeatherData -temperature : float -humidity : float -pressure : float +measurementschanged():void +gettemperature():float +gethumidity():float +getpressure():float +update(subject* changedsubject):voi ForecastDisplay +update(subject* changedsubject):void StatisticsDisplay +update(subject* changedsubject):void 45

Causes for redesign Creating an object by specifying a class explicitly Abstract Factory, Factory Method, Prototype Dependence on specific operation Chain of Responsibility, Command Dependence on hardware and software platforms Abstract Factory, Bridge Dependence on object representations and implementations Abstract Factory, Bridge, Memento, Proxy Algorithmic dependencies Builder, Iterator, Strategy, Template Method, Visitor Tight coupling Abstract Factory, Bridge, Chain of Responsibility, Command, Façade, Mediator, Observer Extending functionality by subclassing Bridge, Chain of Responsibility, Composite, Decorator, Observer, Strategy Inability to alter classes conveniently Adapter, Decorator, Visitor 10/19/05 Software Design Patterns 46

References Design Patterns Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson & John Vlissides 1995 Head First Design Patterns by Eric & Elisabeth Freeman copyright 2004 very well illustrated wickedlysmart.com/headfirst/headfirstdesignpatterns/headfirstp atternsindex.html Thinking in Patterns by Bruce Eckel book is online (www.bruceeckel.com) No Silver Bullet - Essence and Accidents of Software Engineering Computer Magazine; April 1987 by Frederick P. Brooks, Jr., University of North Carolina at Chapel Hill (www.virtualschool.edu/mon/softwareengineering/brooksnosilverb ullet.html) Object-Oriented Design Heuristics by Arthur J. Riel good book on design principles. 10/19/05 Software Design Patterns 47