Wednesday, 11 July 2012

Design Patterns! ThReE TyPeS

My previous posts would have given you an idea about what are design patterns and the various features of it.Yeaaa and i knoww that you are still not clear.Don wry!Even me im still confused!! :p..Design patterns and OOps are concepts with which you can be ALL clear..You learn things,know them and once you work ,you will understand more!!


Now let me discuss about the 3 kinds of design pattern.
-->Structural
-->Behavioural
-->Creational



Structural design patterns:
-->Convert the interface of a class into another interface clients expect.
-->Wraps an existing class.
-->examples are adapter,decorator, bridges etc we will just see a few examples among them.


Adapter:
-->Just like a three way plug is put into a 2 holed socket with the adapter.The adapter classes is used for this purpose.In case
   to draw a rectangle we have a fn accepting (x1,y1,h,w),but we wish to give (x1,y1,x2,y2) we can use an adapter which changes (x1,y1,x2,y2) to (x1,y1,h.w) so that draw_rect() function is accessed!
Steps followed:

Identify the client and adaptee(component to be accessed)
Create an wrapper class for the above.
In the wrapper create the adaptee instance.
Map the client instance to the adaptee instance.

Decorator:
-->Attatching additional informationm dynamically rather than inheritance which is static
-->Decorator describe an adiitional level of indirection to another object.
VAGUE???  Consider d eg)


SUPPOSE COFFEE is a interface..
Coffee can be black coffee or decotion cofee ....Now incase you have to decorate any of them with ,creame,or dryfruits..then you create a decorator class which is agin implementing coffee
Consider

interface coffee
{
};


class decotion_cof implements coffee
{
add()
{//ingredients fr decotion_coffee
}


};




class black_cof implements coffee
{
add()
{//ingredients fr blk cofee
}
};


Create a decorator class


class decorator implements coffee
{
private coffee c1;


decorator(coffee a)
{
c1=a;
}


add()
{
c1.add();
}


};




class creame extends decorator
{




add_creame()
{
}


creame(coffee q)
{
   super(q);
}


add()
{
super.add()+add_creame()
}
};




class dryfruit extends decorator
{




add_dryfruit()
{
}


dryfruit(coffee q)
{
   super(q);
}


add()
{
super.add()+add_dryfruit()
}
};





Now black coffee and dec coffee can all access your decoration class methods!

Main class-----------

coffee c=new black_coffee();
coffee d=new creame(c);
d.add();






BEHAVIOURAL PATTERN:
Identify common communication patterns between objects and realize these patterns.


ITERATOR:
 -->It is used to operate on the different lists you have.
consider u have a tv remote and a dvd remote both have differnt lists of channels.So 2 seperate iterators should be created to operate on the 2 different lists.
-->Iterator can traverse through a composite .
Strategy Pattern:
-->Selection of algorithm on the fly.
-->For instance, a class that performs validation on incoming data may use a strategy pattern to select a validation algorithm based on the type of data, the source of the data, user choice, and/or other discriminating factors. 

Creational Pattern:
Singleton Class:
  A class is a singleton class if only one object can be created an instant of time.How much ever you try that one object only will be created.
Eg is like you are playing with a toy,only once you are done ,someone else can use it.

OBJECT POOL:
   You will have a number of objects in a pool,you can go access any object at any instant when you need it and then again you can release it to the pool, once you have used  ,so that someone else can access it.
Eg) COnsider a series a 5 synchronised list.When you want it you pick one use it to go down or up and once you are done you release it back.
 
AUDIO BLOG FOR TYPES :http://soundcloud.com/ronamaria/design-patterns-ii

- Sourcebits University
Cloud Computing

No comments:

Post a Comment