Вы находитесь на странице: 1из 12

Design Patterns with examples

Part-1
Creational Patterns
By
Chinta Vs RamaSarma
Index
Introduction
What is Design Patterns?
Types of Creational patterns
Factory Pattern
Abstract Factory Pattern
Singleton Pattern
Builder Pattern
Prototype Pattern
Lazy initialization pattern
Object pool
Introduction
What is Design Patterns?
Design pattern is general reusable solutions for design probles! Design patters
are "ind of teplate to sol#e design probles! $ach pattern is a solution for proble
%hich occurs again and again in the en#ironent!
$ach pattern is a three&part' %hich e(presses a relation bet%een a certain conte(t'
a proble and a solution
Creational patterns
)reational patterns tal"s about best possible %ays to instantiate an object in a best
%ay! *ae itself ha#e creational %hich eans it creates objects!
$#ery one "no%s using +ne%, "ey%ord %e can create object!
$g- Student raaSara . ne% Student /01
We create ne% object called raaSara %hich belongs to Student type! But %hat
%e ha#e done is +hard coded,!
)reational patterns create an object %hich is suitable for the situation! 2t %ill
control the object creation! 3hese patterns a"e syste independent of ho% objects are
crated' coposed and represented!
3here are se#en creational patterns as per y "no%ledge! 3hey are
40 Factory Pattern!
50 Abstract Factory Pattern!
60 Singleton Pattern!
70 Builder Pattern!
80 Prototype Pattern!
90 Lazy initialization Pattern!
:0 Object Pool Pattern!
Let e detail each pattern %ith e(aples!
Factory Pattern:-

Factory pattern returns an object for se#eral possible classes %hich you
re;uired! 2n siple %e ha#e super class and sub classes it %ill return an instance of a
class depending on your input /it depending data pro#ided to this pattern0!
Let e sho% sall e(aple to ho% to %rite this pattern!
$(aple-&
Scenario- & 2 ha#e ultiple clients' each client %ant different product based on
client type!
<ser %ill re;uest Product Factory to create object for product! 2t %ill create
product object %hich is re;uired based on his type!
Product interface
public interface =ard%are >
public String getProduct3ype /0 >?
@@!
@@@
@@@!!
?
public )lass Aa ipleent =ard%are >
public String getProduct3ype /0 >
return +Aa,1
?
@!!
@@
@@!!
?
As the sae %ay %e ha#e Botherboard' Bouse' Ceyboard etc products %hich are
ipleented by Product =ard%are!
<ser %ill re;uest for Product Factory to create a product based on his type!
Public class ProductFactory >
Public Product createProduct /String client3ype0 >
Product product.null1
if /client3ype!e;uals /+Aa,0 >
product. ne% Aa /01
? else if /client3ype!e;uals /+Bouse,0 >
product. ne% Bouse /01
? else if @@
@@@@@@!!
@@@@@@@!!
@@@@@@@@!
return product!
?
?

Abstract Factory Pattern:-
Abstract factory pattern is also sae as factory pattern %ith one le#el of
abstraction! 2t %ill return a factory for the se#eral possible factories %hich %ill return
the appropriate object!
Let e brief you %ith an e(aple sae!
Scenario-& 2 ha#e ultiple clients' each client %ant different product based on client
re;uireent! $ach product is di#ided into sub products!
2 ha#e ultiple products li"e =ard%are' Soft%are! =ard%are is di#ided into sub
parts li"e Aa' Ceyboard' Bouse etc1 Soft%are is di#ided into OS' 3ools' and
Bessengers etc! Based on client re;uireent %e need to deli#ery that product!

2 ha#e ultiple abstract classes based on the product type!
public abstract class =ard%are >
public String getProduct3ype >?
@@
@@@!
@@@@@!!
?
public )lass Aa e(tends =ard%are >
public String getProduct3ype /0 >
return +Aa,1
?
@@@
@@@@
@@@@@@
?
public )lass Bouse e(tends =ard%are >
public String getProduct3ype /0 >
return +Bouse,1
?
@@@
@@@@
@@@@@@
?
Siilar %ay 2 ha#e different sub products for =ard%are and Soft%are!
public )lass =ard%areFactory >
=ard%are part.null1
List hard%areList.ne% ArrayList /01
public static List getProduct /String clientAe;uireent0 >
if /clientAe;uireent!e;uals /+Aa,00 >
part.ne% Aa /01
? else if /clientAe;uireent!e;uals /+Bouse,00 >
part.ne% Bouse /01
?
@@@@@@!
@@@@@@@@!
@@@@@@@@@@
hard%areList!add/part01
?
?
public )lass Soft%areFactory >
Soft%are soft%are.null1
public static List getProduct /String clientAe;uireent0 >
if /clientAe;uireent!e;uals /+OS,00 >
soft%are .ne% OS /01
? else if /clientAe;uireent!e;uals /+3ools,00 >
soft%are .ne% 3ools /01
?
@@@@@@!
@@@@@@@@!
@@@@@@@@@@
?
?
public )lass =ardSoft%areFactory >
public List get=ardSoft%areFactory /String type' String nae0 >
List productList.null1
if /type!e;uals /+hard%are,00 >
productList .=ard%areFactory! getProduct /nae01
? else if /type!e;uals /+soft%are,00 >
productList . =ard%areFactory! getProduct /nae01
?
@!!!!!!!!!!!!!
@@@@@@@
@@@@@@@@@
return productList1
?
?
?
ingleton Pattern:-
Singleton pattern %ill share sae object to the all clients! 2t %ill create object
%hen first re;uest coes to it %ill create an object and it %ill share sae object to all
re;uests!

