Design patterns - How much we understand and know ??
-
Upload
vinay-raj -
Category
Technology
-
view
153 -
download
1
description
Transcript of Design patterns - How much we understand and know ??
![Page 1: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/1.jpg)
Design
PatternsFactory Pattern in detail
![Page 2: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/2.jpg)
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.
![Page 3: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/3.jpg)
Guesses ???
![Page 4: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/4.jpg)
GoF
Eric Gamma, Richard Helm, Ralph
Johnson and John Vlissides.
First work 15 years before.
Included 23 design patterns categorised
into 3 types.
![Page 5: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/5.jpg)
Nirvana
What ?
- How well we understand them
Why ?
- How well we know their need
Where ?
- Which design pattern where
![Page 6: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/6.jpg)
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.
![Page 7: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/7.jpg)
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
![Page 8: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/8.jpg)
Architectural Patterns
MVC
MVP
MVVM
N-Tier
Repository
….
![Page 9: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/9.jpg)
![Page 10: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/10.jpg)
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.
![Page 11: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/11.jpg)
Analogy – Building
construction
How many rooms
How many doors
How many exits
Entrance security mechanisms
What should fall next to what
… (Architectural)
![Page 12: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/12.jpg)
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)
![Page 13: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/13.jpg)
![Page 14: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/14.jpg)
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
![Page 15: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/15.jpg)
![Page 16: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/16.jpg)
Case Study – Factory Pattern
![Page 17: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/17.jpg)
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 !!!!
![Page 18: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/18.jpg)
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.
![Page 19: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/19.jpg)
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
![Page 20: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/20.jpg)
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.
![Page 21: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/21.jpg)
Inserting A into 0th index
![Page 22: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/22.jpg)
Removing A from 0th index
![Page 23: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/23.jpg)
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 ?)
![Page 24: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/24.jpg)
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
![Page 25: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/25.jpg)
C code for objectAtIndex
![Page 26: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/26.jpg)
Memory Layout_size > fetchOffset
![Page 27: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/27.jpg)
Memory Layout
_size <= fetchOffset
![Page 28: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/28.jpg)
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
![Page 29: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/29.jpg)
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.
![Page 30: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/30.jpg)
Case of deletion
![Page 31: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/31.jpg)
Case of Addition
![Page 32: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/32.jpg)
Deletion from Middle
![Page 33: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/33.jpg)
Tries to minimize the amount of memory moved. Thus at most half
Of the elements will be moved
![Page 34: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/34.jpg)
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)
![Page 35: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/35.jpg)
Other Implementations
Padded buffers with zeroes from both
ends to make enumeration faster from
both ends.
![Page 36: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/36.jpg)
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
![Page 37: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/37.jpg)
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
![Page 38: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/38.jpg)
![Page 39: Design patterns - How much we understand and know ??](https://reader034.fdocuments.us/reader034/viewer/2022042614/559cd3b81a28ab547f8b481e/html5/thumbnails/39.jpg)
Thanks !!