SlideShare a Scribd company logo
Segue to Design Patterns
Being aware of Design Patterns is the beginning of creating better design
Rahul Rajat Singh
Agenda
• OO Principles
• Design Principles (SOLID)
• Looking a big ball of Mud
• What are Design Patterns
• Types and List of Design Patterns
• Singleton Pattern
• Factory Method
• Adapter Design Pattern
• Strategy Design Pattern
• Revisiting the big ball of mud and refactor
OO Principles
• Encapsulate what varies
• Favour Composition over Inheritance
• Program to interface not implementation
• Strive for loosely coupled design between objects that interact
Design Principles (SOLID)
• SRP: Single Responsibility Principle
• An object should have only a single responsibility & all the responsibility should be entirely
encapsulated by the class.
• There should never be more than one reason for a class to change
• OCP: Open/Closed Principle
• Software entities should be open for extension, but closed for modification
• LSP: Liskov Substituion Principle
• Objects in a program should be replaceable with instances of their subtypes without altering the
correctness of that program
• ISP: Interface Segregation Principle
• many client specific interfaces are better than one general purpose interface
• once an interface has gotten too 'fat' split it into smaller and more specific interfaces so that any
clients of the interface will only know about the methods that pertain to them. No client should be
forced to depend on methods it does not use
• DIP: Dependency Inversion Principle
• Depend upon Abstractions. Do not depend upon concretions.
• Dependency Injection (DI) is one method of following this principle
Looking a big ball of Mud
Problems
• SRP
• OCP
• DIP
What are Design Patterns
• A pattern is a recurring solution to a standard problem, in a context.
• General reusable solution to a commonly occurring problem in
software design.
• Extension of OOP and OOAD.
• Description or template for how to solve a problem that can be used
in many different situations.
Types and List of Design Patterns
• Creational Patterns
Deals with controlled object creation
• Singleton
• Abstract Factory
• Builder
• Factory Method
• Prototype
• Structural Patterns
Deals with composition of classes or
objects
• Adapter
• Bridge
• Composite
• Decorator
• Façade
• Flyweight
• Proxy
• Behavioural Patterns
Deals with interaction between objects
• Chain of
Responsibility
• Command
• Interpreter
• Iterator
• Mediator
• Memento
• Observer
• State
• Strategy
• Template Method
• Visitor
Singleton Pattern
• Used to implement the mathematical concept of a singleton, by
restricting the instantiation of a class to one object.
• Useful when exactly one object is needed to coordinate actions across
the system.
• The concept is sometimes generalized to systems that operate more
efficiently when only one object exists, or that restrict the
instantiation to a certain number of objects.
Implement Singleton in .NET
• Advantages:
• Because the instance is created inside the Instance property method, the class can exercise
additional functionality.
• Lazy instantiation avoids instantiating unnecessary singletons when the application starts.
• Disadvantages:
• Not safe for multithreaded environments (Highly advisable to check the thread safe implantation
too)
Factory Method
• Define an interface for creating an object, but let subclasses decide
which class to instantiate.
• Factory Method lets a class defer instantiation to subclasses.
Implement Factory Method in .NET
Adapter Design Pattern
• Adapter pattern translates one interface for a class into a compatible
interface.
• Allows classes to work together that normally could not because of
incompatible interfaces, by providing its interface to clients while using the
original interface.
• The adapter translates calls to its interface into calls to original interface.
Amount of code necessary to do this is typically small.
• The adapter is also responsible for transforming data into appropriate
forms.
• Often used while working with existing API/code base and while adapting
code for which no source is not available.
Implementing Adaptor
Strategy Design Pattern
• Strategy pattern (also known as the
policy pattern) is a design pattern,
whereby algorithms can be selected at
runtime.
• Intended to provide a means to define a
family of algorithms, encapsulate each
one as an object, and make them
interchangeable.
• The strategy pattern lets the algorithms
vary independently from clients that use
them.
Implement Strategy pattern
Revisiting the big ball of mud and refactor
Let’s Fix issues
• SRP – Having separate classes
responsible for each payment
gateway. The classes will be the
Adaptors for each payment
gateway.
• DIP – The Adaptors will implement
a common interface and this class
will depend on interface. So our
payment classes will also be
concrete strategies.
• OCP – Let’s inject these concrete
strategies using a factory class.
SRP – Separate Adaptors classes for each payment gateway
SBIPaymentGateway
bool InitiatePayment()
PayPalPaymentGateway
bool InitiatePayment()
BillDeskPaymentGateway
bool InitiatePayment()
HDFCPaymentGateway
bool InitiatePayment()
Getting DIP ready by making converting our adaptors to concrete strategy
SBIPaymentGateway
bool InitiatePayment()
PayPalPaymentGateway
bool InitiatePayment()
BillDeskPaymentGateway
bool InitiatePayment()
HDFCPaymentGateway
bool InitiatePayment()
<<Interface>>
IPaymentGateway
bool InitiatePayment()
Inversion of control and OCP support by implementing Factory
Note: A ServiceLocator or IOC container can also be used instead of factory to achieve inversion of
control.
Making the Factory Singleton
How to Select a Design Pattern – Ask yourself
• What do they do?
• What do they decouple/couple?
• When should you use them?
• When should you not use them?
• What missing language feature would make them go away?
• What technical debt do you incur by using it?
• Is there a simpler way to get the job done?
References

