SlideShare ist ein Scribd-Unternehmen logo
1 von 23
Presented by Arsalan Ahmed
What Are Design Patterns?
 Well-understood code patterns
 Solve common problems
 Create common pattern vocabulary
 Speed up design and development
 Encourage creating design standards
Useful Design Principles
 Prefer Composition over Inheritance
 Program to abstractions instead of
concrete implementations
 Design loosely coupled components
 Encapsulate what varies
 Hide the likely-to-change code behind an
interface
Time-Tested Design Principles
 S – Single Responsibility Principle
 One reason to change per class
 O – Open-Closed Principle
 Classes open for extension, closed for modification
 L – Liskov Substitution Principle
 Classes can be substituted if designed to be
referenced via their public contracts
 I – Interface Segregation Principle
 Interfaces should not be bloated
 D – Dependency Inversion Principle
 Classes should not be responsible for creating their
dependencies
Anti-Patterns
 Commonly used
 Often seem harmless
 Degrade overall design
 Avoid like a plague!
Example Anti Patterns
 The Arrowhead
 Constructor Over-Injection
 Gold Plating
 Interface Bloat
 God Object
 Magic Literals
 Reinventing The Wheel
The Gang Of Four
 Authors of the canonical book Design
Patterns: Elements of Reusable Object-
Oriented Software
 Erich Gamma, Richard Helm, Ralph
Johnson, John Vlissides
 Main theme 1: Program to an interface
instead of an implementation
 Main theme 2: Favor composition over
inheritance
GoF Design Patterns
 Creational: Abstract Factory, Builder,
Factory Method, Prototype, Singleton
 Structural: Adapter, Bridge, Composite,
Decorator, Façade, Flyweight, Proxy
 Behavioral: Chain of Responsibility,
Command, Interpreter, Iterator,
Mediator, Memento, Observer, State,
Strategy, Template, Visitor
Enterprise Design Patterns
 Patterns found in enterprise applications
 Major proponent: Martin Fowler of
Thoughtworks
 Enterprise patterns go beyond GoF
patterns
 Often specific to technology stacks
Pattern: The Command
Source: dofactory.com
What Makes It Useful?
 Repeatable pattern to execute tasks as
objects
 Allows parameterizing command
invokers with different commands
 Allows undo operations
 Meta-Command Pattern allows creating
composite commands
 Decouples worker objects from client
objects
Practical Uses
 Background processing agents
 Allow third-party vendors to create and
distribute custom commands
 Supports queuing requests
 Facilitates logging requests
 By serializing/deserializing objects to/from log
files
 Log contains history of all command objects
 Allows implementing transaction semantics
Command Pattern
Demo
Pattern: The Decorator
Source: dofactory.com
What Makes It Useful?
 Facilitates implementing the open-
closed principle
 Allows adding new functionality without
modifying existing classes
 Promotes composition over inheritance
 Promotes iterative development
Practical Uses
 Dynamically creating specialized objects
 Provide flexible API with object
decorators
 Example uses in the .Net framework
 BufferedStream decorates MemoryStream
Decorator Pattern
Demo
Before Using The Decorator
After Using The Decorator
Pattern: Lazy Load
 Avoid performing time-consuming
operations unless unavoidable
 Load data from database or an external
service as late as possible
 Cache fetched data for as long as
feasible
 Reduce database and/or network load
Code Sample
Private Shared ReadOnly Property
DTSearchIndexLocation() As String
Get
If String.IsNullOrEmpty(_dtSearchIndexLocation)
Then
' fetch from database and assign to
_dtSearchIndexLocation
End If
Return _dtSearchIndexLocation
End Get
End Property
Design Patterns In .Net
 Observer
 Iterator
 Decorator
 Adapter
 Factory
 Strategy
 Composite
 Template Method
 Intercepting Filter
 Page Controller
Further Study
 Writings of The Godfather of Enterprise
