0% found this document useful (0 votes)
5 views

Slot 07-Design Pattern in

Uploaded by

htxsy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Slot 07-Design Pattern in

Uploaded by

htxsy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

ĐẠI HỌC FPT CẦN THƠ

Design Pattern in .NET

Lession 5
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

◆ Demo about Factories Method, Prototype Pattern and

Singleton Pattern in .NET applications

Lession 5: Design Pattern in .NET - Huong Hoang Luong 2


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

Design patterns are programming language independent


strategies for solving a common problem. That means a design
pattern represents an idea, not a particular implementation

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Understanding Design Patterns

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

The pattern typically shows relationships and interactions between classes or


objects

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

Lession 5: Design Pattern in .NET - Huong Hoang Luong


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

Lession 5: Design Pattern in .NET - Huong Hoang Luong


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. Design patterns provide general solutions,
documented in a format that doesn't require specifics tied to a particular
problem

Patterns allow developers to communicate using well-known, well


understood names for software interactions. Common design patterns can
be improved over time, making them more robust than ad-hoc designs

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Introduction to GoF Patterns

Behavioral Design Patterns:


Chain of responsibility,
Structural Design Patterns:
Command, Interpreter,
Adapter, Bridge, Composite,
Creational Design Patterns Iterator, Mediator, Memento,
Decorator, Facade, Flyweight,
Null Object, Observer, State,
Private Class Data, and Proxy
Strategy, Template method and
Visitor

While class-creation patterns


These patterns can be further use inheritance effectively in Creational design patterns are
These design patterns are all
categorized into Class- the instantiation process, the Singleton, Factory Method,
about class instantiation or
creational patterns and object- object-creation patterns use Abstract Factory, Builder, and
object creation
creational patterns delegation effectively to get the Prototype
job done

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Creational Design Patterns

The following five patterns are concerned with the


instantiation of objects:
• Singleton: A pattern for enforcing only one instance of a class
• Factory Method: A pattern for creating objects derived from a
class where the specific class is determined at runtime
• Abstract Factory: A pattern for the creation of objects belonging
to a family of classes. The specific object is determined at
runtime
• Builder: A useful pattern for more complex objects where the
construction of the object is controlled externally to the
constructed class
• Prototype: A pattern for copying or cloning an object
Lession 5: Design Pattern in .NET - Huong Hoang Luong
ĐẠI HỌC FPT CẦN THƠ

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

Lession 5: Design Pattern in .NET - Huong Hoang Luong 10


Implement Singleton Pattern

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Implement Singleton Pattern

Lession 5: Design Pattern in .NET - Huong Hoang Luong 12


ĐẠI HỌC FPT CẦN THƠ

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
◆ Factory Method lets a class defer instantiation to

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

(static)factory method which creates and returns


instances, hiding the details of class modules from user

Lession 5: Design Pattern in .NET - Huong Hoang Luong 14


Factories Method Pattern

◆ 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

UML class diagram

Lession 5: Design Pattern in .NET - Huong Hoang Luong 15


Implement Factories Method

UML class diagram

Lession 5: Design Pattern in .NET - Huong Hoang Luong 16


Implement Factories Method

Lession 5: Design Pattern in .NET - Huong Hoang Luong 17


Implement Factories Method

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Implement Factories Method

Lession 5: Design Pattern in .NET - Huong Hoang Luong 19


ĐẠI HỌC FPT CẦN THƠ

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

In Abstract Factory pattern an interface is responsible for


creating a factory of related objects without explicitly
specifying their classes. Each generated factory can give the
objects as per the Factory pattern

Lession 5: Design Pattern in .NET - Huong Hoang Luong


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

Lession 5: Design Pattern in .NET - Huong Hoang Luong


UML class diagram

Lession 5: Design Pattern in .NET - Huong Hoang Luong


ĐẠI HỌC FPT CẦN THƠ

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

The process of constructing an object should be generic so


that it can be used to create different representations of the
same object

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Builder Design Pattern Participants

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

ConcreteBuilder – There may be any number of concrete builder classes


inheriting from Builder. These classes contain the functionality to create a
particular complex product

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Builder Pattern

◆ Director – The director class controls the algorithm that


generates the final product object. A director object is
instantiated and its Construct method is called. The
method includes a parameter to capture the specific
concrete builder object that is to be used to generate the
product. The director then calls methods of the concrete
builder in the correct order to generate the product
object. On completion of the process, the GetProduct
method of the builder object can be used to return the
product

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Builder Pattern

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Advantages of Builder Design Pattern

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

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Disadvantages of Builder Design
Pattern
◆ 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

Lession 5: Design Pattern in .NET - Huong Hoang Luong


ĐẠI HỌC FPT CẦN THƠ

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

Lession 5: Design Pattern in .NET - Huong Hoang Luong


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

Clone is the simplest approach to implement a


prototype pattern. However, it is our call to decide how
to copy existing object based on your business model

Lession 5: Design Pattern in .NET - Huong Hoang Luong


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

34

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Implement Prototype Pattern

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Implement Prototype Pattern

Lession 5: Design Pattern in .NET - Huong Hoang Luong


Implement Prototype Pattern

37

Lession 5: Design Pattern in .NET - Huong Hoang Luong


38

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

Lession 5: Design Pattern in .NET - Huong Hoang Luong


ĐẠI HỌC FPT CẦN THƠ

You might also like