5. Creational Design Patterns
5. Creational Design Patterns
1
UML Revisited
Generalization
Dependency: “uses”
2
Creational Patterns to be Covered
ꟷFactory method
ꟷAbstract factory
ꟷBuilder
ꟷSingleton
3
4
Factory Method
5
Solution
6
Example: Drawing
<<interface>> Client
Shape
+ draw()
7
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
8
Example: Kiosk
<<interface>> Kiosk
PaymentProcessor
+ processPayment()
GetPayment
Cash CreditCard Coupon +getProcessor(type):
PaymentProcessor
+ prcessPayment() + prcessPayment() + prcessPayment()
9
Factory
10
11
12
Another Example Usecase
13
Another Example
https://refactoring.guru/design-patterns/factory-method
14
Use the Factory Method when you want to
provide users of your library or framework
with a way to extend its internal
components
• Imagine that you write an app using an open source UI
framework.
• Your app should have round buttons, but the framework only
provides square ones.
• You extend the standard Button class with a
glorious RoundButton subclass.
• But now you need to tell the main UIFramework class to use the
new button subclass instead of a default one.
• To achieve this, you create a subclass UIWithRoundButtons from a
base framework class and override its createButton method.
• While this method returns Button objects in the base class, you
make your subclass return RoundButton objects.
• Now use the UIWithRoundButtons class instead of UIFramework.
15
Consequences/Benefits
• 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.
16
Abstract Factory
17
Solution
18
Example: Car Factory
<<interface>> Client
HondaFactory
+ createEngine()
+ createTrans()
Engine
CivicFactory AccordFactory
+ createEngine() + createEngine()
+ createTrans() + createTrans()
CivicEngine AccordEngine
Transmission
CivicTrans AccordTrans
19
https://www.tutorialspoint.com/design_pattern/
design_pattern_quick_guide.htm
20
21
22
Abstract Factory
23
24
25
26
27
28
Consequences
29
Singleton
30
Solution
SingletonClass
- instance: SingletonClass
- SingletonClass()
+ getInstance(): SingletonClass
31
Example: DB Connection Manger
public class DbConnection{
private DbConnection() {
connection = connectToDatabase(dbUser,dbPassword,dbName);
}
return instance;
}
}
DbConnection connection=DbConnection.getDbConnection();
32
Consequences
• Controlled access to sole instance
• Reduced namespace
33
Builder
34
Solution
35
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).
36
Consequences
• Lets you vary a product’s internal representation
37
38
39
40
Builder
41
42
43
44