GoF Design patterns I: Introduction + Structural Patterns

Sameh Deabes
Sameh DeabesSoftware Architect em Ministry Of Justice
GOF DESIGN PATTERNS I:
INTRODUCTION +
STRUCTURAL PATTERNS
Sameh M. Deabes
AGENDA
Patterns and Anti-patterns
How to learn design patterns?
Categories of GoF patterns
The Fundamental theorem of software engineering
Real-world problems and how design patterns solve them
Uncovered Structural Patterns
Summary
PATTERNS AND ANTI-PATTERNS
What is a design pattern?
 Well-known good high-level abstract solution templates for common problems
 Blueprints not actual solutions
 No pre-baked solution, you will implement it yourself
 Built on concepts of OOP  notice the wide usage of polymorphism
 Caution: don’t try to apply design patterns on every problem you face, rather
“refactor to patterns” better!
What is an anti-pattern?
 Well-known quick high-level abstract solution templates for common problems that
prove to have extensibility/flexibility limitations.
 Quick not correct solutions
 Don’t be afraid to use them given that you justify their usage, and you consider refactoring to patterns.
[KISS, YAGNI]
It’s a matter of right vs quick solution.
HOW TO LEARN DESIGN
PATTERNS?
Problem with GoF book
 The original text groups patterns in three categories, and then alphabetically within
each. All good for reference purposes, but lousy for learning. -- Michael Mahemoff
Solution: use other learning path
 GoF Design Patterns: Rapid Learning Tips, Michael Mahemoff [Article, 2 pages]
 http://cs.millersville.edu/~ekatz/cs420/designPatterns/MamehoffRapidLearning.pdf
 Head First Design patterns, Freeman [book]
 Professional ASP.NET design patterns, Scott Millet [book]
 My own learning path 
 GoF categories
 Problem-Solving approach
 Similarity/differences
LEVELS OF DESIGN PATTERNS
I: Design Patterns: Elements of Reusable Object-Oriented Software,
GoF [23 patterns]
II: Patterns of Enterprise Application Architecture, Martin Fowler
III: Enterprise Integration Patterns, Gregor Hohpe
Problem specific patterns
 SOA patterns
 Concurrency patterns
 …etc.
CATEGORIES OF GOF PATTERNS
Creational patterns
 Objects construction and decoupling
Structural patterns
 How to shape objects to build more complex objects?
Behavioral patterns
 Communication between objects, SoC, and algorithms.
THE FUNDAMENTAL THEOREM OF
SOFTWARE ENGINEERING
We can solve any problem by introducing an extra level of indirection
– David Wheeler
…except for the problem of too many levels of indirection – Kevlin
Henney
THE IDEA OF “WRAPPING”
We will depend on this idea a lot here, then it worth have an example:
STARTING POINT: LOCAL OBJECT
WHAT IF THE OBJECT IS LOCATED
REMOTELY?
WCF SERVICES
Consuming WCF service in y@ our client application
WCF CLIENT AUTO-GENERATED
CODE• The remote object is wrapped by a local object!
• The web service proxies generated by svcutil.exe and deriving
from System.ServiceModel.ClientBase<TChannel>
NOTES
A layer of indirection/wrapper was created to solve the problem.
The interface was adhered.
The wrapper located at my system to wrap remote object
Congrats! This is the first design pattern, Proxy Pattern (typically,
remote proxy)
More examples:
 When you consume a WebApi service that has no wsdl, you create your own proxy
 When you create a SignalR windows client and you create your own proxy
Another flavor = Reverse Proxy: proxy for external systems to use my
services
 The wrapper located at the remote system to control access to its internal objects
WHAT IF THE OBJECT IS EXPENSIVE?
NAVIGATION PROPERTIES IN
ENTITY FRAMEWORK.
EF inherits the class at runtime and adds its implementation to
support lazy loading of navigation properties
NOTES
A layer of indirection was created to solve the problem.
The interface was adhered.
Congrats! This is another flavor of Proxy Pattern (typically, virtual
proxy)
More examples:
 Mocking frameworks @ unit tests makes intensive use of this idea.
WHAT IF WE WANT TO
CHANGE/EXTEND OBJECT BEHAVIOR
AT RUNTIME?
INHERITANCE AND COMPOSITION
Basic OOP solution: derive a sub-class and override methods
implementation you want to change.
One step forward solution (better for most cases):
 Favor composition over inheritance
 Composition = wrap the object by another object
 Composition is superior to inheritance not because of the sake of itself, but because it allows to handle
