Chapter 5. Creational Patterns
Chapter 5. Creational Patterns
CHƯƠNG 5
CREATIONAL PATTERNS
1
NỘI DUNG
Singleton Pattern
Prototype Pattern
Builder Pattern
Factory Method Pattern
Abstract Factory Pattern
2
SINGLETON PATTERN
4
Singleton Pattern
Real-Life Example:
➢ Suppose you are a member of a sports team and your
team is participating in a tournament. When your
team plays against another team, as per the rules of
the game, the captains of the two sides must have a
coin toss.
➢ If your team does not have a captain, you need to
elect someone to be the captain first. Your team must
have one and only one captain
5
Singleton Pattern
Computer World Example:
➢ In some software systems, you may decide to
maintain only one file system so that you can use it for
the centralized management of resources.
6
Singleton Pattern
Key characteristics:
➢ The constructor is private in this example. So, you
cannot instantiate in a normal fashion (using new).
➢ Before you attempt to create an instance of a class,
you check whether you already have an available
copy.
➢ If you do not have any such copy, you create it;
otherwise, you simply reuse the existing copy.
7
Singleton Pattern
Class Diagram:
8
Singleton Pattern
9
Singleton Pattern
10
Singleton Pattern
Discussion:
➢ The Common Language Runtime (CLR) takes care of
the variable initialization process.
➢ You create an instance when any member of the class
is referenced
➢ The constructor is private. So, you cannot instantiate
the Singleton class inside Main(). This will help you
refer to the one instance that can exist in the system.
11
Singleton Pattern
Discussion:
➢ The public static member ensures a global point of
access. It confirms that the instantiation process will
not start until you invoke the Instance property of the
class (in other words, it supports lazy instantiation).
➢ The sealed keyword prevents the further derivation of
the class (so that its subclass cannot misuse it), and
readonly ensures that the assignment process takes
place during the static initialization.
12
Singleton Pattern
Question:
➢ Why are you complicating stuff? You can simply write
your Singleton class as follows:
13
Singleton Pattern
14
Singleton Pattern
Exercise: Config App class
15
PROTOTYPE PATTERN
17
Prototype Pattern
Real-Life Example:
➢ Suppose you have a master copy of a valuable
document. You need to incorporate some change into
it to analyze the effect of the change. In this case, you
can make a photocopy of the original document and
edit the changes in the photocopied document.
18
Prototype Pattern
Computer World Example:
➢ Let’s assume that you already have an application that
is stable. In the future, you may want to modify the
application with some small changes.
➢ You must start with a copy of your original application,
make the changes, and then analyze further. Surely
you do not want to start from scratch to merely make
a change; this would cost you time and money
19
Prototype Pattern
Class Diagram:
20
Prototype Pattern
21
Prototype Pattern
22
Prototype Pattern
23
Prototype Pattern
24
Prototype Pattern
Question:
1. What are the advantages of using the Prototype
design pattern?
2. What are the challenges associated with using the
Prototype design pattern?
25
Prototype Pattern
Exercise: Computer, Desktop, Laptop
26
BUILDER PATTERN
28
Builder Pattern
Concept:
➢ The Builder pattern is useful for creating complex
objects that have multiple parts.
➢ The creation process of an object should be
independent of these parts; in other words, the
construction process does not care how these parts
are assembled.
➢ In addition, you should be able to use the same
construction process to create different
representations of the objects.
29
Builder Pattern
Real-Life Example:
➢ To complete an order for a computer, different
hardware parts are assembled based on customer
preferences. For example, a customer can opt for a
500GB hard disk with an Intel processor, and another
customer can choose a 250GB hard disk with an AMD
processor.
30
Builder Pattern
Computer World Example:
➢ You can use this pattern when you want to convert
one text format to another text format, such as
converting from RTF to ASCII
31
Builder Pattern
Class Diagram:
32
Builder Pattern
33
Builder Pattern
34
Builder Pattern
35
Builder Pattern
36
Builder Pattern
37
Builder Pattern
38
Builder Pattern
39
Builder Pattern
40
Builder Pattern
Exercise:
41
FACTORY METHOD PATTERN
43
Factory Method Pattern
Real-Life Example:
➢ In a restaurant, based on customer inputs, a chef
varies the taste of dishes to make the final products.
44
Factory Method Pattern
Computer World Example:
➢ In an application, you may have different database users. For
example, one user may use Oracle, and the other may use SQL
Server. Whenever you need to insert data into your database,
you need to create either a SqlConnection or an
OracleConnection and only then can you proceed.
➢ If you put the code into if-else (or switch) statements, you need
to repeat a lot of code, which isn’t easily maintainable. This is
because whenever you need to support a new type of
connection, you need to reopen your code and make those
modifications. This type of problem can be resolved using the
Factory Method pattern
45
Factory Method Pattern
Class Diagram:
46
Factory Method Pattern
47
Factory Method Pattern
48
Factory Method Pattern
49
Factory Method Pattern
50
Factory Method Pattern
51
Factory Method Pattern
52
Factory Method Pattern
Exercise:
53
ABSTRACT FACTORY PATTERN
55
Abstract Factory Pattern
Real-Life Example:
➢ Suppose you are decorating your room with two
different types of tables; one is made of wood and
one of steel. For the wooden type, you need to visit to
a carpenter, and for the other type, you may need to
go to a metal shop. All of these are table factories. So,
based on demand, you decide what kind of factory
you need.
56
Abstract Factory Pattern
Computer World Example:
➢ ADO.NET has already implemented similar concepts
to establish a connection to a database.
57
Abstract Factory Pattern
Class Diagram:
58
Abstract Factory Pattern
59
Abstract Factory Pattern
60
Abstract Factory Pattern
61
Abstract Factory Pattern
62
Abstract Factory Pattern
public class WildAnimalFactory : IAnimalFactory
{
public IDog GetDog()
{
return new WildDog();
}
63
Abstract Factory Pattern
public class PetAnimalFactory : IAnimalFactory
{
public IDog GetDog()
{
return new PetDog();
}
64
Abstract Factory Pattern
65
Abstract Factory Pattern
Exercise:
66