You are on page 1of 79

SVETA KUMARI.

USN. No.:1NZ08MCA18

INTRODUCTION

The Three Models: It is often useful to model a system from three related but different viewpoints, each capturing important aspects of the system, but all required for a complete description. The class model represents the static, structural, data aspects of a system. The state model represents the temporal behavioral,control aspect of a system. The interaction model represents the collaboration of individual objects, the interaction aspects of a system. Pattern: A Pattern for software architecture describes a particular recurring design problem that arises in a specific design contexts, and presents a well proven generic scheme for its solution. !he solution scheme is specified by describing its constituents components, their responsibilities and relationships, and the ways in which they collaborate. Pattern Categories" !o refine classification, patterns are classified into three groups" Architectural patterns #esign Patterns

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Idioms An architectural pattern expresses a fundamental structural organi$ation schema for software system. It provides a set of predefined subsystems, specifies their relationships and includes rules and guidelines for organi$ing the relationships between them. A design pattern provides a schema for refining the subsystems or components of a software system, or the relationships between them. It describes a commonly recurring structure of communicating components that solve a general design problem within a particular context. An idiom is a low level pattern specific to a programming language. An idiom describes how to implement particular aspects of components or the relationships between them using the features of the given language.

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Lab Set EXPERT 1. Using the UML Dra ing Tool b! i"#le"enting the $ode in %a&a de"onstrate the E'#ert Design Pattern. An %xpert pattern is useful in maintaining encapsulation of information, it promotes low coupling and also provides highly cohesive classes which can cause a class to become excessively complex. Class Diagra":

Sales total : float getTotal()

ProductDescription price : float = 43.89f getPrice()

SaleCounter main()

Sales ine!tem "uantit# : int = $%% getSu&Total()

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

UseCase Diagra":

Product Specification

Customer C'oose Product (it' )t#

S'op -eeper *a+e Su&total of ineitem

Total ,ill

Se()en$e Diagra":

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Sales $: Su&total

Sales ine !tem

Product Specification

.: )t#

3: Price 4: description

/: 0PC

1: price(0PC) 2: Su&total

8: Total

Code I"#le"entatio Prod)$t Des$ri#tion.*a&a: import java.io.&' public class Product#escription ( private float price)*+.,-f' public float getPrice ./ ( return price' 0

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

SalesCo)nter.*a&a: import java.io.&' public class 1ale2ounter ( public static void main .1tring 34 args/ ( float total' 1ales s)new 1ales./' total)s.get!otal./' 1ystem.out.println .5!otal sale is" 56total/' 0 0

Sales .*a&a: import java.io.&' public class 1ales ( private float total' public float get!otal./ ( Product#escription pd)new Product#escription ./'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

1ales7ineItem sli)new 1ales7ineItem ./' total).pd.getPrice./&sli.get1ub!otal.//' return total' 0 0

SalesLineIte": public class 1ales7ineItem ( private int quantity)899'