orthogonal concerns separately  will be obvious in Bridge pattern in a following session.
 Or, in S.O.L.I.D. principles: Open/Closed Principle = Classes should be open to
extension, but closed to changes
 Implement a shared interface + inject an instance of the class that we want to
change its behavior
STREAMS IN .NET FRAMEWORK
NOTES
A layer of indirection was created to solve the problem.
The interface was adhered.
Congrats! This is the Decorator Pattern
Decorator is a special case of proxy (also known as Smart Proxy)
More real-world examples:
 ASP.NET MVC has only one resolver, to support more than one: create a single class
that wraps many resolvers, and forward calls to appropriate resolvers among them.
 @ WPF: System.Windows.Controls.Decorator uses the same idea.
WHAT IF MY CODE DEPENDS ON
OTHER SYSTEM THAT DOESN’T EXIST
YET?!
DON’T STUCK!
Before the dependent system exists:
 Create an interface that spells the behavior you need.
 Create a dummy implementation that adheres this interface and returns dummy
data to be able to test your system
 Use the dummy object in your system
Once the dependent system exists:
 Create a new class that adheres the interface you created before
 Wraps the object that do the actual business, and forward calls to it
 Replace the dummy object by this object in your system.
WHAT IF I NEED TO USE OBJECTS
WITH DIFFERENT INTERFACES
INTERCHANGEABLY?!
AGAIN…DON’T STUCK!
Create an interface that matches the behavior you need.
Wrap every incompatible object with a new class that adheres the
interface and forward calls to the underlying object.
Use the desired wrapper in your system (DI/IoC will be useful here!)
Real-world examples:
 GIS application that needs to use Google maps and MS Virtual Maps
interchangeably!
 Creating an anti-corruption layer for a third-party to enable replacing it in future
 OOD principle: design for replacement not for reusability!
 E.g. Create an ILog interface, and an adapter for Log4Net, Nlog, ..etc.
CODE EXAMPLE
NOTES
Congrats! This is the Adapter Pattern (also known as
wrapper)
 This is one of the most useful patterns!
The interface was NOT adhered
 actually the adapter pattern enables classes of incompatible interfaces to be used
together by converting the interface of a class into another interface that my system
expects.
Usually used to avoid changing existing classes by – again - adding a
layer of indirection!
 Optimum when this class is a third-party that you have no control over its code
Examples from .NET world:
 ADO.NET providers, e.g. System.Data.SqlClient.SqlConnection,
System.Data.OleDb.OleDbConnection etc. Each provider is an adapter for its
specific database.
WHAT IF WE NEED TO SIMPLIFY THE
INTERFACE OF A
COMPLEX SUBSYSTEM OR GROUP OF
SUBSYSTEMS?
ADAPTER++
• Do as you did in Adapter BUT wrap multiple objects:
• Create a class that wraps all objects you need to expose
• Define your simple interface
• Forward calls to underlying objects.
EXAMPLE FROM .NET FRAMEWORK
WELCOME TO UML
NOTES
Congrats! This is the Façade Pattern
 This is one of the most useful patterns!
A layer of indirection was created to solve the problem.
The interface was NOT adhered
Similar enterprise pattern: Transaction Script
 Used in service layer
 = Façade + Transaction (atomic/all or none)
More real-world examples:
 Login to windows with fingerprint and AD
 A single method that calls each pre-existing service successively and return a single result!
YOU HAVE A TREE/HIERARCHY OF
ITEMS/PEOPLE AND YOU NEED TO DO
OPERATIONS ON ALL OF THEM, AT ALL
LEVELS.
THINK OF ORGANIZATION CHART AND
ATTENDANCE CALCULATIONS.
POSSIBLE SOLUTIONS
Wrong solution:
 nested for-loops and lengthy code
Perfect solution:
 Group objects into tree-like or hierarchical collection
 Each node in the tree is responsible of its own operations
 Call the root node, which calls the nested nodes recursively to complete the
operation.
 The important point is that each node is responsible of its own calculations
EXAMPLE
RESULTS
NOTES
Congrats! This is the Composite pattern!
More examples from .NET framework:
 System.Windows.Forms.Control
 System.Web.UI.Control
 System.Xml.XmlNode
