Contents
• Rewind the SOLID Principle
• Brief Introduction of Pattern Type
• Command Pattern
 Example
• Visitor Pattern
 Example
• Strategy Pattern
 Example
Problem
• 1-Find out sum of below numbers
45389 + 58942
a) 453263 b)983835
c) 104345 d) 104331
Problem
• 2- Find out the Sum of below numbers:-
( 234000 + 56790)
a) 290799 b) 290720
c) 290755 d) 290790
Problem
3-Find out the sum of below numbers
89436 + 53459
a) 143895 b) 132895
c) 142295 d) 142895
• Given problems if you will see we are trying to implement the
Elimination technique to eliminate the options.
• Use case when we are giving any entrance exam and we have very
less time , we will try to implement this technique
• But the same technique we are not able to implement in third
problem, it is taking time so every pattern have their own use case.
• So Don’t apply technique in every use case
• So the patterns are technique to resolve recurring problem
Always Remember-
• Principle ! = Rule
• Principle Should
• Rule Must
• Principle are use to make the code reusable and increase the
readability
A class should have only one reason to
change
Single Responsibility Principle
Example
Open Close Principle
• software entities should be open for extension, but closed for
modification
Example
Liskov's Substitution Principle(LSP)
• Derived types must be completely substitutable for their base types.
Example
Interface Segregation Principle (ISP)
• Clients should not be forced to depend upon interfaces that they don't use.
Dependency Inversion Principle (DIP)
High-level modules should not depend on low-level modules. Both should depend on abstractions.
Abstractions should not depend on details. Details should depend on abstractions
Design Pattern Type
Contnd…
Command Design Pattern
Example
Command Pattern
• Command Design Pattern is used to encapsulate a request as an
Object[Command]
• and pass to an Invoker,
• wherein Invoker does not knows how to service the request but uses
encapsulated command to perform an action.
Command Pattern Class Diagram
Command Pattern
Command Pattern
Sequence Diagram(Command Pattern)
Demo Example
Command Pattern Class Diagram
Receiver
Concrete Commands
Invoker
Client
Use case –undo and redo or Cut Paste
Command Design Pattern
• Why do we need a “receiver” in the Command design pattern
• We can also Complete Action from Concrete Command:
Answer is
• Pattern Comes from Problems.
• Design Pattern are recurring solutions to common problems of
design!
• Hence As the Problem describe at the end Receiver has to complete
all action
• Like in Hotel Example Cook has to cook all items.
Benefits
• 1- Code Readability by removing if else conditions
• 2- Invoker does not know how to execute the commands.
• 3- Converting the methods into objects.
• 4- We can extend the new commands.(open/close)
• 5- Same code will work for multiple receivers By extending the
Ireceiver
• 6- We can perform Undo and Redo Actions
• 7-Security extension, or interface exposure
• Using a command interface, you can also limit access to your objects,
allowing you to define another level of security
Drawback
• The main disadvantage of the Command pattern is the increase in the
number of classes for each individual command.
• However the command pattern classes are more readable than
creating multiple methods using if else condition.
Visitor Design Pattern
Visitor Pattern Example
Visitor Design Pattern
• If I have to add new operation like Sound() then we have to change
and add new operation all class and interface
• For resolving such kind of problem we use Visitor Pattern.
Visitor Design Pattern
• The Visitor pattern represents an operation to be performed on the
elements of an object structure without changing the classes on
which it operates.
Visitor Class Diagram
Operations
Elements
Visitor
Demo
Shopping Cart example
Strategy Design Pattern
Strategy Pattern Example
Strategy Design Pattern
• The strategy pattern (also known as the policy pattern) is a
behavioral software design pattern that enables an
algorithm's behavior to be selected at runtime. The strategy
patterns
• defines a family of algorithms,
• encapsulates each algorithm, and
• makes the algorithms interchangeable within that family.
Strategy Pattern Class Diagram
Demo
Strategy
Payment Context
Client
Thank u !

Design pattern