Design Patterns: Part 2

Similar documents
Bugsquashing: Command - Pattern. Andreas Fetzer

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Software Engineering I (02161)

Tuesday, October 4. Announcements

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Lecture 13: Design Patterns

Design Patterns. Definition of a Design Pattern

Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Software Engineering I (02161)

Model-View-Controller

Outline. Design Patterns: Part 1. What is a Design Pattern? ENGI 5895: Software Design

SOFTWARE PATTERNS. Joseph Bonello

What is Design Patterns?

Acyclic Visitor Pattern in Formulation of Mathematical Model

Design Patterns. CSC207 Fall 2017

DESIGN PATTERN - INTERVIEW QUESTIONS

Design Patterns. CSC207 Fall 2017

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

An Expert System for Design Patterns Recognition

Advanced Object Oriented PHP

CSE 403: Software Engineering, Winter courses.cs.washington.edu/courses/cse403/16wi/ Design Patterns. Emina Torlak

Design Patterns: Part 1

JOURNAL OF OBJECT TECHNOLOGY

Topics in Object-Oriented Design Patterns

Design Patterns. CSC207 Winter 2017

CPS122 Lecture: Design Patterns Last revised April 22, 2010

Lecture 14: Design Patterns

Tecniche di Progettazione: Design Patterns

26.1 Introduction Programming Preliminaries... 2

The GoF Design Patterns Reference

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

DESIGN PATTERNS FOR MERE MORTALS

A Primer on Design Patterns

Object-Oriented Design

Swing: Building GUIs in Java

CPS122 Lecture: Design Patterns Last revised March 7, 2017

CPS122 Lecture: Design Patterns Last revised March 20, 2012

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

CS211 Lecture: Design Patterns Last revised November 30, 2007

Using Design Patterns in Java Application Development

Levels of Testing Testing Methods Test Driven Development JUnit. Testing. ENGI 5895: Software Design. Andrew Vardy

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

Design Pa*erns. + Anima/on Undo/Redo Graphics and Hints

Object-Oriented Software Development Goal and Scope

Input from Files. Buffered Reader

Wrapping a complex C++ library for Eiffel. FINAL REPORT July 1 st, 2005

25.1 Introduction Façade Design Pattern Identification Problem Structure Participants...

CS 349 / SE 382 Design Patterns. Professor Michael Terry January 21, 2009

INCORPORATING ADVANCED PROGRAMMING TECHNIQUES IN THE COMPUTER INFORMATION SYSTEMS CURRICULUM

Outline. Software Rots

Exception Handling Alternatives (Part 2)

Swing: Building GUIs in Java

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

An Introduction to Patterns

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

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Lecture 11.1 I/O Streams

Command. Comp-303 : Programming Techniques Lecture 22. Alexandre Denault Computer Science McGill University Winter 2004

Levels of Testing Testing Methods Test Driven Development JUnit. Testing. ENGI 5895: Software Design. Andrew Vardy

C++ INTERFACE CLASSES STRENGTHENING ENCAPSULATION

SSJ User s Guide. Package stat Tools for Collecting Statistics. Version: December 21, 2006

Applying the Observer Design Pattern

The Object Recursion Pattern

What is Design Patterns?

An Automatic Visitor Generator for Ada

Basic Java IO Decorator pattern Advanced Java IO. Java IO - part 2 BIU OOP. BIU OOP Java IO - part 2

CSE 70 Final Exam Fall 2009

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

Design Patterns. Gunnar Gotshalks A4-1

Coordination Patterns

Advanced Topics in Software Engineering (02265) Ekkart Kindler

Design Principles: Part 2

developer.* The Independent Magazine for Software Professionals Factory Chain: A Design Pattern for Factories with Generics by Hugo Troche

TO DO: Create a new class which adds statistics to the dice. NOTE: Don t forget to run regression tests

6.170 Lecture 15 Design Patterns

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

SOLID DESIGN PATTERNS FOR MERE MORTALS

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

COT 3530: Data Structures. Giri Narasimhan. ECS 389; Phone: x3748

Towards a Java Framework for Knowledge Representation and Inference

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

Answer Key. 1. General Understanding (10 points) think before you decide.

Case studies: Outline Case Study: Noughts and Crosses

Design Principles: Part 2

2.1 Design Patterns and Architecture (continued)

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

Java Input/Output. 11 April 2013 OSU CSE 1

L23.1 Introduction... 2

2.1 Design Patterns and Architecture (continued)

Outline. Design Principles: Part 2. e.g. Rectangles and Squares. The Liskov Substitution Principle (LSP) ENGI 5895: Software Design.

An Introduction to Patterns

Coverage of Part 2. A Brief Introduction to Java for C++ Programmers: Part 2. import: using packages

Improve Your SystemVerilog OOP Skills

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

Design Patterns. An introduction

w3.ualg.pt/~jvo/poo

Studying software design patterns is an effective way to learn from the experience of others

Parallel Task Executor in Java

Module 6: Binary Trees

Transcription:

Design Patterns: Part 2 ENGI 5895: Software Design Andrew Vardy with code samples from Dr. Rodrigue Byrne and [Martin(2003)] Faculty of Engineering & Applied Science Memorial University of Newfoundland February 5, 2018

Outline 1 Adapter 2 Observer 3 Decorator 4 Command

Adapter Adapter converts the interface of a class into another form. For example, a Switch is used to control a Light. To adhere to the DIP and OCP we introduce a Switchable interface for various devices that can be switched on and off: However, perhaps Light is provided by a third party and has only a toggle method. We need a class to translate between calls to turnon and turnoff and calls to toggle.

Notice the stereotype < <delegates> >. The responsibility of actually controlling the Light is delegated to Light. If you delegate ajob,itmeansyouarenotdoingityourself. In this example, LightAdapter has an associated Light. This is the object form of adapter. There is also a class form...

