design patterns - how much we understand and know ??

39
Design Patterns Factory Pattern in detail

Upload: vinay-raj

Post on 08-Jul-2015

153 views

Category:

Technology


1 download

DESCRIPTION

A quick presentation on design patterns and their classifications. Some point to note slides on to do's and not do's when it comes to implementing and adapting a proper design pattern. A case study of a creation kind of pattern which is factory design pattern.

TRANSCRIPT

Page 1: Design patterns - How much we understand and know ??

Design

PatternsFactory Pattern in detail

Page 2: Design patterns - How much we understand and know ??

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 ??

Guesses ???

Page 4: Design patterns - How much we understand and know ??

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 ??

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 ??

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 ??

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 ??

Architectural Patterns

MVC

MVP

MVVM

N-Tier

Repository

….

Page 9: Design patterns - How much we understand and know ??
Page 10: Design patterns - How much we understand and know ??

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 ??

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 ??

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 ??
Page 14: Design patterns - How much we understand and know ??

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 ??
Page 16: Design patterns - How much we understand and know ??

Case Study – Factory Pattern

Page 17: Design patterns - How much we understand and know ??

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 ??

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 ??

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 ??

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 ??

Inserting A into 0th index

Page 22: Design patterns - How much we understand and know ??

Removing A from 0th index

Page 23: Design patterns - How much we understand and know ??

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 ??

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 ??

C code for objectAtIndex

Page 26: Design patterns - How much we understand and know ??

Memory Layout_size > fetchOffset

Page 27: Design patterns - How much we understand and know ??

Memory Layout

_size <= fetchOffset

Page 28: Design patterns - How much we understand and know ??

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 ??

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 ??

Case of deletion

Page 31: Design patterns - How much we understand and know ??

Case of Addition

Page 32: Design patterns - How much we understand and know ??

Deletion from Middle

Page 33: Design patterns - How much we understand and know ??

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 ??

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 ??

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 ??

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 ??

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 ??
Page 39: Design patterns - How much we understand and know ??

Thanks !!