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

Slides:



Advertisements
Similar presentations
Creational Design Patterns. Creational DP: Abstracts the instantiation process Helps make a system independent of how objects are created, composed, represented.
Advertisements

Chapter 5: The Singleton Pattern
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Plab – Tirgul 12 Design Patterns
Oct Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
Spring 2010ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
Winter 2007ACS-3913 Ron McFadyen1 Singleton To guarantee that there is at most one instance of a class we can apply the singleton pattern. Singleton Static.
7/16/2015Singleton creational design pattern1 Eivind J. Nordby Karlstad University Dept. of Computer Science.
Creational Patterns Making Objects The Smart Way Brent Ramerth Abstract Factory, Builder.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Singleton Christopher Chiaverini Software Design & Documentation September 18, 2003.
Programming Languages and Paradigms Object-Oriented Programming.
Design Patterns.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Object-Oriented Software Engineering Practical Software Development using UML and Java Chapter 6: Using Design Patterns 1.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Tech Talk Go4 Factory Patterns Presented By: Matt Wilson.
Software Components Creational Patterns.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
The Factory Patterns SE-2811 Dr. Mark L. Hornick 1.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Creational Pattern: Factory Method At times, a framework is needed to standardize the behavior of objects that are used in a range of applications, while.
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
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 Introduction
Threads and Singleton. Threads  The JVM allows multiple “threads of execution”  Essentially separate programs running concurrently in one memory space.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Advanced Object-oriented Design Patterns Creational Design Patterns.
Singleton Pattern Presented By:- Navaneet Kumar ise
The Singleton Pattern (Creational)
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.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Design Patterns: Brief Examples
Factory Method Pattern
Design Patterns Spring 2017.
Design Pattern Catalogues
MPCS – Advanced java Programming
Design Patterns C++ Java C#.
Factory Patterns 1.
Design Patterns Lecture 1
Inheritance and Polymorphism
The Singleton Pattern SE-2811 Dr. Mark L. Hornick.
Design Patterns C++ Java C#.
Creational Design Patterns
Factory Method Pattern
Software Engineering Lecture 7 - Design Patterns
SE-2811 Software Component Design
IFS410: Advanced Analysis and Design
CSE 432 Presentation GoF: Factory Method PH: “To Kill a Singleton”
Singleton design pattern
Ms Munawar Khatoon IV Year I Sem Computer Science Engineering
CS 350 – Software Design Singleton – Chapter 21
SE-2811 Software Component Design
Design pattern Lecture 6.
Creational Patterns.
5. Strategy, Singleton Patterns
Presentation transcript:

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

2 Outline  Three Creational Design Patterns  Singleton  Factory  Abstract 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 Recurring Problem Some classes have only one instance. For example, there may be many printers in a system, but there should be only one printer spooler How do we ensure that a class has only one instance and that instance is easily accessible? Solution Have constructor return the same instance when called multiple times Takes responsibility of managing that instance away from the programmer It is simply not possible to construct more instances OO Design Pattern Singleton

6 UML General form as UML (From

7 Java Code General Form // NOTE: This is not thread safe! public class Singleton { private static Singleton uniqueInstance; // other useful instance variables here private Singleton() {} public static Singleton getInstance() { if (uniqueInstance == null) { uniqueInstance = new Singleton(); } return uniqueInstance; } // other useful methods here }

8 Participant Singleton Defines a constructor that returns the (single) instance of this class. Store this instance in the class ( uniqueInstance ). Defines operations ( SingletonOperation() ) and data ( getSingletonData() ) for this instance. Optionally, may also define a static method ( Instance() ) to return the instance, instead of a constructor (in this case, the constructor would be a private method).

9 Implementing Singleton in Java Make constructor(s) private so that they can not be called from outside. Declare a single static private instance of the class. Write a public getInstance() or similar method that allows access to the single instance.

10 Patterns IP-10 Use Example: A PhoneState class PhoneState { private static PhoneState pS = new PhoneState(); public static PhoneState getInstance() { return pS; } private PhoneState() {} public int getNumPeople() { … }

11 Patterns IP-11 An Alternative Approach class PhoneState { private static PhoneState pS; public static PhoneState getInstance() { if (pS == null) { pS = new PhoneState(); } return pS; } private PhoneState() {} public int getNumPeople() { … }

12 OO Design Pattern Factory Method  Name: Factory Method  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.

13 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

14 General Form

15 Product (Page) defines the interface of objects the factory method creates ConcreteProduct implements the Product interface Creator declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object. may call the factory method to create a Product object. ConcreteCreator (Report, Resume) overrides the factory method to return an instance of a ConcreteProduct.

16 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

17 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);

18 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);

19 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

20 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 ();

21 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 €

22 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

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