NEMO ESSW3 SIDC "To understand is to perceive patterns" ROB.
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 ??
Design
PatternsFactory 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 !!