Chapter 7 Creational Design Pattern. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Chapter 6 Introduction to Design Patterns. Sample Design Goals and Ways to Accomplish Them Reusability, Flexibility, and Efficiency o Reuse flexible designs.
SWE 316: Software Design and Architecture Objectives Lecture 3 Object Orientation SWE 316: Software Design and Architecture NOTE: most slides are adapted.
1 Software Testing and Quality Assurance Lecture 21 – Class Testing Basics (Chapter 5, A Practical Guide to Testing Object- Oriented Software)
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Satzinger, Jackson, and Burd Object-Orieneted Analysis & Design
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
Pattern Abstract Factory
Chapter 7: The Object-Oriented Approach to Requirements
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. The Factory Method Design Pattern (1) –A creational design.
Chapter 4 Design Principles I Correctness and Robustness
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture # 05: Design Principles I - Correctness and Robustness SWE 316: Software.
Design Patterns.
Chapter 10 Introduction to Components. Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFramework Detailed.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 12 Object-Oriented.
Chapter 2 Object Orientation. Process Phase Affected by This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design.
Prologue: The Software Process. Main Phases of Software Process 1. Requirements Analysis (answers “WHAT?”) Specifying what the application must do 2.
Chapter 5 Design Principles II: Flexibility, Reusability, and Efficiency.
Design Dan Fleck CS 421 George Mason University. What is the design phase? Analysis phase describes what the system should do Analysis has provided a.
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.
Chapter 8: Writing Graphical User Interfaces
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 7 Requirements and Domain Classes SWE 316: Software Design and Architecture.
SWE 316: Software Design and Architecture Objectives Lecture # 19 Java Beans SWE 316: Software Design and Architecture  Understand  what JavaBeans (“Beans”)
Systems Analysis and Design in a Changing World, 6th Edition
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
Prof. Hertz (as told by xkcd.com)‏. Computer Science 313 – Advanced Programming Topics.
Abstract Factory Design Pattern making abstract things.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CPSC 372 John D. McGregor Module 4 Session 1 Design Patterns.
SWE 316: Software Design and Architecture Objectives Lecture # 18 Introduction to Components SWE 316: Software Design and Architecture To learn:  benefits.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
♦ Use Case Model  Detailled use case - Important  Use case diagram- Refactoring Use case diagram  > 1 Last Lectures.
Lecture 8 Object-Oriented Analysis and Design 20.1 COSC4406: Software Engineering.
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.
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns Objects First with Java - A Practical.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
CPSC 871 John D. McGregor Module 5 Session 1 Design Patterns.
Object-Oriented Modeling: Static Models. Object-Oriented Modeling Model the system as interacting objects Model the system as interacting objects Match.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
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.
ANU comp2110 Software Design lecture 8 COMP2110 Software Design in 2004 lecture 8 Software Architecture 1 of 2 (design, lecture 3 of 6) Goal of this small.
UML (Unified Modeling Language)
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Abstract Factory Pattern
Factory Patterns 1.
Software Design and Architecture
Creational Design Patterns
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Presented by Igor Ivković
Design pattern Lecture 6.
Chapter 5 Architectural Design.
Presented by Igor Ivković
Presentation transcript:

Chapter 7 Creational Design Pattern

Process Phases Discussed in This Chapter Requirements Analysis Design Implementation ArchitectureFrameworkDetailed Design x Key:= secondary emphasis x = main emphasis Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Purpose Create individual objects in situations where the constructor alone is inadequate. Design Pattern Summary Use methods to return required objects. Factory Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Factory Class Model Factory design pattern MyClass createObjectOfRequiredClass(): RequiredClass «create object» RequiredClass Client Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Goal At Work:  Reusability and Corrrectness  We want to write code about automobiles in general: Code that applies to any make, exercised repeatedly (thus reliably). Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Application of Factory design pattern Factory Example Ford createAutomobile() Toyota createAutomobile() Automobile createAutomobile(): Automobile Client «create object» Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

createObjectOfRequiredClass() Sequence Diagram for Factory :MyClass:Client RequiredClass() :RequiredClass Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Typical Output of Generation Example Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Goals At Work:  Correctness and Reusability  We want to separate the code common to all types of customers. We want to separate the specialized code that generates for each type of customer. This makes it easier to check for correctness and to reuse parts. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Application of Factory design pattern Customer getMessage() Client sendMessage() Frequent getMessage() Returning getMessage() Curious getMessage() Newbie getMessage() MailMessage text MailGenerationApplication getCustomerTypeFromUser() «setup» Factory : Generation Example Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Factory Applied to getGraphics() in Java Factory design pattern Component getGraphics(): Graphics «create object» Client Graphics Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Key Concept:  Singleton Design Pattern  -- when a class has exactly one instance. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Purpose S 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 S SS; Make the constructor of S private; define a private static attribute for S of type S; define a public accessor for it. Singleton Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Goal At Work:  Correctness  Singleton enforces the intention that only one User object exists, safeguarding the application from unanticipated User instance creation. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Singleton : Class Model Singleton Design Pattern MyClass getSingletonOfMyClass(): MyClass Client 1 singletonOfMyClass «static» Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

The Singleton Design Pattern -- applied to MyClass 1.Define a private static member variable of MyClass of type MyClass private static MyClass singletonOfMyClass = new MyClass(); 2.Make the constructor of MyClass private private MyClass() { /* …. constructor code …. */ }; 3.Define a public static method to access the member public static MyClass getSingletonOfMyClass() { return singletonOfMyClass; } Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Output for Singleton Experiment Example Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Application of Singleton to Experiment Example Experiment theExperiment: Experiment analyze() getTheExperiment(): Experiment reportResults() Client theExperiment 1 «static» Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Key Concept:  Singleton Design Pattern  When a class must have exactly one instance, make the constructor private and the instance a private static variable with a public accessor. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Word Processor Interaction 1 of 2 ---> Enter title : My Life ---> Enter Heading or “-done”: Birth ---> Enter text: I was born in a small mountain hut …. ---> Enter Heading or “-done”: Youth ---> Enter text: I grew up playing in the woods … ---> Enter Heading or “-done”: Adulthood …. ---> Enter Heading or “-done”: -done ( continued ) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Word Processor Interaction 2 of 2 : Output Options …. >> Enter the style you want displayed: big Title: MY LIFE Section BIRTH --- I was born in a mountain hut …. Section YOUTH --- I grew up sturdy … Section ADULTHOOD --- …. >> Enter the style you want displayed: small My Life Birth I was born in a mountain hut …. Youth I grew up sturdy … Adulthood Option 2Option 1 Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

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 containting a factory method for each class in the family. Abstract Factory * Gamma et al Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Abstract Factory * Abstract Factory Interface Style…. Client StyleAFactoryStyleBFactory Ensemble setAbstractFactory() doAFunction() AbstractFactory getAPart1Object() getAPart2Object() * relationships within pattern application not shown Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Application of Abstract Factory Interface of Abstract Factory Applied to Word Processor Client SmallStyleLargeStyle Style Document setStyle() display() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

«create» The Abstract Factory Idea Ensemble setAbstractFactory() doAFunction() AbstractFactory getAPart1Object() getAPart2Object() StyleAFactory getAPart1Object() getAPart2Object() Part1Part2 Part1StyleAPart2StyleA abstractFactory1 Client Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

«create» Abstract Factory Style…. AbstractFactory getAPart1Object() getAPart2Object() StyleAFactoryStyleBFactory Part1Part2 Part1StyleAPart1StyleBPart2StyleAPart2StyleB abstractFactory1 Ensemble doAFunction() Client 1..n Part… Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

} } Sequence Diagram for Abstract Factory StyleXFactory() :ClientabstractFactory :StyleXFactory :Ensemble setAbstractFactory (abstractFactory ) getAPart_i() Part_iStyleX() Selecting a style Creating a Part_i object in required style getAPart_i() abstractFactory :AbstractFactory :Part_iStyleX doAFunction() Virtual function property Assume that this method requires a Part_i object. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Goals At Work:  Correctness and Reusability  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. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Abstract Factory Applied to Word Processor Document getAHeading() getATitle() grtDocumentFromUser() «create» SmallStyle getAHeading() getATitle() LargeStyle getAHeading() getATitle() Title value Heading value LargeHeading display() SmallHeading display() LargeTitle display() SmallTitle display() Text value 1 0..n Style getAHeading() getATitle() Displayable display() Client getStyleFromUser() displayDocument() style Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