UNCOVERED STRUCTURAL
PATTERNS
Bridge pattern
 It is a mix of Template and Strategy Behavioral patterns, thus postponed till we
learn these patterns.
Flyweight
 Depends on Factory Creational pattern, thus postponed till we learn this pattern.
SUMMARY
Right vs Quick solutions = Patterns vs Anti-patterns
Important OOD principles
 We can solve any problem by introducing an extra level of indirection except for the problem of too many
levels of indirection
 Favor composition over inheritance
 Open/Closed Principle = Classes should be open to extension, but closed to changes
 Design for replacement not for reusability!
Covered GoF Structural Patterns
 Proxy
 Decorator
 Adapter
 Façade
 Composite
Uncovered GoF Structural Patterns
 Bridge
 Flyweight
1 de 38

Recomendados

Software Design Patterns por
Software Design PatternsSoftware Design Patterns
Software Design PatternsSatheesh Sukumaran
1.2K visualizações26 slides
Design Patterns por
Design PatternsDesign Patterns
Design PatternsAnuja Arosha
13.5K visualizações56 slides
Design Patterns Presentation - Chetan Gole por
Design Patterns Presentation -  Chetan GoleDesign Patterns Presentation -  Chetan Gole
Design Patterns Presentation - Chetan GoleChetan Gole
12.1K visualizações24 slides
Let us understand design pattern por
Let us understand design patternLet us understand design pattern
Let us understand design patternMindfire Solutions
35.8K visualizações45 slides
Introduction to design patterns por
Introduction to design patternsIntroduction to design patterns
Introduction to design patternsAmit Kabra
1.7K visualizações44 slides
Design Patterns por
Design PatternsDesign Patterns
Design Patternssoms_1
14.2K visualizações105 slides

Mais conteúdo relacionado

Mais procurados

Design patterns ppt por
Design patterns pptDesign patterns ppt
Design patterns pptAman Jain
28.8K visualizações18 slides
Design pattern por
Design patternDesign pattern
Design patternThibaut De Broca
3.8K visualizações61 slides
Unit 2-Design Patterns.ppt por
Unit 2-Design Patterns.pptUnit 2-Design Patterns.ppt
Unit 2-Design Patterns.pptMsRAMYACSE
126 visualizações54 slides
Design Pattern por
Design PatternDesign Pattern
Design Patternwiradikusuma
1.7K visualizações16 slides
Introduction to Design Pattern por
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design PatternSanae BEKKAR
17K visualizações31 slides
Gof design patterns por
Gof design patternsGof design patterns
Gof design patternsSrikanth R Vaka
19.1K visualizações60 slides

Mais procurados(20)

Design patterns ppt por Aman Jain
Design patterns pptDesign patterns ppt
Design patterns ppt
Aman Jain28.8K visualizações
Design pattern por Thibaut De Broca
Design patternDesign pattern
Design pattern
Thibaut De Broca3.8K visualizações
Unit 2-Design Patterns.ppt por MsRAMYACSE
Unit 2-Design Patterns.pptUnit 2-Design Patterns.ppt
Unit 2-Design Patterns.ppt
MsRAMYACSE126 visualizações
Design Pattern por wiradikusuma
Design PatternDesign Pattern
Design Pattern
wiradikusuma1.7K visualizações
Introduction to Design Pattern por Sanae BEKKAR
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
Sanae BEKKAR17K visualizações
Gof design patterns por Srikanth R Vaka
Gof design patternsGof design patterns
Gof design patterns
Srikanth R Vaka19.1K visualizações
Design pattern-presentation por Rana Muhammad Asif
Design pattern-presentationDesign pattern-presentation
Design pattern-presentation
Rana Muhammad Asif3.3K visualizações
Creational pattern por Himanshu
Creational patternCreational pattern
Creational pattern
Himanshu 3.3K visualizações
Design Patterns Illustrated por Herman Peeren
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
Herman Peeren42.7K visualizações
Design Patterns - General Introduction por Asma CHERIF
Design Patterns - General IntroductionDesign Patterns - General Introduction
Design Patterns - General Introduction
Asma CHERIF628 visualizações
Servlet.ppt por VMahesh5
Servlet.pptServlet.ppt
Servlet.ppt
VMahesh5233 visualizações
Design pattern & categories por Himanshu
Design pattern & categoriesDesign pattern & categories
Design pattern & categories
Himanshu 2.5K visualizações
CS6201 Software Reuse - Design Patterns por Kwangshin Oh
CS6201 Software Reuse - Design PatternsCS6201 Software Reuse - Design Patterns
CS6201 Software Reuse - Design Patterns
Kwangshin Oh4.5K visualizações
Design patterns por Luis Goldster
Design patternsDesign patterns
Design patterns
Luis Goldster708 visualizações
Java Course 11: Design Patterns por Anton Keks
Java Course 11: Design PatternsJava Course 11: Design Patterns
Java Course 11: Design Patterns
Anton Keks6.6K visualizações
Object Oriented Analysis Design using UML por Ajit Nayak
Object Oriented Analysis Design using UMLObject Oriented Analysis Design using UML
Object Oriented Analysis Design using UML
Ajit Nayak7.5K visualizações
Design Pattern For C# Part 1 por Shahzad
Design Pattern For C# Part 1Design Pattern For C# Part 1
Design Pattern For C# Part 1
Shahzad 7.1K visualizações
UML Architecture and Views por Kumar
UML Architecture and ViewsUML Architecture and Views
UML Architecture and Views
Kumar 11K visualizações
Introduction to Unified Modeling Language por AMITJain879
Introduction to Unified Modeling LanguageIntroduction to Unified Modeling Language
Introduction to Unified Modeling Language
AMITJain879133 visualizações
Software Engineering - chp4- design patterns por Lilia Sfaxi
Software Engineering - chp4- design patternsSoftware Engineering - chp4- design patterns
Software Engineering - chp4- design patterns
Lilia Sfaxi14K visualizações

