Software Design Laboratory

Subject Code : 10MCA56

Hours/Week : 3Total Hours: 42

I.A Marks: 50Exam Hours : 03Exam Marks : 50

The student has to draw the necessary analysis and design diagrams in UML,using any suitable UML Drawing Tool and implement in Java OR C++ OR C# a programto demonstrate the Design Pattern specified by the Examiner. For each pattern, anexample is listed here.However, student is free to choose to solve any suitable problem todemonstrate the specified pattern. The Design Pattern is allotted based on lots fromthe following list:1)Publisher-Subscriber: (Communication)Example: An embedded application; An interrupt-driven module keeps track oftemperature of the furnace. When the temperature is beyond preset upper / lower limits, amodule that controls the heating element must be informed. Another module that displaysan indicator also needs to know of such a change. Further, a log module also needs thisinformation.2)Command Processor: (Management)Example: A simple Text Editor; Facilities provided include making the text bold,making the text into all upper case; An Undo feature is to be implemented.3)Forwarder-Receiver: (Communication)Example: A simple peer-to-peer message exchange scenario; Underlyingcommunication protocol is TCP/IP.4)Client-Dispatcher-Server: (Communication)Example: A simplified implementation of RPC5)Proxy: (Access Control)Example: A highly simplified implementation of a proxy web server.6)Whole-Part: (Structural Decomposition)Example: Implementation of any collection like a set.7)Master-Slave: (Organization of work)Example: A multithreaded implementation of any parallelized divide-and-conqueralgorithm

1)

Publishers-Subscriber / Observer Pattern (Communication)

Intent: Define a one-to-many dependency between objects so that when one objectchanges state, all its dependents are notified and updated automatically.Structure:

Subject: Keeps track of its observers. Provides an interface for attaching and detachingObserver objectsObserver: Defines an interface for update notificationConcreteSubject: The object being observed. stores state of interest to ConcreteObserverobjects. Sends a notification to its observers when its state changesConcreteObserver: The observing object. Stores state that should stay consistent withthe subject's. Implements the Observer update interface to keep its state consistent withtheThe Class diagram:

Command Pattern (Management)

Intent: Encapsulate a request as an object, thereby letting you parameterize clients withdifferent requests, queue or log requests, and support undoable operations.Also Known As: Action, TransactionApplicability:Use the command design pattern when we want to:1) Implement a callback functionality.2) Specify, queue, and execute requests at different times.3) Support undo and change operation.4) Structure a system around high-level operations built on primitives operations.Structure:

Collaborations:1)2)3)4)

the client creates a ConcreteCommand object and specifies its receiver.

An invoker object stores the ConcreteCommand object.The invoker issues a request by calling Execute on the Command.The ConcreteCommand object invokes operations on its receiver to carry out therequest.

Participants:Command: declares and interface for executing an operationConcreteCommand: defines a binding between a Receiver object and an action.Implements: Execute by invoking the corresponding Operation(s) on Receiver.Client: creates a ConcreteCommand object and sets its receiver.Invoker: asks the command to carry out the request.Receiver: knows how to perform the operations associated with carrying out a request.Any class may serve as a receiver.Consequences:1) Command decouples the object that invokes the operation from the one thatknows how to perform it.2) Command are first-class objects. They can be manipulated and extended like anyother object.3) Command can be made into a composite command.4) Its easy to add new Commands, because you dont have to change existingclasses.Example:Consider a simple switch. In this example we configure the Switch with 2 commands: toturn the light on and to turn the light off.A benefit of this particular implementation of the command Pattern is that the switch canbe used with any device, not just a light - the Switch in the following example turns alight on and off, but the Switch's constructor is able to accept any subclasses of

Command for its 2 parameters. For example, you could configure the Switch to start anengine.Class Diagram:

Participents:Peer components are responsible for application tasks. To carry out their tasks peersneed to communicate with other peers.Forwarder components are responsible for forwarding all these messages to remotenetwork agents without introducing any dependencies on the underlying IPCmechanisms.Receiver components are responsible for receiving messages. A receiver offers ageneral interface that is an abstraction of a particular IPC mechanism. It includesfunctionality for receiving and unmarshaling messages.

4) Client Dispatcher Pattern (Communication)

