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

DesignPattern CPP

Uploaded by

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

DesignPattern CPP

Uploaded by

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

Topic Learning Outcomes

1. Explain design patterns


2. Apply design pattern to solve a given problem
Design Pattern
1. Introduction to design patterns
2. Creational Patterns: Singleton pattern, Factory
method pattern
3. Behaviour Patterns: Strategy pattern, Template
method pattern
4. Structural Patterns: Adapter pattern, Composite
pattern.
Introduction

• The design pattern is a general reusable solution to a


commonly occurring problem in software design.
• OR , The design pattern are just a formal language to
define common ways to solve software engineering
problems.
• We (most people)designed solutions (class diagrams)
using design patterns, without knowing that they are.
Introduction

• People will naturally come up with solutions that will fit


into a design pattern on their own, but the design
patterns help to define terminology and standard ideas
that can be helpful.
• The main benefit of design patterns is that, they are
ideas that can be defined in ways that are repeatedly
useful (reusable).
Design Patterns: Limitations
• One of the limitation (due to reusability) Is that, the
design patterns stop people from coming up with
proper solution to a specific problem.

• Instead of thinking about how to solve that particular


problem, people try applying one design pattern after
the other.

• However, if we know language of design patterns (UML)


and standard design patterns, we can take advantage of
reusability and we can design our own solutions.
Types of design Pattern
1. Creational Patterns: Singleton pattern, Factory
method pattern
2. Behaviour Patterns: Strategy pattern, Template
method pattern
3. Structural Patterns: Adapter pattern, Composite
pattern.
Creational Design Pattern
• In software engineering, creational design patterns
are design patterns that deal with object creation
mechanisms, trying to create objects in a manner
suitable to the situation.

– Singleton pattern,
– Factory method pattern
Singleton pattern
• Singleton pattern is one of the simplest design pattern,
as this pattern provides one of the best ways to create
an object.

• This pattern involves a single class which is responsible


to create an object while making sure that only single
object gets created.

• This class provides a way to access its only object which


can be accessed directly without need to instantiate
the object of the class.
Singleton pattern: implementation
• This is useful when exactly one object is needed to
coordinate actions across the system. For example, if
you are using a logger, that writes logs to a file, you can
use a singleton class to create such a logger.
Singleton pattern: implementation
Singleton pattern: implementation
Singleton pattern: implementation
Singleton pattern: implementation
int main(){
//cannot create object
Singleton singleton;// compiler ERROR
}
Design Pattern - Factory Pattern
• Factory pattern is one of most used design pattern

• In Factory pattern, we create object without exposing


the creation logic to the client and refer to newly
created object using a common interface.

• The idea is to use a static/non-static member-function


(static factory method) which creates & returns
instances, hiding the details of class modules from user.
Design Pattern - Factory 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.
• What is meant when we talk about library and clients?
A library is something which is provided by some third
party which exposes some public APIs and clients make
calls to those public APIs to complete its task.
Factory Design Pattern

• The Factory Method pattern is a design pattern used to


define a runtime interface for creating an object. It’s
called a factory because it creates various types of
objects without necessarily knowing what kind of
object it creates or how to create it.
Factory Design Pattern
• The Factory Method pattern is generally used in the
following situations:

– A class cannot anticipate the type of objects it needs to


create beforehand.
– A class requires its subclasses to specify the objects it creates.
– You want to localize the logic to instantiate a complex object.
Factory Design Pattern: Implementation
• Steps
– Create a common interface for factory method
– Create sub classes of different objects to be created
– Create client class to use factory method to create objects.
Scenario
• Consider a object Vehicle and TwoWheeler,
ThreeWheeler and FourWheeler are type of Vehicles.
The application required to create different type of
vehicles and use them.
Factory Design Pattern: class diagram
Factory Design Pattern: class diagram
Factory Design Pattern: class diagram
Factory Design Pattern: class diagram
Design Pattern
1. Introduction to design patterns
2. Creational Patterns: Singleton pattern, Factory
method pattern
3. Behaviour Patterns: Strategy pattern, Template
method pattern
4. Structural Patterns: Adapter pattern, Composite
pattern.
Behaviour Pattern: strategy pattern
• In Strategy pattern, a class behaviour or its algorithm can be
changed at run time.
– Example
– Base class reference variable/interface reference variable can be assigned
sub class object
– The type of the object assigned decides which version (object) method
has to be called.
• In Strategy pattern, we create objects which represent various
strategies and a context object whose behavior varies as per its
strategy object.
• The strategy object changes the executing algorithm of the
context object.
Strategy pattern: class diagram
Strategy pattern: implementation
Steps
1. create a Strategy interface defining an action
2. Create concrete strategy classes implementing
the Strategy interface
3. Context is a class which uses a Strategy.
4. Create StrategyPatternDemo (main()) that will
use Context and strategy objects to demonstrate
change in Context behaviour based on strategy it
deploys or uses.
Strategy pattern: implementation
Strategy pattern: implementation
Strategy Pattern
Behaviour pattern: Template pattern
• In Template pattern, an abstract class exposes defined
way(s)/template(s) to execute its methods

• Its subclasses can override the method implementation


as per need but the invocation is to be in the same way
as defined by an abstract class
Template pattern: class diagram

main()
Template Pattern: implementation
Structural pattern: Adapter Pattern
• Adapter pattern works as a bridge between two
incompatible interfaces.
• this pattern combines the capability of two
independent interfaces.
• This pattern involves a single class which is responsible
to join functionalities of independent or incompatible
interfaces
• A real life example could be a case of card reader which acts as
an adapter between memory card and a laptop. You plugin the
memory card into card reader and card reader into the laptop so
that memory card can be read via laptop.
• It allows you to use the old(read already implemented
and working) software which is doing something similar
to what you want. Just that the times (read interfaces)
changed.
• HOW
It is very simple. Get hold of the snazzy new interface
which the client wants to use. Implement the interface
via the Adapter class. Take the legacy class. Call the
legacy class methods inside the adapter class methods.
Scenario:
• Let us suppose we have an old player which plays
songs.
• It has a method called OldPlay which takes the volume
at which the song has to be played as a parameter.
• The new interface which we have has a play method
too.
• But that method does not take the volume as a
parameter.
• So lets put Adapter design pattern in action.
Adapter Pattern: class diagram
Adapter pattern: Implementation
Adapter Pattern: Implementation
Adapter Pattern: Implementation
Adapter Pattern: Implementation
Adapter Pattern
Adapter Design Pattern
• Adapter Design pattern converts the interface of a class
into another interface that clients expect.
• It lets classes work together which is not possible
otherwise because of incompatible interfaces.
Structural Pattern: Composite Pattern
• Composite pattern is used where we need to treat a
group of objects in similar way as a single object.
• Composite pattern composes objects in term of a tree
structure to represent part as well as whole hierarchy.
• This pattern creates a class that contains group of its
own objects.
• This class provides ways to modify its group of same
objects.
Composite Pattern: class diagram
Composite Pattern: Implementation
Composite Pattern: class diagram
Composite Pattern: class diagram
Composite Pattern: class diagram
Composite Pattern: class diagram
THANK YOU

You might also like