Sub: Software Design Laboratory Sub Code:13MCA56
SL. NO. 1. 2. 3. 4. 5. ! #. %. &. 1'. 11.
STAFF DATE
USN: 2VX13MCA06
NAME OF EXPERIMENT Expert Controller Publisher-Subscriber Command Forward-Receie Client-"ispatcher Prox$ Facade Pol$morphism (hole-Part )aster-Slae
PAGE NO
SIGNATURE
Page 1
Sub: Software Design Laboratory Sub Code:13MCA56 1) Expert Problem:
(hat is the most basic principle b$ which responsibilities are assi+ned in ob,ect-oriented desi+n Soluto!:
ssi+n a responsibilit$ to the class that has the in/ormation necessar$ to /ul/ill the responsibilit$. "esi+n )odel ma$ de/ine hundreds or thousands o/ so/tware classes0 and an application ma$ reuire hundreds or thousands o/ responsibilities to be /ul/illed. "urin+ ob,ect desi+n0 wh en the interactions between ob,ects are de/ined0 we mae choices about the assi+nment o/ responsibilities to so/tware classes. / wee chosen well0 s$stems tend to be easier to understand0 maintain0 and extend0 and our choices a//ord more opportunit$ to reuse components in /uture applications. Ex"mple:
Point o/ SaleP6S7 s$stem is a computeri8ed application used in a retail store. t is used to record sales and handle pa$ments9 commercial P6S s$stem will sell to di//erent clients with disparate needs in terms o/ business rule processin+. :he P6S will calculate the total sales at an$ +ien point o/ time (ho (ho should be responsible /or nowin+ the +rand total o/ a sale Stru#ture:
;$ n/ormation Expert0 we should loo /or that class o/ ob,ects that has the in/ormation needed to determine the total. P"rt#p"!t$: I!%orm"to! Expert: :his is the class0 which has the in/ormation to /ul/ill the responsibilit$. responsibilit$. (e (e
ssi+n the responsibilit$ to this class to accomplish the behaior. U$e &"$e D"'r"m: A#tor:
:he user is the C:6R who is interactin+ with the P6SPoint o/ Sale7 to +et the
USN: 2VX13MCA06
Page 2
Sub: Software Design Laboratory Sub Code:13MCA56 1) Expert Problem:
(hat is the most basic principle b$ which responsibilities are assi+ned in ob,ect-oriented desi+n Soluto!:
ssi+n a responsibilit$ to the class that has the in/ormation necessar$ to /ul/ill the responsibilit$. "esi+n )odel ma$ de/ine hundreds or thousands o/ so/tware classes0 and an application ma$ reuire hundreds or thousands o/ responsibilities to be /ul/illed. "urin+ ob,ect desi+n0 wh en the interactions between ob,ects are de/ined0 we mae choices about the assi+nment o/ responsibilities to so/tware classes. / wee chosen well0 s$stems tend to be easier to understand0 maintain0 and extend0 and our choices a//ord more opportunit$ to reuse components in /uture applications. Ex"mple:
Point o/ SaleP6S7 s$stem is a computeri8ed application used in a retail store. t is used to record sales and handle pa$ments9 commercial P6S s$stem will sell to di//erent clients with disparate needs in terms o/ business rule processin+. :he P6S will calculate the total sales at an$ +ien point o/ time (ho (ho should be responsible /or nowin+ the +rand total o/ a sale Stru#ture:
;$ n/ormation Expert0 we should loo /or that class o/ ob,ects that has the in/ormation needed to determine the total. P"rt#p"!t$: I!%orm"to! Expert: :his is the class0 which has the in/ormation to /ul/ill the responsibilit$. responsibilit$. (e (e
ssi+n the responsibilit$ to this class to accomplish the behaior. U$e &"$e D"'r"m: A#tor:
:he user is the C:6R who is interactin+ with the P6SPoint o/ Sale7 to +et the
USN: 2VX13MCA06
Page 2
Sub: Software Design Laboratory Sub Code:13MCA56
A#t(t D"'r"m:
USN: 2VX13MCA06
Page 3
Sub: Software Design Laboratory Sub Code:13MCA56
&l"$$ D"'r"m:
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 4
Sub: Software Design Laboratory Sub Code:13MCA56
&ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 5
Sub: Software Design Laboratory Sub Code:13MCA56
&o+e: Pro+u#tDe$#rpto!.,"("
public class Product"escription = public SaleCounter SaleCounter9 priate /loat price>43.%&/9 public /loat +etPrice7 = return price9 ? ?
S"le.,"("
public class Sale = public SaleCounter SaleCounter9 priate /loat total9 public /loat +et:otal7 = Product"escription pd>new Product"escription79 Sales@inetem sli>new Sales@inetem79 total > pd.+etPrice7 A sli.+etSub:otal779 return total9 ? USN: 2VX13MCA06
Page 6
Sub: Software Design Laboratory Sub Code:13MCA56 ?
S"le&ou!ter.,"("
public class SaleCounter = public Sale Sale9 public Sales@inetem Sales@inetem9 public Product"escription Product"iscription9 public static oid mainStrin+ Bar+s7 = /loat total9 Sale s>new Sale79 total>s.+et:otal79 S$stem.out.printlnD :otal sale is D total79 ? ? S"le$L!eItem.,"("
public class Sales@inetem = public SaleCounter SaleCounter9 priate int uantit$>1''9 public int +etSub:otal7 = return uantit$9 ? ? USN: 2VX13MCA06
Page 7
Sub: Software Design Laboratory Sub Code:13MCA56
Output: T-e Tot"l S"le $ /0.22
3) &o!troller Problem:
:he presentation-tier reuest handlin+ mechanism must control and coordinate processin+ o/ each user across multiple reuests. Such control mechanisms ma$ be mana+ed in either a centrali8ed or centrali8ed manner. Soluto!:
Gse a controller as the initial point o/ contac t /or handlin+ a reuest. :he controller mana+es the handlin+ o/ the reuest0 includin+ inoin+ securit$ serices such as authentication and authori8ation0 dele+atin+ business processin+0 mana+in+ the ch oice o/ an appropriate iew0 handlin+ errors0 and mana+in+ the selection o/ content creation strate+ies. P"rt#p"!t$: &o!troller:
:he controller is the initial contact point /or handlin+ all reuests in the s$stem. :he controller ma$ dele+ate to a helper to complete authentication and authori8ation o/ a user or to initiate contact retrieal. D$p"t#-er:
dispatcher is responsible /or iew mana+ement and nai+ation0 mana+in+ the cho ice o/ the next iew to present to the user0 and proidin+ the mechanism /or ectorin+ control to this resource. 4elper:
helper is responsible /or helpin+ a iew or controller complete its processin+. Helpers can serice reuests /or data /rom the iew b$ simpl$ proidin+ access to the raw data or b$ /ormattin+ the data as (eb content. 5e6:
iew represents and displa$s in/ormation to the client. :he iew retriees in/ormation /rom a model. Helpers support iews b$ encapsulatin+ and adaptin+ the underl$in+ data model /or use in the displa$ USN: 2VX13MCA06
Page 8
Sub: Software Design Laboratory Sub Code:13MCA56 Ex"mple: Point 6/ SaleP6S7 s$stem
tIs is a computeri8ed application used in part7 to record sales and handle pa$ments9 it is t$picall$ used in a retail store. t inter/aces to arious serice app lications0 such as a third-part$ tax calculator and inentor$ control. Creatin+ a commercial P6S s$stem that will sell to di//erent clients with disparate needs in terms o/ business rule processin+. Each client will desire a uniue set o/ lo+ic to execute at certain predictable points in scenarios o/ usin+ the s$stem0 such as when a new sale is initiated or when a new line item is added. :here/ore0 we will need a mechanism to proide this /lexibilit$ and customi8ation. :he P6S will be calculatin+ the total sales at an $ +ien point o/ time. :he Controller Pattern su++ests that eents comin+ /rom G la$er should not be directl$ accessin+ the expert classes lie Sale which calculates total sale. :here should be a Controller class to control these eents. n this example0 SaleController class that will be receiin+ the eents /rom G and /orward it to the Sale class. Jiew class decides how the output will be displa$ed. n this example Console is used /or tain+ input0 and displa$in+ the output. U$e&"$e D"'r"m: A#tor:
:he user is the C:6R who is interactin+ with the P6SPoint o/ Sale7 to +et the
(hat in/ormation do we need to determine the +rand total (e need to now about all the Sales@inetem instances o/ a sale and the sum o/ their subtotals. Sale instance contains these. T-e U$e #"$e +"'r"m:
USN: 2VX13MCA06
Page 9
Sub: Software Design Laboratory Sub Code:13MCA56
A#t(t D"'r"m:
&l"$$ D"'r"m:
USN: 2VX13MCA06
Page 10
Sub: Software Design Laboratory Sub Code:13MCA56
Se*ue!#e D"'r"m:
&ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 11
Sub: Software Design Laboratory Sub Code:13MCA56
&o+e: &le!t.,"("
import ,aa.util.Scanner9 public class Client = public SaleController SaleController9 public static SaleController +etController7 = SaleController tc > new SaleController79 Sale s>new Sale79 iew >new iew79 tc.setSales79 tc.setJiew79 return tc9
USN: 2VX13MCA06
Page 12
Sub: Software Design Laboratory Sub Code:13MCA56 ? public static oid mainStrin+B ar+s7 = SaleController tc > +etController79 Scanner scr>new ScannerS$stem.in79 S$stem.out.printlnDEnter the Kunatit$ D79 int uantit$> scr.nextnt79 S$stem.out.printlnDEnter the Price D79 /loat price>scr.nextFloat79 tc.executeuantit$0 price79 ? ?
S"le.,"("
public class Sale = public SaleController SaleController9 public /loat +et:otalint uantit$0 /loat price7 = return uantit$ A price9 ? ?
S"le&o!troller.,"("
public class SaleController = USN: 2VX13MCA06
Page 13
Sub: Software Design Laboratory Sub Code:13MCA56 public Client Client9 public Sale sale9 public iew iew9 public oid executeint uantit$0/loat price7 = /loat result > sale.+et:otaluantit$0 price79 iew.displa$result79 ? public oid setSaleSale sale7 = this.sale > sale9 ?
public oid setJiewiew iew7 = this.iew > iew9 ? ?
(e6.,"("
public class iew = public SaleController SaleController9 public oid displa$/loat total7 = S$stem.out.printlnD:he :otal Sale is D total79 USN: 2VX13MCA06
Page 14
Sub: Software Design Laboratory Sub Code:13MCA56 ? ?
Output: Enter the Kuantit$ 2' Enter the price 5.' :he :otal Sale is 1''.'
USN: 2VX13MCA06
Page 15
Sub: Software Design Laboratory Sub Code:13MCA56 /) Publ$-er7Sub$#rber8 Ob$er(er P"tter!
D:he Publisher-Subscriber desi+n pattern helps to eep the state o/ cooperatin+ components s$nchroni8ed. t enables one-wa$ propa+ation o/ chan+es one publisher noti/ies an$ number o/ subscribers about chan+es to its state.D Problem
Leed to s$nchroni8e the state o/ cooperatin+ components. Soluto!
-
6ne dedicated component taes the role o/ the publisher. ll components dependent on chan+es in the publisher are its subscribers. :he publisher maintains a re+istr$ o/ currentl$-subscribed components. Component can subscriber uses the subscribe inter/ace o/ publisher7 or unsubscribe.
(heneer the publisher chan+es state0 it sends a noti/ication to all its subscribers. - :he subscribers in turn retriee the chan+ed data at their discretion P"rt#p"!t$ &o!#rete Sub,e#t: cts as a Publisher. t is responsible /or noti/$in+ and updatin+ the chan+es tain+ place in data. &o!#rete Ob$er(er: - cts as subscriber. :he chan+es made to the Concrete Sub,ectPublisher7 will be re/lected to it. Ex"mple: (eather n/ormation s$stem
:he weather Station will be broadcastin+ the current weather conditions lie temperature0 humidit$ and pressure. :he application receies the weather cond itions and displa$s them in di//erent /orms.i.e. displa$in+ the current weather c onditions0 displa$in+ weather /orecast7. ll displa$s will be updatin+ the weather conditions in real time.
T-e U$e #"$e +"'r"m: A#tor$: (eather reporter who is interactin+ with the weather in/ormation s$stem USN: 2VX13MCA06
Page 16
Sub: Software Design Laboratory Sub Code:13MCA56 U$e#"$e: "ispla$in+ Current (eather data and Forecasted (eather data.
A#t(t D"'r"m:
USN: 2VX13MCA06
Page 17
Sub: Software Design Laboratory Sub Code:13MCA56
&l"$$ D"'r"m:
USN: 2VX13MCA06
Page 18
Sub: Software Design Laboratory Sub Code:13MCA56 Se*ue!#e D"'r"m:
&ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 19
Sub: Software Design Laboratory Sub Code:13MCA56 &o+e: &urre!t&o!+to!D$pl".,"("
import ,aa.util.6bserable9 import ,aa.util.6bserer9 public class CurrentCondition"ispla$ implements 6bserer = priate /loat temperature9 6bserable obserable9 public CurrentCondition"ispla$6bserable obserable7 = this.obserable > obserable9 obserable.add6bsererthis79 ? public oid update6bserable obs0 6b,ect ar+7 = i/obs instanceo/ (eather"ata7 = (eather"ata weather"ata >(eather"ata7obs9 this.temperature > weather"ata.+et:emprature79 displa$79 ? ? public oid displa$7 = S$stem.out.printlnDCurrent conditions D temperature DF de+rees D79? ?
?
USN: 2VX13MCA06
Page 20
Sub: Software Design Laboratory Sub Code:13MCA56
Fore&"$tD$pl".,"("
import ,aa.util.6bserable9 import ,aa.util.6bserer9 public class ForeCast"ispla$ implements 6bserer = priate /loat currentPressure > 2&.&2/0 lastPressure9 public ForeCast"ispla$6bserable obserable7 = obserable.add6bsererthis79 ? public oid update6bserable obserable0 6b,ect ar+7 = i/obserable instanceo/ (eather"ata7 = (eather"ata weather"ata >(eather"ata7obserable9 lastPressure > currentPressure9 currentPressure > weather"ata.+etPressure79 displa$79 ? ? public oid displa$7 = S$stem.out.printDForecast D79 i/currentPressure M lastPressure7 = S$stem.out.printlnDmproin+ weather on the wa$ND79 USN: 2VX13MCA06
Page 21
Sub: Software Design Laboratory Sub Code:13MCA56 ? else i/currentPressure >> lastPressure7 = S$stem.out.printlnD)ore o/ the sameD79 ? else i/currentPressure O lastPressure7 = S$stem.out.printlnD(atch out /or cooler0 rain$ weatherD79 ? ? ? 9e"t-erD"t".,"("
import ,aa.util.6bserable9 public class (eather"ata extends 6bserable = priate /loat temperature0 pressure9 public /loat +et:emprature7 = return temperature9 ? public /loat +etPressure7 = return pressure9 ? public oid set)easurementsChan+ed7 = setChan+ed79 USN: 2VX13MCA06
Page 22
Sub: Software Design Laboratory Sub Code:13MCA56 noti/$6bserers79 ? public oid set)easurements/loat temp0 /loat hum0 /loat prs7 = temperature>temp9 pressure>prs9 set)easurementsChan+ed79 ? ? 9e"t-erSt"to!.,"("
public class (eatherStation = public static oid mainStrin+B ar+s7 = (eather"ata weather"ata > new (eather"ata79 CurrentCondition"ispla$ currentConditions > new CurrentCondition"ispla$weather"ata79 ForeCast"ispla$ /orecast"ispla$ > new ForeCast"ispla$weather"ata79 weather"ata.set)easurements%'0 !50 3'.4/79 weather"ata.set)easurements%20 #'0 2&.2/79 weather"ata.set)easurements#%0 &'0 2&.2/79 ? ?
Output:7
Forecast mproin+ weather on the wa$N Current conditions %'.'F de+rees Forecast (atch out /or cooler0 rain$ weather Current conditions %2.'F de+rees Forecast )ore o/ the same Current conditions #%.'F de+rees USN: 2VX13MCA06
Page 23
Sub: Software Design Laboratory Sub Code:13MCA56
) &omm"!+ D:he Command Processor desi+n pattern separates the reuest /or a serice /rom its execution. command processor component mana+es reuests as separa te ob,ects0 schedules their execution0 and proides additional serices such as the storin+ o/ reuest ob,ects /or later undo.D Problem
-
Chan+es to the user inter/ace should be possible at runtime Leed to issue reuests to ob,ects without nowin+ an$thin+ about the operation bein+ reuested or the receier o/ the reuest. Soluto! Reuest is encapsulated into ob,ects. (heneer a user calls a speci/ic /unction o/ the application0 the reuest is turned into a command ob,ect. :he Command Processor pattern illustrates more speci/icall$ how command ob,ects are mana+ed. central component o/ the Command Processor pattern description taes care o/ all command ob,ects. :he command processor schedules the execution o/ commands0 ma$ store them /or later undo0 and ma$ proide other serices such as lo++in+ the seuence o/ commands /or testin+ purposes. Each command ob,ect dele+ates the execution o/ its tas to supplier components within the /unctional core o/ the application. P"rt#p"!t$ &omm"!+ "eclares and inter/ace /or executin+ an operation &o!#rete&omm"!+: "e/ines a bindin+ between a Receier ob,ect and an action. mplements
execute7 b$ inoin+ the correspondin+ 6perations7 on Receier. &le!t creates a ConcreteCommand ob,ect and sets its receier. I!(oer: ss the command to carr$ out the reuest. Re#e(er: nows how to per/orm the operations associated with carr$in+ out a reuest Ex"mple: Simple switch
Switch is con/i+ured with 2 commands 6L and 6FF. Switch can be used with an$ deice0 not ,ust a li+ht but also with other deices lie /an0 :J0 etc.
USN: 2VX13MCA06
Page 24
Sub: Software Design Laboratory Sub Code:13MCA56
U$e &"$e D"'r"m: A#tor: Gser0 who is usin+ s$stem. U$e&"$e: Functionalities proided b$ the s$stem lie0 li+ht 6L Q 6FF0 /an 6L Q 6FF
A#t(t D"'r"m:
USN: 2VX13MCA06
Page 25
Sub: Software Design Laboratory Sub Code:13MCA56
&l"$$ D"'r"m:
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 26
Sub: Software Design Laboratory Sub Code:13MCA56
&ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 27
Sub: Software Design Laboratory Sub Code:13MCA56
&o+e: &omm"!+.,"("
public inter/ace Command = public oid execute79 ? L'-to%%#omm"!+.,"("
public class @i+ht6//Command implements Command = @i+ht li+ht9 public @i+ht6//Command@i+ht li+ht7 = this.li+ht > li+ht9 ? public oid execute7 = li+ht.o//79 ? ? L'-to!#omm"!+.,"("
public class @i+ht6nCommand implements Command USN: 2VX13MCA06
Page 28
Sub: Software Design Laboratory Sub Code:13MCA56 = @i+ht li+ht9 public @i+ht6nCommand@i+ht li+ht7 = this.li+ht > li+ht9 ? public oid execute7 = li+ht.on79 ? ? Remotelo"+er.,"("
public class Remote@oader = public static oid mainStrin+B ar+s7 = RemoteControl remoteControl > new RemoteControl79 @i+ht liin+Room@i+ht > new @i+htD@iin+ RoomD79 @i+ht itchen@i+ht > new @i+htDitchenD79 @i+ht6nCommand liin+Room@i+ht6n > new @i+ht6nCommandliin+Room@i+ht79 @i+ht6//Command liin+Room@i+ht6// > new @i+ht6//Commandliin+Room@i+ht79 @i+ht6nCommand itchen@i+ht6n>new @i+ht6nCommanditchen@i+ht79 @i+ht6//Command itchen@i+ht6//>new @i+ht6//Commanditchen@i+ht79 remoteControl.setCommand'0 liin+Room@i+ht6n0liin+Room@i+ht6//79 remoteControl.setCommand10 itchen@i+ht6n0 itchen@i+ht6//79 S$stem.out .printlnremoteControl79 remoteControl.on;utton(asPushed'79 remoteControl.o//;utton(asPushed'79 remoteControl.on;utton(asPushed179 remoteControl.o//;utton(asPushed179 remoteControl.on;utton(asPushed279 remoteControl.o//;utton(asPushed279 ? ?
Remote#o!trol.,"("
public class RemoteControl USN: 2VX13MCA06
Page 29
Sub: Software Design Laboratory Sub Code:13MCA56 = CommandB onCommands0 o//Commands9 public RemoteControl7 = onCommands > new CommandB39 o//Commands > new CommandB39 Command noCommand > new Receier79 /orint i > '9 i O 39 i7 = onCommandsBi > noCommand9o//CommandsBi > noCommand9 ? ? public oid setCommandint slot0 Command onCommand0 Command o//Command7 = onCommandsBslot > onCommand9 o//CommandsBslot > o//Command9 ? public oid on;utton(asPushedint slot7 = onCommandsBslot.execute79 ? public oid o//;utton(asPushedint slot7 = o//CommandsBslot.execute79 ? ? L'-t.,"("
public class @i+ht = Strin+ location > DD9 public @i+htStrin+ location7 = this.location > location9 ? public oid on7 = S$stem.out.printlnlocation D li+ht is onD79 ? public oid o//7 = S$stem.out.printlnlocation D li+ht is o//D79 ? ? USN: 2VX13MCA06
Page 30
Sub: Software Design Laboratory Sub Code:13MCA56
Re#e(er.,"("
public class Receier implements Command = public oid execute7 = ? ?
Output
RemoteControl%14b5b/# @iin+ Room li+ht is on @iin+ Room li+ht is o// itchen li+ht is on itchen li+ht is o//
;) For6"r+er7Re#e(er D:he Forwarder-Receier desi+n pattern proides transparent interprocess communication /or so/tware s$stems with a peer-to-peer interaction model. t introduces /orwarders and receiers to decouple peers /rom the underl$in+ communication mechanisms.D P"rt#p"!t$ For6"r+er:
-
tIs a components send messa+es across process boundaries. /orwarder proides a +eneral inter/ace that is an abstraction o/ a particular PC mechanism0
-
and includes /unctionalit$ /or marshalin+ and delier$ o/ messa+es. t contains a mappin+ /rom names to ph$sical addresses. (hen a /orwarder sends a messa+e to a remote peer0 it determines the ph$sical location o/
-
the recipient b$ usin+ its name-to-address mappin+. n the transmitted messa+e the /orwarder speci/ies the name o/ its own peer0 so that the remote peer is able to send a response to the messa+e ori+inator.
Re#e(er
-
tIs a component responsible /or receiin+ messa+es. receier o//ers a +eneral inter/ace that is an abstraction o/ a particular PC mechanism. t includes /unctionalit$ /or receiin+ and unmarshalin+ messa+es.
USN: 2VX13MCA06
Page 31
Sub: Software Design Laboratory Sub Code:13MCA56 Ex"mple: pplications /or the mana+ement o/ computer networs U$e &"$e D"'r"m: A#tor:
Peer is the actor9 he is interactin+ with the s$stem /or sendin+ and receiin+ the messa+e. U$e &"$e:
Send messa+e and receie messa+e are the two /unctionalities so the$ are the use cases here.
A#t(t D"'r"m:
USN: 2VX13MCA06
Page 32
Sub: Software Design Laboratory Sub Code:13MCA56
USN: 2VX13MCA06
Page 33
Sub: Software Design Laboratory Sub Code:13MCA56 &l"$$ D"'r"m:
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 34
Sub: Software Design Laboratory Sub Code:13MCA56
&ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 35
Sub: Software Design Laboratory Sub Code:13MCA56
&o+e: USN: 2VX13MCA06
Page 36
Sub: Software Design Laboratory Sub Code:13MCA56 E!tr.,"("
class Entr$ = priate Strin+ destinationd9 QQ tar+et machine priate int portLr9 QQ socet port public Entr$Strin+ the"est0 int theport7 = destinationd > the"est9 portLr > theport9 ? public Strin+ dest7 = return destinationd9 ? public int port7 = return portLr9 ? ?
For6"r+er.,"("
import ,aa.io.A9 import ,aa.net.Socet9 public class Forwarder = public Serer Forwarder9 priate Socet s9 USN: 2VX13MCA06
Page 37
Sub: Software Design Laboratory Sub Code:13MCA56 priate 6utputStream oStr9 priate Strin+ m$Lame9 priate Re+istr$ re+9 public ForwarderStrin+ theLame0Re+istr$ re+7 = m$Lame > theLame9 this.re+>re+9 ? priate b$teB marshal)essa+e the)s+7 = return the)s+.data.+et;$tes79 ? priate oid delierStrin+ the"est0 b$teB data7 = tr$ = Entr$ entr$ > re+.+etthe"est79 s > new Socetentr$.dest7 0entr$.port779 oStr > s.+et6utputStream79 oStr.writedata79 oStr./lush79 oStr.close79 s.close79 ? catch6Exception e7 = S$stem.out.printlnDForerrorDe79 USN: 2VX13MCA06
Page 38
Sub: Software Design Laboratory Sub Code:13MCA56 ? ? public oid send)s+Strin+ the"est 0 )essa+e the)s+7 = delierthe"est0 marshalthe)s+779 ? ?
Me$$"'e.,"("
class )essa+e = public Strin+ sender9 public Strin+ data9 public )essa+eStrin+ thesender0 Strin+ raw"ata7 = sender > thesender9 data > raw"ata9 ? ?
Re#e(er.,"("
import ,aa.io.6Exception9 import ,aa.io.nputStream9 import ,aa.net.SererSocet9 import ,aa.net.Socet9 public class Receier = USN: 2VX13MCA06
Page 39
Sub: Software Design Laboratory Sub Code:13MCA56 priate SererSocet srS9 priate Re+istr$ re+9 priate Socet s9 priate nputStream iStr9 priate Strin+ m$Lame9 public ReceierStrin+ theLame0Re+istr$ re+7 = m$Lame > theLame9 this.re+>re+9 tr$ = Entr$ entr$ > re+.+etm$Lame79 srS > new SererSocetentr$.port779 S$stem.out.printlnDSerer startedD79 ? catchException e7 = e.printStac:race79 ? QQS$stem.out.printlnre+.+ettheLame7.port779 ? priate )essa+e unmarshalb$te B anarra$7 = return new )essa+em$Lame0new Strin+anarra$779 ?
priate b$teB receie7 USN: 2VX13MCA06
Page 40
Sub: Software Design Laboratory Sub Code:13MCA56 = int al9 b$te bu//er B > null9 tr$ = s > srS.accept79 iStr > s.+etnputStream79 al > iStr. read79 bu//er > new b$te Bal 9 iStr.readbu//er79 iStr.close79 s.close79 srS.close79 ? catch6Exception e7 = S$stem.out.printlnDErrorD e79 ? return bu//er9 ? public )essa+e receie)s+7 = return unmarshalreceie779 ? ?
Re'$tr.,"(" USN: 2VX13MCA06
Page 41
Sub: Software Design Laboratory Sub Code:13MCA56 import ,aa.util.A9 class Re+istr$ = priate Hashtable h:able > new Hashtable79 public oid putStrin+ thee$0 Entr$ theEntr$7 = h:able.putthee$0theEntr$79 ? public Entr$ +etStrin+ ae$7 = returnEntr$7 h:able.+etae$79 ? ? Ser(er.,"("
class Serer = Receier r9 Forwarder /9 static Re+istr$ re+>new Re+istr$79 public oid execute7 = )essa+e result > null9 r > new Receier DSererD0re+79 / > new ForwarderDSererD 0re+79 )essa+e ms+ > new )essa+eDSererD0D (E @@ RE S:@@ @JED79 /.send)s+DSererD0 ms+79 result > r.receie)s+79 USN: 2VX13MCA06
Page 42
Sub: Software Design Laboratory Sub Code:13MCA56 S$stem.out.println result.data.trim779 ? public static oid mainStrin+ ar+sB7 = Entr$ entr$ > new Entr$D12#.'.'.1D0 2&''79 re+.putDClientD0 entr$79 entr$ > new Entr$D12#.'.'.1D02&''79 re+.putDSererD0 entr$79 new Serer7.execute79 ? ?
Output:7
Serer started (E @@ RE S:@@ @JE
USN: 2VX13MCA06
Page 43
Sub: Software Design Laboratory Sub Code:13MCA56 <) &le!t7D$p"t#-er T:he Client-"ispatcher-Serer desi+n pattern introduces an intermediate la$er between clients and serers0 the dispatcher component. t proides location transparenc$ b$ means o/ a name serice0 and hides the details o/ the establishment o/ the communication connection between clients and serers.U Problem
(hen a so/tware s$stem uses serers distributed oer a networ it must proide a means /or communication between them. n man$ cases a connection between components ma$ hae to be established be/ore the communication can tae place0 dependin+ on the aailable communication /acilities. Howeer0 the core /unctionalit$ o/ the components should be separate /rom the details o/ communication mechanisms. Clients should not need to now where serers are located. :his allows $ou to chan+e the location o/ serers d$namicall$0 and proides resilience to networ or serer /ailures. Soluto!:
Proide a dispatcher component to act as an intermediate la$er between clients and serers. :he dispatcher implements a name serice that allows clients to re/er to serers b$ names instead o/ ph$sical locations0 thus proidin+ location transparenc$. n addition0 the dispatcher is responsible /or establishin+ the communication channel between a client and a serer. P"rt#p"!t$ T-e &le!t:
-
Per/orms domain-speci/ic tass. :he client accesses operations o//ered b$ serer. ;e/ore sendin+ a reuest to a serer0 the client ass the dispatcher /or a communication channel. :he client uses this channel to communicate with the serer.
T-e Ser(er:
-
serer proides a set o/ operations to clients. t either re+isters itsel/ or is re+istered with the dispatcher b$ its name and address. serer component ma$ be located on the same computer as a client0 or ma$ be reachable ia a networ.
T-e D$p"t#-er: USN: 2VX13MCA06
Page 44
Sub: Software Design Laboratory Sub Code:13MCA56 -
:he dispatcher o//ers /unctionalit$ /or establishin+ communication channels between clients and
-
serers. :o do this0 it taes the name o/ a serer component and maps this name to the ph$sical location
-
o/ the serer component. t establishes a communication lin to the serer usin+ the aailable communication mechanism
-
and returns a communication handle to the client. / the dispatcher cannot initiate a communication lin with the reuested serer0 it in/orms the
-
client about the error it encountered. :o proide its name serice0 the dispatcher implements /unctions /or re+isterin+ and locatin+ serers.
Ex"mple: so/tware s$stem /or the retrieal o/ new scienti/ic in/ormation.
:he in/ormation proiders are both on our local networ and distributed oer the world. :o access an indiidual in/ormation proider0 it is necessar$ to speci/$ its location and the serice to be executed. (hen an in/ormation proider receies a reuest /rom a client application0 it runs the appropriate serice and returns the reuested in/ormation to the client. U$e &"$e D"'r"m:
USN: 2VX13MCA06
Page 45
Sub: Software Design Laboratory Sub Code:13MCA56
A#t(t D"'r"m:
&l"$$ D"'r"m:
USN: 2VX13MCA06
Page 46
Sub: Software Design Laboratory Sub Code:13MCA56
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 47
Sub: Software Design Laboratory Sub Code:13MCA56
USN: 2VX13MCA06
Page 48
Sub: Software Design Laboratory Sub Code:13MCA56 &ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 49
Sub: Software Design Laboratory Sub Code:13MCA56
&o+e: &DS.,"("
public class C"S = public static "ispatcher disp > new "ispatcher79 public static oid mainStrin+B ar+s7 = Serice sl > new PrintSericeDprintSc1D0DsrlD79 Serice s2 > new PrintSericeDprintSc2D0Dsr2D79 Client client > new Client79 client.do:as79 ? ?
&le!t.,"("
public class Client = priate "ispatcher dispatcher9 priate PrintSerice printserice9 public oid do:as7 = Serice s9 tr$ = s > C"S.disp. locateSererDprintSc1D79 s.runSerice79 ? USN: 2VX13MCA06
Page 50
Sub: Software Design Laboratory Sub Code:13MCA56 catchLotFound n7 = S$stem.out.printlnDLot aailableD79 ? tr$ = s > C"S.disp.locateSererDprintSc2D79 s.runSerice79 ? catchLotFound n7 = S$stem.out.printlnDLot aailableD79 ? tr$ = s > C"S.disp.locateSererDdrawScD79 s.runSerice79 ? catchLotFound n7 = S$stem.out.printlnDLot aailableD79 ? ? ?
D$p"t#-er.,"("
import ,aa.util.A9 class LotFound extends Exception =? public class "ispatcher = priate Client client9 priate PrintSerice printserice9 Hashtable re+istr$ > new Hashtable79 Random rnd > new Random12345!79 QQ /or random access public oid re+isterSericeStrin+ sc0 Serice ob,7 USN: 2VX13MCA06
Page 51
Sub: Software Design Laboratory Sub Code:13MCA56 = Jector > Jector7 re+istr$.+etsc79 i/ >> null7 = > new Jector79 re+istr$.putsc0 79 ?
.addElementob,79 ? public Serice locateSererStrin+ sc7throws LotFound = Jector >Jector7 re+istr$.+etsc79 i/ >> null7 throw new LotFound79 i/.si8e7 >> '7 throw new LotFound79 return Serice7.elementtrnd.nextnt7 V .si8e779 ?
? Pr!tSer(#e.,"("
public class PrintSerice extends Serice = public PrintSericeStrin+ sc0 Strin+ sr7 = supersc0 sr79 ? public oid runSerice7 = S$stem.out.printlnDSerice D nameo/serice D b$ D nameo/serer79 USN: 2VX13MCA06
Page 52
Sub: Software Design Laboratory Sub Code:13MCA56 ? ? Ser(#e.,"("
public abstract class Serice = Strin+ nameo/serice0 nameo/serer9 QQ serice name0 serer name public SericeStrin+ sc0 Strin+ sr7 = nameo/serice > sc9 nameo/serer > sr9 C"S.disp.re+isterSericenameo/serice0this79 ? abstract public oid runSerice79 QQ serice proided ?
Output:7
Serice printSc1 b$ srl Serice printSc2 b$ sr2 Lot aailable
USN: 2VX13MCA06
Page 53
Sub: Software Design Laboratory Sub Code:13MCA56 =) Prox :he Prox$ desi+n pattern maes the clients o/ a component communicate with a representatie rather than to the component tsel/. ntroducin+ such a placeholder can sere man$ purposes0 includin+ enhanced e//icienc$0 easier access and protection /rom unauthori8ed access. Problem:
t is o/ten inappropriate to access a component directl$. (e do not want to hard-code its ph$sical location into clients0 and direct and unrestricted access to the component ma$ be ine//icient or een insecure. dditional control mechanisms are needed. Soluto!:
@et the client communicate with a representatie rather than the component itsel/. :his representatie-called a prox$-o//ers the inter/ace o/ the component but per/orms additional pre- and postprocessin+ such as access-control checin+ or main+ read-onl$ copies o/ the ori+inal. P"rt#p"!t$: Prox: ProxEm"lSer(#e Sub,e#t: Em"lSer(#e Re"l Sub,e#t: Re"lEm"lSer(#e Ex"mple:
ma+ine that we are creatin+ an pplication that is main+ use o/ e mail-serice such as send and receie email. ssumin+ that the Client pplication wont be alwa$s accessin+ the Email Serice0 the Email Serice is an ideal candidate to be modeled as a Jirtual Prox$. U$e &"$e D"'r"m:
USN: 2VX13MCA06
Page 54
Sub: Software Design Laboratory Sub Code:13MCA56
A#t(t D"'r"m:
USN: 2VX13MCA06
Page 55
Sub: Software Design Laboratory Sub Code:13MCA56
&l"$$ D"'r"m:
USN: 2VX13MCA06
Page 56
Sub: Software Design Laboratory Sub Code:13MCA56
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 57
Sub: Software Design Laboratory Sub Code:13MCA56
&ommu!#"to! D"'r"m: USN: 2VX13MCA06
Page 58
Sub: Software Design Laboratory Sub Code:13MCA56
USN: 2VX13MCA06
Page 59
Sub: Software Design Laboratory Sub Code:13MCA56 &o+e: Appl#"to!.,"("
public class pplication = public E)ailSerice locateE)ailSerice7 = return new Prox$E)ailSerice79 ? ? Appl#"to!&le!t.,"("
public class pplicationClient = public static oid mainStrin+B ar+s7 = pplication application > new pplication79 E)ailSerice emailSerice > application.locateE)ailSerice79 emailSerice.send)ailDabcd+mail.comD0 DHelloD0 D test mailD79 emailSerice.receie)ailDabcd+mail.comD79 ? ? EM"lSer(#e.,"("
public inter/ace E)ailSerice = public oid send)ailStrin+ receier0 Strin+ sub,ect0 Strin+ text79 public oid receie)ailStrin+ receier79 ? USN: 2VX13MCA06
Page 60
Sub: Software Design Laboratory Sub Code:13MCA56 ProxEM"lSer(#e.,"("
public class Prox$E)ailSerice implements E)ailSerice = priate RealE)ailSerice emailSerice9 public oid receie)ailStrin+ receier7 = i/emailSerice >> null7 = emailSerice > new RealE)ailSerice79 ? emailSerice.receie)ailreceier79 ? public oid send)ailStrin+ receier0 Strin+ sub,ect0 Strin+ text7 = i/emailSerice >> null7 = emailSerice > new RealE)ailSerice79 ? emailSerice.send)ailreceier0 sub,ect0 text79 ? ?
USN: 2VX13MCA06
Page 61
Sub: Software Design Laboratory Sub Code:13MCA56
Re"lEM"lSer(#e.,"("
public class RealE)ailSerice implements E)ailSerice = public oid send)ailStrin+ receier0 Strin+ sub,ect0 Strin+ text7 = S$stem.out.printlnDSendin+ mail to D receier DD D with sub,ect D sub,ect DD D and messa+e D text DD79 ? public oid receie)ailStrin+ receier7 = S$stem.out.printlnDReceiin+ mail /rom D receier DD79 ? ?
Output:7
Sendin+ mail to abcd+mail.com with sub,ect Hello and messa+e test mail Receiin+ mail /rom abcd+mail.com
USN: 2VX13MCA06
Page 62
Sub: Software Design Laboratory Sub Code:13MCA56 0) F">"+e se+ment o/ the client communit$ needs a simpli/ied inter/ace to the oerall /unctionalit$ o/ a complex subs$stem. FaWade de/ines a hi+her-leel inter/ace that maes the subs$stem easier to use. Problem:
n almost eer$ so/tware s$stem ob,ects that are composed o/ other ob,ects exist.
Soluto!:
-
Structurin+ a s$stem into subs$stems helps reduce complexit$ Subs$stems are +roups o/ classes0 or +roups o/ classes and other subs$stems :he inter/ace exposed b$ the classes in a subs$stem or set o/ subs$stems can become uite
-
complex 6ne wa$ to reduce this complexit$ is to introduce a /acade ob,ect that proides a sin+le0 simpli/ied inter/ace to the more +eneral /acilities o/ a subs$stem
P"rt#p"!t$: F"#"+e: 4omeT-e"treF"#"+e Sub$$tem$: Ampl%er? D(+Pl"er? Pro,e#tor Ex"mple: Home :heater S$stem
:he home theater s$stem will consist o/ di//erent components lie Pro,ector0 to pro,ect the moie on screen0 "J" Pla$er to pla$ the moie0 mpli/ier to control the sound e//ects. For watchin+ a moie the user starts switchin+ on each component one b$ one and en,o$s the moie. /ter watchin+ the moie heQshe will hae to switch o// each deice. ;ut now the user is /ed up o/ this process and donIt want to start switchin+ on each d eice b$ him. He wants to control all the deices usin+ sin+le remote so that when he wants to watch a moie he can use this remote to start watchin+ moie and in the end he can close all the deices b$ one command usin+ same remote.
USN: 2VX13MCA06
Page 63
Sub: Software Design Laboratory Sub Code:13MCA56 U$e &"$e D"'r"m:
A#t(t D"'r"m:
USN: 2VX13MCA06
Page 64
Sub: Software Design Laboratory Sub Code:13MCA56
&l"$$ D"'r"m:
USN: 2VX13MCA06
Page 65
Sub: Software Design Laboratory Sub Code:13MCA56
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 66
Sub: Software Design Laboratory Sub Code:13MCA56
USN: 2VX13MCA06
Page 67
Sub: Software Design Laboratory Sub Code:13MCA56 &ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 68
Sub: Software Design Laboratory Sub Code:13MCA56
&o+e: Ampl%er.,"("
public class mpli/ier = Strin+ description9 "dPla$er dd9 public mpli/ierStrin+ description7 = this.description > description9 ? public oid on7 = S$stem.out.printlndescription D onD79 ? public oid o//7
USN: 2VX13MCA06
Page 69
Sub: Software Design Laboratory Sub Code:13MCA56 = S$stem.out.printlndescription D o//D79 ? public Strin+ toStrin+7 = return description9 ? ?
D(+Pl"er.,"("
public class "dPla$er = Strin+ description0 moie9 mpli/ier ampli/ier9 public "dPla$erStrin+ description0 mpli/ier ampli/ier7 = this.description > description9 this.ampli/ier > ampli/ier9 ? public oid on7 = S$stem.out.printlndescription D onD79 ? public oid o//7 USN: 2VX13MCA06
Page 70
Sub: Software Design Laboratory Sub Code:13MCA56 = S$stem.out.printlndescription D o//D79 ? public oid pla$Strin+ moie7 = this.moie > moie9 S$stem.out.printlndescription D pla$in+ DD moie DDD79 ? public oid stop7 = S$stem.out.printlndescription D stopped DD moie DDD79 ? public Strin+ toStrin+7 = return description9 ? ? 4omeT-e"terF"#"+e.,"("
public class Home:heaterFacade = mpli/ier amp9 "dPla$er dd9 Pro,ector pro,ector9 Home:heaterFacadempli/ier amp0 "dPla$er dd0 Pro,ector pr,7 = this.amp > amp9 this.dd > dd9 USN: 2VX13MCA06
Page 71
Sub: Software Design Laboratory Sub Code:13MCA56 this.pro,ector > pr,9 ? public oid watch)oieStrin+ moie7 = S$stem.out.printlnD
public oid end)oie7 = S$stem.out.printlnDShuttin+ moie theater down...D79 pro,ector.o//79 amp.o//79 dd.stop79 dd.o//79 ? ?
Pro,e#tor.,"("
public class Pro,ector = Strin+ description9 "dPla$er ddPla$er9 USN: 2VX13MCA06
Page 72
Sub: Software Design Laboratory Sub Code:13MCA56 public Pro,ectorStrin+ description0 "dPla$er ddPla$er7 = this.description > description9 this.ddPla$er > ddPla$er9 ? public oid on7 = S$stem.out.printlndescription D onD79 ? public oid o//7 = S$stem.out.printlndescription D o//D79 ? public oid t)ode7 = S$stem.out.printlndescription D in t modeD79 ? public Strin+ toStrin+7 = return description9 ? ?
U$er.,"("
public class Gser = public static oid mainStrin+B ar+s7 USN: 2VX13MCA06
Page 73
Sub: Software Design Laboratory Sub Code:13MCA56 = mpli/ier amp > new mpli/ierD:op-6-@ine mpli/ierD79 "dPla$er dd > new "dPla$erD:op-6-@ine "J" Pla$erD0 amp79 Pro,ector pro,ector > new Pro,ectorD:op-6-@ine Pro,ectorD0 dd79 Home:heaterFacade home:heater > new Home:heaterFacadeamp0 dd0 pro,ector79 home:heater.watch)oieD(rite $our own moie name NND79 home:heater.end)oie79 ? ?
Output:7
USN: 2VX13MCA06
Page 74
Sub: Software Design Laboratory Sub Code:13MCA56 ) Polmorp-$m Problem:
How to handle alternaties based on t$pe How to create plu++able so/tware components Soluto!:
(hen related alternaties or behaiors ar$ b$ t$peclass70 assi+n responsibilit$ /or the behaior usin+ pol$morphic operations to the t$pes /or which the behaior aries. P"rt#p"!t$: T-eI!ter%"#e:
:his inter/ace will proide the behaior which aries accordin+ to the class t$pe. ll classes implementin+ this inter/ace will write the method accordin+l$. Impleme!ter:
:he classes will implement the operations /rom the inter/ace as per the pol$morphic nature. Ex"mple: pplication to draw di//erent shapes
:he user will use this application to draw shapes lie Circle0 Rectan+le0 trian+le. t a later sta+e we can hae some more shapes be added to the application. U$e #"$e +"'r"m
USN: 2VX13MCA06
Page 75
Sub: Software Design Laboratory Sub Code:13MCA56
A#t(t D"'r"m:
+r"6 &r#le
+r"6 Tr"!'le
+r"6 Re#t"!'le
&l"$$ D"'r"m:
USN: 2VX13MCA06
Page 76
Sub: Software Design Laboratory Sub Code:13MCA56
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 77
Sub: Software Design Laboratory Sub Code:13MCA56
USN: 2VX13MCA06
Page 78
Sub: Software Design Laboratory Sub Code:13MCA56
&ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 79
Sub: Software Design Laboratory Sub Code:13MCA56
&o+e: S-"pe.,"("
public inter/ace Shape = public oid draw79 ?
&r#le.,"("
public class Circle implements Shape = public oid draw7 = S$stem.out.printlnD:his is a CircleD79 ? ? Re#t"!'le.,"("
public class Rectan+le implements Shape = public oid draw7 = S$stem.out.printlnD:his is a Rectan+leD79 ? ?
USN: 2VX13MCA06
Page 80
Sub: Software Design Laboratory Sub Code:13MCA56
Tr"!'le.,"("
public class :rian+le implements Shape = public oid draw7 = S$stem.out.printlnD:his is a :rian+leD79 ? ? Dr"6S-"pe.,"("
public class "rawShape = priate static Shape shape9 public static oid mainStrin+ aB7 = S$stem.out.printlnDnoin+ CircleIs draw method..D79 shape > new Circle79 shape.draw79 S$stem.out.printlnDnoin+ Rectan+leIs draw method..D79 shape > new Rectan+le79 shape.draw79 S$stem.out.printlnDnoin+ :rian+leIs draw method..D79 shape > new :rian+le79 shape.draw79 ? ? USN: 2VX13MCA06
Page 81
Sub: Software Design Laboratory Sub Code:13MCA56
Output:7
noin+ CircleIs draw method.. :his is a Circle noin+ Rectan+leIs draw method.. :his is a Rectan+le noin+ :rian+leIs draw method.. :his is a :rian+le
USN: 2VX13MCA06
Page 82
Sub: Software Design Laboratory Sub Code:13MCA56 12) 9-ole7P"rt Helps with the a++re+ation o/ components parts7 that to+ether /orm a semantic unit whole7. "irect access to the Parts is not possible. (hole-Part lets clients treat indiidual ob,ects and compositions o/ ob,ect uni/orml$ Problem: •
So/tware s$stem ob,ects that are composed o/ other ob,ects exist.
•
complex ob,ect should either be decomposed into smaller ob,ects0 or composed o/ existin+ ob,ects0 to support reusabilit$0 chan+eabilit$ and the recombination o/ the constituent ob,ects in other t$pes o/ a++re+ate.
•
Clients should see the a++re+ate ob,ect as an atomic ob,ect that does not allow an$ direct access to its constituent parts.
Soluto!: •
ntroduce a component that encapsulates smaller ob,ects0 and preents clients /rom accessin+ these constituent parts directl$.
•
"e/ine an inter/ace /or the a++re+ate that is the onl$ means o/ access to the /unctionalit$ o/ the encapsulated ob,ects0 allowin+ the a++re+ate to appear as a semantic unit.
U$e#"$e D"'r"m
USN: 2VX13MCA06
Page 83
Sub: Software Design Laboratory Sub Code:13MCA56
A#t(t D"'r"m:
&l"$$ D"'r"m:
USN: 2VX13MCA06
Page 84
Sub: Software Design Laboratory Sub Code:13MCA56
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 85
Sub: Software Design Laboratory Sub Code:13MCA56 &ommu!#"to! D"'r"m:
USN: 2VX13MCA06
Page 86
Sub: Software Design Laboratory Sub Code:13MCA56
&o+e: Appl#"to!.@"("
import ,aa.util.A9 QQcollection seeral colddrins7 as a whole pac7 public class pplication = public pplication7 = ? Suppress(arnin+sDunchecedD7 public static oid mainStrin+B ar+s7 = @ist items > new rra$@ist79 items.addnew Cold"rinDPepsiD0Dcold drinD01'779 items.addnew Cold"rinDCoeD0Dcold drinD02'779 items.addnew Cold"rinDma8aD0Dcold drinD015779 Cold"rinFamil$Pac /amil$Pac > new Cold"rinFamil$Pacitems79 S$stem.out .println/amil$Pac.+etPrice779 @ist item2s > new rra$@ist79 item2s.addnew Cold"rinDPepsiD0Dcold drinD01'779 item2s.addnew Cold"rinDCoeD0Dcold drinD02'779 item2s.addnew Cold"rinDma8aD0Dcold drinD015779 item2s.addnew Cold"rinDPepsiD0Dcold drinD01'779 item2s.addnew Cold"rinDCoeD0Dcold drinD02'779 item2s.addnew Cold"rinDma8aD0Dcold drinD015779 Cold"rinPart$Pac part$Pac > new Cold"rinPart$Pacitem2s79 S$stem.out .printlnpart$Pac.+etPrice779 ? ?
USN: 2VX13MCA06
Page 87
Sub: Software Design Laboratory Sub Code:13MCA56
&ol+Dr!.@"("
public class Cold"rin implements tem = priate Strin+ itemLame9 priate Strin+ iem"esc9 priate double price9 public Cold"rinStrin+ itemLame0 Strin+ desc0 double price7 = this.itemLame>itemLame9 this.iem"esc> desc9 this.price > price9 ? public double +etPrice7 = return price9 ? QApublic oid settemLameStrin+ itemLame7 = this.itemLame > itemLame9 ?AQ public Strin+ +ettemLame7 = return itemLame9 ? QApublic oid setem"escStrin+ iem"esc7 = this.iem"esc > iem"esc9 ?AQ public Strin+ +etem"esc7 = return iem"esc9 ? ? USN: 2VX13MCA06
Page 88
Sub: Software Design Laboratory Sub Code:13MCA56
&ol+Dr!F"mlP"#.@"("
import ,aa.util.@ist9 public class Cold"rinFamil$Pac extends Composite = public Cold"rinFamil$Pac@ist items7 = super.addllitems79 ? public double +etPrice7 = return super.+etPrice7 - super.+etPrice7A.159 QQ +et 15V discount on /amil$ pac ? ? &ol+Dr!P"rtP"#.@"(" import ,aa.util.@ist9 public class Cold"rinPart$Pac extends Composite = public Cold"rinPart$Pac@ist items7 = super.addllitems79 ? public double +etPrice7 = return super.+etPrice7 - super.+etPrice7A.259 QQ +et 25V discount on /amil$ pac ? ? &ompo$te.@"(" import ,aa.util.A9 public abstract class Composite implements tem = QAA A associates O=demp.composite.tem?M AQ @istOtemM items > new rra$@istOtemM79 public oid addtem itm7 = USN: 2VX13MCA06
Page 89
Sub: Software Design Laboratory Sub Code:13MCA56
items.additm79 ?
public oid remoetem itm7 = items.remoeitm79 ? public oid addll@ist lst7 = items.addlllst79 ? public double +etPrice7 = double sum>'9 /or tem i items7 = sum > i.+etPrice79 ? return sum9 ?
?
Item.@"("
public inter/ace tem = public double +etPrice79 ?
Output 3%.25 !1.5
USN: 2VX13MCA06
Page 90
Sub: Software Design Laboratory Sub Code:13MCA56 11) M"$ter7Sl"(e :he )aster-Slae pattern is o/ten used /or multi-threaded applications in which man$ instances o/ the same problem must be soled. :he master creates and launches slaes to sole these instances in DparallelD. (hen all o/ the slaes hae /inished0 the master harests the results.
Ex"mple:
multithreaded implementation o/ an$ paralleli8ed diide-and-conuer al+orithm Soluto!:
)aster slae desi+n pattern uses diide and con uer concept 0 in this pattern instance one master and n X number slaes0 where master diide s the responsibilities to slaes0 here both master and slaes uses the same resources0 in the pro+ram we are usin+ the multithreadin+ concept )ultithreadin+ a. create a thread b$ extendin+ T-re"+ class b. then ru!) method 0 which acts as the threads entr$ point #. $t"rt) method inoes the execution o/ the thread0 b$ callin+ ru!)
U$e&"$e D"'r"m
USN: 2VX13MCA06
Page 91
Sub: Software Design Laboratory Sub Code:13MCA56 A#t(t D"'r"m
&l"$$ D"'r"m:
USN: 2VX13MCA06
Page 92
Sub: Software Design Laboratory Sub Code:13MCA56
Se*ue!#e D"'r"m:
USN: 2VX13MCA06
Page 93
Sub: Software Design Laboratory Sub Code:13MCA56
&omm u!#"to! D"'r"m:
USN: 2VX13MCA06
Page 94
Sub: Software Design Laboratory Sub Code:13MCA56 &o+e: Te$tM"$ter.,"("
public class :est)aster :est)aster = public static oid mainStrin+B ar+s7 ar+s7 = )aster master > new )aster79 master.run79 ? ? M"$ter. @"(":
public class )aster = priate int slaeCount > 29 priate Resource res > new Resource79 priate SlaeB slaes > new SlaeBslaeCount9 public oid run7 = QQ create slaes /orint i > '9 i O slaeCount9 i7 = slaesBi > new Slaeres79 ?
QQ start slaes /orint i > '9 i O slaeCount9 i7 = slaesBi.start79 ? QQ wait /or slaes to die /orint i > '9 i O slaeCount9 i7 = tr$ = QQis used to wait /or a thread to /inish and terminate slaesBi.,oin79 ? catchnterruptedException ie7 = S$stem.err.printlnie.+et)essa+e779 ? /inall$ =
USN: 2VX13MCA06
Page 95
Sub: Software Design Laboratory Sub Code:13MCA56 S$stem.out.printlnslaesBi.+etLame7 S$stem.out.printlnslaesBi .+etLame7 D has diedD79 ? ? S$stem.out.printlnD:he master will now die ... D79 ? ? Sl"(e.@"(": class Slae extends :hread = priate Resource sharedResource9 priate boolean done > /alse9 QQconstructor public SlaeResource rcs7 = sharedResource > rcs9 ?
public oid halt7 = done > true9 ? protected boolean tas7 = QQ access sharedResource here int status > sharedResource.incStatus79 return status M> 2'79 ? public oid run7 = while done N> true7 = done > tas79 QQ be cooperatie tr$ = :hread.sleep5''79 ? QQ sleep /or 1 sec. catch Exception e7 = ? ? ? ?
USN: 2VX13MCA06
Page 96
Sub: Software Design Laboratory Sub Code:13MCA56
Re$our#$.@"(":
public class Resource = priate int status > '9 public s$nchroni8ed int incStatus7 = int local > status9 S$stem.out.printlnDstatus > D local79 local9 tr$ = :hread.sleep5'79 ? catchException e7 = ? status > local9 S$stem.out.printlnDnow status > D local79 return status9 ? ?
OUTPUT status > ' now status > 1 status > 1 now status > 2 status > 2 now status > 3 status > 3 now status > 4 status > 4 now status > 5 status > 5 now status > ! status > ! now status > # status > # now status > % status > % now status > & status > & now status > 1' USN: 2VX13MCA06
Page 97