SlideShare a Scribd company logo
1 of 39
Design 
Patterns 
Factory Pattern in detail
A bit of History 
 First work by Christopher Alexander a Civil 
Engineer. Documented how time and 
again the same architecture for buildings 
when used was liked and produced the 
desired output. 
 About 15 years ago Software 
Proffessionals inspired by Alexander’s work 
started incorporating it into SW 
development practices.
Guesses ???
GoF 
 Eric Gamma, Richard Helm, Ralph 
Johnson and John Vlissides. 
 First work 15 years before. 
 Included 23 design patterns categorised 
into 3 types.
Nirvana 
What ? 
- How well we understand them 
Why ? 
- How well we know their need 
Where ? 
- Which design pattern where
What are Design Patterns 
 Time tested solutions to recurring problems 
 Template that has to be implemented in 
correct situation 
 Relationship between classes and objects 
with defined responsibilities that act in 
concert to carry out the solution. 
 Language independent. Not just a class 
or library. Much more than that.
Types 
 Structural 
- relationship between entities making it 
easier for entities to work together. 
 Creational 
- instantiation mechanisms making it easier 
to create objects suiting the situation 
 Behavioral 
- communication between entities and 
make it more easier and comfortable for entities 
to communicate
Architectural Patterns 
 MVC 
 MVP 
 MVVM 
 N-Tier 
 Repository 
….
GoF and Architectural 
 Architectural is the pattern used overall 
the project. Bigger Impact. Used at a 
higher level of problem solving. 
 Design Pattern (GoF) can be occuring all 
over the place in Architectural. 
Conentrates on solving a particular 
situational problem.
Analogy – Building 
construction 
 How many rooms 
 How many doors 
 How many exits 
 Entrance security mechanisms 
What should fall next to what 
… (Architectural)
Analogy - Continued 
 Templates for a bay area (cubicles, open 
bays, adjacent bays..) 
 Out of the box solution for the selected 
authentication type (Strategy Design 
Pattern being the most famous) 
 Design for exits (fire exits, lifts etc) 
… (Analogy for GoF design patterns)
Structure Of A Design Pattern 
 Design Patterns are highly structured 
 Documented from a template that 
identifies the information needed to 
understand the problem and the context 
 Relationship between classes and objects 
needed to implement the solution
Case Study – Factory Pattern
The Factory Method Pattern is a creation pattern. 
It does exactly as it sounds ; A class that acts as a factory of 
object instances 
Based on the context the type of object that has to be created 
is decided in the logic of the class 
Generally has one interface class with many sub classes providing 
other templates of object that can be created 
Example : I had to once create a generic class for rendering 
maps in a mobile app. Based on selection the class was 
expected to either render a google map(google sdk for iOS) 
or native Apple maps. 
First time I got introduced to Factory Pattern 
Yaaaaaayyyyyy !!!!
When can I use this 
When you have multiple variations of a 
single entity. 
 Define an interface for creating an object 
but let sub classes decide which class to 
instantiate 
 Let a class defer instantiation to sub 
classes.
Case Study – Mutable Array 
Wondered on how mutable arrays work 
internally 
 Immutable arrays provide enormous 
benefits. Thread safe as well as copying is 
free. 
 They are quite boring – contents cant be 
modified
Problem of Plain old C Arrays 
 Continuous segment of memory that can 
be easily read and written to. 
 Malloc-ed block of memory for dynamic 
array. 
 If an item has to be added at index 0 ? 
 If an item at index 0 has to be deleted ? 
 Memmove is to be used.
Inserting A into 0th index
Removing A from 0th index
So the real questions ? 
 How arrays are so powerful at indexing 
even when mutable ? 
 How deletion at a particular index ensures 
minimal shifting and minimal traversing ? 
 How addition at a particular index ensures 
minimal shifting and minimal traversing ? 
 How is it internally implemented ? (Linked 
list ? Hash table ? Hash of linked lists ?)
iVars in Mutable Array 
 _used which indicates the count of 
