You are on page 1of 36

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

1. Publisher-Subscriber
USECASE DIAGRAM

PATTERN INSTANCE

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

Page | 1

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

CLASS DIAGRAM

SEQUENCE DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

Page | 2

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

ACTIVITY DIAGRAM:

Java Code Implementation:


CLASS 1:Weatherstation.java
public class weatherstation
{
public static void main(String []args)
{
WeatherData wd=new WeatherData();
CurrentConditionDisplay ccd=new CurrentConditionDisplay(wd);
ForecastDisplay fd=new ForecastDisplay(wd);
wd.setmeasurement(80f, 65f, 30.4f);
wd.setmeasurement(82f, 70f, 29.2f);
// wd.setmeasurement(78.2f, 90f, 29.2f);
}
}
CLASS 2:WeatherData.java
import java.util.Observable;
public class WeatherData extends Observable {
private Float Temprature;
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

Page | 3

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

public Float getTemprature()


{
return Temprature;
}
public void setTemprature(Float theTemprature)
{
Temprature = theTemprature;
}
private Float Pressure;
public Float getPressure()
{
return Pressure;
}
public void setPressure(Float thePressure)
{
Pressure = thePressure;
}
private Float Humidity;
public Float getHumidity()
{
return Humidity;
}
public void setHumidity(Float theHumidity)
{
Humidity = theHumidity;
}
public WeatherData()
{
}
public void measurementchanged()
{
this.setChanged();
this.notifyObservers(); //call update method of each observer
}
public void setmeasurement(Float temp,Float hum,Float p)
{
this.Temprature=temp;
this.Humidity=hum;
this.Pressure=p;
this.measurementchanged();
}
}
CLASS 3:ForecastDisplay.java
import java.util.Observable;
import java.util.Observer;
public class ForecastDisplay implements Observer
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

Page | 4

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