public int get1ub!otal./ ( return quantity' 0 0

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

CONTROLLER +. Using the UML Dra ing Tool b! i"#le"enting the $ode in %a&a de"onstrate the Controller Design Pattern. !he Model View Controller architectural pattern divides an interactive application into three components. !he model contains the core functionality and data. :iews display information to the user. 2ontrollers handle user input. :iews and 2ontrollers together comprise the user interface. A change propagation mechanism ensures consistency between the user interface and the model. Class Diagra":
Calc3ie( !!4!T!5 635 07 : String = 8$8 reset() get0ser!nput() setTotal() S'o(7rror() add*ultiple istener() addClear istener() Calc3ie(() Calc*3C main() Calc*3C() 9t'eCalc3ie(

9t'eCalc*odel Calc*odel !4!T!5 635 07 : String = 8$8 m6Total : ,ig!nteger Calc*odel() reset() multipl#,#() set3alue() get3alue()

9t'eCalc*odel

CalcController CalcController()

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Se()en$e Diagra":
CalcController $: !n;o+es Calc3ie( Calc*odel

.: !n;o+es

3: :esets

4: Set ;alues

/: *ultiplies t'e ;alues

1: <&tains *odel 2: :eset

8: =ets 0ser input

9: Set Total

$%: *ultiplies listeners

$$: Clears t'e isteners

$.: Displa#s 7rror message

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Code I"#le"entataion: Cal$Model.*a&a: import java.math.;igInteger'

public class 2alc<odel ( private 1tring I=I!IA7>:A7?% ) 585' private ;igInteger m>total'

public 2alc<odel./ ( reset./' 0 public void reset./ ( m>total)new ;igInteger.I=I!IA7>:A7?%/' 0 public void multiply;y.1tring operand/ ( m>total ) m>total.multiply.new ;igInteger.operand//'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

0 public void set:alue.1tring value/ ( m>total ) new ;igInteger.value/' 0 public 1tring get:alue./ ( return m>total.to1tring./' 0 0

Cal$Controller.*a&a: import java.awt.event.&'

public class 2alc2ontroller ( private 2alc<odel m>model' private 2alc:iew m>view'

2alc2ontroller.2alc<odel model, 2alc:iew view/ ( m>model ) model' m>view ) view' view.add<ultiply7istener.new <ultiply7istener.//'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

view.add2lear7istener.new 2lear7istener.//' 0

class <ultiply7istener implements Action7istener ( public void actionPerformed.Action%vent e/ ( 1tring userInput ) 55' try ( userInput ) m>view.get?serInput./' m>model.multiply;y.userInput/' m>view.set!otal.m>model.get:alue.//' 0 catch.=umber@ormat%xception nfex/ ( m>view.1how%rror.5;ad Input"A56userInput65A5/' 0 0 0

class 2lear7istener implements Action7istener ( public void actionPerformed.Action%vent e/

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

( m>model.reset./' m>view.reset./' 0 0 0

Cal$,ie .*a&a: import java.awt.&' import javax.swing.&' import java.awt.event.&'

public class 2alc:iew extends B@rame ( private static final 1tring I=I!IA7>:A7?% ) 585' private B!ext@ield m>userInput!f)new B!ext@ield.C/' private B!ext@ield m>total!f ) new B!ext@ield.D9/' private B;utton m>multiply;tn ) new B;utton.5<ultiply5/' private B;utton m>clear;tn ) new B;utton.52lear5/' private 2alc<odel m>model'

2alc:iew.2alc<odel model/ ( m>model ) model'


07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

m>model.set:alue.I=I!IA7>:A7?%/' m>total!f.set!ext.m>model.get:alue.//' m>total!f.set%ditable.false/' BPanel content)new BPanel./' content.set7ayout.new @low7ayout.//'

content.add.new B7abel.5I=P?!5//' content.add.m>userInput!f/' content.add.m>multiply;tn/' content.add.new B7abel.5!otal5//' content.add.m>total!f/' content.add.m>clear;tn/'

this.set2ontentPane.content/' this.pacE./' this.set!itle.51imple 2alc><:25/' this.set#efault2loseFperation.B@rame.%GI!>F=>27F1%/' 0

public void reset./ ( m>total!f.set!ext.I=I!IA7>:A7?%/' 0

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public 1tring get?serInput./ ( return m>userInput!f.get!ext./' 0

public void set!otal.1tring new!otal/ ( m>total!f.set!ext.new!otal/' 0

public void 1how%rror.1tring err<sg/ ( BFptionPane.show<essage#ialog.this,err<sg/' 0

public void add<ultiply7istener.Action7istener mul/ ( m>multiply;tn.addAction7istener.mul/' 0

public void add2lear7istener.Action7istener cal/ ( m>clear;tn.addAction7istener.cal/' 0

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Cal$M,C.*a&a: import javax.swing.&'

public class 2alc<:2 ( public static void main.1tring34 args/ ( 2alc<odel model ) new 2alc<odel./' 2alc:iew view ) new 2alc:iew.model/' 2alc2ontroller controller ) new 2alc2ontroller.model,view/' view.set:isible.true/' 0 0

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

PU-LIS.ER/SU-SCRI-ER0O-SER,ER1 2. Using the UML Dra ing Tool b! i"#le"enting the $ode in %a&a de"onstrate the Obser&er Design Pattern. !he Publisher-Subscriber design pattern helps to Eeep the state of cooperating components synchroni$ed. !o achieve this it enables one way propagation of changes" one publisher notifies any number of subscribers about changes to its state. Class Diagra":
Concrete6Su&>ect name : String price : float Concrete6Su&>ect() get4ame() getPrice() setPrice() set4ame()

Price<&ser;er price : float Price<&ser;er() 0pdate()

Testo&ser;er 4ame<&ser;er name : String 4ame<&ser;er() 0pdate() main() Testo&ser;er()

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Se()en$e Diagra":
Test<&ser;er Concreat Su&>ect $.!n;o+es 4ame<&ser;er Price<&ser;er

..!n;o+es

3.in;o+es

4.respones

Code I"#le"entation:

Obser&er.*a&a:

import java.util.Fbservable' import java.util.Fbserver'

public class 2oncrete>1ubject extends Fbservable (

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

private 1tring name' private float price' public 2oncrete>1ubject.1tring name, float price/ ( this.name)name' this.price)price' 1ystem.out.println.52oncrete subject 2reated 5 6name6 5 at 5 6price/' 0 public 1tring get=ame./ ( return name' 0 public float getPrice./ ( return price' 0 public void setPrice.float price/ ( this.price)price' set2hanged./' notifyFbservers.new @loat.price//' 0 public void set=ame.1tring name/

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

( this.name)name' set2hanged./' notifyFbservers.name/' 0 0

Na"eObser&er.*a&a:

import java.util.Fbservable' import java.util.Fbserver'

public class =ameFbserver implements Fbserver ( private 1tring name' public =ameFbserver./ ( name)null' 1ystem.out.println.5=ame Fbserver 2reated" =ame is 56name/' 0 public void update.Fbservable obj, Fbject arg/ ( if.arg instanceof 1tring/ (

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

name).1tring/arg' 1ystem.out.println.5=ame Fbserver"=ame 2hanged to 56name6 5Hn5/' 0 else ( 1ystem.out.println.5=ameFbserver" =ot 2hangedHn5/' 0 0 0

Pri$eObser&er.*a&a:

import java.util.Fbservable' import java.util.Fbserver'

public class PriceFbserver implements Fbserver ( private @loat price' public PriceFbserver./ ( price)9f' 1ystem.out.println.5Price Fbserver 2reated is" 5 /'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

0 public void update.Fbservable obj, Fbject arg/ ( if.arg instanceof @loat/ ( price)..@loat/arg/.float:alue./' 1ystem.out.println.5HnPriceFbserver" price changed to 5 6price/' 0 else ( 1ystem.out.println.5HnPriceFbserver" =ot changed 5/' 0 0 0

TestObser&er.*a&a:

import java.io.&'

public class !estobserver ( public static void main.1tring arg34/ (

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

2oncrete>1ubject 2oncrete>1ubject.52ornflaEes5,8.D-f/'

new

=ameFbserver nameobs)new =ameFbserver./' PriceFbserver priceobs)new PriceFbserver./' s.addFbserver.nameobs/' s.addFbserver.priceobs/' s.set=ame.5Ioasted @laEes5/' s.setPrice.*8.C9f/' s.setPrice.-.C9f/' s.set=ame.51ugar5/' 0 0

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

COMM3ND 4.Using the UML Dra ing Tool b! i"#le"enting the $ode in %a&a de"onstrate the Co""and Design Pattern. !he Command Processor design pattern separates the request for a service from the execution. A command processor component manages requests as separate objects, schedules their execution, and provides additional services as the storing of request objects for later undo.
!tem*anager

Class Diagra":

Command !nterface e?ecute()

setCommand() process()

5ddCommand 5ddCommand() e?ecute()

DeleteCommand CommandTest DeleteCommand() e?ecute() main()

!tem categories : @as'*ap desc : String

Categor# items : @as'*ap desc : String Categor#() PAGE: getDesc() add() delete()

07MCA57 getDesc()
add() delete()

!tem()

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

D!na"i$s: !he following diagram shows a typical scenario of the 2ommand Processor pattern implementing an undo mechanism. A request to capitali$e a selected word arrives, is performed and then undone. !he following steps occur"

!he controller accepts the request from the user within its event loop and creates a Jcapitali$eK command object. !he controller transfers the new command object to the command processor for execution and further handling. !he command processor activates the execution of the command and stores it for later undo. !he capitali$e command retrieves the currently selected text from its supplier, stores the text and its position in the document, and asEs the supplier to actually capitali$e the selection. After accepting an undo request, the controller transfers this request to the command processor. !he command processor invoEes the undo procedure of the most recent command. !he capitali$e command resets the supplier to the previous state, by replacing the saved text in its original position.

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

If no further activity is required or possible of the command, the command processor deletes the command object.

Se()en$e Diagra":

Client

!n;o+es

Controller

7?ecutor

7?ecutor

Command Test

!tem *anager

Command !nterface

5dd Command

Delete Command

$: !n;o+es .: 7?ecutes 3: 5dd 4: Delete

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Code I"#le"entation: 3ddCo""and.*a&a import java.util.Lash<ap'

public class Add2ommand implements 2ommandInterface ( 2ategory cat' Item item' public Add2ommand.Item i, 2ategory c/ ( item)i' cat)c' 0

public void execute./ ( item.add.cat/' cat.add.item/' 0 0

Categor!.*a&a: import java.util.Lash<ap'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public class 2ategory ( private Lash<ap items' private 1tring desc'

public 2ategory.1tring s/ ( desc)s' items)new Lash<ap./' 0

public 1tring get#esc./ ( return desc' 0

public void add.Item i/ ( items.put.i.get#esc./,i/' 1ystem.out.println.5Item 56i.get#esc./65 has been added to the 56get#esc./65 2ategory5/' 0

public void delete.Item i/

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

( items.remove.i.get#esc.//' 1ystem.out.println.5Item 5 6i.get#esc./65 has been deleted from the 56get#esc./65 2ategory5/' 0 0

Co""andInter5a$e: public interface 2ommandInterface ( public void execute./' 0

DeleteCo""and: import java.util.Lash<ap'

public class #elete2ommand implements 2ommandInterface ( 2ategory cat' Item item'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public #elete2ommand.Item i, 2ategory c/ ( item)i' cat)c' 0

public void execute./ ( item.delete.cat/' cat.delete.item/' 0 0

Ite".*a&a: import java.util.Lash<ap'

public class Item ( private Lash<ap categories' private 1tring desc'

public Item.1tring s/ (
07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

desc)s' categories)new Lash<ap./' 0 public 1tring get#esc./ ( return desc' 0 public void add.2ategory cat/ ( categories.put.cat.get#esc./,cat/' 0

public void delete.2ategory cat/ ( categories.remove.cat.get#esc.//' 0 0

Ite"Manager.*a&a: import java.util.Lash<ap'

public class Item<anager ( 2ommandInterface command'


07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public void set2ommand.2ommandInterface c/ ( command)c' 0 public void process./ ( command.execute./' 0 0

Co""andTest.*a&a: import java.util.Lash<ap'

public class 2ommand!est ( public static void main.1tring34 args/ ( Item 2#)new Item.5A ;eautiful <ind5/' 2ategory cat2#)new 2ategory.52#5/' 2ommandInterface command)new Add2ommand.2#,cat2#/' Item<anager manager)new Item<anager./' manager.set2ommand.command/' manager.process./'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

2#)new Item.52lassical5/' cat2#)new 2ategory.52#5/' cat2#)new 2ategory.52#5/' command)new Add2ommand.2#,cat2#/' manager.set2ommand.command/' manager.process./' command)new #elete2ommand.2#,cat2#/' manager.set2ommand.command/' manager.process./' 0 0

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

63R73RDER/RECEI,ER C. ?sing the ?<7 #rawing !ool by implementing the code in Bava demonstrate the 6or ard/Re$ei&e Design Pattern. !he Forwarder-Receiver design pattern provides transparent

interprocess communication for software systems with a peer to peer interaction model. It introduces forwarders and receivers to decouple peers from the underlying communication mechanisms. Client Class Diagra":
:ecei;er m#4ame : String :ecei;er() un*ars'al() recei;e() recei;e*sg() main()

*essage sender : String data : String *essage() 9t'e:ecei;er 9t'eAor(arder

Aor(arder m#4ame : String Aor(arder() mars'al() deli;er() send*sg()

7ntr# destination!d : String port4r : int

Ser;er run() main() PAGE:

07MCA57
7ntr#() dest() port()

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

D!na"i$s: !he following scenario illustrates @orwarder Ieceiver structure. !wo peers P8 and PD communicate with each other. P8 uses a forwarder @orw8 and a receiver Iecv8. PD handles all messages transfers with a forwarder @orwD and a receiver IecvD"

P8 request a service from a remote peer PD. @or this purpose, it sends the request to its forwarder @orw8 and specifies the name of the recipient. @orw8 determines the physical location of the remote peer and marshals the message. @orw8 delivers the message to the remote receiver IecvD.

At some earlier time PD has requested its receiver IecvD to wait for an incoming request. =ow, IecvD receives the message arriving from @orw8. IecvD unmarshals the message and forwards it to its peer PD. <eanwhile, P8 calls its receiver Iecv8 to wait for a response.

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

PD performs the requested service, and sends the result and the name of the recipient P8 to the forwarder @orwD. !he forwarder marshals the result and delivers it Iecv8. Iecv8 receives the response from pD, unmarshals it and determines it to P8

Peer P$

Aor(arder Aor($

:ecei;er :ec;$

:ecei;er. :ec;.

Aor(arder Aor(. $: rec;*sg

Peer P.

.: re"Ser;ice 3: send*sg

4: *ars'al

. Se()en$e Diagra":
/: Deli;er*sg 1: recei;e

2: un*ars'al 8: Deli;er*sg

9: recei;e*sg $%: send*sg

$$: mars'al $.: Deli;er*sg $3: :ecei;e

$4: un*ars'al

$/: Deli;er*sg

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Code I"#le"entation: Entr!.*a&a: public class %ntry ( private 1tring destinationId' private int port=r' public %ntry.1tring the#est, int thePort/ ( destinationId)the#est' port=r)thePort' 0
07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public 1tring dest./ ( return destinationId' 0 public int port./ ( return port=r' 0 0

6or arder.*a&a:

import java.io.Futput1tream' import java.io.?nsupported%ncoding%xception' import java.net.1ocEet'

public class @orwarder ( private 1tring my=ame' private 1ocEet s' private Futput1tream o1tr' public @orwarder.1tring the=ame/ (

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

1ystem.out.println.5Hn@FIMAI#%I 27A11 2I%A!%#........N5/' my=ame)the=ame' 0 private byte34 marshal.<essage the<sg/ ( byte34 return:alue)null' int i,empty1equence' 1tring str' empty1equence)89 the<sg.sender.length./' str)5 56the<sg.sender' for.i)9'iOempty1equence'i66/ str6)5 5' str6)the<sg.data' 1ystem.out.println.5Hn@orwarder 56str/' try ( return:alue)str.get;ytes.5?!@ ,5/' 0 catch .?nsupported%ncoding%xception e/ ( 0 1ystem.out.println.5Hn#ata @orward " 56return:alue/' return return:alue'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

0 private void deliver.1tring the#est, byte34 data/ ( try ( %ntry entry' if.the#est.equals.52lient5// ( entry)new %ntry.58DP.9.9.85,,,,,/' 1ystem.out.println.5Hn1%I:%I Q@FIMAI#%I5/' 0 else ( entry)new %ntry.58DP.9.9.85,----/' 1ystem.out.println.5Hn27I%=! Q@FIMAI#%I5/' 0 s)new 1ocEet.entry.dest./,entry.port.//' 1ystem.out.println.5Hn2atching 6entry.port.//' o1tr)s.getFutput1tream./' o1tr.write.data/' o1tr.flush./' o1tr.close./' 56entry.dest./

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

s.close./' 0 catch .%xception e/ ( 0 0 public void send<sg.1tring the#est, <essage the<sg/ ( deliver.the#est,marshal.the<sg//' 0 0

Message.*a&a:

public class <essage ( public 1tring sender' public 1tring data' public <essage.1tring the1ender, 1tring raw#ata/ ( sender)the1ender' data)raw#ata' 0 0
07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Re$ei&er.*a&a:

import java.io.Input1tream' import java.io.?nsupported%ncoding%xception' import java.net.1erver1ocEet' import java.net.1ocEet'

public class Ieceiver ( private 1tring my=ame' private 1erver1ocEet srv1' private 1ocEet s' private Input1tream i1tr' public Ieceiver.1tring the=ame/ ( 1ystem.out.println.5HnI%2%I:%I 27A11 2I%A!%#...........N5/' my=ame)the=ame' 0 public <essage un<arshal.byte34 anArray/ ( 1tring str)5Ieciever one5,sender,data' 1ystem.out.println.5HnI%2%I:%I" 56str/' sender)str.substring.9,-/'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

1ystem.out.println.5Hn1%=#%I" 56sender/' data)str.substring.89/' <essage msg)new <essage.sender.trim./,data/' return msg' 0 private byte34 receive./ ( int val' byte buffer34)null' try ( %ntry entry' if.my=ame.equals.52lient5// ( entry)new %ntry.58DP.9.9.85,,,,,/' 1ystem.out.println.5Hn27I%=! QI%2%I:%I5/' 0 else ( entry)new %ntry.58DP.9.9.85,----/' 1ystem.out.println.5Hn1%I:%I QI%2%I:%I5/' 0 srv1)new 1erver1ocEet.entry.port.//'
07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

s)srv1.accept./' i1tr)s.getInput1tream./' val)i1tr.read./' buffer)new byte3val4' i1tr.read.buffer/' 1ystem.out.println.5Hn#A!A I%2%I:%# " 56buffer/' try ( 1ystem.out.println.5Hn?=<AI1LA7"56new 1tring.buffer,5?!@ ,5//' 0 catch .?nsupported%ncoding%xception e8/ ( e8.print1tacE!race./' 0 i1tr.close./' s.close./' srv1.close./' 0 catch .%xception e/ ( 1ystem.out.println.5HnA %rror Fccured 56e.get<essage.//' 0 return buffer'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

0 public <essage receive<sg./ ( return un<arshal.receive.//' 0 0

Ser&er.*a&a:

public class 1erver extends !hread ( Ieceiver r' @orwarder f' public void run./ ( 1ystem.out.println.5Hn1%I:%I 27A11 2I%A!%#.....................NN5/' <essage result)null' r)new Ieceiver.51erver5/' result)r.receive<sg./' 1ystem.out.println.5Hn1%I:%I I%1?7! " 56result.sender/' f)new @orwarder.51erver5/'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

<essage A7I:%...............N5/'

msg)new

<essage.51erver5,5I

A<

f.send<sg.52lient5,msg/' 0 public static void main.1tring34 args/ ( 1erver a1erver)new 1erver./' a1erver.start./' 0 0

Client.*a&a:

public class 2lient ( public static void main.1tring34 args/ ( @orwarder f)new @orwarder .new 1tring.52lient5//' <essage msg)new <essage .52lient5,5Lelloooooooo............5/' f.send<sg.51erver5,msg/' <essage result)null' Ieceiver r)new Ieceiver .new 1tring.52lient5//' result)r.receive<sg./' 0

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

CLIENT/DISP3TC.ER/SER,ER

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

8.Using the UML Dra ing Tool b! i"#le"enting the $ode in %a&a de"onstrate the Client/Dis#at$her Design Pattern. !he Client-Dispatcher-Server design pattern introduces an

intermediate layer between clients and servers, the dispatcher component. It provides location transparency by means of a name service, and hides the details of the establishment of the communication connection between clients and servers. Class/Diagra":
Client doTas+() Client() Ser;ice name<fser;ice : String name<fSer;er : String Ser;ice() Ser;ice() PrintSer;ice Ser;ice() PrintSer;ice()

4otAound 4otAound()

Dispatc'er registr# : @as'ta&le = ne( @as'ta&le rnd : :andom = ne( :andom($.34/1) 9t'eDispatc'er registr#() locate() Dispatc'er()

ClientDispatc'er main() ClientDispatc'er()

D!na"i$s: A typical scenario for the 2lient #ispatcher 1erver design pattern includes the following phases"

A server registers itself with the dispatcher component.

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

At a later time, a client asEs the dispatcher for a communication channel to a specified server.

!he dispatcher looEs up the server that is associated with the name specified by the client in its registry. !he dispatcher establishes a communication linE to the server. If it is able to initiate the connection successfully, it returns the communication channel to the client. If not, it sends the client an error message.

!he client uses the communication channel to send a request directly to the server. After recogni$ing the incoming request, the server executes the appropriate service. Mhen the service execution is completed, the server sends the results bacE to the client.

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Se()en$e Diagra"
Client Dispatc'er Ser;er

$: registerSer;ice .: getC'annel 3: locateSer;er

4: esta&lis'C'annel /: acceptConnection 2: send:e"uest 1: connection5ccepted

8: recei;e:e"uest 9: runSer;ice

$%: ser;iceSer;ed

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Code I"#le"entation: Client:

import java.util.&' import java.io.&'

public class 2lient ( public void do!asE./ ( 1ervice s' try ( s)2lient#ispatcher.disp.locate.5Print1ervice5/' s.1ervice./' 0 catch .=ot@ound n/ ( 1ystem.out.println.5=ot avaliable5/' 0 try ( s)2lient#ispatcher.disp.locate.5<ail1ervice5/'
07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

s.1ervice./' 0 catch .=ot@ound n/ ( 1ystem.out.println.5=ot avaliable5/' 0 try ( s)2lient#ispatcher.disp.locate.5#raw1ervice5/' s.1ervice./' 0 catch .=ot@ound n/ ( 1ystem.out.println.5=ot avaliable5/' 0 0 0

Dis#at$her.*a&a:

import java.io.&' import java.util.&'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public class #ispatcher ( Lashtable registry ) new Lashtable./' Iandom rnd ) new Iandom.8D+*CR/' public void registry.1tring 1:2, 1ervice obj/ ( :ector v).:ector/registry.get.1:2/' if.v))null/ ( v)new :ector./' registry.put.1:2,v/' 0 v.add%lement.obj/' 0 public 1ervice locate.1tring 1:2/ throws =ot@ound ( :ector v).:ector/registry.get.1:2/' if.v))null/ throw new =ot@ound./' if.v.si$e./))9/ throw new =ot@ound./' int i)rnd.nextInt./Sv.si$e./' return.1ervice/v.elementAt.i/' 0 0

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Not6o)nd.*a&a: import java.io.&' import java.util.&'

class =ot@ound extends %xception ( 0

Ser&i$e.*a&a: import java.io.&' import java.util.&'

abstract class 1ervice ( 1tring nameFfservice' 1tring nameFf1erver' public 1ervice.1tring 1:2, 1tring 1I:/ ( nameFfservice)1:2' nameFf1erver)1I:' 2lient#ispatcher.disp.registry.nameFfservice,this/' 0 abstract public void 1ervice./'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

0 PrintSer&i$e.*a&a:

import java.util.&' import java.io.&'

public class Print1ervice extends 1ervice ( public Print1ervice.1tring 1:2, 1tring 1I:/ ( super.1:2,1I:/' 0 public void 1ervice./ ( 1ystem.out.println.51ervice56nameFfservice65 6nameFf1erver/' 0 0 by 5

Client/Dis#at$her.*a&a

import java.util.&' import java.io.&'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public class 2lient#ispatcher ( public static #ispatcher disp)new #ispatcher./' public static void main.1tring args34/ ( 1ervice s8)new Print1ervice.5Print1ervice5,51erver85/' 1ervice sD)new Print1ervice.5<ail1ervice5,51erverD5/' TT1ervice s+)new Print1ervice.5#raw1ervice5,51erver+5/' 2lient client)new 2lient./' client.do!asE./' 0 0

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

PROX9 :. Using the UML Dra ing Tool b! i"#le"enting the $ode in %a&a de"onstrate the Pro'! Design Pattern. !he Prox design pattern maEes the clients of a component

communicate with a representative rather than to the component itself. Introducing such a placeholder can serve many purposes, including enhanced efficiency, easier access and protection from unauthori$ed access. Class Diagra":
5&stractClass str : String = 88 5&stractClass()

Client main() Client()

9t'ePro?#

Pro?# Ser;er4ame() Pro?#()

Ser;er getser;ice() Ser;er()

D!na"i$s: !he following diagram shows a typical dynamic scenario of a proxy structure.

Mhile worEing on its tacE the client asEs the proxy to carry out a service.
PAGE: DEPARTMENT OF CA, NHCE

07MCA57

SVETA KUMARI.

USN. No.:1NZ08MCA18

!he proxy receives the incoming service request and pre processes it. !his pre processing involves actions such as looEing up the address of the original, or checEing a local cache to see if the requested information is already available. If the proxy has to consult the original to fulfill the request, it forwards the request to the original using the proper communication protocols and security measures. !he original accepts the request and fulfills it. It sends the response bacE to the proxy. !he proxy receives the response. ;efore or after transferring it to the client it may carry out additional post processing actions such as caching the result, calling the destructor of the original or releasing a locE on a resource.

Se()en$e Diagra":

Client

Pro?#

Ser;er

$: :e"uest for Ser;ice

.: Aind in t'e cac'e

3: =et t'e ser;ice from Ser;er

4: =et t'e Ser;ice

/: Cac'e t'e ne( information

1: =et t'e Ser;ice done

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Collaboration Diagra"
Ser;er 3: :esponding (it' t'e Ser;ice

1: Cac'ed Ser;ice (ill &e gi;en 4: Cac'es t'e Ser;ice

Pro?#

.: :e"uest for t'e Ser;ice

$: :e"uest for Ser;ice Client /: :etr#ing to access t'e ser;ice

Code I"#le"entation:

3bstra$tClas.*a&a

import java.io.&'

#)bli$ $lass 3bstra$tClass (


07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public 1tring str ' public Abstract2lass./ throws IF%xception ( str)55' 0 0

Pro'!.*a&a:

import java.io.&'

public class Proxy extends Abstract2lass ( public Abstract2lass theAbstract2lass' public 1erver m>1erver' public 2lient m>client' public Proxy./ throws IF%xception ( 0 public void 1erver=ame./ throws IF%xception ( 1ystem.out.println.5 Proxy)))Q5/' if.str))55/ (

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

1ystem.out.println.5 !rying to get the service from server 5/' m>1erver)new 1erver./' str)m>1erver.getservice./' 1ystem.out.println.5 1erver says 5 6str/' 0 else ( 1ystem.out.println.5 Proxy also cached your request 5/' 1ystem.out.println.5 Proxy says 56str/' 0 0 0

Ser&er.*a&a

import java.io.&'

public class 1erver extends Abstract2lass ( public Proxy m>proxy' public 1tring getservice./

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

( 1ystem.out.println.5 1erver Initiali$ing5/' str)5 Lello.........N5' return.str/' 0 0

Client.*a&a:

import java.io.&'

public class 2lient ( public Proxy m>Proxy' public static void main.1tring args34/ throws IF%xception ( Proxy m>Proxy8)new Proxy./' 1ystem.out.println.5HnHnHn 8st time %stablishing 2onnection5/' m>Proxy8.1erver=ame./' 1ystem.out.println.5HnHnHn Dnd time Ietrying for 2onnection5/' m>Proxy8.1erver=ame./' 0 0

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

63C3DE

;.Using the UML Dra ing Tool b! i"#le"enting the $ode in %a&a de"onstrate the 6a$ade Design Pattern. A @aUade is a Pattern you can taEe a complex subsystem and maEe it easier to use by implementing a @aUade class that provides one, more reasonable Interface to handle the 2lasses and its corresponding sub classes. Class Diagra":
Su&Classes

Credit @as=oodCredit()

oan @as4o,ad oans()

,an+ @asSufficientSa;ings()

Customer 4ame : String Customer()

*ain5pp main()

*ortgage is7ligi&le()

implement *ain

Aacade

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Se()en$e Diagra":
*ain5pp *ortgage ,an+ Credit oan

$: !n;o+es .: !n;o+es

3: !n;o+es

4: !n;o+es

/: :esponse

Code I"le"entation:

-an<.*a&a:

import java.io.&'

public class ;anE ( public boolean Las1ufficient1avings.2ustomer c, int amount/ ( 1ystem.out.println.52hecE ;anE for 5 6c.=ame/'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

return true' 0 0

Credit.*a&a:

import java.io.&'

public class 2redit ( public boolean LasVood2redit.2ustomer c/ ( 1ystem.out.println.52hecE credit for 5 6c.=ame/' return true' 0 0

C)sto"er.*a&a:

import java.io.&'

public class 2ustomer ( 1tring =ame'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

public 2ustomer.1tring name/ ( this.=ame)name' 0 0

Loan.*a&a: import java.io.&' public class 7oan ( public boolean Las=o;ad7oans.2ustomer c/ ( 1ystem.out.println.52hecE 7oans for 56c.=ame/' return true' 0 0

Mortgage.*a&a:

import java.io.&' public class <ortgage ( private 2redit >credit)new 2redit./' private 7oan >loan) new 7oan./'
07MCA57 PAGE: DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

private ;anE >banE)new ;anE./' public boolean Is%ligible.2ustomer cust, int amount/ throws IF%xception ( 1ystem.out.println.cust.=ame65 applies for 5 6amount65 loanHn5/' boolean eligible)true' if.N>banE.Las1ufficient1avings.cust,amount// ( eligible)false' 0 else if.N>loan.Las=o;ad7oans.cust// ( eligible)false' 0 else if.N>credit.LasVood2redit.cust// ( eligible)false' 0 return eligible' 0 0

Main3##.*a&a:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

import java.io.&' import java.util.&'

public class <ainApp ( public static void main.1tring args34/ throws IF%xception ( <ortgage mortgage)new <ortgage./' 2ustomer customer)new 2ustomer.5A=?1LA5/' boolean eligible)mortgage.Is%ligible.customer,8DC99/' 1ystem.out.println.5Hn56customer.=ame65 has been 5 6.eligibleW5 Approved 5"5 Iejected 5//' 0 0

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

POL9MORP.ISM =. Using the UML Dra ing Tool b! i"#le"enting the $ode in %a&a de"onstrate the Pol!"or#his" Design Pattern. Polymorphism is a method of single objects taEing different forms. It is easier and more reliable then using explicit selection logic, it also helps to add additional behaviors later on, increases the number classes in a design. Inter5a$e Pol!"or#his":

5ctor act() 5ctor() @app#5ctor act() @app#5ctor() 9t'e5ctor Sad5ctor act() Sad5ctor()

Transmogrif# main() Transmogrif#() Stage C'ange() PerformPla#() Stage()

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Se()ential Inter5a$e #ol!"or#his":

!nterface Pol# pi : float = 3.$4f area()

S"uare final. : float area() TestPol# main()

Circle final$ : float area()

Se()en$e Diagra":
Transmogrif# Stage @app#5ctor Sad5ctor

$: in;o+es .: re"uest

3: re"uest

4: response

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

Code I"#le"entation:

Inter5a$e Pol!"or#his":

Inter5a$ePol!.*a&a:

public interface InterfacePoly ( float pi ) +.8*f' public void area.float f/' 0

Cir$le.*a&a:

import java.util.&'

public class 2ircle implements InterfacePoly (

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

private float final8' public void area.float radius/ ( final8)pi&radius&radius' 1ystem.out.println.5Area of 2ircle) 56final8/' 0 0

S()are.*a&a:

import java.io.&'

public class 1quare implements InterfacePoly ( private float finalD' public void area.float side/ ( finalD)side&side' 1ystem.out.println.5Area of the 1quare) 56finalD/' 0 0

Test#ol!.*a&a

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

import java.util.&'

public class !estPoly ( public static void main.1tring args34/ ( 2ircle c8)new 2ircle./' 1quare s8)new 1quare./' c8.area.89.Df/' s8.area.P.,f/' 0 0

Code I"#le"entation:

Actor.java"

abstract class Actor ( public abstract void act./ ' 0

.a##!3$tor.*a&a:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

import java.io.&'

public class LappyActor extends Actor ( public void act./ ( 1ystem.out.println.5LAPPX A2!FI5/' 0 0

Sad3$tor.*a&a:

import java.io.&'

public class 1adActor extends Actor ( public void act./ ( 1ystem.out.println.51A# A2!FI5/' 0 0

Stage.*a&a:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

class 1tage ( public Actor actor)new LappyActor./' public void change./ ( actor)new 1adActor./' 0 public void PerformPlay./ ( actor.act./' 0 0

Trans"ogri5!.*a&a:

import java.util.&'

public class !ransmogrify ( public static void main.1tring34 args/ ( 1tage stage)new 1tage./' stage.PerformPlay./' stage.change./'

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

stage.PerformPlay./' 0 0

O)t#)t:

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

SVETA KUMARI.

USN. No.:1NZ08MCA18

07MCA57

PAGE:

DEPARTMENT OF CA, NHCE

You might also like