Creational Design Pattern
Creational Design Pattern
Creational Patterns
Tai Nguyen
Mobile Division
By Purpose
Overview
What is it
How to implement
When to use
How to expose
Creational Patterns
Creational Patterns
Deal with object creation mechanisms, trying to create objects in a manner
suitable to the situation
❖ Singleton
1. Give a problem
❖ Factory Method
2. Solved by general way
❖ Abstract Factory
3. Solved by Design pattern
❖ Builder
(Give example by Java)
❖ Prototype
Problem 1: You need to use the same single instance of an object in
whole your application
You want to have a object to manage Student list like ‘StudentManager’ class with simple functionality:
add, show. And then use it in whole application to synchronize data.
Solved by general way
Solved by
Singleton pattern
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".
● Prevent creating more than one instance
Diagram
Solved by
Singleton pattern
Implementation
Solved by
Singleton pattern
Test
Output
Singleton pattern
When to use
● Application has only one instance, and provide a global point of access
● Shared resource, Logger, Configuration, Caching, Data manager,...
Problem 2: You don’t know ahead of time what exact type and its
dependencies your code should work with
A store sells goods and accepts some Banks to pay such as TPBank, VietcomBank,...
Solved by general way
Solved by
Factory Method pattern
Intent
● Define an interface for creating an object, but let subclasses decide which class to
instantiate
● Create object without exposing the creation logic to the client and refer to newly created
object using a common interface
Solved by
Factory Method pattern
Diagram
Solved by
Factory Method pattern
Solved by
Factory Method pattern
Factory Method pattern
When to use
● A superclass has a lot of subclass, based on imputed type we return a subclass
● We don't know what subclasses will be needed in the future. When need to
extend, create subclass and implement more factory method for instantiating
this subclass
Problem 3: you have a complex conditional logic in your code
You need to implement either a switch statement with several cases or an if-else logic with several
conditions.
Example:
A company produces of chairs (Chair): plastic chairs (PlasticChair) and wooden chairs (WoodChair),
Due to earn too much money, They decided to expand th production of tables (Table): plastic
(PlasticTable) and wood (WoodTable)
Problem 3: you have a complex conditional logic in your code
Solved by general way
1. General way
Solved by general way
Intent
● Provide an interface for creating families of related or dependent objects
without specifying their concrete classes
● This factory is also called as factory of factories
Solved by
Abstract Factory pattern
Solved by
Abstract Factory pattern
Solved by
Abstract Factory pattern
Abstract Factory pattern
1. Title
2. Message
3. Button
○ Positive button
○ Negative button
Solved by general way
1. Bunch of constructor
Solved by general way
2. Setter
Solved by
Builder pattern
Intent
● Separate the construction of a complex object from its representation so that
the same construction process can create different representations
Solved by
Builder pattern
Implement Builder
● static nested class with copy of all properties of main class (AlertDialog)
● setter methods for optional properties and return itself (Builder)
● provide build method to return the object (AlertDialog)
When to use
● Create a complex object: have many properties, and some are required, some
are optional.
● When there are too many constructor functions
● Want to control the build process.
Problem 5: You need to make several exact copies of a complex
object.
Intent
● Specify the kinds of objects to create using a prototypical instance, and create
new objects by copying this prototype
Diagram
Solved by
Prototype pattern
Implement
Depend on language that has specific clone interface of Prototype. In Java, we use Cloneable
and then override clone() method
Solved by
Prototype pattern
Prototype pattern
When to use
● Create another new object based on the original object that cannot use the new
operator or constructor functions to initialize.
● Pass an object to a function for processing without changing itself
● The cost of creating a new object (using the new operator) is large.
● Hide the complexity of client-side object initialization.
Q&A
Download demo sample
Reference
[1]. Huong-dan-java-design-pattern-prototype
[2]. Tutorialspoint
[3]. Sourcemaking
[4]. scientificprogrammer