Topics in Object-Oriented Design Patterns

Similar documents
SDC Design patterns GoF

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

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

CISC 322 Software Architecture

Object-Oriented Oriented Programming

Design Patterns. Gunnar Gotshalks A4-1

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

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

Design Patterns Reid Holmes

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

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

Object-Oriented Design

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

Using Design Patterns in Java Application Development

Object-Oriented Design

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

An Introduction to Patterns

Design Patterns. An introduction

Object Oriented Paradigm

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

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

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

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

Pattern Examples Behavioural

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

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

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

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

An Introduction to Patterns

SOFTWARE PATTERNS. Joseph Bonello

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

Design Pattern: Composite

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

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

DESIGN PATTERN - INTERVIEW QUESTIONS

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

What is Design Patterns?

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

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

Cocoa Design Patterns. Erik M. Buck October 17, 2009

Second Midterm Review

Tuesday, October 4. Announcements

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

Lecture 13: Design Patterns

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

Summary of the course lectures

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

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

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

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

Brief Note on Design Pattern

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

Design Patterns. GoF design patterns catalog

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

Introduction to Software Engineering: Object Design I Reuse & Patterns

Chapter 12 (revised by JAS)

The GoF Design Patterns Reference

Object Design II: Design Patterns

Design Patterns Reid Holmes

3 Product Management Anti-Patterns by Thomas Schranz

Design patterns. Jef De Smedt Beta VZW

Coordination Patterns

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

A Reconnaissance on Design Patterns

2.1 Design Patterns and Architecture (continued)

Advanced Object Oriented PHP


Composite Pattern. IV.4 Structural Pattern

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

Applying the Observer Design Pattern

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

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

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

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

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

An Expert System for Design Patterns Recognition

2.1 Design Patterns and Architecture (continued)

Design Patterns. Definition of a Design Pattern

What is Design Patterns?

Review Software Engineering October, 7, Adrian Iftene

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

Software Eningeering. Lecture 9 Design Patterns 2

Design Patterns Lecture 2

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

Design Patterns. CSC207 Winter 2017

Object-oriented Software Design Patterns

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

Information systems modelling UML and service description languages

Crash course on design patterns

Introduction and History

Design Patterns. CSC207 Fall 2017

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

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

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

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

Design Patterns V Structural Design Patterns, 2

Behavioral patterns. Command Iterator Observer Strategy Template method

Tecniche di Progettazione: Design Patterns

Design Patterns IV Structural Design Patterns, 1

CSE870: Advanced Software Engineering (Cheng) 1

Transcription:

Software design Topics in Object-Oriented Design Patterns Material mainly from the book Design Patterns by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides; slides originally by Spiros Mancoridis; modified by Ian Davis and Grant Weddell. 1

Outline Terminology and motivation. Reusable object-oriented design patterns. creational patterns structural patterns behavior patterns 2

Design patterns Good designers know not to solve every problem from first principles. They reuse solutions. Practitioners do not do a good job of recording experience in software design for others to use. 3

Design patterns (cont d) A design pattern systematically names, explains, and evaluates an important and recurring design. We describe a set of well-engineered design patterns that practitioners can apply when crafting their applications. 4

Becoming a skilled designer We first learn the basics. algorithms data structures languages Then, programming in the medium. structured programming modular programming object-oriented programming 5

Becoming a skilled designer (cont d) Ultimately, a designer needs to become familiar with the good designs and design ideas of others. Design patterns must be understood, memorized, and applied. There are thousands of existing design patterns. 6

Reusable object-oriented design patterns 7

Creational patterns Factory Abstract factory Builder Prototype Singleton 8

Factory: intent Introduces a layer of software which allows run time creation of objects that are identified by parametric value rather than by compile time name. May use polymorphism to create different objects in distinct subclasses sharing common factory method. 9

Factory: motivation May wish to restrict conditions under which objects returned by factory may be created. The classes employed within software may not be known until run-time; e.g., OLE pictures etc. may be inserted into documents. May wish to identify classes by GUID s, and to register them. 10