Destaque

introduction-to-gprs-egprs- por
introduction-to-gprs-egprs-introduction-to-gprs-egprs-
introduction-to-gprs-egprs-Dawood Aqlan
2.3K visualizações121 slides
Multiband Transceivers - [Chapter 7] Multi-mode/Multi-band GSM/GPRS/TDMA/AMP... por
Multiband Transceivers - [Chapter 7]  Multi-mode/Multi-band GSM/GPRS/TDMA/AMP...Multiband Transceivers - [Chapter 7]  Multi-mode/Multi-band GSM/GPRS/TDMA/AMP...
Multiband Transceivers - [Chapter 7] Multi-mode/Multi-band GSM/GPRS/TDMA/AMP...Simen Li
3.6K visualizações38 slides
Is that a Time Machine? Some Design Patterns for Real World Machine Learning ... por
Is that a Time Machine? Some Design Patterns for Real World Machine Learning ...Is that a Time Machine? Some Design Patterns for Real World Machine Learning ...
Is that a Time Machine? Some Design Patterns for Real World Machine Learning ...Justin Basilico
8.8K visualizações33 slides
Design Patterns For 70% Of Programmers In The World por
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The WorldSaurabh Moody
24.8K visualizações60 slides
GPRS por
GPRSGPRS
GPRSObject-Frontier Software Pvt. Ltd
32.4K visualizações52 slides
Quick Summary of LTE Voice Summit 2014 #LTEVoice por
Quick Summary of LTE Voice Summit 2014 #LTEVoiceQuick Summary of LTE Voice Summit 2014 #LTEVoice
Quick Summary of LTE Voice Summit 2014 #LTEVoiceeXplanoTech
42.9K visualizações52 slides

Destaque(10)

