Professional Documents
Culture Documents
Sub Code:13MCA56
INDEX
SL.
NO.
1.
2.
3.
4.
5.
6
7.
8.
9.
10.
11.
STAFF
DATE
USN: 2VX13MCA06
NAME OF EXPERIMENT
Expert
Controller
Publisher-Subscriber
Command
Forward-Receive
Client-Dispatcher
Proxy
Facade
Polymorphism
Whole-Part
Master-Slave
PAGE NO
SIGNATURE
Page 1
USN: 2VX13MCA06
Page 2
Activity Diagram:
USN: 2VX13MCA06
Page 3
Class Diagram:
Sequence Diagram:
USN: 2VX13MCA06
Page 4
Communication Diagram:
USN: 2VX13MCA06
Page 5
Code:
ProductDescription.java
public class ProductDescription
{
public SaleCounter SaleCounter;
private float price=43.89f;
public float getPrice()
{
return price;
}
}
Sale.java
public class Sale
{
public SaleCounter SaleCounter;
private float total;
public float getTotal()
{
ProductDescription pd=new ProductDescription();
SalesLineItem sli=new SalesLineItem();
total =( pd.getPrice() * sli.getSubTotal());
return total;
}
USN: 2VX13MCA06
Page 6
SaleCounter.java
public class SaleCounter
{
public Sale Sale;
public SalesLineItem SalesLineItem;
public ProductDescription ProductDiscription;
public static void main(String []args)
{
float total;
Sale s=new Sale();
total=s.getTotal();
System.out.println(" Total sale is :" + total);
}
}
SalesLineItem.java
public class SalesLineItem
{
public SaleCounter SaleCounter;
private int quantity=100;
public int getSubTotal()
{
return quantity;
}
}
USN: 2VX13MCA06
Page 7
Output:
The Total Sale is 4389.00
2) Controller
Problem:
The presentation-tier request handling mechanism must control and coordinate processing of each
user across multiple requests. Such control mechanisms may be managed in either a centralized or
centralized manner.
Solution:
Use a controller as the initial point of contact for handling a request. The controller manages the
handling of the request, including invoking security services such as authentication and
authorization, delegating business processing, managing the choice of an appropriate view,
handling errors, and managing the selection of content creation strategies.
Participants:
Controller:
The controller is the initial contact point for handling all requests in the system. The controller
may delegate to a helper to complete authentication and authorization of a user or to initiate
contact retrieval.
Dispatcher:
A dispatcher is responsible for view management and navigation, managing the choice of the
next view to present to the user, and providing the mechanism for vectoring control to this
resource.
Helper:
A helper is responsible for helping a view or controller complete its processing. Helpers can
service requests for data from the view by simply providing access to the raw data or by
formatting the data as Web content.
View:
A view represents and displays information to the client. The view retrieves information from a
model. Helpers support views by encapsulating and adapting the underlying data model for use
in the display
USN: 2VX13MCA06
Page 8
USN: 2VX13MCA06
Page 9
Activity Diagram:
Class Diagram:
USN: 2VX13MCA06
Page 10
Sequence Diagram:
Communication Diagram:
USN: 2VX13MCA06
Page 11
Code:
Client.java
import java.util.Scanner;
public class Client
{
public SaleController SaleController;
public static SaleController getController()
{
SaleController tc = new SaleController();
Sale s=new Sale();
view v=new view();
tc.setSale(s);
tc.setView(v);
return tc;
USN: 2VX13MCA06
Page 12
Sale.java
public class Sale
{
public SaleController SaleController;
public float getTotal(int quantity, float price)
{
return quantity * price;
}
}
SaleController.java
public class SaleController
{
USN: 2VX13MCA06
Page 13
view.java
public class view
{
public SaleController SaleController;
public void display(float total)
{
System.out.println("The Total Sale is: " + total);
USN: 2VX13MCA06
Page 14
Output:
Enter the Quantity
20
Enter the price
5.0
The Total Sale is: 100.0
USN: 2VX13MCA06
Page 15
Whenever the publisher changes state, it sends a notification to all its subscribers.
- The subscribers in turn retrieve the changed data at their discretion
Participants
Concrete Subject:
Acts as a Publisher. It is responsible for notifying and updating the changes taking place in
data.
Concrete Observer:
- Acts as subscriber. The changes made to the Concrete Subject(Publisher) will be reflected to
it.
Example: Weather Information system
The weather Station will be broadcasting the current weather conditions like temperature,
humidity and pressure. The application receives the weather conditions and displays them in
different forms.(i.e. displaying the current weather conditions, displaying weather forecast). All
displays will be updating the weather conditions in real time.
Page 16
Activity Diagram:
USN: 2VX13MCA06
Page 17
Class Diagram:
USN: 2VX13MCA06
Page 18
Communication Diagram:
USN: 2VX13MCA06
Page 19
USN: 2VX13MCA06
Page 20
ForeCastDisplay.java
import java.util.Observable;
import java.util.Observer;
public class ForeCastDisplay implements Observer
{
private float currentPressure = 29.92f, lastPressure;
public ForeCastDisplay(Observable observable)
{
observable.addObserver(this);
}
public void update(Observable observable, Object arg)
{
if(observable instanceof WeatherData)
{
WeatherData weatherData =(WeatherData)observable;
lastPressure = currentPressure;
currentPressure = weatherData.getPressure();
display();
}
}
public void display()
{
System.out.print("Forecast: ");
if(currentPressure > lastPressure)
{
System.out.println("Improving weather on the way!");
USN: 2VX13MCA06
Page 21
Page 22
Page 23
4) Command
"The Command Processor design pattern separates the request for a service from its execution. A
command processor component manages requests as separate objects, schedules their execution, and
provides additional services such as the storing of request objects for later undo."
Problem
- Changes to the user interface should be possible at runtime
- Need to issue requests to objects without knowing anything about the operation being
- requested or the receiver of the request.
Solution
Request is encapsulated into objects. Whenever a user calls a specific function of the application,
the request is turned into a command object. The Command Processor pattern illustrates more
specifically how command objects are managed. A central component of the Command Processor
pattern description takes care of all command objects. The command processor schedules the
execution of commands, may store them for later undo, and may provide other services such as
logging the sequence of commands for testing purposes. Each command object delegates the
execution of its task to supplier components within the functional core of the application.
Participants
Command: Declares and interface for executing an operation
ConcreteCommand: 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
Example: Simple switch
Switch is configured with 2 commands: ON and OFF. Switch can be used with any device, not just a
light but also with other devices like fan, TV, etc.
USN: 2VX13MCA06
Page 24
Activity Diagram:
USN: 2VX13MCA06
Page 25
Class Diagram:
Sequence Diagram:
USN: 2VX13MCA06
Page 26
Communication Diagram:
USN: 2VX13MCA06
Page 27
Code:
Command.java
public interface Command
{
public void execute();
}
Lightoffcommand.java
public class LightOffCommand implements Command
{
Light light;
public LightOffCommand(Light light)
{
this.light = light;
}
public void execute()
{
light.off();
}
}
Lightoncommand.java
public class LightOnCommand implements Command
USN: 2VX13MCA06
Page 28
Remotecontrol.java
public class RemoteControl
USN: 2VX13MCA06
Page 29
Page 30
Output
RemoteControl@814b5bf7
Living Room light is on
Living Room light is off
Kitchen light is on
Kitchen light is off
5) Forwarder-Receiver
"The 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."\
Participants
Forwarder:
-
Receiver
-
USN: 2VX13MCA06
Page 31
Activity Diagram:
USN: 2VX13MCA06
Page 32
USN: 2VX13MCA06
Page 33
Sequence Diagram:
USN: 2VX13MCA06
Page 34
Communication Diagram:
USN: 2VX13MCA06
Page 35
Code:
USN: 2VX13MCA06
Page 36
Forwarder.java
import java.io.*;
import java.net.Socket;
public class Forwarder
{
public Server Forwarder;
private Socket s;
USN: 2VX13MCA06
Page 37
Page 38
Message.java
class Message
{
public String sender;
public String data;
public Message(String thesender, String rawData)
{
sender = thesender;
data = rawData;
}
}
Receiver.java
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Receiver
{
USN: 2VX13MCA06
Page 39
Page 40
Registry.java
USN: 2VX13MCA06
Page 41
Page 42
Output:Server started
WE ALL ARE STILL ALIVE
USN: 2VX13MCA06
Page 43
The Server:
-
The Dispatcher:
USN: 2VX13MCA06
Page 44
The dispatcher offers functionality for establishing communication channels between clients and
servers.
To do this, it takes the name of a server component and maps this name to the physical location
USN: 2VX13MCA06
Page 45
Activity Diagram:
Class Diagram:
USN: 2VX13MCA06
Page 46
Sequence Diagram:
USN: 2VX13MCA06
Page 47
USN: 2VX13MCA06
Page 48
USN: 2VX13MCA06
Page 49
Client.java
public class Client
{
private Dispatcher dispatcher;
private PrintService printservice;
public void doTask()
{
Service s;
try
{
s = CDS.disp. locateServer("printSvc1"); s.runService();
}
USN: 2VX13MCA06
Page 50
Dispatcher.java
import java.util.*;
class NotFound extends Exception {}
public class Dispatcher
{
private Client client;
private PrintService printservice;
Hashtable registry = new Hashtable();
Random rnd = new Random(123456); // for random access
public void registerService(String svc, Service obj)
USN: 2VX13MCA06
Page 51
Page 52
USN: 2VX13MCA06
Page 53
USN: 2VX13MCA06
Page 54
Activity Diagram:
USN: 2VX13MCA06
Page 55
Class Diagram:
USN: 2VX13MCA06
Page 56
Sequence Diagram:
USN: 2VX13MCA06
Page 57
Communication Diagram:
USN: 2VX13MCA06
Page 58
USN: 2VX13MCA06
Page 59
Page 60
USN: 2VX13MCA06
Page 61
RealEMailService.java
public 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 + "'");
}
}
Output:Sending mail to 'abcd@gmail.com' with subject 'Hello' and message 'A test mail'
Receiving mail from 'abcd@gmail.com'
USN: 2VX13MCA06
Page 62
complex
One way to reduce this complexity is to introduce a facade object that provides a single,
simplified interface to the more general facilities of a subsystem
Participants:
Facade: HomeTheatreFacade
Subsystems: Amplifier, DvdPlayer, Projector
Example: Home Theater System
The home theater system will consist of different components like Projector, to project the movie on
screen, DVD Player to play the movie, Amplifier to control the sound effects.
For watching a movie the user starts switching on each component one by one and enjoys the movie.
After watching the movie he/she will have to switch off each device. But now the user is fed up of
this process and dont want to start switching on each device by him. He wants to control all the
devices
using single remote so that when he wants to watch a movie he can use this remote to start watching
movie and in the end he can close all the devices by one command using same remote.
USN: 2VX13MCA06
Page 63
Activity Diagram:
USN: 2VX13MCA06
Page 64
Class Diagram:
USN: 2VX13MCA06
Page 65
Sequence Diagram:
USN: 2VX13MCA06
Page 66
USN: 2VX13MCA06
Page 67
USN: 2VX13MCA06
Page 68
Code:
Amplifier.java
public class Amplifier
{
String description;
DvdPlayer dvd;
public Amplifier(String description)
{
this.description = description;
}
public void on()
{
System.out.println(description + " on");
}
public void off()
USN: 2VX13MCA06
Page 69
DvdPlayer.java
public class DvdPlayer
{
String description, movie;
Amplifier amplifier;
public DvdPlayer(String description, Amplifier amplifier)
{
this.description = description;
this.amplifier = amplifier;
}
public void on()
{
System.out.println(description + " on");
}
public void off()
USN: 2VX13MCA06
Page 70
Page 71
Projector.java
public class Projector
{
String description;
DvdPlayer dvdPlayer;
USN: 2VX13MCA06
Page 72
User.java
public class User
{
public static void main(String[] args)
USN: 2VX13MCA06
Page 73
USN: 2VX13MCA06
Page 74
USN: 2VX13MCA06
Page 75
Activity Diagram:
draw Circle
draw Triangle
draw Rectangle
Class Diagram:
USN: 2VX13MCA06
Page 76
Sequence Diagram:
USN: 2VX13MCA06
Page 77
USN: 2VX13MCA06
Page 78
Communication Diagram:
USN: 2VX13MCA06
Page 79
Code:
Shape.java
public interface Shape
{
public void draw();
}
Circle.java
public class Circle implements Shape
{
public void draw()
{
System.out.println("This is a Circle");
}
}
Rectangle.java
public class Rectangle implements Shape
{
public void draw()
{
System.out.println("This is a Rectangle");
}
}
USN: 2VX13MCA06
Page 80
Triangle.java
public class Triangle implements Shape
{
public void draw()
{
System.out.println("This is a Triangle");
}
}
DrawShape.java
public class DrawShape
{
private static Shape shape;
public static void main(String a[])
{
System.out.println("Invoking Circles draw method..");
shape = new Circle();
shape.draw();
System.out.println("Invoking Rectangles draw method..");
shape = new Rectangle();
shape.draw();
System.out.println("Invoking Triangles draw method..");
shape = new Triangle();
shape.draw();
}
}
USN: 2VX13MCA06
Page 81
USN: 2VX13MCA06
Page 82
A complex object should either be decomposed into smaller objects, or composed of existing
objects, to support reusability, changeability and the recombination of the constituent objects in
other types of aggregate.
Clients should see the aggregate object as an atomic object that does not allow any direct access
to its constituent parts.
Solution:
Introduce a component that encapsulates smaller objects, and prevents clients from accessing
these constituent parts directly.
Define an interface for the aggregate that is the only means of access to the functionality of the
encapsulated objects, allowing the aggregate to appear as a semantic unit.
Usecase Diagram
USN: 2VX13MCA06
Page 83
Activity Diagram:
Class Diagram:
USN: 2VX13MCA06
Page 84
Sequence Diagram:
USN: 2VX13MCA06
Page 85
USN: 2VX13MCA06
Page 86
USN: 2VX13MCA06
Page 87
ColdDrink.Java
public class ColdDrink implements Item
{
private String itemName;
private String iemDesc;
private double price;
public ColdDrink(String itemName, String desc, double price)
{
this.itemName=itemName;
this.iemDesc= desc;
this.price = price;
}
public double getPrice()
{
return price;
}
/*public void setItemName(String itemName)
{
this.itemName = itemName;
}*/
public String getItemName()
{
return itemName;
}
/*public void setIemDesc(String iemDesc)
{
this.iemDesc = iemDesc;
}*/
public String getIemDesc()
{
return iemDesc;
}
}
USN: 2VX13MCA06
Page 88
ColdDrinkFamilyPack.Java
import java.util.List;
public class ColdDrinkFamilyPack extends Composite
{
public ColdDrinkFamilyPack(List items)
{
super.addAll(items);
}
public double getPrice()
{
return super.getPrice() - super.getPrice()*.15; // get 15% discount on family pack
}
}
ColdDrinkPartyPack.Java
import java.util.List;
public class ColdDrinkPartyPack extends Composite
{
public ColdDrinkPartyPack(List items)
{
super.addAll(items);
}
public double getPrice()
{
return super.getPrice() - super.getPrice()*.25; // get 25% discount on family pack
}
}
Composite.Java
import java.util.*;
public abstract class Composite implements Item
{
/**
* @associates <{demp.composite.Item}>
*/
List<Item> items = new ArrayList<Item>();
public void add(Item itm)
{
USN: 2VX13MCA06
Page 89
Item.Java
public interface Item
{
public double getPrice();
}
Output
38.25
61.5
USN: 2VX13MCA06
Page 90
USN: 2VX13MCA06
Page 91
Class Diagram:
USN: 2VX13MCA06
Page 92
Sequence Diagram:
USN: 2VX13MCA06
Page 93
Comm
unication Diagram:
USN: 2VX13MCA06
Page 94
Page 95
USN: 2VX13MCA06
Page 96
Resourcs.Java:
public class Resource
{
private int status = 0;
public synchronized int incStatus()
{
int local = status;
System.out.println("status = " + local);
local++;
try {
Thread.sleep(50);
} catch(Exception e) {
}
status = local;
System.out.println("now status = " + local);
return status;
}
}
OUTPUT
status = 0
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 = 6
status = 6
now status = 7
status = 7
now status = 8
status = 8
now status = 9
status = 9
now status = 10
USN: 2VX13MCA06
Page 97
USN: 2VX13MCA06
Page 98