CHAPTER 6: CREATIONAL DESIGN PATTERNS

Similar documents
CHAPTER 6: CREATIONAL DESIGN PATTERNS

Object-Oriented Design

CHAPTER 5: PRINCIPLES OF DETAILED DESIGN

Design Patterns. Gunnar Gotshalks A4-1

An Introduction to Patterns

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

The GoF Design Patterns Reference

Design Patterns. An introduction

DESIGN PATTERNS FOR MERE MORTALS

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

An Introduction to Patterns

Chapter 12 (revised by JAS)

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

What is Design Patterns?

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

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

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

Using Design Patterns in Java Application Development

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

Tuesday, October 4. Announcements

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

SOLID DESIGN PATTERNS FOR MERE MORTALS

DESIGN PATTERN - INTERVIEW QUESTIONS

Advanced Object Oriented PHP

What is Design Patterns?

Topics in Object-Oriented Design Patterns

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

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

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

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

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

Design patterns. OOD Lecture 6

An Expert System for Design Patterns Recognition

Design Pattern and Software Architecture: IV. Design Pattern

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

Lecture 13: Design Patterns

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

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

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

What is Design Patterns?

2.1 Design Patterns and Architecture (continued)

2.1 Design Patterns and Architecture (continued)

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

James Newkirk

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

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

Applying the Observer Design Pattern

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

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

CPSC 310 Software Engineering. Lecture 11. Design Patterns

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

26.1 Introduction Programming Preliminaries... 2

INCORPORATING ADVANCED PROGRAMMING TECHNIQUES IN THE COMPUTER INFORMATION SYSTEMS CURRICULUM

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

Crash course on design patterns

L33.1 Introduction... 2

SOFTWARE PATTERNS. Joseph Bonello

Summary of the course lectures

3 Product Management Anti-Patterns by Thomas Schranz

Design Patterns. CSC207 Fall 2017

C++ INTERFACE CLASSES STRENGTHENING ENCAPSULATION

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


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

Review Software Engineering October, 7, Adrian Iftene

6.170 Lecture 15 Design Patterns

SDC Design patterns GoF

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

Bugsquashing: Command - Pattern. Andreas Fetzer

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

Reuse at Design Level: Design Patterns

A Primer on Design Patterns

The Object Recursion Pattern

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

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

CS/CE 2336 Computer Science II

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

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

CS 247: Software Engineering Principles. Design Patterns

Design Patterns. CSC207 Fall 2017

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Introduction and History

Singleton Pattern Creational

Configuration Provider: A Pattern for Configuring Threaded Applications

Applying the Decorator Design Pattern

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

Lecture 17: Patterns Potpourri. Copyright W. Howden 1

Design Patterns: Part 2

Model-View-Controller

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Applying the Factory Method Design Pattern

CS251 Software Engineering Lectures 18: Intro to DP

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

Object-Oriented Design

Design Patterns. Definition of a Design Pattern

Object Oriented Paradigm

Introduction to Software Engineering: Object Design I Reuse & Patterns

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

6.3 Patterns. Definition: Design Patterns

Transcription:

CHAPTER 6: CREATIONAL DESIGN PATTERNS SESSION III: BUILDER, PROTOTYPE, SINGLETON Software Engineering Design: Theory and Practice by Carlos E. Otero Slides copyright 2012 by Carlos E. Otero For non-profit educational use only May be reproduced only for student use when used in conjunction with Software Engineering Design: Theory and Practice. Any other reproduction or use is prohibited without the express written permission of the author. All copyright information must appear if these slides are posted on a website for student use. 10/29/2012 Software Engineering Design: Theory and Practice 1

SESSION S AGENDA Creational Patterns in Detailed Design Builder Code generator example Prototype Character cloning example Singleton Event manager example What s next 10/29/2012 Software Engineering Design: Theory and Practice 2

