Slot 07-Design Pattern in
Slot 07-Design Pattern in
Lession 5
Objectives
Design patterns are not meant for project development. Design patterns are
meant for common problem-solving and whenever there is a need, we have
to implement a suitable pattern to avoid such problems in the future
By using the design patterns we can make our code more flexible, reusable,
and maintainable
The Gang of Four (GoF) patterns are generally considered the foundation for
all other patterns. They are categorized in three groups: Creational,
Structural, and Behavioral
Singleton Pattern
Singleton Pattern
◆ Defines an Instance operation that lets clients access its unique instance. Instance is a
class operation
◆ Responsible for creating and maintaining its own unique instance
◆ Make the class of the single instance responsible for access and "initialization on
first use". The single instance is a private static attribute. The accessor function is a
public static method
subclasses
◆ In Factory pattern, we create object without exposing
the creation logic to client and the client use the same
common interface to create new type of object
◆ The idea is to use a (static)member-function or
◆ A factory pattern is one of the core design principles to create an object, allowing
clients to create objects of a library in a way such that it doesn’t have tight coupling
with the class hierarchy of the library
Builder Pattern
Builder Pattern
Product – The product class defines the type of the complex object that is to be
generated by the builder pattern.
Builder – This abstract base class defines all of the steps that must be taken in
order to correctly create a product. Each step is generally abstract as the actual
functionality of the builder is carried out in the concrete subclasses. The
GetProduct method is used to return the final product. The builder class is often
replaced with a simple interface
The parameters to the constructor are reduced and are provided in highly
readable method calls
Prototype Pattern
Prototype Pattern
34
37
Summary