Abstract factory: intent Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Allows entire suites of classes to optionally be interchanged easily at run time. 11

Abstract factory: behavior Sometimes we have systems that support different representations depending on external factors. There is an abstract factory that provides an interface for the client. In this way the client can obtain a specific object through this abstract interface. 12

Abstract factory: example 13

Abstract factory: structure 14

Abstract factory: participants Abstract Factory: Declares an interface for operations that create abstract product objects. Concrete Factory: Implements the operations to create concrete product objects. 15

Abstract factory: participants (cont d) Abstract Product: Declares an interface for a type of product object. Concrete Product: Defines a product object to be declared by the corresponding concrete factory. (Implements the Abstract Product interface). Client: Uses only interfaces declared by Abstract Factory and Abstract Product classes. 16

Builder Methods of a builder class provide an interface which allows the builder class to receive in a pre-determined manner the material from which something is to be built, produced or used. The builder also provides a method to return the result of construction if appropriate. 17

Builder: motivation Want to divorce the issue of what is used to build a result from how the result is built. May want to build many different things from the same input. Easy to create and introduce into system new builder classes, if they all have the same external behaviour. Essentially, the builder pattern reinforces the importance of polymorphism. 18

Builder: example SGML parser identifies types of data token traversed within document. Want to tell application what has been seen while traversing document. Don t want to tell application what to do with this data. Application may format this data, build electronic indices from it, etc. May want to view document in many concurrent windows and styles. 19

Prototype Don t want to explicitly remember the knowledge of exactly what class, and with what parameters a class should be created when requested. Instead encapsulate this information by holding a dummy instance of the class to be created following a given request. Clone copies of this dummy instance as needed. 20

Prototype: examples Visio has vast numbers of visual symbols stored in template sheets. The symbols can be identified by GUID. The GUID allows the class supporting the representation of that symbol to be loaded. Once loaded the visual symbol can be cloned multiple times. It may appear multiple times in a drawing. 21

Singleton Provides global access to single instances of a class transparently. Centralizes access to this single instance. Embeds appropriate concurrency control within the singleton pattern. Avoids potential misuse of global objects. 22

Singleton: example Threads may wish to emit error messages. How error messages are handled is not the threads problem. May want to direct error messages to a single shared error message area. Need to avoid conflict when multiple threads concurrently report errors. Easy to change so that each thread has their own error message area if desired. 23

Structural patterns Adapter Facade Composite 24

Adapter Intent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Motivation: When we want to reuse classes in an application that expects a different interface, we do not want to (and often cannot) change the reusable classes to suit our application. 25

Adapter: example 26

Adapter: structure with multiple inheritance 27

Adapter: structure using object composition 28

Adapter: participants Target: Defines the application-specific interface that clients use. Client: Collaborates with objects conforming to the target interface. Adaptee: Defines an existing interface that needs adapting. Adapter: Adapts the interface of the adaptee to the target interface. 29

Facade: intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. 30

Facade: motivation Structuring a system into subsystems helps reduce complexity. A common design goal is to minimize the communication and dependencies between subsystems. Use a facade object to provide a single simplified interface to the more general facilities of a subsystem. 31

Facade: example 32

Facade: structure 33

Facade: participants Facade: Knows which subsystem classes are responsible for a request. Delegates client requests to appropriate subsystem objects. Subsystem Classes: Implement subsystem functionality. Handle work assigned by the facade object. Have no knowledge of the facade; that is, they keep no references to it. 34

Composite: intent Compose objects into tree structures to represent partwhole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. 35

Composite: motivation If the composite pattern is not used, client code must treat primitive and container classes differently, making the application more complex than is necessary. 36

Composite: example 37

Composite: structure 38

Composite: participants Component: Declares the interface for objects in the composition. Implements default behavior for the interface common to all classes. Declares an interface for accessing and managing its child components. Defines an interface for accessing a component's parent in the recursive structure (optional). 39