elements 
 _list which is a pointer to the buffer 
 _size is the size of the buffer 
 _offset is the index of the first element in 
buffer
C code for objectAtIndex
Memory Layout 
_size > fetchOffset
Memory Layout 
_size <= fetchOffset
Data Structure used for buffer 
 Circular buffer 
 Contents wrap around when either end is 
reached 
 Unless buffer is full, insertion/deletion 
doesn’t need any memory to be moved
How its superior to C array 
 Addition and deletion at any index 
doesn’t need as many relocations in 
memory as a C array needs 
 Not much memory operations needed as 
that in case of C arrays.
Case of deletion
Case of Addition
Deletion from Middle
Tries to minimize the amount of memory moved. Thus at most half 
Of the elements will be moved
Some points about Mutable 
Arrays 
 Non integral growth factor – buffer size 
grows by 1.625 times once it is full 
 Once grown it doesn’t shrink. 
 Initial capacity doesn’t matter (its max 16 
till all the blocks in the buffer are filled)
Other Implementations 
 Padded buffers with zeroes from both 
ends to make enumeration faster from 
both ends.
Class Clustering 
 All such different implementations are in 
different classes which all are sub classes 
of public interface NSMutableArray in 
Objective C 
 Hence when we place an order to create 
an object of type Mutable Array, the 
order is received by the public interface 
and based on the situation an instance of 
one of its sub class type is returned
Benefits 
 Even if the underlying implementation in 
the class cluster changes the code will 
have no impact. 
 Can keep adding as many sub classes as 
needed in the under lying class clusters
Thanks !!

More Related Content

Similar to Design patterns - How much we understand and know ??

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
danhaley45372
 

Similar to Design patterns - How much we understand and know ?? (20)

Let us understand design pattern
Let us understand design patternLet us understand design pattern
Let us understand design pattern
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
 
Java mcq
Java mcqJava mcq
Java mcq
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Sda 9
Sda   9Sda   9
Sda 9
 
Architecture and design
Architecture and designArchitecture and design
Architecture and design
 
Introduction To Design Patterns
Introduction To Design PatternsIntroduction To Design Patterns
Introduction To Design Patterns
 
Introduction to Design Patterns
Introduction to Design PatternsIntroduction to Design Patterns
Introduction to Design Patterns
 
Review oop and ood
Review oop and oodReview oop and ood
Review oop and ood
 
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...
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
Design Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur UniversityDesign Pattern Notes: Nagpur University
Design Pattern Notes: Nagpur University
 
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 Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Software Design Patterns
Software Design PatternsSoftware Design Patterns
Software Design Patterns
 
Patterns Overview
Patterns OverviewPatterns Overview
Patterns Overview
 
Object Oriented Programming In .Net
Object Oriented Programming In .NetObject Oriented Programming In .Net
Object Oriented Programming In .Net
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Module 2 design patterns-2
Module 2   design patterns-2Module 2   design patterns-2
Module 2 design patterns-2
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