This is the class form of adapter. The only difference is whether the Adapter class inherits from Light or has a Light. Inheritance is slightly easier since LightAdapter will not need a pointer to Light. However, inheritance forever binds LightAdapter to Light. It may be the case that we can re-use LightAdapter in another situation. In this case, we would prefer association over inheritance.

Observer We may have one or more objects that need to update whenever some activity occurs in a subject object. The idea is for these observing or listening objects to subscribe to the subject. The subject will then automatically notify the observer objects to let them know that something has changed. Classes which are the subject of observation should extend Subject. Observers should implement Observer!

[Figure repeated] A class such as Subject should either be abstract (some implementation allowed) or a pure interface (no implementation). It is often convenient for Subject to be abstract and provide implementations for methods such as Attach and Notify. The designers of Java were so taken with Observer that they provided classes called java.util.observable (i.e. Subject) and java.util.observer. They also use Observer for Java s GUI event processing model (as we will see).

e.g. Temperature Observer

import java. util. Observable ; class TemperatureSensor extends Observable { private double currenttemp ; public double gettemp () { return currenttemp ; public void settemp( double currenttemp ) { if ( this. currenttemp!= currenttemp ) { this. currenttemp = currenttemp ; setchanged (); // setchanged is protected notifyobservers ();

import java. util. Observer ; import java. util. Observable ; class NotifyWorld implements Observer { public void update( Observable obs, Object obj) { TemperatureSensor sens = (TemperatureSensor) obs ; System. out. printf("world new temp is %g%n", sens. gettemp () ); import java. util. Observable ; import java. util. Observer ; class UpdateDisplay implements Observer { public void update( Observable obs, Object obj) { TemperatureSensor sens = (TemperatureSensor) obs ; System. out. printf("display : %g%n", sens. gettemp () );

public class ObserverDemo { public static void main( String [] args ) { TemperatureSensor ts = new TemperatureSensor (); ts. addobserver( new NotifyWorld () ); ts. addobserver( new UpdateDisplay () ); ts. settemp( 16.0 );

Decorator The Decorator pattern is used to add new behaviours to an object, without inheritance. Inheritance is used for adding new behaviours but must be specified at compile-time. Decorator allows new behaviours to be added at run-time! e.g. In the Java API the basic class for reading character streams is Reader. Reader is abstract but has the following concrete subclasses: FileReader: Used to read characters from a file BufferedReader: Allows text to be read from a character-input stream in manageable chunks (e.g. lines) LineNumberReader: Keeps track of line numbers The constructors for BufferedReader and LineNumberReader require a Reader. We build a Reader with the required set of behaviour by instantiating a chain of objects with each one being decorated by the next.

import java. io. IOException ; import java. io. BufferedReader ; import java. io. FileReader ; import java. io. LineNumberReader ; public class BufferLineCountReader { public static void main( String [] args ) throws IOException { if( args. length!= 1 ) { System. out. println( "usage: java BufferLineCountReader file "); System. exit( 1 ); FileReader fr = new FileReader( args [0] ); BufferedReader br = new BufferedReader( fr ); LineNumberReader lnr = new LineNumberReader( br ); String line = null ; while( (line=lnr. readline ())!= null ) { System. out. printf("%5d: %s%n", lnr. getlinenumber (), line ); lnr. close ();

Here is the general structure of Decorator from [Gamma et al.(1995)gamma, Helm, Johnson, and Vlissides]: Component: Defines the interface for objects that can be decorated with new behaviours (e.g. Reader) ConcreteComponent: A basic object that can be decorated (but is not a Decorator) (e.g. FileReader) Decorator: Decorates some Component (already decorated or a ConcreteComponent) with new behaviour (e.g. BufferedReader, LineNumberReader)

e.g. Coffee The coffee example from Wikipedia provides a nice introduction to the implementation of Decorator: http://en.wikipedia.org/wiki/decorator_pattern Here is the class diagram for this example:

Command The Command pattern treats requests as objects. Instead of a direct function call, we create an object that provides an execute method and stores the parameters of the function call. Instead of simply calling a method of object receiver: receiver. dostuff (12); we create a class to represent such a request. In general, a Command is something with an execute method. interface Command { void execute (); We need a Command specifically for receiver.dostuff(int)...

public class DoStuffCommand implements Command { Receiver receiver ; int value ; public DoStuffCommand( Receiver receiver, int value) { this. receiver = receiver ; this. value = value ; public void execute () { receiver. dostuff( value ); Now lets see how this is used: import java. util. ArrayList ; public class TestCommand { public static void main( String [] args) { Receiver receiver = new Receiver (); ArrayList<Command> commands = new ArrayList<Command >(); // commands. add( new DoStuffCommand( receiver, 12) ); // //... other commands added... time passes... // for ( Command cmd : commands) cmd. execute ();

This seems like a very indirect way of calling receiver.dostuff(12). Butthisindirectionbuysussomething. Here are some applications: Queue up Commands for later execution Provide logging by modifying execute or adding a log method Support transactions (e.g. bank account transactions) such that new transactions are created not by modifying existing code, but by create new concrete Command classes Support unlimited undo / redo: Incorporate an undo method into Command to reverse the effects of execute Requires some storage of the previous state of the receiver by execute Executed commands are maintained in a history list that is traversed backwards for undo operations (by calling undo) and forwards for redo operations (by calling execute)

The following is the overall structure for Command [Gamma et al.(1995)gamma, Helm, Johnson, and Vlissides]: Sequence diagram:

References Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley Professional, 1995. Robert C. Martin. Agile Software Development: Principles, Patterns, and Practices. Prentice Hall, 2003.