Composite: participants (cont d) Leaf: Represents leaf objects in the composition. A leaf has no children. Defines behavior for primitive objects in the composition. Composite: Defines behavior for components having children. Stores child components. Implements child-related operations in the component interface. 40

Composite: participants (cont d) Client: Manipulates objects in the composition through the component interface. 41

Behavioral patterns Command Interpreter Iterator Template Observer Master-slave 42

Command: intent Encapsulate a request as an object, thereby letting one parameterize clients with different requests, queue or log requests, and support undoable operations. 43

Command: motivation Sometimes it's necessary to issue requests to objects without knowing anything about the operation being requested or the receiver of the request. 44

Command: example 45

Command: structure 46

Command: structure (cont d) 47

Command: participants Command: Declares an interface for executing an operation. Concrete Command: Defines a binding between a Receiver object and an action. Implements Execute by invoking the corresponding operations(s) on Receiver. 48

Command: participants (cont d) Client: Creates a Concrete Command object and sets its receiver. Invoker: Asks the command to carry out the request. Receiver: Knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver. 49

Interpreter: intent and motivation Want to compute something capable of being expressed in a language. The computation is performed on structural representation of the language operations to be performed. Structural objects representing operations have built in knowledge of how to perform their own operation. 50

Interpreter: example A language statement may be converted to an expression tree of operations to be performed. Each operation takes as its input the results returned by it s child expression trees. Such an expression tree is often called a plan. 51

Iterator: intent Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. Move the responsibility for access and traversal from the aggregate object to the iterator object. Potentially preserves state information which speeds traversal. 52

Iterator: motivation One might want to traverse an aggregate object in different ways. One might want to have more than one traversal pending on the same aggregate object. Not all types of traversals can be anticipated a priori. One should not bloat the interface of the aggregate object with all these traversals. 53

Iterator: example 54

Iterator: structure 55

Iterator: participants Iterator: Defines an interface for accessing and traversing elements. Concrete Iterator: Implements an iterator interface and keeps track of the current position in the traversal of the aggregate. Aggregate: Defines an interface for creating an iterator object. Concrete Aggregate: Implements the iterator creation interface to return an instance of the proper concrete iterator. 56

Template: intent Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. The Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. 57

Template: motivation By defining some of the steps of an algorithm using abstract operations, the template method fixes their ordering. 58

Template: structure 59

Template: participants Abstract Class: Defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm. Implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in Abstract Class or those of other objects. 60

Template: participants (cont d) Concrete Class: Implements the primitive operations to carry out subclass-specific steps to the algorithm. 61

Observer: intent Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. 62

Observer: motivation A common side-effect of partitioning a system into a collection of cooperating classes is the need to maintain consistency between related objects. You don't want to achieve consistency by making the classes tightly coupled because that reduces their reusability. 63

Observer: example 64

Observer: structure 65

Observer: structure (cont d) The key objects in this pattern are subject and observer. A subject may have any number of dependent observers. All observers are notified whenever the subject undergoes a change in state. 66

Observer: participants Subject: Knows its numerous observers. Provides an interface for attaching and detaching observer objects. Sends a notification to its observers when its state changes. Observer: Defines an updating interface for concrete observers. 67

Observer: participants (cont d) Concrete Subject: Stores state of interest to concrete observers. Concrete Observer: Maintains a reference to a concrete subject object. Stores state that should stay consistent with the subject's. Implements the updating interface. 68

Master-slave: intent Handles the computation of replicated services within a software system to achieve fault tolerance and robustness. Independent components providing the same service; slaves are separated from a component master responsible for invoking them and for selecting a particular result from the results returned by the slaves. 69

Master-slave: motivation Fault tolerance is a critical factor in many systems. Replication of services and delegation of the same task to several independent suppliers is a common strategy to handle such cases. 70

Master-slave: example 71

Master-slave: structure 72

Master-slave: participants Slave: Implements a service. Master: Organizes the invocation of replicated services. Decides which of the results returned by its slaves is to be passed to its clients. Client: Requires a certain service in order to solve its own task. 73