You are on page 1of 4

1) Client Dispatcher Pattern (Communication)

Intent: The 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. Structure:

Collaboration:

Participants: Client: The tas of a client is to perform domain-specific tas s. The client accesses operations offered by servers in order to carry out its processing tas s. !efore sending a re"uest to a server, the client as s the dispatcher for a communication channel. The client uses this channel to communicate with the server. Server: A server provides a set of operations to clients. It either registers itself or is registered with the dispatcher by its name and address. # server component may be located on the same computer as a client, or may be reachable via a networ . Dispatcher: The dispatcher offers functionality for establishing communication channels between clients and servers. To do this, it ta es the name of a server component and maps this name to the physical location of the server component. The dispatcher establishes a communication lin to the server using the available communication mechanism and returns a communication handle to the client. If the dispatcher cannot initiate a communication lin with the re"uested server, it informs

the client about the error it encountered. To provide its name service, the dispatcher implements functions for registering and locating servers.

The Class diagram:

Java Code: $$CDS.%ava public class CDS & public static Dispatcher disp ' new Dispatcher()* public static void main(String+, args) & Service sl ' new -rintService(.printSvc/.,.srvl.)* Service s0 ' new -rintService(.printSvc0.,.srv0.)* Client client ' new Client ()* client.doTas ()* 1 1 $$Dispatcher.%ava import %ava.util.2* import %ava.io.2* public class Dispatcher & private Client client* private -rintService printservice* 3ashtable registry ' new 3ashtable()* 4andom rnd ' new 4andom(/05678)* $$ for random access public void registerService(String svc, Service ob%) & 9ector v ' (9ector) registry. get (svc) * i (v '' null) & v ' new 9ector ()* registry.put(svc, v) * 1 v.add:lement(ob%)* 1 public void unregisterService() & 1 public Service locateServer(String svc)throws ;ot<ound & 9ector v ' (9ector) registry .get (svc) * i (v '' null) throw new ;ot<ound ( ) * i (v.si=e() '' >) throw new ;ot<ound()* int i ' rnd.ne?tInt () @ v.si=e() * return (Service) v. element#t (i) * 1 public void establishChannel() & 1 public void getChannel() & 1 1

$$Service.%ava (an abstract class to provide runService method and to register all the Services to the dispatcher, it is the server participant) public abstract class Service & String nameofservice* $$ service name String nameofserver* $$ server name public Service(String svc, String srv) & nameofservice ' svc* nameofserver ' srv* CDS.disp.registerService(nameofservice,this) * 1 abstract public void runService() * $$ service provided 1 $$-rintService.%ava (# real service class e?tending the abstract class Service) public class -rintService e!tends Service& private Dispatcher dispatcher* private Client client* public -rintService(String svc, String srv) & super(svc, srv)* 1 public void acceptConnection() & 1 public void runService() & $$test output System.out.println(.Service . A nameofservice A . by . A nameofserver)* $$here the service code would be implemented 1 public void receive4e"uest() & 1 1 $$Client.%ava:( # client class with do Tas operation) public class Client & private Dispatcher dispatcher* private -rintService printservice* public void doTas () & Service s* tr" & s ' CDS .disp. locateServer (.printSvc/.) * s . runService ( ) * 1catch (;ot<ound n) & System.out .println (.;ot available.) * 1 tr" & s ' CDS .disp.locateServer(.printSvc0.)* s .runService ( ) * 1catch (;ot<ound n) & System.out.println(.;ot available.)* 1 tr" & s ' CDS .disp.locateServer(.drawSvc.)* s .runService () * 1catch (;ot<ound n) & System.out.println(.;ot available.) * 1 1 public void send4e"uest() & 1 1 Butput: Service printSvc/ by srvl Service printSvc0 by srv0

;ot available

You might also like