0% found this document useful (0 votes)
26 views18 pages

Chap - 3 - Creational Design Patterns

creational design patterns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views18 pages

Chap - 3 - Creational Design Patterns

creational design patterns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Software component

design
Chapter Two

Creational Design Pattern

27/01/2025 intro. to software component base 1


d development
Creational design
patterns
 Deals with object creation and initialization,
providing guidance about which objects are
created for a given situation.
 Are concerned with the way of creating objects.
 These patterns can be further categorized into
o Class-creation patterns use inheritance
effectively in the instantiation process,
o Object-creation patterns use delegation
effectively to get the job done.

27/01/2025 intro. to software component base 2


d development
Creational design
patterns…
 Types of creational design patterns
o Singleton
o Factory Method
o Abstract Factory
o Prototype
o Builder
o Object pool

27/01/2025 intro. to software component base 3


d development
1. singleton design
pattern
 It ensures that at most only one instance of an
object exists throughout application.
 It define a class that has only one instance and
provides a global point of access to it.
 In other words, a class must ensure that only
single instance should be created and single
object can be used by all other classes.
 There are two forms of singleton design pattern
o Early Instantiation - creation of instance at load time.
o Lazy Instantiation - creation of instance when
required.

27/01/2025 intro. to software component base 4


d development
1. singleton design
pattern…
 Characteristics of Singleton pattern
o They are static in nature

o Private constructor

o Private instance of class

o No parameter to the constructor

27/01/2025 intro. to software component base 5


d development
1. singleton design
pattern…
 Pattern name: Singleton pattern
 Problem: How can we guarantee that one and
only one instance of a class can be created?
 Solution: Create a class with a class operation
getInstance().
o On subsequent calls of getInstance(), no new
instance is created, but identity of existing
object is returned.

27/01/2025 intro. to software component base 6


d development
1. singleton design
pattern…
Application areas
 Use Singleton Design Pattern when
o Resources that are expensive to create (like
database connection objects)
o Classes which provide access to configuration
settings for the application
o Classes that contain resources that are
accessed in shared mode
o It's good practice to keep all loggers as
Singletons which increases performance

27/01/2025 intro. to software component base 7


d development
1. singleton design
pattern…
Singleton structure

27/01/2025 intro. to software component base 8


d development
1. singleton design
pattern…
Singleton patterns participants
 Singleton
o Declare all constructors private and provide
only one entry for obtaining a reference to the
sole instance.
 Client
o Clients can get to the sole instance of Singleton
by asking Singleton to return a reference to it.
 Collaborations
o A client can only call getInstance() to get a ref-
erence to the sole instance.
o A client cannot create any instance of Singleton

27/01/2025 intro. to software component base 9


d development
1. singleton design
pattern…
,

27/01/2025 intro. to software component base 10


d development
1. singleton design
pattern…
Sample code..
Step 1
 public class SingleObject {
//create an object of SingleObject
private static SingleObject instance = new SingleObject();
//make the constructor private so that this class cannot be
//instantiated
private SingleObject(){}
//Get the only object available
public static SingleObject getInstance(){
return instance;
}
public void showMessage(){
System.out.println("Hello World!");
}
}

27/01/2025 intro. to software component base 11


d development
1. singleton design
pattern…
Sample code..
Step 2
 public class SingletonPatternDemo {
public static void main(String[] args) {
//illegal construct
//Compile Time Error: The constructor SingleObject() is not
visible
//SingleObject object = new SingleObject();
//Get the only object available
SingleObject object = SingleObject.getInstance();
//show the message
object.showMessage();
}}
 Step3
Hello World!
27/01/2025 intro. to software component base 12
d development
2. Factory method
design pattern
 It define an interface or abstract class for creating
an object without specifying their concrete
classes.
 Control instantiation
 Subclasses are responsible to create the instance
of the class.
 Define an interface for creating an object, but let
subclasses decide which class to instantiate
 also called Virtual Constructor.

27/01/2025 intro. to software component base 13


d development
2. Factory method
design pattern…
Example
 Suppose you want to create multiple instances of
similar kind and want to achieve loose coupling
then you can go for Factory pattern.
 A class implementing factory design pattern
works as a bridge between multiple classes.
 Consider an example of using multiple database
servers like SQL Server and Oracle.
 If you are developing an application using SQL
Server database as backend, but in future need to
change backend database to oracle, you will need
to modify all your code, if you haven’t written
your code by factory
27/01/2025 design
intro. to software pattern.
component base 14
d development
2. Factory method
design pattern…
 Name : Factory method
 Problem : A framework with abstract application
classes and application-specific subclasses to in-
stantiate to realize different implementations
 Solution : The Factory Method pattern encapsu-
lates the knowledge of which subclass to create
and moves this knowledge out of the framework.

27/01/2025 intro. to software component base 15


d development
2. Factory method
design pattern…
Use the Factory Method pattern when
 A class can't anticipate the class of objects it
must create.
 A class wants its subclasses to specify the objects
it creates

27/01/2025 intro. to software component base 16


d development
1. singleton design
pattern…
 Mmm
 Mmm

27/01/2025 intro. to software component base 17


d development
Software component
design
 Mmm
 Mmm

27/01/2025 intro. to software component base 18


d development

You might also like