0% found this document useful (0 votes)
23 views40 pages

1 Creational Design Patterns

Uploaded by

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

1 Creational Design Patterns

Uploaded by

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

Creational Patterns

1
UML Revisited

Aggregation: “is part of”

Composition: “is entirely made of”

Generalization

Dependency: “uses”
2
Creational Patterns to be Covered

ꟷFactory method
ꟷAbstract factory
ꟷBuilder
ꟷSingleton

Examples taken from:


https://www.tutorialspoint.com/design_pattern/
https://www.javatpoint.com/design-patterns-in-java

3
Factory Method

Intent • Define an interface for creating an object, but


let subclasses decide which class to instantiate.
Factory Method lets a class defer instantiation
to subclasses.

Problem • A framework needs to standardize the


architectural model for a range of applications,
but allow for individual applications to define
their own domain objects and provide for their
instantiation.
• Enable the creator to defer product creation to
sub-class.

4
Solution

5
Example: Drawing

<<interface>> Client
Shape
+ draw()

Circle Square Rectangle ShapeFactory


+drawShape(type):Shape
+ draw() + draw() + draw()

6
public interface Shape {
void draw();
}
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Inside Rectangle::draw() method.");
}
}
Similar implementation for Square and Circle

7
Example: Kiosk

<<interface>> Kiosk
PaymentProcessor
+ processPayment()

GetPayment
Cash CreditCard Coupon +getProcessor(type):
PaymentProcessor
+ prcessPayment() + prcessPayment() + prcessPayment()

8
Factory

9
10
11
Consequences
• Factory design pattern provides approach to code for interface rather
than implementation.
• Factory pattern removes the instantiation of actual implementation
classes from client code. Factory pattern makes our code more
robust, less coupled and easy to extend. For example, we can easily
change lower class implementation because client program is
unaware of this.
• Factory pattern provides abstraction between implementation and
client classes through inheritance.

12
Abstract Factory

Intent • Provide an interface for creating families of


related or dependent objects without
specifying their concrete classes.

Problem • A portable application needs to encapsulate


platform dependencies
• Consider an application that support multiple
look and feels.
• An application need to work with multiple
types of DBMS

13
Solution

14
Example: Car Factory
<<interface>> Client
HondaFactory
+ createEngine()
+ createTrans()

Engine

CivicFactory AccordFactory
+ createEngine() + createEngine()
+ createTrans() + createTrans()
CivicEngine AccordEngine

Transmission

CivicTrans AccordTrans

15
https://www.tutorialspoint.com/design_pattern/design_pattern
_quick_guide.htm

16
17
18
Abstract Factory

19
20
21
22
23
24
Consequences
• Isolates the concrete class
• Makes exchanging product family easy
• Promotes consistency among products
• Abstract Factory design pattern provides approach to code for
interface rather than implementation.
• Abstract Factory pattern is “factory of factories” and can be easily
extended to accommodate more products.
• Abstract Factory pattern is robust and avoid conditional logic of
Factory pattern.
• Supporting new types of products is difficult

25
Singleton

Intent • Ensure a class has only one instance, and


provide a global point of access to it.
• Encapsulated "just-in-time initialization" or
"initialization on first use".

Problem • Application needs one, and only one, instance


of an object. Additionally, lazy initialization and
global access are necessary.

26
Solution

SingletonClass
- instance: SingletonClass

- SingletonClass()
+ getInstance(): SingletonClass

27
Example: DB Connection Manger
public class DbConnection{

private static DbConnection instance=null;


private SQLConnection connection;

private DbConnection() {
connection = connectToDatabase(dbUser,dbPassword,dbName);
}

public static getDbConnection() {


if (instance== null )
instance = new DbConnection() ;

return instance;
}
}

DbConnection connection=DbConnection.getDbConnection();

28
Consequences
• Controlled access to sole instance

• Reduced namespace

• Permits variable number of instances

29
Builder

Intent • Separate the construction of a complex object


from its representation so that the same
construction process can create different
representations.
• Parse a complex representation, create one of
several targets.
Problem • An application needs to create the elements of
a complex aggregate. The specification for the
aggregate exists on secondary storage and one
of many representations needs to be built in
primary storage.

30
Solution

31
Examples

To create a computer,
different parts are assembled
depending upon the order
received by the customer
(e.g., a customer can demand
a 500 GB hard disk with an
Intel processor; another
customer can choose a 250
GB hard disk with an AMD
processor).

32
Consequences
• Lets you vary a product’s internal representation

• Isolates construction and representation

• Gives you finer control over the construction process

33
34
35
36
Builder

37
38
39
40

You might also like