Chapter 05 - Design Pattern in
Chapter 05 - Design Pattern in
NET
Constructivism Topics
07/26/2024 2
Objectives
Overview about Design Pattern
Why must use Design Pattern?
Explain about Singleton Pattern
Explain about Factories Pattern
Explain about Abstract Factory Pattern
Explain about Builder Pattern
Explain about Prototype Pattern
Explain about Observer Pattern
Demo about Factories Method, Prototype Pattern and Singleton Pattern
in .NET applications
07/26/2024 3
Understanding Design Patterns
A design pattern provides a general reusable solution for the common
problems that occur in software design
A design pattern isn't a finished design that can be transformed directly into
code. It is a description or template for how to solve a problem that can be
used in many different situations
07/26/2024 5
Why use Design Pattern?
Design patterns can speed up the development process by providing tested,
proven development paradigms
Effective software design requires considering issues that may not become
visible until later in the implementation
Reusing design patterns helps to prevent subtle issues that can cause major
problems and improves code readability for coders and architects familiar with
the patterns
07/26/2024 6
Why use Design Pattern?
Often, we only understand how to apply certain software design techniques to
certain problems. These techniques are difficult to apply to a broader range of
problems
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
07/26/2024 11
Implement Singleton Pattern
07/26/2024 12
Implement Singleton Pattern
07/26/2024 13
Factories Method Pattern
Factories Method Pattern
Factory method related to object creation. Define an interface for creating an
object, but let subclasses decide which class to instantiate
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
07/26/2024 16
Implement Factories Method
07/26/2024 18
Implement Factories Method
07/26/2024 19
Implement Factories Method
07/26/2024 20
Abstract Factory Pattern
Abstract Factory Patterns
Abstract Factory patterns work around a super-factory which creates other
factories
This type of design pattern comes under creational pattern as this pattern
provides one of the best ways to create an object
07/26/2024 22
Abstract Factory Design Pattern Participants
AbstractFactory: declares an interface for operations that create abstract
products
ConcreteFactory: implements the operations to create concrete product
objects
AbstractProduct: declares an interface for a type of product object
Product: defines a product object to be created by the corresponding concrete
factory and implements the AbstractProduct interface
Client: uses interfaces declared by AbstractFactory and AbstractProduct
classes
07/26/2024 23
UML class diagram
07/26/2024 24
Builder Pattern
Builder Pattern
Builder pattern aims to “Separate the construction of a complex object from its
representation so that the same construction process can create different
representations.”
It is used to construct a complex object step by step and the final step will
return the object
07/26/2024 28
Builder Pattern
07/26/2024 29
Advantages and Disadvantages
Advantages
The parameters to the constructor are reduced and are provided in highly readable
method calls
Builder design pattern also helps in minimizing the number of parameters in constructor
and thus there is no need to pass in null for optional parameters to the constructor
Object is always instantiated in a complete state
Immutable objects can be build without much complex logic in object building process
Disadvantages
The number of lines of code increase at least to double in builder pattern, but the effort
pays off in terms of design flexibility and much more readable code
Requires creating a separate ConcreteBuilder for each different type of Product
07/26/2024 30
Prototype Pattern
Prototype Pattern
The prototype allows us to hide the complexity of making new instances from
the client
The concept is to copy an existing object rather than creating a new instance
from scratch, something that may include costly operations
The existing object acts as a prototype and contains the state of the object
The newly copied object may change the same properties only if required. This
approach saves costly resources and time, especially when object creation is a
heavy process
07/26/2024 32
Prototype Pattern
Prototype patterns are required, when object creation is a time-consuming, and
costly operation, so we create an object with the existing object itself
One of the best available ways to create an object from existing objects is the
clone() method
07/26/2024 33
Prototype Design Pattern Participants
Prototype: This is the prototype of the actual object
Prototype registry: This is used as a registry service to have all prototypes
accessible using simple string parameters
Client: The client will be responsible for using the registry service to access
prototype instances
07/26/2024 34
Implement Prototype Pattern
07/26/2024 35
Implement Prototype Pattern
07/26/2024 36
Implement Prototype Pattern
07/26/2024 37
Observer Pattern
Observer Pattern
The Observer Pattern is a behavioral design pattern that defines a one-to-
many dependency between objects so that when one object changes state, all
its dependents are notified and updated automatically.
Intent: To decouple the subject (publisher) from its observers (subscribers),
allowing for loosely coupled systems.
07/26/2024 39
Observer Pattern
UML diagram depicting the structure of the
Observer Pattern.
Subject: Represents the publisher that
maintains a list of observers and notifies
them of changes.
Observer: Defines an interface for objects
that should be notified of changes in the
subject.
ConcreteSubject: Implements the Subject
interface and manages its list of observers.
ConcreteObserver: Implements the Observer
interface and receives notifications from the
subject.
07/26/2024 40
Observer Pattern
Use the Observer pattern when changes to the state of one object may
require changing other objects, and the actual set of objects is unknown
beforehand or changes dynamically.
Use the pattern when some objects in your app must observe others, but only
for a limited time or in specific cases.
The subscription list is dynamic, so subscribers can join or leave the list
whenever they need to.
07/26/2024 41
Observer Pattern
07/26/2024 42
Summary
Concepts were introduced:
Overview about Design Pattern in .NET
Why must use Design Pattern?
Explain about Singleton Pattern
Explain about Factories Pattern
Explain about Abstract Factory Pattern
Explain about Builder Pattern
Explain about Prototype Pattern
Demo about Factories Method, Prototype Pattern and Singleton Pattern in .NET
applications
43