More Related Content

Similar to Segue to design patterns

Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Codersebbe
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
Dr. Syed Hassan Amin
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
Alfred Jett Grandeza
 
Code decoupling from Symfony (and others frameworks) - PHP Conf Asia 2018
Code decoupling from Symfony (and others frameworks) - PHP Conf Asia 2018Code decoupling from Symfony (and others frameworks) - PHP Conf Asia 2018
Code decoupling from Symfony (and others frameworks) - PHP Conf Asia 2018
Miguel Gallardo
 
Design Patterns .Net
Design Patterns .NetDesign Patterns .Net
Design Patterns .NetHariom Shah
 
Software Design Principles (SOLID)
Software Design Principles (SOLID)Software Design Principles (SOLID)
Software Design Principles (SOLID)
ASIMYILDIZ
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
RushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
RushikeshChikane2
 
Code reviews
Code reviewsCode reviews
Code reviewsRoger Xia
 
Solid principles
Solid principlesSolid principles
Solid principles
Kumaresh Chandra Baruri
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
Sachin Patidar
 
Clean architecture
Clean architectureClean architecture
Clean architecture
Travis Frisinger
 
I gotta dependency on dependency injection
I gotta dependency on dependency injectionI gotta dependency on dependency injection
I gotta dependency on dependency injection
mhenroid
 
Software Design principales
Software Design principalesSoftware Design principales
Software Design principales
ABDEL RAHMAN KARIM
 
Software Design
Software DesignSoftware Design
Software Design
Ahmed Misbah
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design pattern
Beroza Paul
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
Bhavin Gandhi
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
Mohammad Shawahneh
 
Oracle ADF Architecture TV - Design - ADF Architectural Patterns
Oracle ADF Architecture TV - Design - ADF Architectural PatternsOracle ADF Architecture TV - Design - ADF Architectural Patterns
Oracle ADF Architecture TV - Design - ADF Architectural Patterns
Chris Muir
 
System design process.pptx
System design process.pptxSystem design process.pptx
System design process.pptx
NajibMuhammad16
 

Similar to Segue to design patterns (20)

Orthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable CodeOrthogonality: A Strategy for Reusable Code
Orthogonality: A Strategy for Reusable Code
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 
Code decoupling from Symfony (and others frameworks) - PHP Conf Asia 2018
Code decoupling from Symfony (and others frameworks) - PHP Conf Asia 2018Code decoupling from Symfony (and others frameworks) - PHP Conf Asia 2018
Code decoupling from Symfony (and others frameworks) - PHP Conf Asia 2018
 
Design Patterns .Net
Design Patterns .NetDesign Patterns .Net
Design Patterns .Net
 
Software Design Principles (SOLID)
Software Design Principles (SOLID)Software Design Principles (SOLID)
Software Design Principles (SOLID)
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Code reviews
Code reviewsCode reviews
Code reviews
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Creational Design Patterns.pptx
Creational Design Patterns.pptxCreational Design Patterns.pptx
Creational Design Patterns.pptx
 
Clean architecture
Clean architectureClean architecture
Clean architecture
 
I gotta dependency on dependency injection
I gotta dependency on dependency injectionI gotta dependency on dependency injection
I gotta dependency on dependency injection
 
Software Design principales
Software Design principalesSoftware Design principales
Software Design principales
 
Software Design
Software DesignSoftware Design
Software Design
 
Benefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design patternBenefits of using software design patterns and when to use design pattern
Benefits of using software design patterns and when to use design pattern
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
 
Oracle ADF Architecture TV - Design - ADF Architectural Patterns
Oracle ADF Architecture TV - Design - ADF Architectural PatternsOracle ADF Architecture TV - Design - ADF Architectural Patterns
Oracle ADF Architecture TV - Design - ADF Architectural Patterns
 
System design process.pptx
System design process.pptxSystem design process.pptx
System design process.pptx
 

Recently uploaded

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
ArianaBusciglio
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
Mohammed Sikander
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
Scholarhat
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 

Recently uploaded (20)

CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Group Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana BuscigliopptxGroup Presentation 2 Economics.Ariana Buscigliopptx
Group Presentation 2 Economics.Ariana Buscigliopptx
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
Multithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race conditionMultithreading_in_C++ - std::thread, race condition
Multithreading_in_C++ - std::thread, race condition
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Azure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHatAzure Interview Questions and Answers PDF By ScholarHat
Azure Interview Questions and Answers PDF By ScholarHat
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 