introduction-to-gprs-egprs- por Dawood Aqlan
introduction-to-gprs-egprs-introduction-to-gprs-egprs-
introduction-to-gprs-egprs-
Dawood Aqlan2.3K visualizações
Multiband Transceivers - [Chapter 7] Multi-mode/Multi-band GSM/GPRS/TDMA/AMP... por Simen Li
Multiband Transceivers - [Chapter 7]  Multi-mode/Multi-band GSM/GPRS/TDMA/AMP...Multiband Transceivers - [Chapter 7]  Multi-mode/Multi-band GSM/GPRS/TDMA/AMP...
Multiband Transceivers - [Chapter 7] Multi-mode/Multi-band GSM/GPRS/TDMA/AMP...
Simen Li3.6K visualizações
Is that a Time Machine? Some Design Patterns for Real World Machine Learning ... por Justin Basilico
Is that a Time Machine? Some Design Patterns for Real World Machine Learning ...Is that a Time Machine? Some Design Patterns for Real World Machine Learning ...
Is that a Time Machine? Some Design Patterns for Real World Machine Learning ...
Justin Basilico8.8K visualizações
Design Patterns For 70% Of Programmers In The World por Saurabh Moody
Design Patterns For 70% Of Programmers In The WorldDesign Patterns For 70% Of Programmers In The World
Design Patterns For 70% Of Programmers In The World
Saurabh Moody24.8K visualizações
Quick Summary of LTE Voice Summit 2014 #LTEVoice por eXplanoTech
Quick Summary of LTE Voice Summit 2014 #LTEVoiceQuick Summary of LTE Voice Summit 2014 #LTEVoice
Quick Summary of LTE Voice Summit 2014 #LTEVoice
eXplanoTech42.9K visualizações
5G: A 2020 Vision por eXplanoTech
5G: A 2020 Vision5G: A 2020 Vision
5G: A 2020 Vision
eXplanoTech16.4K visualizações
Gprs architecture ppt por Arpita Sanghani
Gprs architecture pptGprs architecture ppt
Gprs architecture ppt
Arpita Sanghani46.2K visualizações
Design Patterns & JDK Examples por Ender Aydin Orak
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK Examples
Ender Aydin Orak15.8K visualizações
Gprs ppt por Shams Tabrez
Gprs pptGprs ppt
Gprs ppt
Shams Tabrez41.6K visualizações

Similar a GoF Design patterns I: Introduction + Structural Patterns

L05 Design Patterns por
L05 Design PatternsL05 Design Patterns
L05 Design PatternsÓlafur Andri Ragnarsson
1K visualizações105 slides
L03 Design Patterns por
L03 Design PatternsL03 Design Patterns
L03 Design PatternsÓlafur Andri Ragnarsson
1.8K visualizações79 slides
Jump start to OOP, OOAD, and Design Pattern por
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternNishith Shukla
9.8K visualizações58 slides
Jump Start To Ooad And Design Patterns por
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsLalit Kale
1.2K visualizações58 slides
L05 Design Patterns por
L05 Design PatternsL05 Design Patterns
L05 Design PatternsÓlafur Andri Ragnarsson
1.3K visualizações99 slides
Patterns (contd)Software Development ProcessDesign patte.docx por
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxdanhaley45372
4 visualizações12 slides

Similar a GoF Design patterns I: Introduction + Structural Patterns(20)

Jump start to OOP, OOAD, and Design Pattern por Nishith Shukla
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
Nishith Shukla9.8K visualizações
Jump Start To Ooad And Design Patterns por Lalit Kale
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
Lalit Kale1.2K visualizações
Patterns (contd)Software Development ProcessDesign patte.docx por danhaley45372
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley453724 visualizações
Why Design Patterns Are Important In Software Engineering por Protelo, Inc.
Why Design Patterns Are Important In Software EngineeringWhy Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software Engineering
Protelo, Inc.4.6K visualizações
Introduction to Design Patterns por Prageeth Sandakalum
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
Prageeth Sandakalum1.4K visualizações
Design pattern por Shreyance Jain
Design patternDesign pattern
Design pattern
Shreyance Jain469 visualizações
Design poo my_jug_en_ppt por agnes_crepet
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
agnes_crepet577 visualizações
Evolve Your Code por RookieOne
Evolve Your CodeEvolve Your Code
Evolve Your Code
RookieOne618 visualizações
Clean code-v2.2 por Bình Trọng Án
Clean code-v2.2Clean code-v2.2
Clean code-v2.2
Bình Trọng Án705 visualizações
P Training Presentation por Gaurav Tyagi
P Training PresentationP Training Presentation
P Training Presentation
Gaurav Tyagi1.1K visualizações
Software design principles - jinal desai por jinaldesailive
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
jinaldesailive705 visualizações
Typescript design patterns applied to sharepoint framework - Sharepoint Satur... por Luis Valencia
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
Luis Valencia603 visualizações
Bp301 por Bill Buchan
Bp301Bp301
Bp301
Bill Buchan546 visualizações
Clean Code .Net Cheetsheets por NikitaGoncharuk1
Clean Code .Net CheetsheetsClean Code .Net Cheetsheets
Clean Code .Net Cheetsheets
NikitaGoncharuk1343 visualizações
Design patterns por mudabbirwarsi
Design patternsDesign patterns
Design patterns
mudabbirwarsi1.8K visualizações
Single Responsibility Principle por BADR
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
BADR421 visualizações
AEM Clean Code - Miklos Csere por Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
Miklos Csere270 visualizações