Scenario- & 2 ha#e an application in y application 2 ha#e data source and 2 need
to gi#e the sae object to all y re;uests!
2 ha#e only one connection for your database or your DDB) dri#er has trouble
%ith ultithreading' the Singleton a"es sure that only one connection is ade or that
only one thread can access the connection at a tie!
public class Singleton >
public static Singleton singleton.null1
Pri#ate Singleton /0 >
?
Public static Singleton get2nstance /0 >
if /singleton..null0>
singleton. ne% Singleton /01
?
return singleton1
?
?
!uilder Pattern:-
<sing Builder pattern %e can create cople( objects by siple step by step
process! 2t %ill separate constructor for the cople( object and its representation! 2ts
nae iplies that it builds cople( objects!
Let e e(plain
LetEs ta"e a non&soft%are e(aple for this! Say' %e ha#e to plan for a asseble
coputers! 2t is coprises' a processer' AAB' Botherboard' Ceyboard' Bouse!!!
3his is coon to all the assebling coputers! =ere' %hat is iportant? $#ery
tie a coputer is ordered' the ser#ice boy %ill asseble a coputer and gi#e it to the
custoer! *o% suppose' there are 6 types of processors! 2ntel' )eleron' Athlon 5 types of
AABFs DDAAAB' SDAAB a#ailable!
So' the order ight be a cobination of one of these' but the process %ill be the
sae! One Processer' One AAB! One Botherboard' One Ceyboard' One Bouse@!!!! All
these ites are needed to asseble and gi#e it to custoer!
Public class )oputer >
Pri#ate String AAB1
Pri#ate String processer1
Public #oid setAa /String ra0 >
3his!AAB.ra
?
Public #oid setProcesser /String processer0 >
3his! processer . processer
?
Public #oid getAa /0 >
return AAB1
?
Public String getProcesser /0 >
return processer1
?
?
Public abstract class )oputerBuilder >
pri#ate )oputer coputer1
Public )oputer get)oputer /0 >
Aeturn coputer1
?
Public #oid create)oputer /0 >
coputer. ne% )oputer /01
?
Public abstract #oid setAa /0
Public abstract #oid setProcesser /01
?
Public class 2ntel e(tends )oputerBuilder >
Public #oid setAa /0 >
coputer!setAa/+AAB,0
?
Public #oid setProcesser /0 >
coputer! setProcesser /+2ntel,0
?
?
Public class )eleron e(tends )oputerBuilder >
Public #oid setAa /0 >
coputer!setAa /+AAB,0
?
Public #oid setProcesser /0 >
coputer! setProcesser /+)eleron,0
?
?
Public class Asseble >
Pri#ate )oputerBuilder coputerBuilder1
?
public #oid set)oputerBuilder /)oputerBuilder cb0 >
coputerBuilder . cb1
?

public )oputer get)oputer/0 >
return coputerBuilder!create)oputer/01
?

public #oid asseble)oputer/0 >
coputerBuilder!create)oputer/01
coputerBuilder!setAa/01
coputerBuilder!setProcesser/01
?
?

GHH A gi#en type of )oputer being constructed! HG
public class Builder$(aple >
public static #oid ain/StringIJ args0 >
Asseble asseble . ne% Asseble/01
)oputerBuilder intel)oputer . ne% 2ntel/01
)oputerBuilder celeron)oputer . ne% )eleron/01

asseble!set)oputerBuilder/intel)oputer01
asseble!asseble)oputer/01

)oputer coputer . asseble!asseble)oputer/01
?
?
Prototype Pattern:-
Prototype pattern tell us create object using clone' donEt create object %ith +ne%,
"ey%ord if it is #ery cost creating object %ith the resources! We use )loneable interface
to and clone the objects! 2t %ill a"e a copy of original object and gi#e it to user as per
the re;uireent!
Let us ta"e sall e(aple
2 ha#e one Design patterns boo" pdf #ersion' one of y friend %ant that boo"'
%hat 2 %ill do 2 %ill copy that boo" and paste it soe %here and 2 %ill gi#e this ne% boo"
to y friend' %hat 2 did is 2 ha#e ade a copy of the original boo" and 2 ha#e gi#en the
ne%ly created boo" to y friend!
public class Boo")opy ipleents )loneable >
public Object copy /0 >
try >
return super!clone/01
?catch/$(ception e0 >
Syste!out!println/K$(ception occured- KLe!getBessage/001
return null1
?
?
?
public class )opyBoo" >
public static #oid ain/StringIJ args0 >
Boo")opy boo")opy4 . ne% Boo")opy/01
Boo")opy boo")opy5 . /Boo")opy0boo")opy4!split/01
?
?
"a#y initiali#ation Pattern:-
3his %ills delay construct objects! 2t %ill create object %hen it %as re;uired!
Let us ta"e an e(aple
2 ha#e big Fruit shop1 2 %ill sell fruits as per the season!
3he abo#e e(aple 2 %ill lazy 2nitialize season as per the season!
public class Season >
pri#ate static final BapMString' SeasonN types . ne% =ashBapMString' SeasonN /01
pri#ate final String type1
pri#ate Season/String season0 >
this!season . season1
?

public static synchronized Season getSeason/String season0 >
if /Oseason!containsCey/Season00
types!put/season' ne% Season/season001
return types!get/season01
?
?
$b%ect pool:-
Object pooling is a set of initialized objects those object are ready to use' %hen
client re;uires any object %e %ill pic" up fro the pool and gi#e it to client! Once the
client is done %ith his %or" instead of destroying that object %e %ill push bac" to pool! 2t
%ill boost the perforance!
3he best e(aple is connection pooling it %ill create connections before client
re;uest for connection' %hen client re;uired that he %ill loo" up it fro D*D2 and he can
use that object reference!

Вам также может понравиться