DesignPattern CPP
DesignPattern CPP
– 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.
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