UNIT-III dp
-
Upload
theju-sweet -
Category
Documents
-
view
218 -
download
0
Transcript of UNIT-III dp
-
7/21/2019 UNIT-III dp
1/30
UNIT-III
Creational Patterns
-
7/21/2019 UNIT-III dp
2/30
S.NO. TOPIC
1 Creational Pattern Part-I Introduction
4 Factory Method
5 Prototype6 Singleton
7 Repeated key points or Structural Patterns !Intent"Moti#ation" $lso %no&n $s '''''(
8 !discussion o Creational patterns( Re#ie& )nit-III
* $+stract Factory
, uilder
PP. Slides
/1 4 0
/* 2 0 *
/, *2 0 ,2
/4 43 0 47
/5 4 0 54
/6 55 0 66
-
7/21/2019 UNIT-III dp
3/30
Creational Patterns
$+stracts instantiation process
Makes syste independent o ho& its o+ects
are created
coposed
represented
ncapsulates kno&ledge a+out &hich concrete
classes the syste uses ides ho& instances o these classes are
created and put together
/1
-
7/21/2019 UNIT-III dp
4/30
Creational Patterns
$+stract the instantiation process Make a syste independent o ho& o+ects are created" coposed"
and represented
Iportant i systes e#ol#e to depend ore on o+ect
coposition than on class inheritance
phasis shits ro hardcoding i8ed sets o +eha#iors to&ards asaller set o coposa+le undaental +eha#iors
ncapsulate kno&ledge a+out concrete classes a syste
uses
ide ho& instances o classes are created and put together
/1
-
7/21/2019 UNIT-III dp
5/30
Benefts o creationalpatterns Creational patterns let you program to
an interace defned by an abstractclass
That lets you confgure a system withproduct objects that vary widely instructure and unctionality
Example !"# systems #nter$iews !"# class library %ultiple loo&'and'eels (bstract )actories or di*erent screen
components
/1
-
7/21/2019 UNIT-III dp
6/30
Benefts o creationalpatterns !eneric instantiation + ,bjects are instantiated
without having to identiy a specifc class typein client code -(bstract )actory. )actory/
0implicity + %a&e instantiation easier callersdo not have to write long complex code toinstantiate and set up an object -Builder.1rototype pattern/
Creation constraints + Creational patterns canput bounds on whocan create objects. howthey are created. and whenthey are created
/1
-
7/21/2019 UNIT-III dp
7/30
ABSTRACT FACTORY
(Object Creational) Intent9
Pro#ide an interace or creating ailies o
related or dependent o+ects &ithout speciyingtheir concrete classes
Also Known As: %it:
/*
-
7/21/2019 UNIT-III dp
8/30
Motivation
; Moti#ation9;)ser interace toolkit supports ultiple
look-and-eel standards
!Moti" Presentation Manager(
;
-
7/21/2019 UNIT-III dp
9/30
Widget Factory
CreateScrollar!(
Create=indo&!(
MotifWidgetFactory
CreateScrollar!(
Create=indo&!(
PMWidgetFactory
CreateScrollar!(
Create=indo&!(
Client
Windows
PMWindow MotifWindow
ScrollBar
PMScrollBar MotifScrollBar
ABSTRACT FACTORY
Motivation
/*
-
7/21/2019 UNIT-III dp
10/30
; Solution9;$+stract =idget Factory class
;Interaces or creating each +asic kind
o &idget;$+stract class or each kind o
&idgets"
;Concrete classes ipleent speciic
look-and-eel:
/*
-
7/21/2019 UNIT-III dp
11/30
Alicabilit!
)se the $+stract Factory pattern &hen
$ syste should +e independent :
$ syste should +e conigured &ith one o ultiple
ailies o products:
$ aily o related product o+ects is designed to +e
used together:
>ou &ant to pro#ide a class li+rary o products:
/*
-
7/21/2019 UNIT-III dp
12/30
?perations9
CreateProd$! (
CreateProc! (
$+stractFactory
?perations9
CreateProd$! (
CreateProc! (
ConcreteFactory1
?perations9
CreateProd$! (
CreateProc! (
ConcreteFactory*
$+stractProduct$
ConcreteProduct$1 ConcreteProduct$*
$+stractProduct
ConcreteProduct1 ConcreteProduct*
client
Abstract Factor! Str"ct"re/*
-
7/21/2019 UNIT-III dp
13/30
ABSTRACT FACTORY Particiants $+tractFactory!=idget Factory(
-
7/21/2019 UNIT-III dp
14/30
Collaborations ?nly one ConcreteFactory instance is used or an
acti#ation" atched to a speciic application conte8t: It
+uilds a speciic product aily or client use -- the client
doesn@t care &hich aily is used -- it siply needs the
ser#ices appropriate or the current conte8t:
.he client ay use the $+stractFactory interace to
initiate creation" or soe other agent ay use the
$+stractFactory on the client@s +ehal:
/*
-
7/21/2019 UNIT-III dp
15/30
Conse#"ences .he $+stract Factory Pattern has the ollo&ing +eneits9
It isolates concrete classes ro the client: >ou use the $+stract Factory to control the classes o
o+ects the client creates:
8changing product ailies is easy: Aone o the client code +reaks +ecause the a+stract
interaces don@t change:
ecause the a+stract actory creates a coplete aily o
products" the &hole product aily changes &hen the
concrete actory is changed: It prootes consistency aong products:
It is the concrete actory@s o+ to ake sure that the right
products are used together:
/*
-
7/21/2019 UNIT-III dp
16/30
I$le$entation Factories as singletons:
$pplication needs only one instance o aConcreteFactory:
Creating the products $+stract Factory only declares an interace or creating
products:
-
7/21/2019 UNIT-III dp
17/30
%no& Uses Inter#ie&s
used to generate Blook and eel or speciic user
interace o+ects
uses the %it sui8 to denote $+stractFactory classes"
e:g:" =idget%it and
-
7/21/2019 UNIT-III dp
18/30
Relate' Patterns
Factory Method -- a B#irtual constructor
Prototype -- asks products to clone
thesel#es
Singleton -- allo&s creation o only a single
instance
/*
-
7/21/2019 UNIT-III dp
19/30
BUI*R
PATT*RN Intent9Separate the construction o a cople8 o+ect ro
its representation so that the sae constructionprocess can create dierent representations
Moti#ation9 R.F reader should +e a+le to con#ert R.F to
any te8t orat $dding ne& con#ersions &ithout odiying thereader should +e easy
/,
-
7/21/2019 UNIT-III dp
20/30
; Solution9
;Conigure R.FReader class &ith a .e8tCon#erter o+ect
;Su+classes o .e8t Con#erter specialiGe
in dierent con#ersions and orats;.e8t=idgetCon#erter &ill produce a
cople8 )I o+ect and lets the user see
and edit the te8t
/,
-
7/21/2019 UNIT-III dp
21/30
RTFReader
ParseR.F!(
&hile!tHget the ne8t token(
s&itch t:.ype
C$R9
+uilder-JCon#ertCharacter!t:Char(
F?A.9
+uilder-JCon#entFontCharnge!t:Font(
P$R$9
uilder-JCon#entParagraph!(
K
K
TextConverter
Con#ertCharacter!char(
Con#ertFontChange!Font(
Con#ertParagraph!(
ASCConverter
Con#ertCharacter!char(
Let$SCII.e8t!(
TextConverter
Con#ertCharacter!char(
Con#ertFontChange!Font(
Con#ertParagraph!(
Let.eE.e8t!(
TextWidgestConverter
Con#ertCharacter!char(
Con#ertFontChange!Font(
Con#ertParagraph!(
Let.e8t=idget!(
$SCII.e8t .eE.e8t.e8t=idget
+uilders
BUI*R Motivation
/,
-
7/21/2019 UNIT-III dp
22/30
Alicabilit!
)se the uilder pattern &hen .he algorith or creating a cople8 o+ect should
+e independent o the parts that ake up the
o+ect and ho& they are asse+led .he construction process ust allo& dierent
representations or the o+ect that is constructed
/,
-
7/21/2019 UNIT-III dp
23/30
!irector
Construct !(
or all o+ects in structure
+uilder-JuildPart !(
K
B"ilder
uildPart !(
ConcreteB"ilder
uildPart !(
LetResult !(
Prod"ct
+uilders
BUI*R
Str"ct"re
/,
-
7/21/2019 UNIT-III dp
24/30
Particiants uilder!.e8tCon#erter(
Speciies an a+stract interace or creating parts o
a Product o+ect:
Concreteuilder!$SCIICon#erter".eECon#erter".e8t
=idgetCon#erter( Constructs and asse+les parts o the product:
-
7/21/2019 UNIT-III dp
25/30
B"il'er - Collaborations
Client creates
-
7/21/2019 UNIT-III dp
26/30
a
-
7/21/2019 UNIT-III dp
27/30
Conse#"ences
It lets #ary a product@s internal representation:
It isolates code or construction and
representation:
It gi#es iner control o#er the construction
process:
-
7/21/2019 UNIT-III dp
28/30
I$le$entation
Assembly and construction interface uilders construct their products in step-+y-step
ashion:
No abstract classes for interface Empty methods as default in Builder.
In cDD" the +uild ethods are not declared as pure
#irtual e+er unctions:
-
7/21/2019 UNIT-III dp
29/30
%no&n Uses
R.F application is ro .DD:
uilder is a coon pattern in Salltalk-3:
-
7/21/2019 UNIT-III dp
30/30
Relate' Patterns
$+stract actory is siilar to uilder: It too
constructs cople8 o+ects:
Coposite is oten &hat the uilder +uilds: