1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
The Bridge Pattern.. Intent Decouple an abstraction from its implementation so that the two can vary independently Also known as: Handle/Body.
Plab – Tirgul 12 Design Patterns
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Object-Oriented Design Patterns CSC 335: Object-Oriented Programming and Design.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Design Patterns.
Chapter 2: Objects and Primitive Data Classes and Objects String, Random, Math, NumberFormat, DecimalFormat and Wrapper Classes.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Advanced Programming Rabie A. Ramadan 7.
Objects First With Java A Practical Introduction Using BlueJ Designing applications 1.0.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Abstract Factory Design Pattern making abstract things.
Factory Method A Creational Design Pattern. Factory Method Key Features  Defines an interface for creating objects without needing to know each object’s.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Designing applications Main concepts to be covered Discovering classes CRC cards Designing interfaces Patterns Objects First with Java - A Practical.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
CS 4233 Review Feb February Review2 Outline  Previous Business – My.wpi.edu contains all grades to date for course – Review and contact.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Unit 21 Factory Method Summary prepared by Kirk Scott 1.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
SWE 316: Software Design and Architecture – Dr. Khalid Aljasser Objectives Lecture 13 Creational Design Pattern SWE 316: Software Design and Architecture.
CSC 480 Software Engineering Design With Patterns.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
DESIGN PATTERNS -CREATIONAL PATTERNS WATTANAPON G SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO 1.
Design Patterns Introduction
Dependency Injection Frameworks Technion – Institute of Technology Author: Assaf Israel - Technion 2013 ©
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Singleton Pattern Presented By:- Navaneet Kumar ise
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
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.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Generator Design Patterns: Singleton and Prototype
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Design Patterns Spring 2017.
MPCS – Advanced java Programming
Factory Patterns 1.
Software Design and Architecture
Intent (Thanks to Jim Fawcett for the slides)
Singleton design pattern
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
Software Design Lecture : 28.
Presentation transcript:

1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design

2 Outline  Three Creational Design Patterns  Singleton  Factory  Prototype

3 To use new or to not use new? That is the question  Since most object-oriented languages provide object instantiation with new and initialization with constructors  There may be a tendency to simply use these facilities directly without forethought to future consequences  The overuse of this functionality often introduces inflexibility in the system

4 Creational Patterns  Creational patterns describe object-creation mechanisms that enable greater levels of reuse in evolving systems: Builder, Singleton, Prototype  The most widely used is Factory  This pattern calls for the use of a specialized object solely to create other objects

5 OO Design Pattern Factory Design Pattern  Name: Factory  Problem: A Client needs an object and it doesn't know which of several objects to instantiate  Solution: Let an object instantiate the correct object from several choices. The return type is an abstract class or an interface type.

6 Characteristics  A method returns an object  The return type is an abstract class or interface  The interface is implemented by two or more classes or the class is extended by two or more classes

7 Example from Java  Border is an interface  AbstractBorder is an abstract class  BorderFactory has a series of static methods returning different types that implement Border  This hides the implementation details of the subclasses  The factory methods directly call the constructors of the subclasses of AbstractBorder

8 One type setSize(250, 100); JPanel toBeBordered = new JPanel(); Border border = BorderFactory.createMatteBorder(2, 1, 5, 9, Color.RED); toBeBordered.add(new JLabel("" + border.getClass())); toBeBordered.setBorder(border); getContentPane().add(toBeBordered, BorderLayout.CENTER);

9 Another type setSize(250, 100); JPanel toBeBordered = new JPanel(); Border border = BorderFactory.createEtchedBorder(); toBeBordered.add(new JLabel("" + border.getClass())); toBeBordered.setBorder(border); getContentPane().add(toBeBordered, BorderLayout.CENTER);

10 Lots of Subclasses javax.swing.border.AbstractBorder java.lang.Object javax.swing.border.AbstractBorder All Implemented Interfaces: SerializableSerializable, BorderBorder Direct Known Subclasses: BasicBorders.ButtonBorderBasicBorders.ButtonBorder, BasicBorders.FieldBorder, BasicBorders.MarginBorder, BasicBorders.MenuBarBorder, BevelBorder, CompoundBorder, EmptyBorder, EtchedBorder, LineBorder, MetalBorders.ButtonBorder, MetalBorders.Flush3DBorder, MetalBorders.InternalFrameBorder, MetalBorders.MenuBarBorder, MetalBorders.MenuItemBorder, MetalBorders.OptionDialogBorder, MetalBorders.PaletteBorder, MetalBorders.PopupMenuBorder, MetalBorders.ScrollPaneBorder, MetalBorders.TableHeaderBorder, MetalBorders.ToolBarBorder, TitledBorderBasicBorders.FieldBorder BasicBorders.MarginBorderBasicBorders.MenuBarBorder BevelBorderCompoundBorderEmptyBorderEtchedBorder LineBorderMetalBorders.ButtonBorder MetalBorders.Flush3DBorderMetalBorders.InternalFrameBorder MetalBorders.MenuBarBorderMetalBorders.MenuItemBorder MetalBorders.OptionDialogBorderMetalBorders.PaletteBorder MetalBorders.PopupMenuBorderMetalBorders.ScrollPaneBorder MetalBorders.TableHeaderBorderMetalBorders.ToolBarBorder TitledBorder

11 Iterators?  The iterator methods isolate the client from knowing the class to instantiate List list = new ArrayList (); Iterator itr = list.iterator(); System.out.println(itr.getClass().toString());  What type is itr ? class java.util.AbstractList$Itr  What type is itr with this change? List list = new Linked List ();

12 Do we need new?  Objects can be returned without directly using new double amount = ; NumberFormat formatter = NumberFormat.getCurrencyInstance(); System.out.println(formatter.format(amount)); Output if the computer is set to US $12, Change the computer setting to Germany and we get this: ,12 €

13 What Happened?  getCurrencyInstance returns an instance of DecimalFormat where methods like setCurrency help build the appropriate object  It encapsulates the creation of objects  Can be useful if the creation process is complex, for example if it depends on settings in configuration files or the jre or the OS

14 Behind the scenes  Client: main method  Factory Method: getCurrencyInstance  Product: a properly configured instance of DecimalFormat  This is another example of Factory in use