BUILDER DESIGN PATTERN The Builder Design pattern is an object creational pattern that encapsulates both the creational process and the representation of product objects. Unlike the Abstract Factory, in which various product objects are created all at once, Builder allows clients to control the (multistep) creational process of a single product object, allowing them to dictate the creation of individual parts of the object at discrete points throughout software operations. According to the GoF, the intent of the Builder is to [1] Separate the construction of a complex object from its representation so that the same construction process can create different representations. These interface methods specify the multistep creational process for the Product! This guy orchestrates the creational process! Creator Classes Concrete Builders must provide implementation for creational interface functions! Product Class 10/29/2012 Software Engineering Design: Theory and Practice 3

BUILDER DESIGN PATTERN EXAMPLE Consider a code generator: Code generated from an interface specification document (ICD) used to monitor and control custom developed hardware. Company sells hardware, but also includes library to monitor and control its hardware. Generator reads a text document containing information about a messaging specification. It then generates C++ classes that can be used to monitor and control hardware. This is also shipped to customers so that they can use the hardware easily. The ICD is quite complex, and customers are demanding support for other languages, such as Java and C#. Java Code IN Code Generator Open and read file Find message name, attributes, operations Generate language-specific code Close file OUT C++ Code Different representation! Same construction process! C# Code 10/29/2012 Software Engineering Design: Theory and Practice 4

BUILDER DESIGN PATTERN EXAMPLE Abstract Builder Director Notice that we moved the getproduct ( )method one level up. This may not be possible in other problems! Concrete Builders 10/29/2012 Software Engineering Design: Theory and Practice 5

BUILDER DESIGN PATTERN EXAMPLE The Builder Interface for generating code! The same steps used to create products in all target languages! 10/29/2012 Software Engineering Design: Theory and Practice 6

