SlideShare une entreprise Scribd logo
1  sur  38
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

Contenu connexe

Tendances

如何培養架構性思考(談軟體架構師必經之路)
如何培養架構性思考(談軟體架構師必經之路)如何培養架構性思考(談軟體架構師必經之路)
如何培養架構性思考(談軟體架構師必經之路)Gelis Wu
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxKunalYadav65140
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design PatternSanae BEKKAR
 
Spark 2013 Presentation of making the enterprise agile
Spark 2013 Presentation of making the enterprise agileSpark 2013 Presentation of making the enterprise agile
Spark 2013 Presentation of making the enterprise agilegbgruver
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented ProgrammingRajesh Ganesan
 
Introduction to Skia by Ryan Chou @20141008
Introduction to Skia by Ryan Chou @20141008Introduction to Skia by Ryan Chou @20141008
Introduction to Skia by Ryan Chou @20141008Ryan Chou
 
事件風暴-領域建模
事件風暴-領域建模事件風暴-領域建模
事件風暴-領域建模國昭 張
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns IllustratedHerman Peeren
 
Gof design pattern
Gof design patternGof design pattern
Gof design patternnaveen kumar
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsMichael Heron
 
Design Pattern in Software Engineering
Design Pattern in Software EngineeringDesign Pattern in Software Engineering
Design Pattern in Software EngineeringManish Kumar
 

Tendances (20)

如何培養架構性思考(談軟體架構師必經之路)
如何培養架構性思考(談軟體架構師必經之路)如何培養架構性思考(談軟體架構師必經之路)
如何培養架構性思考(談軟體架構師必經之路)
 
JAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptxJAVA-PPT'S-complete-chrome.pptx
JAVA-PPT'S-complete-chrome.pptx
 
Introduction to Design Pattern
Introduction to Design  PatternIntroduction to Design  Pattern
Introduction to Design Pattern
 
Waterfall model
Waterfall modelWaterfall model
Waterfall model
 
RUP
RUPRUP
RUP
 
Spark 2013 Presentation of making the enterprise agile
Spark 2013 Presentation of making the enterprise agileSpark 2013 Presentation of making the enterprise agile
Spark 2013 Presentation of making the enterprise agile
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
Introduction to Skia by Ryan Chou @20141008
Introduction to Skia by Ryan Chou @20141008Introduction to Skia by Ryan Chou @20141008
Introduction to Skia by Ryan Chou @20141008
 
事件風暴-領域建模
事件風暴-領域建模事件風暴-領域建模
事件風暴-領域建模
 
Spiral Model
Spiral ModelSpiral Model
Spiral Model
 
Software Process Models
Software Process ModelsSoftware Process Models
Software Process Models
 
Design Patterns Illustrated
Design Patterns IllustratedDesign Patterns Illustrated
Design Patterns Illustrated
 
Gof design pattern
Gof design patternGof design pattern
Gof design pattern
 
PATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design PatternsPATTERNS04 - Structural Design Patterns
PATTERNS04 - Structural Design Patterns
 
Design Pattern in Software Engineering
Design Pattern in Software EngineeringDesign Pattern in Software Engineering
Design Pattern in Software Engineering
 
Sdlc
SdlcSdlc
Sdlc
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Process models
Process modelsProcess models
Process models
 
Introduction to DevOps
Introduction to DevOpsIntroduction to DevOps
Introduction to DevOps
 

En vedette

introduction-to-gprs-egprs-
introduction-to-gprs-egprs-introduction-to-gprs-egprs-
introduction-to-gprs-egprs-Dawood Aqlan
 
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...
Multiband Transceivers - [Chapter 7] Multi-mode/Multi-band GSM/GPRS/TDMA/AMP...Simen Li
 
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 ...
Is that a Time Machine? Some Design Patterns for Real World Machine Learning ...Justin Basilico
 
Design Patterns For 70% Of Programmers In The World
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
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns pptAman Jain
 
Quick Summary of LTE Voice Summit 2014 #LTEVoice
Quick Summary of LTE Voice Summit 2014 #LTEVoiceQuick Summary of LTE Voice Summit 2014 #LTEVoice
Quick Summary of LTE Voice Summit 2014 #LTEVoiceeXplanoTech
 