Design Patterns a.k.a. Martin Fowler
 Martinfowler.com
 Head-First Design Patterns by O’Reilly
 Writings of Uncle Bob a.k.a. Robert C.
Martin
 ButUncleBob.com, blog.objectmentor.com
 Presentations by Jeremy Miller
 codebetter.com/blogs/jeremy.miller

Weitere ähnliche Inhalte

Ähnlich wie Some Cool Design Patterns, Practices, and Principles

Top tips from what we've learned from our 10 years experience
Top tips from what we've learned from our 10 years experienceTop tips from what we've learned from our 10 years experience
Top tips from what we've learned from our 10 years experienceJoomlaDay Australia
 
PHP - Procedural To Object-Oriented
PHP - Procedural To Object-OrientedPHP - Procedural To Object-Oriented
PHP - Procedural To Object-OrientedVance Lucas
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxdanhaley45372
 
Software Engineering in PHP
Software Engineering in PHPSoftware Engineering in PHP
Software Engineering in PHPM A Hossain Tonu
 
Adapter design-pattern2015
Adapter design-pattern2015Adapter design-pattern2015
Adapter design-pattern2015Vic Tarchenko
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Pythondn
 
Selenium Design Patterns
Selenium Design PatternsSelenium Design Patterns
Selenium Design PatternsLiraz Shay
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Javawiradikusuma
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Steven Smith
 
Code Craftsmanship Checklist
Code Craftsmanship ChecklistCode Craftsmanship Checklist
Code Craftsmanship ChecklistRyan Polk
 
Writting Better Software
Writting Better SoftwareWritting Better Software
Writting Better Softwaresvilen.ivanov
 
Architecting Applications the Microsoft Way
Architecting Applications the Microsoft WayArchitecting Applications the Microsoft Way
Architecting Applications the Microsoft WayClint Edmonson
 
Software design and Architecture.pptx
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptxSHAHZAIBABBAS13
 

Ähnlich wie Some Cool Design Patterns, Practices, and Principles (20)

Oops design pattern_amitgupta
Oops design pattern_amitguptaOops design pattern_amitgupta
Oops design pattern_amitgupta
 
Design pattern
Design patternDesign pattern
Design pattern
 
Top tips from what we've learned from our 10 years experience
Top tips from what we've learned from our 10 years experienceTop tips from what we've learned from our 10 years experience
Top tips from what we've learned from our 10 years experience
 
L03 Design Patterns
L03 Design PatternsL03 Design Patterns
L03 Design Patterns
 
PHP - Procedural To Object-Oriented
PHP - Procedural To Object-OrientedPHP - Procedural To Object-Oriented
PHP - Procedural To Object-Oriented
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
Software Engineering in PHP
Software Engineering in PHPSoftware Engineering in PHP
Software Engineering in PHP
 
Adapter design-pattern2015
Adapter design-pattern2015Adapter design-pattern2015
Adapter design-pattern2015
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
L03 Software Design
L03 Software DesignL03 Software Design
L03 Software Design
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
Selenium Design Patterns
Selenium Design PatternsSelenium Design Patterns
Selenium Design Patterns
 
Dtacs
DtacsDtacs
Dtacs
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
 
Code Craftsmanship Checklist
Code Craftsmanship ChecklistCode Craftsmanship Checklist
Code Craftsmanship Checklist
 
Writting Better Software
Writting Better SoftwareWritting Better Software
Writting Better Software
 
Bp301
Bp301Bp301
Bp301
 
Architecting Applications the Microsoft Way
Architecting Applications the Microsoft WayArchitecting Applications the Microsoft Way
Architecting Applications the Microsoft Way
 
Software design and Architecture.pptx
Software design and Architecture.pptxSoftware design and Architecture.pptx
Software design and Architecture.pptx
 