Design patterns - How much we understand and know ??

  • 1. Design Patterns Factory Pattern in detail
  • 2. A bit of History  First work by Christopher Alexander a Civil Engineer. Documented how time and again the same architecture for buildings when used was liked and produced the desired output.  About 15 years ago Software Proffessionals inspired by Alexander’s work started incorporating it into SW development practices.
  • 4. GoF  Eric Gamma, Richard Helm, Ralph Johnson and John Vlissides.  First work 15 years before.  Included 23 design patterns categorised into 3 types.
  • 5. Nirvana What ? - How well we understand them Why ? - How well we know their need Where ? - Which design pattern where
  • 6. What are Design Patterns  Time tested solutions to recurring problems  Template that has to be implemented in correct situation  Relationship between classes and objects with defined responsibilities that act in concert to carry out the solution.  Language independent. Not just a class or library. Much more than that.
  • 7. Types  Structural - relationship between entities making it easier for entities to work together.  Creational - instantiation mechanisms making it easier to create objects suiting the situation  Behavioral - communication between entities and make it more easier and comfortable for entities to communicate
  • 8. Architectural Patterns  MVC  MVP  MVVM  N-Tier  Repository ….
  • 9.
  • 10. GoF and Architectural  Architectural is the pattern used overall the project. Bigger Impact. Used at a higher level of problem solving.  Design Pattern (GoF) can be occuring all over the place in Architectural. Conentrates on solving a particular situational problem.
  • 11. Analogy – Building construction  How many rooms  How many doors  How many exits  Entrance security mechanisms What should fall next to what … (Architectural)
  • 12. Analogy - Continued  Templates for a bay area (cubicles, open bays, adjacent bays..)  Out of the box solution for the selected authentication type (Strategy Design Pattern being the most famous)  Design for exits (fire exits, lifts etc) … (Analogy for GoF design patterns)
  • 13.
  • 14. Structure Of A Design Pattern  Design Patterns are highly structured  Documented from a template that identifies the information needed to understand the problem and the context  Relationship between classes and objects needed to implement the solution
  • 15.
  • 16. Case Study – Factory Pattern
  • 17. The Factory Method Pattern is a creation pattern. It does exactly as it sounds ; A class that acts as a factory of object instances Based on the context the type of object that has to be created is decided in the logic of the class Generally has one interface class with many sub classes providing other templates of object that can be created Example : I had to once create a generic class for rendering maps in a mobile app. Based on selection the class was expected to either render a google map(google sdk for iOS) or native Apple maps. First time I got introduced to Factory Pattern Yaaaaaayyyyyy !!!!
  • 18. When can I use this When you have multiple variations of a single entity.  Define an interface for creating an object but let sub classes decide which class to instantiate  Let a class defer instantiation to sub classes.
  • 19. Case Study – Mutable Array Wondered on how mutable arrays work internally  Immutable arrays provide enormous benefits. Thread safe as well as copying is free.  They are quite boring – contents cant be modified
  • 20. Problem of Plain old C Arrays  Continuous segment of memory that can be easily read and written to.  Malloc-ed block of memory for dynamic array.  If an item has to be added at index 0 ?  If an item at index 0 has to be deleted ?  Memmove is to be used.
  • 21. Inserting A into 0th index
  • 22. Removing A from 0th index
  • 23. So the real questions ?  How arrays are so powerful at indexing even when mutable ?  How deletion at a particular index ensures minimal shifting and minimal traversing ?  How addition at a particular index ensures minimal shifting and minimal traversing ?  How is it internally implemented ? (Linked list ? Hash table ? Hash of linked lists ?)
  • 24. iVars in Mutable Array  _used which indicates the count of elements  _list which is a pointer to the buffer  _size is the size of the buffer  _offset is the index of the first element in buffer
  • 25. C code for objectAtIndex
  • 26. Memory Layout _size > fetchOffset
  • 27. Memory Layout _size <= fetchOffset
  • 28. Data Structure used for buffer  Circular buffer  Contents wrap around when either end is reached  Unless buffer is full, insertion/deletion doesn’t need any memory to be moved
  • 29. How its superior to C array  Addition and deletion at any index doesn’t need as many relocations in memory as a C array needs  Not much memory operations needed as that in case of C arrays.
  • 33. Tries to minimize the amount of memory moved. Thus at most half Of the elements will be moved
  • 34. Some points about Mutable Arrays  Non integral growth factor – buffer size grows by 1.625 times once it is full  Once grown it doesn’t shrink.  Initial capacity doesn’t matter (its max 16 till all the blocks in the buffer are filled)
  • 35. Other Implementations  Padded buffers with zeroes from both ends to make enumeration faster from both ends.
  • 36. Class Clustering  All such different implementations are in different classes which all are sub classes of public interface NSMutableArray in Objective C  Hence when we place an order to create an object of type Mutable Array, the order is received by the public interface and based on the situation an instance of one of its sub class type is returned
  • 37. Benefits  Even if the underlying implementation in the class cluster changes the code will have no impact.  Can keep adding as many sub classes as needed in the under lying class clusters
  • 38.