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

Similar documents
Laboratorio di Progettazione di Sistemi Software Design Pattern Creazionali. Valentina Presutti (A-L) Riccardo Solmi (M-Z)

Design Pattern- Creational pattern 2015

Creational Patterns. Factory Method (FM) Abstract Factory (AF) Singleton (SI) Prototype (PR) Builder (BU)

Object-Oriented Design

GoF Design Pattern Categories

An Introduction to Patterns

ECE 449 OOP and Computer Simulation Lecture 11 Design Patterns

Design Patterns Reid Holmes

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

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

CHAPTER 6: CREATIONAL DESIGN PATTERNS

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

SDC Design patterns GoF

Design Pattern and Software Architecture: IV. Design Pattern

A few important patterns and their connections

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

What is Design Patterns?

Material and some slide content from: - GoF Design Patterns Book. Design Patterns #1. Reid Holmes. Lecture 11 - Tuesday October

Factory Method. Comp435 Object-Oriented Design. Factory Method. Factory Method. Factory Method. Factory Method. Computer Science PSU HBG.

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

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Object-Oriented Oriented Programming Factory Method Pattern Abstract Factory Pattern. CSIE Department, NTUT Woei-Kae Chen

What is Design Patterns?

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

Creational Design Patterns

The GoF Design Patterns Reference

Lecture 13: Design Patterns

What is Design Patterns?

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

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

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

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

Topics in Object-Oriented Design Patterns

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

The Design Patterns Matrix From Analysis to Implementation

Tuesday, October 4. Announcements

Factory Method Pattern Creational. » Define an interface for creating an object but lets subclasses decide the specific class to instantiate

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

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

Singleton Pattern Creational

C++ for System Developers with Design Pattern

Design Patterns. An introduction

DESIGN PATTERNS FOR MERE MORTALS

Factories, Builders and Singletons. Steven R. Bagley

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

CS342: Software Design. November 21, 2017

Object Oriented Paradigm

Object-oriented Software Design Patterns

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

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

EINDHOVEN UNIVERSITY OF TECHNOLOGY

Final Exam Review (extended)

DESIGN PATTERN - INTERVIEW QUESTIONS

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

Introduction to Software Engineering: Object Design I Reuse & Patterns

An Introduction to Patterns

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

A Reconnaissance on Design Patterns

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

Prototype Description. Interpreter. interpreter Calculator Design Rationales. Prototype Participants. Interpreter with Factory Method.

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

CPSC 310 Software Engineering. Lecture 11. Design Patterns

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

SOLID DESIGN PATTERNS FOR MERE MORTALS

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

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

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

Software Development Project. Kazi Masudul Alam

MSO Lecture 12. Wouter Swierstra (adapted by HP) October 26, 2017

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

LOG6306 : Études empiriques sur les patrons logiciels

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. Dr. Rania Khairy. Software Engineering and Development Tool

Singleton Pattern Creational. » Ensure a class has only one instance» Provide a global point of access

Reuse at Design Level: Design Patterns

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Software Design Patterns. Aliaksei Syrel

The Singleton Pattern. Design Patterns In Java Bob Tarr

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

Inheritance STL. Entity Component Systems. Scene Graphs. Event Systems

Object-Oriented Oriented Programming

Tecniche di Progettazione: Design Patterns

IBS Software Services Technical Interview Questions. Q1. What is the difference between declaration and definition?

Tecniche di Progettazione: Design Patterns

Lecture 21: Design Patterns III

I, J. Key-value observing (KVO), Label component, 32 text property, 39

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

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

Using Design Patterns in Java Application Development

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

Laboratorio di Tecnologie dell'informazione. Ing. Marco Bertini

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Dr. Xiaolin Hu. Review of last class

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

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

Information systems modelling UML and service description languages

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

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

Transcription:

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

Creational Design Patterns What are creational design patterns? Types Examples Structure Effects