Segue to design patterns

  • 1. Segue to Design Patterns Being aware of Design Patterns is the beginning of creating better design Rahul Rajat Singh
  • 2. Agenda • OO Principles • Design Principles (SOLID) • Looking a big ball of Mud • What are Design Patterns • Types and List of Design Patterns • Singleton Pattern • Factory Method • Adapter Design Pattern • Strategy Design Pattern • Revisiting the big ball of mud and refactor
  • 3. OO Principles • Encapsulate what varies • Favour Composition over Inheritance • Program to interface not implementation • Strive for loosely coupled design between objects that interact
  • 4. Design Principles (SOLID) • SRP: Single Responsibility Principle • An object should have only a single responsibility & all the responsibility should be entirely encapsulated by the class. • There should never be more than one reason for a class to change • OCP: Open/Closed Principle • Software entities should be open for extension, but closed for modification • LSP: Liskov Substituion Principle • Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program • ISP: Interface Segregation Principle • many client specific interfaces are better than one general purpose interface • once an interface has gotten too 'fat' split it into smaller and more specific interfaces so that any clients of the interface will only know about the methods that pertain to them. No client should be forced to depend on methods it does not use • DIP: Dependency Inversion Principle • Depend upon Abstractions. Do not depend upon concretions. • Dependency Injection (DI) is one method of following this principle
  • 5. Looking a big ball of Mud Problems • SRP • OCP • DIP
  • 6. What are Design Patterns • A pattern is a recurring solution to a standard problem, in a context. • General reusable solution to a commonly occurring problem in software design. • Extension of OOP and OOAD. • Description or template for how to solve a problem that can be used in many different situations.
  • 7. Types and List of Design Patterns • Creational Patterns Deals with controlled object creation • Singleton • Abstract Factory • Builder • Factory Method • Prototype • Structural Patterns Deals with composition of classes or objects • Adapter • Bridge • Composite • Decorator • Façade • Flyweight • Proxy • Behavioural Patterns Deals with interaction between objects • Chain of Responsibility • Command • Interpreter • Iterator • Mediator • Memento • Observer • State • Strategy • Template Method • Visitor
  • 8. Singleton Pattern • Used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. • Useful when exactly one object is needed to coordinate actions across the system. • The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
  • 9. Implement Singleton in .NET • Advantages: • Because the instance is created inside the Instance property method, the class can exercise additional functionality. • Lazy instantiation avoids instantiating unnecessary singletons when the application starts. • Disadvantages: • Not safe for multithreaded environments (Highly advisable to check the thread safe implantation too)
  • 10. Factory Method • Define an interface for creating an object, but let subclasses decide which class to instantiate. • Factory Method lets a class defer instantiation to subclasses.
  • 12. Adapter Design Pattern • Adapter pattern translates one interface for a class into a compatible interface. • Allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface. • The adapter translates calls to its interface into calls to original interface. Amount of code necessary to do this is typically small. • The adapter is also responsible for transforming data into appropriate forms. • Often used while working with existing API/code base and while adapting code for which no source is not available.
  • 14. Strategy Design Pattern • Strategy pattern (also known as the policy pattern) is a design pattern, whereby algorithms can be selected at runtime. • Intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. • The strategy pattern lets the algorithms vary independently from clients that use them.
  • 16. Revisiting the big ball of mud and refactor Let’s Fix issues • SRP – Having separate classes responsible for each payment gateway. The classes will be the Adaptors for each payment gateway. • DIP – The Adaptors will implement a common interface and this class will depend on interface. So our payment classes will also be concrete strategies. • OCP – Let’s inject these concrete strategies using a factory class.
  • 17. SRP – Separate Adaptors classes for each payment gateway SBIPaymentGateway bool InitiatePayment() PayPalPaymentGateway bool InitiatePayment() BillDeskPaymentGateway bool InitiatePayment() HDFCPaymentGateway bool InitiatePayment()
  • 18. Getting DIP ready by making converting our adaptors to concrete strategy SBIPaymentGateway bool InitiatePayment() PayPalPaymentGateway bool InitiatePayment() BillDeskPaymentGateway bool InitiatePayment() HDFCPaymentGateway bool InitiatePayment() <<Interface>> IPaymentGateway bool InitiatePayment()
  • 19. Inversion of control and OCP support by implementing Factory Note: A ServiceLocator or IOC container can also be used instead of factory to achieve inversion of control.
  • 20. Making the Factory Singleton
  • 21. How to Select a Design Pattern – Ask yourself • What do they do? • What do they decouple/couple? • When should you use them? • When should you not use them? • What missing language feature would make them go away? • What technical debt do you incur by using it? • Is there a simpler way to get the job done?