(Type The Document Title) : 1.expert
(Type The Document Title) : 1.expert
1.Expert
a.Pattern Instance:
b.class diagram:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
Sale.java:
import java.util.Set;
}
SalesLineItem.java:
ProductSpecification.java:
{
private double price = 43.89;
public double getPrice()
{
return price;
}
}
Register.java:
import java.util.Set;
public class Register
{
public static void main(String[] args)
{
Sale s = new Sale();
s.getTotal();
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
2.controller
a.Pattern Instance:
b.ClassDiagram:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
HomeView.java:
StudentView.java:
Dispatcher.java:
FrontController.java:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
FrontControllerPAtternDemo.java:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
3.Publisher-Subscriber
b.Class diagram
c.Sequence Diagram
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
TemperatureController.java:
import java.util.Observable;
import java.util.Observer;
public class TemperatureController implements Observer
{
Observable observable;
private float temperature;
public TemperatureController(Observable observable)
{
this.observable = observable;
observable.addObserver(this);
}
public void update(Observable obs, Object arg)
{
if (obs instanceof TemperatureTracker)
{
TemperatureTracker temperatureTracker =(TemperatureTracker) obs;
this.temperature = temperatureTracker.getTemperature();
display();
}
}
public void display()
{
System.out.println(" TemperatureController : Current temp : "
+ temperature + " F Degrees");
}
}
TemperatureDisplay.java:
import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
public class TemperatureDisplay implements Observer
{
List<Float> temps = new ArrayList<Float>();
private float temperature;
public TemperatureDisplay(Observable observable)
{
observable.addObserver(this);
}
public void update(Observable observable, Object arg)
{
if (observable instanceof TemperatureTracker)
{
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
temperature = TemperatureTracker.getTemperature();
display();
}
}
public void display()
{
System.out.println(" Temperature Display : temperature list below or higher than
the cutoff");
for (int i = temps.size() - 1; i >= 0; i--)
{
System.out.println(temps.get(i) + "F Degrees ");
}
}
}
TemperatureTracker.java:
import java.util.*;
public class TemperatureTracker extends Observable
{
private float temperature;
private float upperThreshold = 80.0f;
private float lowerThreshold = 30.0f;
public TemperatureTracker()
{
}
public void measurementsChanged()
{
setChanged();
notifyObservers();
}
public void setMeasurements(float temp)
{
temperature = temp;
if (temperature < lowerThreshold)
{
measurementsChanged();
}
else if (temperature > upperThreshold)
{
measurementsChanged();
}
}
public float getTemperature()
{
return temperature;
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
TemperatureStation.java:
import java.util.Scanner;
public class TemperatureStation
{
public static void main(String[] args)
{
TemperatureTracker Tc = new TemperatureTracker();
TemperatureDisplay currentCondition = new TemperatureDisplay(Tc);
TemperatureController forecastDisplay = new TemperatureController(Tc);
Scanner sc = new Scanner(System.in);
for(int i=0;i<5;i++)
{
System.out.println("Enter temperature " );
float temp = sc.nextFloat();
Tc.setMeasurements(temp);
}
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
Enter Temprature:
15
TempratureController:current temp:15.0Fdegrees
TempratureDisplay:tempratures list below or higher than cutoff
15.0Fdegrees
Enter Temprature:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
4.Command:
a.Class Diagram:
Myevents.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Myevents implements ActionListener
{
JTextField jtf;
Font f;
Myevents(JTextField ob,Font f1)
{
jtf=ob;
f=f1;
}
public void actionPerformed(ActionEvent ae)
{
String show = jtf.getText();
if(ae.getActionCommand().equals("Bold"))
jtf.setFont(new Font("Dialog",Font.BOLD,15));
else if(ae.getActionCommand().equals("Capitalize"))
jtf.setText((jtf.getText()).toUpperCase());
else if(show.equals(show.toLowerCase()))
jtf.setText((jtf.getText()).toLowerCase());
else
jtf.setFont(f);
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
DisplayText.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DisplayText extends JFrame
{
JTextField jtf = new JTextField(10);
JLabel jlb = new JLabel("sentence");
DisplayText()
{
Container container = getContentPane();
container.setLayout(new FlowLayout());
JButton button1 = new JButton("Bold");
JButton button2 = new JButton("Capitalize");
JButton button3 = new JButton("Undo");
container.add(jlb);
container.add(jtf);
container.add(button1);
container.add(button2);
container.add(button3);
Font f = jtf.getFont();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
button1.addActionListener(new Myevents(jtf,f));
button2.addActionListener(new Myevents(jtf,f));
button3.addActionListener(new Myevents(jtf,f));
}
public static void main(String [] args)
{
DisplayText dt = new DisplayText();
dt.setSize(500,500);
dt.setVisible(true);
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
5.Forwarder-Receiver:
Example: A simple peer-to-peer message exchange scenario; Underlying
communication protocol is TCP/IP.
a.Pattern Instance
b.Class diagram
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
c.Sequence Diagram
Entry.java:
Forwarder.java:
import java.io.*;
import java.net.Socket;
public class Forwarder
{
public Server Forwarder;
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
private Socket s;
private OutputStream oStr;
private String myName;
private Registry reg;
public Forwarder(String theName, Registry reg)
{
myName = theName;
this.reg = reg;
}
private byte[] marshal(Message theMsg)
{
return theMsg.data.getBytes();
}
private void deliver(String theDest, byte[] data)
{
try
{
Entry entry = reg.get(theDest);
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
Reciver.java:
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class Reciver
{
private ServerSocket srvS;
private Registry reg;
private Socket s;
private InputStream iStr;
private String myName;
public Reciver(String theName, Registry reg)
{
myName = theName;
this.reg = reg;
try
{
Entry entry = reg.get(myName);
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
iStr.close();
s.close();
srvS.close();
}
catch (IOException e)
{
System.out.println("Error" + e);
}
return buffer;
}
Registry.java:
import java.util.*;
Server.java:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
{
Message result = null;
r = new Reciver("Server", reg);
f = new Forwarder("Server", reg);
Message msg = new Message("Server", " I am alive");
f.sendMsg("Server", msg);
result = r.receiveMsg();
System.out.println(result.data.trim());
}
public static void main(String args[])
{
Entry entry = new Entry("127.0.0.1", 2900);
reg.put("Client", entry);
entry = new Entry("127.0.0.1", 2900);
reg.put("Server", entry);
new Server().execute();
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
6.Client-Dispatcher-Server
a. Pattern Instance:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
CDS.java
public class CDS
{
public static Dispatcher disp = new Dispatcher();
public static void main(String[] args)
{
Service s1 = new PrintService("printSvc1","srv1");
Service s2 = new PrintService("printSvc2","srv2");
Client client = new Client();
client.doTask();
}
}
Client.java
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
}
catch(NotFound n)
{
System.out.println("Not Available");
}
}
}
Dispatcher.java
import java.util.*;
import java.io.*;
class NotFound extends Exception
{
}
public class Dispatcher
{
private Client client;
private PrintService printservice;
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
PrintService.java
public class PrintService extends Service
{
private Client client;
private Dispatcher dispatcher;
public PrintService(String svc,String srv)
{
super(svc,srv);
}
public void runService()
{
System.out.println("Service : " + nameofservice + " by " + nameofserver);
}
}
Service.java
public abstract class Service
{
String nameofservice;
String nameofserver;
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
7. Proxy
a.Pattern Instance :
b.Class Diagram :
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
c.Sequence Diagram
Application.java
public class Application
{
public EMailService locateEMailService()
{
EMailService emailService = new ProxyEMailService();
return emailService;
}
}
ApplicationClient.java
public class ApplicationClient
{
public static void main(String[] args)
{
Application application = new Application();
EMailService emailService = application.locateEMailService();
emailService.sendMail("[email protected]","Hello","A test mail");
emailService.receiveMail("[email protected]");
} }
EMailService.java
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
ProxyEMailService.java
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
Sending mail to [email protected] with subject' Hello' and message' A test mail'.
Receiving mail from [email protected].
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
8.Facade
a.Pattern Instance:
b.Class Diagram:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
Shape.java:
Rectangle.java:
Square.java:
Circle.java:
ShapeMaker.java:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
public ShapeMaker()
{
Circle=new Circle();
Rectangle = new Rectangle();
Square = new Square();
}
public void drawCircle()
{
Circle.draw();
}
public void drawRectangle()
{
Rectangle.draw();
}
public void drawSquare()
{
Square.draw();
}
}
FacedePatternDemo.java:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
9. Polymorphism
a.Pattern Instance:
b.Class Diagram
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
Shape.java
public abstract class Shape
{
public abstract double area();
public abstract double perimeter();
}
Circle.java
public class Circle extends Shape
{
private final double radius;
final double pi=Math.PI;
public Circle()
{
this(1);
}
public Circle (double radius)
{
this.radius=radius;
}
@Override
public double area()
{
return pi*Math.pow(radius,2);
}
public double perimeter()
{
return 2*pi*radius;
}
Rectangle.java
public class Rectangle extends Shape
{
private final double width,length;
public Rectangle()
{
this(1,1);
}
public Rectangle(double width,double length)
{
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
this.width=width;
this.length=length;
}
@Override
public double area()
{
return width*length;
}
@Override
public double perimeter()
{
return 2*(width+length);
}
}
Triangle.java
public class Triangle extends Shape
{
private final double a,b,c;
public Triangle()
{
this(1,1,1);
}
public Triangle(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
}
@Override
public double area()
{
double s=(a+b+c)/2;
return Math.sqrt(s*(s-a)*(s-b)*(s-c));
}
@Override
public double perimeter()
{
return a+b+c;
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
TestShape.java
public class TestShape
{
public static void main(String [] args)
{
double width=5,length=7;
Shape rectangle=new Rectangle(width,length);
System.out.println("rectangle width:"+width+"and length:"+length+"\n
resulting area:"+rectangle.area()+"\n resulting perimeter:"+rectangle.perimeter()+"\n");
double radius=5;
Shape Circle=new Circle(radius);
System.out.println("Circle radius:"+radius+"\n resulting
area:"+Circle.area()+"\n resulting perimeter"+Circle.area()+"\n resulting
perimeter"+Circle.perimeter()+"\n");
double a=5,b=3,c=4;
Shape Triangle=new Triangle(a,b,c);
System.out.println("triangle sides length:"+a+","+b+","+c+"\n resulting area
:"+Triangle.area()+"\nResulting perimeter:"+Triangle.area()+"\n");
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
10.Whole-Part
Example: Implementation of any collection like a set.
a.Pattern instance:
b.Class diagram:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
Application.java:
import java.util.*;
public Application()
@SuppressWarnings("unchecked")
System.out.println(familyPack.getPrice());
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
System.out.println(partyPack.getPrice());
ColdDrink.java:
this.itemName=itemName;
this.iemDesc= desc;
this.price = price;
return price;
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
return itemName;
return iemDesc;
ColdDrinkFamilyPack.java:
import java.util.List;
super.addAll(items);
ColdDrinkPartyPack.java:
import java.util.List;
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
super.addAll(items);
Composite.java
import java.util.*;
items.add(itm);
items.remove(itm);
items.addAll(lst);
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
double sum=0;
sum += i.getPrice();
return sum;
Item.java
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
11.Master-Slave
Example: A multithreaded implementation of any parallelized divide-and-
conquer algorithm
a.Pattern instance:
b.Class diagram:
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
Master.java
public class Master
{
private int SlaveCount=2;
private Resource res=new Resource();
private Slave[] slaves=new Slave[SlaveCount];
public void run()
{
for(int i=0;i<SlaveCount;i++)
{
slaves[i]=new Slave(res);
}
for(int i=0;i<SlaveCount;i++)
{
slaves[i].start();
}
for(int i=0;i<SlaveCount;i++)
{
try
{
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
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...");
}
}
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("nowStatus="+local);
return Status;
}
}
Slave.java
import com.ibm.oti.shared.Shared;
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
TestMaster.java
public class TestMaster
{
public static void main(String [] args)
{
Master master=new Master();
master.run();
}
}
KLEDRMSSCET, Dept.ofMCA,Belagavi
[Type the document title]
OUTPUT
KLEDRMSSCET, Dept.ofMCA,Belagavi