“Word Processor” Interaction Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

An Abstract Factory Application: Java ToolKit ToolKit createButton() createImage() ConcreteToolkit1 createButton() createImage() ConcreteToolkit2 createButton() createImage() ButtonPeer ConcreteToolkit3 createButton() createImage() ButtonPeer implementation 3 ImagePeer implementation 3 ImagePeer Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Abstract Factory: Narrow Interface Style…. AbstractFactory getAPart1Object() getAPart2Object() StyleAFactoryStyleBFactory abstractFactory1 Ensemble setStyleA() setStyleB() … Client.. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Key Concept:  Abstract Factory Design Pattern  To design an application in which there are several possible styles for a collection of objects, capture styles as classes with coordinated factory methods. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Prototype Design Example: A Selection Graphics courtesy COREL Click on choice of storage: Click on choice of chair: Click on choice of desk: Furniture color Furniture hardware type colonial Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

A Simplified Prototype Example Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Purpose Create a set of almost identical objects whose type is determined at runtime. Design Pattern Assume that a prototype instance is known; clone it whenever a new instance is needed. Prototype Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Prototype Interface With Clients Ensemble createEnsemble() Client Part1 clone() Part2 clone() (optional part of interface) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Prototype Design Pattern The Prototype Idea Ensemble createEnsemble() Client MyPart clone(): MyPart MyPartStyleA clone() MyPartStyleB clone() myPartPrototype1 // To create a MyPart instance: MyPart p = myPartPrototype.clone(); Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Prototype Class Model Ensemble createEnsemble() Client Part1 clone() Part2 clone() Part1StyleA clone() Part1StyleB clone() Part2StyleA clone() Part2StyleB clone() part1Prototypepart2Prototype // To create a Part1 object: Part1 p1 = part1Prototype.clone(); …. Part1StyleB returnObject = new Part1StyleB(); …. Part1StyleC clone() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Sequence Diagram for Prototype partNPrototype :PartN :Ensemble createEnsemble() clone() partNPrototype :PartNStyleB PartNStyleB() (virtual function property) Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Design Goals At Work:  Correctness and Reusability  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. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Customer Information Entry : Typical Scenario Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

OfficeProcess doAProcess() Prototype Example Outline HiVolCustomer clone() LoVolCustomer clone() Customer clone(): Customer customerPrototype Client 1 MedVolCustomer clone() Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Requirement for (Deep) Cloning Class1Class2 c1:Class1c2:Class2 (2) c1 an instance of Class1 : c1.clone should be as follows (deep clone): c1.clone:Class1x*:Class2 * a clone of c2 In shallow cloning, c1.clone actually as follows! c1.clone:Class1  Given: (1) Class model: Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Output for Demonstration of Shallow Cloning Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Output for Demonstration of Deep Cloning Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Key Concept:  Prototype Pattern  -- when designing for multiple instances which are the same in key respects, create them by cloning a prototype. Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Summary of Creational Patterns Use Creational Design Pattern s when creating complex objects o Factory when creating individuals o Abstract Factory when creating families o Prototype to “mix & match” o Singleton for exactly one, safely Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.

Automobile Drawing ClassicDragsterRefined Fenders: RedGreen Purple GrainyGloss Color: Texture: ClassicDragsterRefined Style: Car style: Tires: … … Graphics courtesy of Corel Lights: … Adapted from Software Design: From Programming to Architecture by Eric J. Braude (Wiley 2003), with permission.