{
private Float currentpressure=29.92f;
public Float getCurrentpressure()
{
return currentpressure;
}
public void setCurrentpressure(Float theCurrentpressure)
{
currentpressure = theCurrentpressure;
}
private Float lastpressure;
public Float getLastpressure()
{
return lastpressure;
}
public void setLastpressure(Float theLastpressure)
{
lastpressure = theLastpressure;
}
public ForecastDisplay(Observable obs)
{
obs.addObserver(this);
}
public void update(Observable obs,Object org) {
if(obs instanceof WeatherData)
{
WeatherData wd=(WeatherData)obs;
this.lastpressure=this.currentpressure;
this.currentpressure=wd.getPressure();
this.display();
}
}
public void display()
{
System.out.println("Forecast");
if(this.currentpressure >this.lastpressure)
{
System.out.print("Improving weather anway");
}
else if(this.currentpressure ==this.lastpressure)
{
System.out.print("Same presure");
}
else if(this.currentpressure <this.lastpressure)
{
System.out.print("cool,rainy weather anyway");
}
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

Page | 5

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

}
}
CLASS 4:CurrentConditionDisplay.java
import java.util.Observable;
import java.util.Observer;
public class CurrentConditionDisplay implements Observer
{
private Object Observable;
private float Temperature;
private float Humidity;
public CurrentConditionDisplay(Observable observable)
{
this.Observable=observable;
observable.addObserver(this);
}
public void Display()
{
System.out.println("\nCurrent condition Display");
System.out.println("Current conditions:"+Temperature+" F Degrees
and"+Humidity+"% Humidity \n");
}
public void update(Observable o, Object arg)
{
if (o instanceof WeatherData)
{
WeatherData weatherdata=(WeatherData)o;
this.Temperature=weatherdata.getTemprature();
this.Humidity=weatherdata.getHumidity();
Display();
}
}
}
OUTPUT:Forecast
Improving weather anyway
Current condition Display
Current conditions:80.0 F Degrees and65.0% Humidity
Forecast
cool,rainy weather anway
Current condition Display
Current conditions:82.0 F Degrees and70.0% Humidity

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

Page | 6

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

2.Command Design
USECASE DIAGRAM

PATTERN INSTANCE

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

Page | 7

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

CLASS DIAGRAM

SEQUENCE DIAGRAM
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

Page | 8

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

ACTIVITY DIAGRAM

Java Code Implementation:


Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

Page | 9

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

CLASS1:
RemoteLoader.java
public class RemoteLoader {
public RemoteController InkRemoteController;
public static void main (String[] args) {
RemoteController r= new RemoteController();
r.setCommand();
Light LR = new Light("Living Room");
LightOnCommand LRON = new LightOnCommand(LR);
LRON.execute();
LightOffCommand LROFF = new LightOffCommand(LR);
LROFF.execute();
r.setCommand();
Light KIT=new Light("Kitchen");
LightOnCommand KITON = new LightOnCommand(KIT);
KITON.execute();
LightOffCommand KITOFF = new LightOffCommand(KIT);
KITOFF.execute();
}

CLASS2:
Command.java
public interface Command {
public void execute();
}
RemoteController.java
public class RemoteController {
public Light InkLight;

public void setCommand() {


System.out.println("\tLight On/Off Command Invoked");
}

CLASS3:
LightOnCommand.java
public class LightOnCommand implements Command {
private Light receiver;
public void execute() {
receiver.On();
}
public LightOnCommand(Light receiver) {
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 10

TOCE
}

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13
this.receiver = receiver;

CLASS4:
LightOffCommand.java
public class LightOffCommand implements Command {
private Light receiver;
public void execute() {
receiver.Off();
}
public LightOffCommand(Light receiver) {
}

this.receiver = receiver;

CLASS5:
Light.java
public class Light {
String desc;
public Light(String desc) {
this.desc=desc;
}
public void Off() {
System.out.println(desc+ " Light was switched OFF!!");
}
public void On() {
System.out.println(desc+ " Light was switched ON!!");
}
}

OUTPUT
Light On/Off Command Invoked
Living Room Light was switched ON!!
Living Room Light was switched OFF!!
Light On/Off Command Invoked
Kitchen Light was switched ON!!
Kitchen Light was switched OFF!!

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 11

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

3.Forwarder-Receiver
USECASE DIAGRAM

PATTERN INSTANCE

CLASS DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 12

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

SEQUENCE DIAGRAM

ACTIVITY DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 13

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

Java Code Implementation:


CLASS1:Forwarder.java
public class Forwarder
{
public Peer peer;
public Peer getPeer()
{
return peer;

public void setPeer(Peer thePeer)


{
peer=thePeer; }
public Receiver receiver;
public Receiver getReceiver()
{
return receiver;

public void setReceiver(Receiver theReceiver)


{
receiver = theReceiver;
}
public void marshal() {

public void deliver() {

public void sendMsg(String msg,Peer dest,Peer src)


{
Receiver rt=dest.getReceiver();
rt.receiveMsg(msg,src);
}
}
CLASS2:Receiver.java
public class Receiver
{
public Peer peer;
public Peer getPeer()
{
return peer; }
public void setPeer(Peer thePeer)
{
peer = thePeer;

public Forwarder forwarder;


public Forwarder getForwarder()
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 14

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

return forwarder;

public void setForwarder(Forwarder theForwarder)


{
forwarder = theForwarder; }
public void receive() {

public void unmarshal() { }


public void receiveMsg(String msg,Peer src)
{
System.out.println("\n\nMsg received from peer:"+src);
System.out.println("Msg:"+msg);
}
public void IPCmsg() {

CLASS3:Peer.java
public class Peer
{
public String name;
public Peer(String a)
{
name=a;
public Receiver receiver;
public Receiver getReceiver()
{
return receiver;

public void setReceiver(Receiver theReceiver)


{
receiver = theReceiver;
}
public Forwarder forwarder;
public Forwarder getForwarder()
{
return forwarder;

public void setForwarder(Forwarder theForwarder)


{
forwarder = theForwarder; }
public void service(String msg,Peer dest)
{
System.out.println("sending msg to"+dest);
this.forwarder.sendMsg(msg,dest,this);
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 15

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

}
public String toString()
{
return " "+name;

CLASS4:mainclass.java
public class mainclass {
public static void main(String[] args)
{
Forwarder f1=new Forwarder();
Forwarder f2=new Forwarder();
Receiver r1=new Receiver();
Receiver r2=new Receiver();
Peer p1=new Peer("peer1");
p1.setForwarder(f1);
p1.setReceiver(r1);
Peer p2=new Peer("peer2");
p2.setForwarder(f2);
p2.setReceiver(r2);
p1.service("hi.. Welcome to TOCE MCA 5th SEM SD LAB",p2);
p2.service("hi.. messege receive",p1);
}
}

OUTPUT
sending msg to peer2
Msg received from peer: peer1
Msg: hi.. Welcome to TOCE MCA 5th SEM SD LAB
Msg received from peer: peer2
Msg: hi.. messege receive

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 16

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

4. Client-Dispatcher-Server
USECASE DIAGRAM

PATTERN INSTANCE

CLASS DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 17

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

SEQUENCE DIAGRAM

ACTIVITY DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 18

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

Java Code Implementation


CLASS1:CDS.java
public class CDS {
static Dispatcher dispatcher=new Dispatcher(); //defined by user

public static void main(String args[]) {


Service s1 = new PrintService("printService1","server1"); //service,server
Service s2 = new PrintService("printService2","server2"); //service,server
Client client = new Client();
client.doTask();

//runservice of service

}
}
CLASS2:Client.java
public class Client {
Service service;
public void doTask() {
service=CDS.dispatcher.locateServer("printService1");//change service name enable else part
//if(service == null)
//System.out.println(" Service not available ");
//else
service.runService();

//print service name & server name

service=CDS.dispatcher.locateServer("printService2"); //find in hash table


service.runService();
}
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 19

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

}
CLASS3:Dispatcher.java
import java.util.*; //we need java.util.Hashtable
public class Dispatcher {
Hashtable registry = new Hashtable(); //user
//

Random rnd = new Random(123456);

//user

public void registerService(String svc,Service obj) { //class diagram


/*Vector v =(Vector) registry.get(svc);
if(v == null)
v=new Vector();
registry.put(svc,v);
v.addElement(obj);*/
registry.put(svc,obj);
}
public Service locateServer(String svc) {

//class diagram

/*Vector v=(Vector)registry.get(svc);
if(v==null || v.size()==0)
return null;
int i=rnd.nextInt() % v.size();
return (Service) v.elementAt(i); */
//if((Service)registry.get(svc)==null)
//

throw new NotFound();


return (Service)registry.get(svc);

}
}
CLASS4:Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 20

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

Service.java
public abstract class Service { //make abstact from properties in class diagram italics form
String nameofService;
String nameofServer;

public Service(String svc, String srv) { //make change in class diagram


nameofService=svc;
nameofServer=srv;
CDS.dispatcher.registerService(nameofService,this);
}
public abstract void runService(); //make abstract in class
}
CLASS5:PrintService.java
public class PrintService extends Service {
public void runService() {
System.out.println(" Service : " + nameofService + " by "+ nameofServer);
}
public PrintService(String svc, String srv) { //make change in class diagram
super(svc,srv);
}
}

OUTPUT:Service : printservice1 ran by server1


Service : printservice2 ran by server2

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 21

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

5. Proxy Pattern
USECASE DIAGRAM

PATTERN INSTANCE

CLASS DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 22

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

SEQUENCE DIAGRAM

ACTIVITY DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 23

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

Java Code Implementation:


CLASS 1:ApplicationClient.java
public class ApplicationClient {
public static void main(String[] args) {
Application application = new Application();
EmailService emailService = application.locateEmailService();
emailService.sendMail("abc@gmail.com","Hello","A Text Mail");
emailService.receiveMail("abc@gmail.com");}}
CLASS 2:Application.java
public class Application {
public EmailService locateEmailService()
{
EmailService eS = new ProxyEmailService();
return eS;
}
}
CLASS 3:Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 24

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

EmailService.java
public interface EmailService {
public void sendMail(String receive,String subject,String text);
public void receiveMail(String receive);
}
CLASS 4:ProxyEmailService.java
public class ProxyEmailService implements EmailService {
private RealEmailService emailService;
public void receiveMail(String receive)
{
if(emailService==null)
{
emailService=new RealEmailService();
}
emailService.receiveMail(receive);
}
public void sendMail(String receive, String subject, String text)
{
if(emailService==null)
{
emailService = new RealEmailService();
}
emailService.sendMail(receive,subject,text);
}
}
CLASS 5:RealEmailService.java
public class RealEmailService implements EmailService
{
public void sendMail(String receive,String subject,String text)
{
System.out.println("Sending mail to '" + receive + "'" + "with Subject '" + subject + "' " + " and
message '" + text + "'");
}
public void receiveMail(String receive)
{
System.out.println("Receiving Mail from ' " + receive +"'");
}
}
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 25

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

OUTPUT:Sending mail to 'abc@gmail.com'with Subject 'Hello' and message 'A Text Mail'
Receiving Mail from ' abc@gmail.com'

6.Whole-Part Pattern
USECASE DIAGRAM

PATTERN INSTANCE

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 26

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

CLASS DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 27

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

SEQUENCE DIAGRAM
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 28

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

JAVA CODE IMPLEMENTATION :Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 29

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

CLASS 1:Application.java
public class Application {
public static void main(String[] args) {
Elipse ellipse1=new Elipse();
Elipse ellipse2=new Elipse();
Elipse ellipse3=new Elipse();
Elipse ellipse4=new Elipse();
CompositeGraphic graphic=new CompositeGraphic();
CompositeGraphic graphic1=new CompositeGraphic();
CompositeGraphic graphic2=new CompositeGraphic();
graphic1.add(ellipse1);
graphic1.add(ellipse2);
graphic1.add(ellipse3);
graphic2.add(ellipse4);
graphic2.remove(ellipse4);
System.out.println("Adding the Element");
graphic.add(graphic1);
graphic.add(graphic2);
graphic.print();
}
}

CLASS 2:Graphic.java
public interface Graphic {
public void print();
}

CLASS 3:Elipse.java
public class Elipse implements Graphic {

public void print() {


System.out.println("List of the
System.out.println("Ellipse");
}

Software Design Laboratory [10MCA56]


Section

Elements");

Dept of MCA, 5th Sem B-

P a g e | 30

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

CLASS 4:CompositeGraphic.java
public class CompositeGraphic implements Graphic {
private List <Graphic> childGraphics = new ArrayList<Graphic>();
public void print() {
for(Graphic graphic:childGraphics)
{
graphic.print();
}
}
public void add(Graphic graphic) {
childGraphics.add(graphic);
}
public void remove(Graphic graphic) {
childGraphics.remove(graphic);

OUTPUT :List Of the Elements Added


Add Element in the List
Ellipse
Add Element in the List
Ellipse
Add Element in the List
Ellipse

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 31

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

7. Master-Slave Pattern
USECASE DIAGRAM

PATTERN INSTANCE:

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 32

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

CLASS DIAGRAM

SEQUENCE DIAGRAM

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 33

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

JAVA CODE IMPEMENTATION:CLASS 1:TestMaster.java


public class TestMaster {
public static void main(String[] args) {
Master master = new Master();
master.run();
}
}

CLASS 2:Master.java
public class Master {
private int slaveCount = 2;
private Resource res = new Resource();
private Slave[] slaves = new Slave[slaveCount];
public void run() {
// create slaves:
for(int i = 0; i < slaveCount; i++) {
slaves[i] = new Slave(res);
}
// start slaves:
for(int i = 0; i < slaveCount; i++) {
slaves[i].start();
}
// wait for slaves to die:
for(int i = 0; i < slaveCount; i++) {
try {
//is used to wait for a thread to finish and terminate
slaves[i].join();
} catch(InterruptedException ie) {
System.err.println(ie.getMessage());
} finally {
System.out.println(slaves[i].getName() + " has died");
}
}
System.out.println("The master will now die ... ");
}
}

CLASS 3:Slave.java
class Slave extends Thread {
private Resource sharedResource;
private boolean done = false;
Software Design Laboratory [10MCA56]
Section

Dept of MCA, 5th Sem B-

P a g e | 34

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

public void halt() {


done = true;
}
//constructor
public Slave(Resource rcs) {
sharedResource = rcs;
}
protected boolean task() {
// access sharedResource here
// for example:
int status = sharedResource.incStatus();
return (status >= 20);
// 20 <= status;
}

@Override
public void run() {
while (done != true) {
done = task();
// be cooperative:
try {
Thread.sleep(500);
} // sleep for 1 sec.
catch (Exception e) {
}
}
}

CLASS 4:Resource.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;
}

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 35

TOCE

NAME :
PRASANNA SANTHANA KUMAR S
USN: 1OY12MCA13

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
status = 10
now status = 11
status = 11
now status = 12
status = 12
now status = 13
status = 13
now status = 14
status = 14
now status = 15
status = 15
now status = 16
status = 16
now status = 17
status = 17
now status = 18
status = 18
now status = 19
status = 19
now status = 20
status = 20
now status = 21
Thread-0 has died
Thread-1 has died
The master will now die ...

Software Design Laboratory [10MCA56]


Section

Dept of MCA, 5th Sem B-

P a g e | 36

You might also like