SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs.
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 14 Structural Design Patterns SWE 316: Software Design and Architecture.
IEG3080 Tutorial 7 Prepared by Ryan.
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Abstract Factory Pattern.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture # 05: Design Principles I - Correctness and Robustness SWE 316: Software.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Design Patterns.
Chapter 5 Design Principles II: Flexibility, Reusability, and Efficiency.
Lecture # 06 Design Principles II
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 11 : Frameworks SWE 316: Software Design and Architecture  To understand.
Chapter 5 Design Principles II: Flexibility, Reusability, and Efficiency.
Abstract Factory Design Pattern making abstract things.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Chapter 6 Introduction to Design Patterns. Process Phase Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Design Patterns -- Omkar. Introduction  When do we use design patterns  Uses of design patterns  Classification of design patterns  Creational design.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Reuse Earlier we have covered the re-usable Architectural Styles as design patterns for High-Level Design. At mid-level and low-level, design patterns.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
CS212: Object Oriented Analysis and Design Lecture 38: Design Pattern-II.
Design Patterns Introduction
Design Patterns Introduction “Patterns are discovered, not invented” Richard Helm.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Chapter 7 Creational Design Pattern. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory Pattern Jiaxin Wang CSPP Winter 2010.
Design Patterns: MORE Examples
…… Notes …… Lecture 8: Structural Design Patterns Monday Nov 05, 2012
Abstract Factory Pattern
Design Patterns: Brief Examples
Design Patterns Lecture part 2.
Factory Patterns 1.
…… Notes …… Lecture 2: Design Attributes and Goals
Software Design and Architecture
Creational Design Patterns
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Presented by Igor Ivković
Software Engineering Lecture 7 - Design Patterns
Object Oriented Design Patterns - Creational Patterns
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Creational Patterns.
Presented by Igor Ivković
Message Passing Systems
Presentation transcript:

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture  To learn the creational design patterns and when to use them. Ch 7 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Creational design patterns to be covered  Creational design patterns:  Singleton  Factory  Abstract factory  Prototype SingletonFactoryAbstract FactoryPrototypeSummary 2/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Singleton Design Pattern  Intent/ Design Purpose  when a class has exactly one instance.  Ensure that there is exactly one instance of a class S. Be able to obtain the instance from anywhere in the application.  Design Pattern Summary  Make the constructor of S private; define a private static attribute for S of type S; define a public accessor for it. 7.3 Singleton FactoryAbstract FactoryPrototypeSummary 3/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Singleton applicability  Use the singleton pattern when  There must be exactly one instance of a class, and it must be accessible to client 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. Singleton enforces the intention that only one User object exists, safeguarding the application from unanticipated User instance creation. KEY CONCEPT Design Goal: Correctness Singleton FactoryAbstract FactoryPrototypeSummary 4/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Singleton consequences  Singleton has following benefits:  Controlled access to sole instance.  Permits a variable number of instances.  More flexible than class operations. Singleton FactoryAbstract FactoryPrototypeSummary 5/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Singleton : Class Model MyClass getSingletonOfMyClass(): MyClass Client 1 singletonOfMyClass «static» Singleton FactoryAbstract FactoryPrototypeSummary 6/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Singleton: Sample code 7 Define a private static member variable of type MyClass 1 Make the constructor of MyClass private 2 Define a public static method to access the member 3 Singleton FactoryAbstract FactoryPrototypeSummary 7/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Comments on Singleton  This form of Singleton is simple but it creates the Singleton object even if the object is never needed; this is wasteful if Singleton is large.  The idea of Singleton can be extended to the problem of having just two instances of a class. When a class must have exactly one instance, make the constructor private and the instance a private static variable with a public accessor. KEY CONCEPT Singleton Design Pattern Singleton FactoryAbstract FactoryPrototypeSummary 8/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Factory Design Pattern  Design Purpose Create individual objects in situations where the constructor alone is inadequate.  Design Pattern Summary Use methods to return required objects. 7.2 Singleton Factory Abstract FactoryPrototypeSummary 9/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Factory Class Model Factory design pattern MyClass createObjectOfRequiredClass(): RequiredClass «create object» RequiredClass Client Singleton Factory Abstract FactoryPrototypeSummary 10/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Application of Factory design pattern Factory Example (Figure 7.4) Ford createAutomobile() Toyota createAutomobile() Automobile createAutomobile(): Automobile Client «create object» We want to write code about automobiles in general: Code that applies to any make, exercised repeatedly (thus reliably). KEY CONCEPT Design Goal : Reusability and Correctness Singleton Factory Abstract FactoryPrototypeSummary 11/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Application of Factory design pattern Client sendMessage() Customer getMessage() Frequent getMessage() Returning getMessage() Curious getMessage() Newbie getMessage() MailMessage text MailGenerationApplication getCustomerTypeFromUser() «setup» Factory : Generation Example Singleton Factory Abstract FactoryPrototypeSummary 12/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Comments on Factory  Applications of Factory have been increasingly common in API’s because they improve robustness by ensuring that objects created respect necessary constraints. Singleton Factory Abstract FactoryPrototypeSummary 13/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Abstract Factory  Design Purpose “Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”*  Design Pattern Capture family creation in a class containing a factory method for each class in the family. * Gamma et al 7.4 SingletonFactory Abstract Factory PrototypeSummary 14/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Abstract Factory * Abstract Factory Interface (Figure 7.17) Style…. Client StyleAFactoryStyleBFactory Ensemble setAbstractFactory() doAFunction() AbstractFactory getAPart1Object() getAPart2Object() * relationships within pattern application not shown SingletonFactory Abstract Factory PrototypeSummary 15/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Application of Abstract Factory Interface of Abstract Factory Applied to Word Processor Client SmallStyleLargeStyle Style Document setStyle() display() SingletonFactory Abstract Factory PrototypeSummary 16/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser «create» The Abstract Factory Idea (Figure 7.19) AbstractFactory getAPart1Object() getAPart2Object() StyleAFactory getAPart1Object() getAPart2Object() Part1StyleAPart2StyleA Part1Part2 abstractFactory 1 Ensemble setAbstractFactory() doAFunction() Client SingletonFactory Abstract Factory PrototypeSummary 17/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser «create» Abstract Factory (Figure 7.20) Style…. AbstractFactory getAPart1Object() getAPart2Object() StyleAFactoryStyleBFactory Part1StyleAPart1StyleBPart2StyleAPart2StyleB Part1Part2 abstractFactory1 Ensemble doAFunction() Client 1..n Part… SingletonFactory Abstract Factory PrototypeSummary 18/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser We want to separate the code parts that format the document in each style. We also want to separate the common document generation code. This facilitates reusing parts and checking for correctness. KEY CONCEPT Design Goals : Correctness and Reusability SingletonFactory Abstract Factory PrototypeSummary 19/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser To design an application in which there are several possible styles for a collection of objects, capture styles as classes with coordinated factory methods. KEY CONCEPT Abstract Factory Design Pattern SingletonFactory Abstract Factory PrototypeSummary 20/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Prototype Design Pattern  Design Purpose  Create a set of almost identical objects whose type is determined at runtime.  Assume that a prototype instance is known; clone it whenever a new instance is needed. -- when designing for multiple instances which are the same in key respects, create them by cloning a prototype. KEY CONCEPT Prototype Pattern SingletonFactoryAbstract Factory Prototype Summary 21/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Prototype Design Example: A Selection Graphics courtesy COREL Click on choice of storage: Click on choice of chair: Click on choice of desk: Furnit ure color Furnitu re hardwa re type coloni al Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission. SingletonFactoryAbstract Factory Prototype Summary 22/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Prototype consequences  It hides the concrete product classes from the client.  It let client work with application-specific classes without modification.  It adds and removes products at run-time.  It configures an application with classes dynamically. SingletonFactoryAbstract Factory Prototype Summary 23/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser The Prototype Idea Ensemble createEnsemble() Client MyPart clone(): MyPart MyPartStyleA clone() MyPartStyleB clone() myPartPrototype1 // To create a MyPart instance: MyPart p = myPartPrototype.clone(); SingletonFactoryAbstract Factory Prototype Summary 24/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Prototype Class Model Ensemble createEnsemble() Client Part1StyleA clone() Part1StyleB clone() Part2StyleA clone() Part2StyleB clone() Part1 clone() Part2 clone() part1Prototypepart2Prototype // To create a Part1 object: Part1 p1 = part1Prototype.clone(); …. Part1StyleB returnObject = new Part1StyleB(); …. Part1StyleC clone() SingletonFactoryAbstract Factory Prototype Summary 25/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser We want to isolate the parts pertaining to each type of customer. We also want to isolate the common customer code. This makes it easier to check the design and implementation for correctness, and to reuse the parts. KEY CONCEPT Design Goals : Correctness and Reusability SingletonFactoryAbstract Factory Prototype Summary 26/27

SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Summary of Creational Patterns  Use Creational Design Patterns when creating complex objects  Singleton  for exactly one, safely  when a class has exactly one instance  Factory when creating individuals  Abstract Factory when creating families  Prototype to “mix & match” SingletonFactoryAbstract FactoryPrototype Summary 27/27