5G: A 2020 Vision
5G: A 2020 Vision5G: A 2020 Vision
5G: A 2020 VisioneXplanoTech
 
Design Patterns & JDK Examples
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK ExamplesEnder Aydin Orak
 

En vedette (12)

introduction-to-gprs-egprs-
introduction-to-gprs-egprs-introduction-to-gprs-egprs-
introduction-to-gprs-egprs-
 
Gof design patterns
Gof design patternsGof design patterns
Gof design patterns
 
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...
Multiband Transceivers - [Chapter 7] Multi-mode/Multi-band GSM/GPRS/TDMA/AMP...
 
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 ...
Is that a Time Machine? Some Design Patterns for Real World Machine Learning ...
 
Design Patterns For 70% Of Programmers In The World
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
 
Design patterns ppt
Design patterns pptDesign patterns ppt
Design patterns ppt
 
GPRS
GPRSGPRS
GPRS
 
Quick Summary of LTE Voice Summit 2014 #LTEVoice
Quick Summary of LTE Voice Summit 2014 #LTEVoiceQuick Summary of LTE Voice Summit 2014 #LTEVoice
Quick Summary of LTE Voice Summit 2014 #LTEVoice
 
5G: A 2020 Vision
5G: A 2020 Vision5G: A 2020 Vision
5G: A 2020 Vision
 
Gprs architecture ppt
Gprs architecture pptGprs architecture ppt
Gprs architecture ppt
 
Design Patterns & JDK Examples
Design Patterns & JDK ExamplesDesign Patterns & JDK Examples
Design Patterns & JDK Examples
 
Gprs ppt
Gprs pptGprs ppt
Gprs ppt
 

Similaire à GoF Design patterns I: Introduction + Structural Patterns

Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternNishith Shukla
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsLalit Kale
 
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
 
Why Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software EngineeringWhy Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software EngineeringProtelo, Inc.
 
Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_pptagnes_crepet
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your CodeRookieOne
 
P Training Presentation
P Training PresentationP Training Presentation
P Training PresentationGaurav Tyagi
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desaijinaldesailive
 
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...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...Luis Valencia
 
Clean Code .Net Cheetsheets
Clean Code .Net CheetsheetsClean Code .Net Cheetsheets
Clean Code .Net CheetsheetsNikitaGoncharuk1
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility PrincipleBADR
 
AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere Miklos Csere
 

Similaire à GoF Design patterns I: Introduction + Structural Patterns (20)

L05 Design Patterns
L05 Design PatternsL05 Design Patterns
L05 Design Patterns
 
L03 Design Patterns
L03 Design PatternsL03 Design Patterns
L03 Design Patterns
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
L05 Design Patterns
L05 Design PatternsL05 Design Patterns
L05 Design Patterns
 
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
 
Why Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software EngineeringWhy Design Patterns Are Important In Software Engineering
Why Design Patterns Are Important In Software Engineering
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Design pattern
Design patternDesign pattern
Design pattern
 
Design poo my_jug_en_ppt
Design poo my_jug_en_pptDesign poo my_jug_en_ppt
Design poo my_jug_en_ppt
 
Evolve Your Code
Evolve Your CodeEvolve Your Code
Evolve Your Code
 
Clean code-v2.2
Clean code-v2.2Clean code-v2.2
Clean code-v2.2
 
P Training Presentation
P Training PresentationP Training Presentation
P Training Presentation
 
Software design principles - jinal desai
Software design principles - jinal desaiSoftware design principles - jinal desai
Software design principles - jinal desai
 
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...
Typescript design patterns applied to sharepoint framework - Sharepoint Satur...
 
Bp301
Bp301Bp301
Bp301
 
Clean Code .Net Cheetsheets
Clean Code .Net CheetsheetsClean Code .Net Cheetsheets
Clean Code .Net Cheetsheets
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Single Responsibility Principle
Single Responsibility PrincipleSingle Responsibility Principle
Single Responsibility Principle
 
AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere AEM Clean Code - Miklos Csere
AEM Clean Code - Miklos Csere
 

Dernier

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024Mind IT Systems
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionOnePlan Solutions
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsBert Jan Schrijver
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 

Dernier (20)

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 

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