BUILDER DESIGN PATTERN EXAMPLE C++ Builder Step 1 class MessageOne { Step 2 class MessageOne { private: int attributeone; int attributetwo; Step 3 class MessageOne { private: int attributeone; int attributetwo; public: void setvalueone(unsigned char x); Step 4 class MessageOne { private: int attributeone; int attributetwo; public: void setvalueone(unsigned char x); }; C++ Product Created! 10/29/2012 Software Engineering Design: Theory and Practice 7

BUILDER DESIGN PATTERN EXAMPLE In the previous slide, we presented how a C++ message builder could be created. Product representation, i.e., generated code, depends on the Builder passed in as parameter! In similar fashion, C# or Java builders can be created. The process used for identifying names, attributes, and operations in the ICD could be quite complex! The same creational process is used for all target languages! However, if we manage to separate this process from its ultimate product representation (e.g., java vs. c++ code), we can reuse it to generate these and other future products by extension and NOT modification! 10/29/2012 Software Engineering Design: Theory and Practice 8

BUILDER DESIGN PATTERN EXAMPLE Steps for designing using the Builder pattern: 1. Identify and design the product class (e.g., GeneratedProduct) 2. Identify the product s creational process and algorithm, and design a class for its execution (e.g., CodeGenerator). 3. Using the knowledge acquired from steps 1 and 2, design the builder interface, which specifies the parts that need to be created for the whole product to exist. These are captured as abstract interface methods that need to be implemented by derived concrete builders. 4. Identify and design classes for the different representations of the product (e.g., CppMessageBuilder, JavaMessageBuilder, etc.) The Builder provides the following benefits: Separates an object s construction process from its representation; therefore future representations can be added easily to the software. Changes to existing representations can be made without modifying the code for the creational process. Provides finer control over the construction process so that objects can be created at discrete points in time, which differs from the Abstract Factory pattern. 10/29/2012 Software Engineering Design: Theory and Practice 9

PROTOTYPE DESIGN PATTERN The Prototype design pattern is a class creational pattern that allows clients to create duplicates of prototype objects at run-time without knowing the objects specific type. According to the GoF, the intent of the prototype design pattern is to [1] Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Clients use the prototype interface to create objects without knowing their specific types! Derived classes must obey the prototype interface! Both Product and Creator Classes 10/29/2012 Software Engineering Design: Theory and Practice 10

PROTOTYPE DESIGN PATTERN EXAMPLE 10/29/2012 Software Engineering Design: Theory and Practice 11

PROTOTYPE DESIGN PATTERN EXAMPLE Method to create copies of Terrestrial characters Method to create copies of Aerial characters Consider how client code would look like when programmed against this design 10/29/2012 Software Engineering Design: Theory and Practice 12

PROTOTYPE DESIGN PATTERN EXAMPLE Yikes! What happens if we introduce a new character to the game? This violates OCP! Let s see how we can improve things with the Prototype design pattern 10/29/2012 Software Engineering Design: Theory and Practice 13

PROTOTYPE DESIGN PATTERN EXAMPLE Add the prototype clone interface method to the Character interface The client can now rely on the clone interface to create Character objects, regardless of which Character subtype is actually active! Concrete Characters must obey the Character interface! 10/29/2012 Software Engineering Design: Theory and Practice 14

PROTOTYPE DESIGN PATTERN EXAMPLE 10/29/2012 Software Engineering Design: Theory and Practice 15

PROTOTYPE DESIGN PATTERN EXAMPLE We can now replace this with that! 10/29/2012 Software Engineering Design: Theory and Practice 16

PROTOTYPE DESIGN PATTERN EXAMPLE Steps to design using the Prototype design pattern include: 1. Identify and design the common interface that needs duplication. As part of the interface, the clone method (or equivalent) needs to be specified. 2. Identify and design concrete products, which realize the interface created in Step 1. 3. For each concrete product created in Step 2, implement the clone method in terms of that particular concrete product. Benefits of the Prototype design pattern include: Clients are shielded from knowing the internal structure of objects; therefore, adding products at runtime becomes easier. This reduces the client s complexity. Reduced number of classes; instead of having two classes (i.e., creator and product classes), the prototype is both, therefore eliminating the need for one class for each product. 10/29/2012 Software Engineering Design: Theory and Practice 17

SINGLETON DESIGN PATTERN The Singleton design pattern is an object creational design pattern used to prevent objects from being instantiated more than once in a running program. According to the GoF, the intent of the Singleton is to [1] Ensure a class only has one instance, and provide a global point of access to it. Private Constructor to restrict object instantiation Private static instance Public static getinstance() method to create and provide access to the one and only object instance 10/29/2012 Software Engineering Design: Theory and Practice 18

SINGLETON DESIGN PATTERN EXAMPLE 10/29/2012 Software Engineering Design: Theory and Practice 19

SINGLETON DESIGN PATTERN EXAMPLE Sample code to log events using the Singleton design pattern Warning! Singleton has been documented to cause memory leaks in multi- threaded environments! 10/29/2012 Software Engineering Design: Theory and Practice 20

SINGLETON DESIGN PATTERN EXAMPLE Steps to design using the Singleton design pattern: 1. Set the visibility of the constructor to private 2. Define a private static member attribute that can store a reference (i.e., a pointer) to the one instance of the singleton. 3. Create a public static getinstance() method that can access the private constructor to instantiate one object of the singleton type and return it to clients. Benefits of the Singleton design pattern include: It provides controlled access to single a single instance of a given type. It has reduced namespace since it provides an alternatives to global variables. It can be customized to permit a variable number of instances. 10/29/2012 Software Engineering Design: Theory and Practice 21

WHAT S NEXT In this session, we continued the discussion on creational design patterns, including: Builder Prototype Singleton This finalizes the discussion on creational design patterns. In the next module, we will present structural and behavioral design patterns in detailed design. Specifically, we will cover: Adapter Composite Façade Iterator Observer 10/29/2012 Software Engineering Design: Theory and Practice 22

REFERENCES [1] Gamma, Erich, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Boston: Addison-Wesley, 1995. 10/29/2012 Software Engineering Design: Theory and Practice 23