Some Cool Design Patterns, Practices, and Principles

  • 2. What Are Design Patterns?  Well-understood code patterns  Solve common problems  Create common pattern vocabulary  Speed up design and development  Encourage creating design standards
  • 3. Useful Design Principles  Prefer Composition over Inheritance  Program to abstractions instead of concrete implementations  Design loosely coupled components  Encapsulate what varies  Hide the likely-to-change code behind an interface
  • 4. Time-Tested Design Principles  S – Single Responsibility Principle  One reason to change per class  O – Open-Closed Principle  Classes open for extension, closed for modification  L – Liskov Substitution Principle  Classes can be substituted if designed to be referenced via their public contracts  I – Interface Segregation Principle  Interfaces should not be bloated  D – Dependency Inversion Principle  Classes should not be responsible for creating their dependencies
  • 5. Anti-Patterns  Commonly used  Often seem harmless  Degrade overall design  Avoid like a plague!
  • 6. Example Anti Patterns  The Arrowhead  Constructor Over-Injection  Gold Plating  Interface Bloat  God Object  Magic Literals  Reinventing The Wheel
  • 7. The Gang Of Four  Authors of the canonical book Design Patterns: Elements of Reusable Object- Oriented Software  Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides  Main theme 1: Program to an interface instead of an implementation  Main theme 2: Favor composition over inheritance
  • 8. GoF Design Patterns  Creational: Abstract Factory, Builder, Factory Method, Prototype, Singleton  Structural: Adapter, Bridge, Composite, Decorator, Façade, Flyweight, Proxy  Behavioral: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template, Visitor
  • 9. Enterprise Design Patterns  Patterns found in enterprise applications  Major proponent: Martin Fowler of Thoughtworks  Enterprise patterns go beyond GoF patterns  Often specific to technology stacks
  • 11. What Makes It Useful?  Repeatable pattern to execute tasks as objects  Allows parameterizing command invokers with different commands  Allows undo operations  Meta-Command Pattern allows creating composite commands  Decouples worker objects from client objects
  • 12. Practical Uses  Background processing agents  Allow third-party vendors to create and distribute custom commands  Supports queuing requests  Facilitates logging requests  By serializing/deserializing objects to/from log files  Log contains history of all command objects  Allows implementing transaction semantics
  • 15. What Makes It Useful?  Facilitates implementing the open- closed principle  Allows adding new functionality without modifying existing classes  Promotes composition over inheritance  Promotes iterative development
  • 16. Practical Uses  Dynamically creating specialized objects  Provide flexible API with object decorators  Example uses in the .Net framework  BufferedStream decorates MemoryStream
  • 18. Before Using The Decorator
  • 19. After Using The Decorator
  • 20. Pattern: Lazy Load  Avoid performing time-consuming operations unless unavoidable  Load data from database or an external service as late as possible  Cache fetched data for as long as feasible  Reduce database and/or network load
  • 21. Code Sample Private Shared ReadOnly Property DTSearchIndexLocation() As String Get If String.IsNullOrEmpty(_dtSearchIndexLocation) Then ' fetch from database and assign to _dtSearchIndexLocation End If Return _dtSearchIndexLocation End Get End Property
  • 22. Design Patterns In .Net  Observer  Iterator  Decorator  Adapter  Factory  Strategy  Composite  Template Method  Intercepting Filter  Page Controller
  • 23. Further Study  Writings of The Godfather of Enterprise Design Patterns a.k.a. Martin Fowler  Martinfowler.com  Head-First Design Patterns by O’Reilly  Writings of Uncle Bob a.k.a. Robert C. Martin  ButUncleBob.com, blog.objectmentor.com  Presentations by Jeremy Miller  codebetter.com/blogs/jeremy.miller

Hinweis der Redaktion

  1. Say more with less, shared vocabulary speeds up development and reduces chances of confusion and misunderstanding.
  2. Constructor Over Injection: If the class can function without a particular dependent object, it shouldn’t demand it in the constructor.