Intent: The Client-Dispatcher-Server design pattern introduces an intermediate layerbetween clients and servers, the dispatcher component. It provides location transparencyby means of a name service, and hides the details of the establishment of thecommunication connection between clients and servers.Structure:

Collaboration:

Participants:Client: The task of a client is to perform domain-specific tasks. The client accesses operationsoffered by servers in order to carry out its processing tasks. Before sending a request to a server,the client asks the dispatcher for a communication channel. The client uses this channel tocommunicate with the server.Server: A server provides a set of operations to clients. It either registers itself or is registeredwith the dispatcher by its name and address. A server component may be located on the samecomputer as a client, or may be reachable via a network.Dispatcher: The dispatcher offers functionality for establishing communication channelsbetween clients and servers. To do this, it takes the name of a server component and maps thisname to the physical location of the server component. The dispatcher establishes acommunication link to the server using the available communication mechanism and returns acommunication handle to the client. If the dispatcher cannot initiate a communication link with

the requested server, it informs the client about the error it encountered. To provide itsname service, the dispatcher implements functions for registering and locating servers.The Class diagram:

5) Proxy Pattern (Access Control)

Intent: Provide a surrogate or placeholder for another object to control access to itMotivation:1.A proxy is-a person authorized to act for another person-an agent or substitute-the authority to act for another2.There are situations in which a client does not or can not reference an object directly,but wants to still interact with the object3.A proxy object can act as the intermediary between the client and the target objectThe proxy object has the same interface as the target objectThe proxy holds a reference to the target object and can forward requests to the target asrequired (delegation!)In effect, the proxy object has the authority the act on behalf of the client to interact withthe target objectApplicability:Proxies are useful wherever there is a need for a more sophisticated reference to a objectthan a simple pointer or simple reference can provideTypes of Proxies:Remote Proxy - Provides a reference to an object located in a different address space onthe same or different machineVirtual Proxy - Allows the creation of a memory intensive object on demand. The objectwill not be created until it is really needed.Copy-On-Write Proxy - Defers copying (cloning) a target object until required by clientactions. Really a form of virtual proxy.Protection (Access) Proxy - Provides different clients with different levels of access to atarget objectCache Proxy - Provides temporary storage of the results of expensive target operationsso that multiple clients can share the resultsFirewall Proxy - Protects targets from bad clients (or vice versa)Synchronization Proxy - Provides multiple accesses to a target objectSmart Reference Proxy - Provides additional actions whenever a target object isreferenced such as counting the number of references to the object

Structure:

Collaboration:

Example:ProxyImagine that we are creating an Application that is making use of email-service such assend and receive email.. Assuming that the Client Application won't be always accessingthe Email Service, the Email Service is an ideal candidate to be modeled as a VirtualProxy.

Use Case Diagram:

Class Diagram:

The Sequence Diagram for EmailServices

The Collaboration Diagram:

The Java Code :

EMailService.javapublic interface EMailService {public void sendMail(String receiver,String subject,String text);public void receiveMail(String receiver);}The above is the interface declaration for EMailService which provides methods forsending and receiving mails through its sendMail() and receiveMail() methods. Now,let us look into the real implementation for this EMailService interface.RealEMailService.javapublic class RealEMailService implements EMailService {public void sendMail(String receiver,String subject,String text){System.out.println("Sending mail to'" + receiver + "'" + "with subject '" +subject + "'" + "and message '" + text + "'");}public void receiveMail(String receiver) {System.out.println("Receiving mail from '" + receiver + "'");}}As we see, the above class should be loaded on demand only, i.e, an instance for theabove class should be created only when the client is accessing for the first time.Now, let us create a proxy class for the above real implementation. It should benoted that the proxy class should conform to the methods of the real implementationclass so that the clients are provided with a single unified interface. In manyinstances, the clients are even unaware of the fact that they are making calls on theproxy. The following proxy implementation implements the EMailService interfaceand just delegates the method calls to the real implementation. It means that theproxy class should maintain a reference to the original real class object and shouldcreate the real object on demand.ProxyEMailService.javapublic class ProxyEMailService implements EMailService {private RealEMailService emailService;public void receiveMail(String receiver) {if (emailService == null){emailService = new RealEMailService();}emailService.receiveMail(receiver);