Creational Patterns Design patterns that deal with object creation mechanisms Create objects suitable to the situation Abstracts instantiation process Makes system independent of how its objects are Created Composed Represented Details hiding

Why Creational Patterns? The basic form of object creation could result in design problems added complexity to the design Creational design patterns solve this problem by controlling the object creation They increase system's flexibility in terms of what, who, how, and when of object creation

Categories Two categories: Object creational patterns deals with object creation defers part of its object creation to another object Class creational patterns deals with class instantiation defers its object creation to subclasses

When to Use? Consider applying creational patterns when: A system should be independent of how its objects are created. A set of related objects is designed to be used together. Hiding the implementations of a class library of product, revealing only their interfaces. Constructing different representation of independent complex objects. A class want its subclass to implement the object it creates. The class instantiations are specified at run-time. There must be a single instance and client can access this instance at all times. Instance should be extensible without being modified.

Structure Diagram shows the structure most CPs should have in common. Different creational patterns require additional and different participated classes. Participants: Creator: Declares object interface. Returns object. ConcreteCreator: Implements object's interface.

Most Commonly Used CPs Abstract factory Factory Builder Prototype Singleton

Abstract Factory Pattern Provides an interface for creating families of related or dependent objects without specifying their concrete classes

Example What happens when I want to create a Computer of Model B with Model B memory, CPU and Modem?

Solution: Using Abstract Factory

Solution: Using Abstract Factory Type of Computer gets created depends on which implementation of ComputerFactory is handling the call.

Structure

Effects of Abstract Factory Isolates concrete classes Makes exchanging product families easy Promotes consistency amongst products Supporting new kinds of products is difficult

Factory Method Define an interface for creating an object Let subclasses decide which class to instantiate Also known as virtual constructor Very common in toolkits and frameworks where library code needs to create objects of types that may be subclassed by applications using the framework

Example We want to develop a framework of a Computer that has memory, CPU, and Modem. The actual memory, CPU, and Modem that is used depends on the actual computer model being used. We want to provide a configure function that will configure any computer with appropriate parts. This function must be written such that it does not depend on the specifics of a computer model or the components.

Example using Factory Method

When to Use? The creation of an object makes reuse impossible without significant duplication of code. The creation of an object requires access to information or resources that should not be contained within the composing class. The lifetime management of the generated objects must be centralized to ensure a consistent behavior within the application.

Structure

Effects of using Factory Method Provides hooks for subclasses Connects parallel class hierarchies Refactoring an existing class to use factories breaks existing clients Pattern heavily relies on using private constructor meaning it cannot be extended unless constructor is visible. Subclass must provide its own re-implementation of all factory methods

Factory Method Vs. Other Patterns Abstract Factory is often implemented with Factory Method Factory Methods are usually called within Template Methods Prototypes don t require subclassing the creator. However, they often require initialization of Product class. Factory method doesn t require such an operation. Increased number of subclasses Intention is to increase polymorphism

Builder Pattern The intention of the builder pattern is to find a solution to the telescoping constructor anti-pattern The telescoping constructor anti-pattern occurs when the increase of object constructor parameter combination leads to an exponential list of constructors. The builder pattern uses another object, a builder, that receives each initialization parameter step by step and then returns the resulting constructed object at once.

Example We have a Car class. The problem is that a car has many options. The combination of each option would lead to a huge list of constructors for this class. So we will create a builder class, CarBuilder. We will send to the CarBuilder each car option step by step and then construct the final car with the right options:

Example class Car is Can have GPS, trip computer and a various number of seats. Can be a city car, a sport car or a cabriolet. class CarBuilder is method getresult() is output: a Car with the right options Construct and return the car. method setseaters(number) is input: the number of seaters the car may have. Tell the builder the number of seaters. method setcitycar() is Make the builder remember that the car is a city car.