Último

Benefits in Software Development por
Benefits in Software DevelopmentBenefits in Software Development
Benefits in Software DevelopmentJohn Valentino
5 visualizações15 slides
Navigating container technology for enhanced security by Niklas Saari por
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas SaariMetosin Oy
14 visualizações34 slides
Bootstrapping vs Venture Capital.pptx por
Bootstrapping vs Venture Capital.pptxBootstrapping vs Venture Capital.pptx
Bootstrapping vs Venture Capital.pptxZeljko Svedic
14 visualizações17 slides
Introduction to Maven por
Introduction to MavenIntroduction to Maven
Introduction to MavenJohn Valentino
6 visualizações10 slides
FIMA 2023 Neo4j & FS - Entity Resolution.pptx por
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptxNeo4j
17 visualizações26 slides
predicting-m3-devopsconMunich-2023-v2.pptx por
predicting-m3-devopsconMunich-2023-v2.pptxpredicting-m3-devopsconMunich-2023-v2.pptx
predicting-m3-devopsconMunich-2023-v2.pptxTier1 app
9 visualizações33 slides

Último(20)

Benefits in Software Development por John Valentino
Benefits in Software DevelopmentBenefits in Software Development
Benefits in Software Development
John Valentino5 visualizações
Navigating container technology for enhanced security by Niklas Saari por Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy14 visualizações
Bootstrapping vs Venture Capital.pptx por Zeljko Svedic
Bootstrapping vs Venture Capital.pptxBootstrapping vs Venture Capital.pptx
Bootstrapping vs Venture Capital.pptx
Zeljko Svedic14 visualizações
Introduction to Maven por John Valentino
Introduction to MavenIntroduction to Maven
Introduction to Maven
John Valentino6 visualizações
FIMA 2023 Neo4j & FS - Entity Resolution.pptx por Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j17 visualizações
predicting-m3-devopsconMunich-2023-v2.pptx por Tier1 app
predicting-m3-devopsconMunich-2023-v2.pptxpredicting-m3-devopsconMunich-2023-v2.pptx
predicting-m3-devopsconMunich-2023-v2.pptx
Tier1 app9 visualizações
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx por animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm15 visualizações
Keep por Geniusee
KeepKeep
Keep
Geniusee78 visualizações
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action por Márton Kodok
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionGen Apps on Google Cloud PaLM2 and Codey APIs in Action
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action
Márton Kodok15 visualizações
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P... por NimaTorabi2
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
NimaTorabi215 visualizações
Agile 101 por John Valentino
Agile 101Agile 101
Agile 101
John Valentino9 visualizações
Generic or specific? Making sensible software design decisions por Bert Jan Schrijver
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
Bert Jan Schrijver6 visualizações
Dapr Unleashed: Accelerating Microservice Development por Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski12 visualizações
tecnologia18.docx por nosi6702
tecnologia18.docxtecnologia18.docx
tecnologia18.docx
nosi67025 visualizações
EV Charging App Case por iCoderz Solutions
EV Charging App Case EV Charging App Case
EV Charging App Case
iCoderz Solutions9 visualizações
Using Qt under LGPL-3.0 por Burkhard Stubert
Using Qt under LGPL-3.0Using Qt under LGPL-3.0
Using Qt under LGPL-3.0
Burkhard Stubert13 visualizações
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated... por TomHalpin9
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
Dev-HRE-Ops - Addressing the _Last Mile DevOps Challenge_ in Highly Regulated...
TomHalpin96 visualizações
ADDO_2022_CICID_Tom_Halpin.pdf por TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin95 visualizações
JioEngage_Presentation.pptx por admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254556 visualizações
predicting-m3-devopsconMunich-2023.pptx por Tier1 app
predicting-m3-devopsconMunich-2023.pptxpredicting-m3-devopsconMunich-2023.pptx
predicting-m3-devopsconMunich-2023.pptx
Tier1 app7 visualizações