Example method setcabriolet() is Make the builder remember that the car is a cabriolet. method setsportcar() is Make the builder remember that the car is a sport car. method settripcomputer() is Make the builder remember that the car has a trip computer. method unsettripcomputer() is Make the builder remember that the car does not have a trip computer. method setgps() is Make the builder remember that the car has a global positioning system. method unsetgps() is Make the builder remember that the car does not have a global positioning system.

Example Construct a CarBuilder called carbuilder carbuilder.setseaters(2) carbuilder.setsportcar() carbuilder.settripcomputer() carbuilder.unsetgps() car := carbuilder.getresult()

Structure

When to Use? Algorithms for creating complex object should be independent of the parts that make up the object and how they are assembled Construction process must allow different representation for the object that is constructed

Effects of Builder Pattern Lets product s internal representation to vary Isolates code for construction and representation Gives finer control over construction process

Builder Vs. Others Builder takes care of complete creating of a complex product step by step Abstract Factory emphasizes on creation of families of products focusing one components at a time Builder builds a composite

Prototype Pattern Specify the kinds of objects to create using a prototypical instance. Create new objects by cloning prototype.

Example We want to write an occurrence browser class for a text. This class lists the occurrences of a word in a text. Such an object is expensive to create as the locations of the occurrences need an expensive process to find. So, to duplicate such an object, we use the prototype pattern:

Example class WordOccurrences is field occurrences is The list of the index of each occurrence of the word in the text. constructor WordOccurrences(text, word) is input: the text in which the occurrences have to be found input: the word that should appear in the text Empty the occurrences list for each textindex in text ismatching := true for each wordindex in word if the current word character does not match the current text character then ismatching := false if ismatching is true then Add the current textindex into the occurrences list

Example method getoneoccurrenceindex(n) is input: a number to point on the nth occurrence. output: the index of the nth occurrence. Return the nth item of the occurrences field if any method clone() is output: a WordOccurrences object containing the same data. Call clone() on the super class. On the returned object, set the occurrences field with the value of the local occurrences field. Return the cloned object.

Example text := A pattern can lead to another pattern by examining the current pattern." word := "pattern" searchengine := new WordOccurrences(text, word) anothersearchengine := searchengine.clone()

Structure

When to Use? System should be independent of how its products are created, composed, and represented Classes to instantiate are specified at runtime You want to avoid creating class hierarchy of factories that parallel the class hierarchy of products.

Effects of Prototype Pattern Adding and removing products at runtime Specifying new objects by varying values and structure Reduced subclassing Configuring application with classes dynamically Each subclass must implement clone operation

Prototype Vs. Others Abstract Factory is competing pattern, however, may work together as well Composite and decorator benefit form Prototype

Singleton Pattern Ensure a class has only one instance and provide a global point to access it The concept is sometimes generalized to systems that operate more efficiently when only one object exists The term comes from the mathematical concept of a singleton

Example The singleton pattern must be carefully constructed in multi-threaded applications. If two threads are to execute the creation method at the same time when a singleton does not yet exist, they both must check for an instance of the singleton and then only one should create the new one

Example public class SingletonDemo { private static volatile SingletonDemo instance = null; private SingletonDemo() { } public static SingletonDemo getinstance() { if (instance == null) { synchronized (SingletonDemo.class){ if (instance == null) { instance = new SingletonDemo (); } } } return instance; } }

Structure

When to Use? There must be exactly one instance of a class, and it must be accessible to clients from a well known access point. When the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Effects of Singleton Pattern Controlled access to sole instance Reduced name space Permits refinement of operations and representation Permits a variable number of instances More flexible than class operator

Singleton Vs. Others Several patterns are implemented using Singleton. AbstractFactory needs a singleton pattern for single instance of the factory. Facade Objects are often Singletons because only one Facade object is required. State objects are often Singletons. Singletons are often preferred to global variables because: They do not pollute the global name space with unnecessary variables. They permit lazy allocation and initialization, whereas global variables in many languages will always consume resources.