GoF Design patterns I: Introduction + Structural Patterns

  • 1. GOF DESIGN PATTERNS I: INTRODUCTION + STRUCTURAL PATTERNS Sameh M. Deabes
  • 2. AGENDA Patterns and Anti-patterns How to learn design patterns? Categories of GoF patterns The Fundamental theorem of software engineering Real-world problems and how design patterns solve them Uncovered Structural Patterns Summary
  • 3. PATTERNS AND ANTI-PATTERNS What is a design pattern?  Well-known good high-level abstract solution templates for common problems  Blueprints not actual solutions  No pre-baked solution, you will implement it yourself  Built on concepts of OOP  notice the wide usage of polymorphism  Caution: don’t try to apply design patterns on every problem you face, rather “refactor to patterns” better! What is an anti-pattern?  Well-known quick high-level abstract solution templates for common problems that prove to have extensibility/flexibility limitations.  Quick not correct solutions  Don’t be afraid to use them given that you justify their usage, and you consider refactoring to patterns. [KISS, YAGNI] It’s a matter of right vs quick solution.
  • 4. HOW TO LEARN DESIGN PATTERNS? Problem with GoF book  The original text groups patterns in three categories, and then alphabetically within each. All good for reference purposes, but lousy for learning. -- Michael Mahemoff Solution: use other learning path  GoF Design Patterns: Rapid Learning Tips, Michael Mahemoff [Article, 2 pages]  http://cs.millersville.edu/~ekatz/cs420/designPatterns/MamehoffRapidLearning.pdf  Head First Design patterns, Freeman [book]  Professional ASP.NET design patterns, Scott Millet [book]  My own learning path   GoF categories  Problem-Solving approach  Similarity/differences
  • 5. LEVELS OF DESIGN PATTERNS I: Design Patterns: Elements of Reusable Object-Oriented Software, GoF [23 patterns] II: Patterns of Enterprise Application Architecture, Martin Fowler III: Enterprise Integration Patterns, Gregor Hohpe Problem specific patterns  SOA patterns  Concurrency patterns  …etc.
  • 6. CATEGORIES OF GOF PATTERNS Creational patterns  Objects construction and decoupling Structural patterns  How to shape objects to build more complex objects? Behavioral patterns  Communication between objects, SoC, and algorithms.
  • 7. THE FUNDAMENTAL THEOREM OF SOFTWARE ENGINEERING We can solve any problem by introducing an extra level of indirection – David Wheeler …except for the problem of too many levels of indirection – Kevlin Henney
  • 8. THE IDEA OF “WRAPPING” We will depend on this idea a lot here, then it worth have an example:
  • 10. WHAT IF THE OBJECT IS LOCATED REMOTELY?
  • 11. WCF SERVICES Consuming WCF service in y@ our client application
  • 12. WCF CLIENT AUTO-GENERATED CODE• The remote object is wrapped by a local object! • The web service proxies generated by svcutil.exe and deriving from System.ServiceModel.ClientBase<TChannel>
  • 13. NOTES A layer of indirection/wrapper was created to solve the problem. The interface was adhered. The wrapper located at my system to wrap remote object Congrats! This is the first design pattern, Proxy Pattern (typically, remote proxy) More examples:  When you consume a WebApi service that has no wsdl, you create your own proxy  When you create a SignalR windows client and you create your own proxy Another flavor = Reverse Proxy: proxy for external systems to use my services  The wrapper located at the remote system to control access to its internal objects
  • 14. WHAT IF THE OBJECT IS EXPENSIVE?
  • 15. NAVIGATION PROPERTIES IN ENTITY FRAMEWORK. EF inherits the class at runtime and adds its implementation to support lazy loading of navigation properties
  • 16. NOTES A layer of indirection was created to solve the problem. The interface was adhered. Congrats! This is another flavor of Proxy Pattern (typically, virtual proxy) More examples:  Mocking frameworks @ unit tests makes intensive use of this idea.
  • 17. WHAT IF WE WANT TO CHANGE/EXTEND OBJECT BEHAVIOR AT RUNTIME?
  • 18. INHERITANCE AND COMPOSITION Basic OOP solution: derive a sub-class and override methods implementation you want to change. One step forward solution (better for most cases):  Favor composition over inheritance  Composition = wrap the object by another object  Composition is superior to inheritance not because of the sake of itself, but because it allows to handle orthogonal concerns separately  will be obvious in Bridge pattern in a following session.  Or, in S.O.L.I.D. principles: Open/Closed Principle = Classes should be open to extension, but closed to changes  Implement a shared interface + inject an instance of the class that we want to change its behavior
  • 19. STREAMS IN .NET FRAMEWORK
  • 20. NOTES A layer of indirection was created to solve the problem. The interface was adhered. Congrats! This is the Decorator Pattern Decorator is a special case of proxy (also known as Smart Proxy) More real-world examples:  ASP.NET MVC has only one resolver, to support more than one: create a single class that wraps many resolvers, and forward calls to appropriate resolvers among them.  @ WPF: System.Windows.Controls.Decorator uses the same idea.
  • 21. WHAT IF MY CODE DEPENDS ON OTHER SYSTEM THAT DOESN’T EXIST YET?!
  • 22. DON’T STUCK! Before the dependent system exists:  Create an interface that spells the behavior you need.  Create a dummy implementation that adheres this interface and returns dummy data to be able to test your system  Use the dummy object in your system Once the dependent system exists:  Create a new class that adheres the interface you created before  Wraps the object that do the actual business, and forward calls to it  Replace the dummy object by this object in your system.
  • 23. WHAT IF I NEED TO USE OBJECTS WITH DIFFERENT INTERFACES INTERCHANGEABLY?!
  • 24. AGAIN…DON’T STUCK! Create an interface that matches the behavior you need. Wrap every incompatible object with a new class that adheres the interface and forward calls to the underlying object. Use the desired wrapper in your system (DI/IoC will be useful here!) Real-world examples:  GIS application that needs to use Google maps and MS Virtual Maps interchangeably!  Creating an anti-corruption layer for a third-party to enable replacing it in future  OOD principle: design for replacement not for reusability!  E.g. Create an ILog interface, and an adapter for Log4Net, Nlog, ..etc.
  • 26. NOTES Congrats! This is the Adapter Pattern (also known as wrapper)  This is one of the most useful patterns! The interface was NOT adhered  actually the adapter pattern enables classes of incompatible interfaces to be used together by converting the interface of a class into another interface that my system expects. Usually used to avoid changing existing classes by – again - adding a layer of indirection!  Optimum when this class is a third-party that you have no control over its code Examples from .NET world:  ADO.NET providers, e.g. System.Data.SqlClient.SqlConnection, System.Data.OleDb.OleDbConnection etc. Each provider is an adapter for its specific database.
  • 27. WHAT IF WE NEED TO SIMPLIFY THE INTERFACE OF A COMPLEX SUBSYSTEM OR GROUP OF SUBSYSTEMS?
  • 28. ADAPTER++ • Do as you did in Adapter BUT wrap multiple objects: • Create a class that wraps all objects you need to expose • Define your simple interface • Forward calls to underlying objects.
  • 29. EXAMPLE FROM .NET FRAMEWORK
  • 31. NOTES Congrats! This is the Façade Pattern  This is one of the most useful patterns! A layer of indirection was created to solve the problem. The interface was NOT adhered Similar enterprise pattern: Transaction Script  Used in service layer  = Façade + Transaction (atomic/all or none) More real-world examples:  Login to windows with fingerprint and AD  A single method that calls each pre-existing service successively and return a single result!
  • 32. YOU HAVE A TREE/HIERARCHY OF ITEMS/PEOPLE AND YOU NEED TO DO OPERATIONS ON ALL OF THEM, AT ALL LEVELS. THINK OF ORGANIZATION CHART AND ATTENDANCE CALCULATIONS.
  • 33. POSSIBLE SOLUTIONS Wrong solution:  nested for-loops and lengthy code Perfect solution:  Group objects into tree-like or hierarchical collection  Each node in the tree is responsible of its own operations  Call the root node, which calls the nested nodes recursively to complete the operation.  The important point is that each node is responsible of its own calculations
  • 36. NOTES Congrats! This is the Composite pattern! More examples from .NET framework:  System.Windows.Forms.Control  System.Web.UI.Control  System.Xml.XmlNode
  • 37. UNCOVERED STRUCTURAL PATTERNS Bridge pattern  It is a mix of Template and Strategy Behavioral patterns, thus postponed till we learn these patterns. Flyweight  Depends on Factory Creational pattern, thus postponed till we learn this pattern.
  • 38. SUMMARY Right vs Quick solutions = Patterns vs Anti-patterns Important OOD principles  We can solve any problem by introducing an extra level of indirection except for the problem of too many levels of indirection  Favor composition over inheritance  Open/Closed Principle = Classes should be open to extension, but closed to changes  Design for replacement not for reusability! Covered GoF Structural Patterns  Proxy  Decorator  Adapter  Façade  Composite Uncovered GoF Structural Patterns  Bridge  Flyweight