Professional Documents
Culture Documents
Case 1
Suppose we are building a cricket app that notifies viewers about the information such as current score,
run rate etc. Suppose we have made two display elements CurrentScoreDisplay and
AverageScoreDisplay. CricketData has all the data (runs, bowls etc.) and whenever data changes the
display elements are notified with new data and they display the latest data accordingly. Write its
implementation by using a suitable design pattern.
import java.util.ArrayList; {
import java.util.Iterator;
interface Subject for (Iterator<Observer> it =
{ observerList.iterator();
it.hasNext();)
public void registerObserver(Observer o);
public void unregisterObserver(Observer {
o); public void notifyObservers();
}
class CricketData implements Subject
{
int runs;
int wickets;
float overs;
ArrayList<Observer> observerList;
public CricketData() {
}
@Override
}
@Override
public void notifyObservers()
1
runs = getLatestRuns();
} wickets = getLatestWickets();
private int getLatestRuns() overs = getLatestOvers();
{ notifyObservers();
return 90; }
} }
private int getLatestWickets() interface Observer
{ {
return 2;
} public void update(int runs, int wickets,
private float getLatestOvers() float overs);
{
return (float)10.2; }
2
Observer o = it.next(); public void update(int runs, int wickets,
o.update(runs,wickets,overs); float overs)
} {
1
OUTPUT
PredictedScore: 441
3
Current Score Display:
Runs: 90
Wickets:2
Overs: 10.2
Runs: 90
Wickets:2
Overs: 10.2
4
Case 2
IMPLEMENTATION
public class Connect_db
Case 3
5
Suppose you have a Bird class with fly() , and makeSound()methods. And also a ToyDuck
class with squeak() method. Let’s assume that you are short on ToyDuck objects and you
would like to use Bird objects in their place. Birds have some similar functionality but
implement a different interface. Write its implementation by using a suitable design
pattern.
IMPLEMENTATION
interface Bird
{
public void fly();
public void makeSound();
}
class Sparrow implements Bird
{
public void fly()
{
System.out.println("Flying");
}
public void makeSound()
{
System.out.println("Chirp Chirp");
}
}
interface ToyDuck
{
public void squeak();
}
class PlasticToyDuck implements ToyDuck
{
public void squeak()
{
System.out.println("Squeak");
}
}
class BirdAdapter implements ToyDuck
{
Bird bird;
}
class Main
6
{
public static void main(String args[])
{
Sparrow sparrow = new Sparrow();
ToyDuck toyDuck = new PlasticToyDuck();
ToyDuck birdAdapter = new BirdAdapter(sparrow);
System.out.println("Sparrow...");
sparrow.fly();
sparrow.makeSound();
System.out.println("ToyDuck...");
toyDuck.squeak();
System.out.println("BirdAdapter...");
birdAdapter.squeak();
}
}
OUTPUT
Sparrow...
Flying
Chirp Chirp
ToyDuck...
Squeak
BirdAdapter...
Chirp Chirp
7
3
8
Case 4
Consider the scenario of college internet. Students should have access to the limited sites.
Social networking sites are not allowed to access. Write its implementation by using
suitable design pattern.
Interface of Internet
package com.anju.demo.proxy;
{
public void connectTo(String serverhost) throws Exception;
}
RealInternet.java
package com.anju.demo.proxy;
@Override
public void connectTo(String serverhost)
{
ProxyInternet.java
package com.anju.demo.proxy;
import java.util.ArrayList;
import java.util.List;
static
{
bannedSites = new ArrayList<String>();
bannedSites.add("Instagram.com");
bannedSites.add("Google+.com");
bannedSites.add("MayureshD6.com");
bannedSites.add("lnm.com");
}
@Override
9
public void connectTo(String serverhost) throws Exception
{
if(bannedSites.contains(serverhost.toLowerCase()))
{
throw new Exception("Access Denied");
}
internet.connectTo(serverhost);
}
Client.java
package com.anju.demo.proxy;
{
public static void main (String[] args)
{
Internet internet = new ProxyInternet();
try
{
internet.connectTo("Facebook.org");
internet.connectTo("abc.com");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
OUTPUT
Connecting to Facebook.org
Access Denied
10
Case 5
Suppose we are building an application that requires us to maintain a list of notifications. we
are creating a notification bar in our application that displays all the notifications which are held
in a notification collection. Write its implementation by using suitable design pattern.
String notification;
{
return notification;
}
}
interface Collection
{
public Iterator createIterator();
{
static final int MAX_ITEMS = 6;
int numberOfItems = 0;
Notif[] notificationList;
public NotifCollec()
{
notificationList = new Notif[MAX_ITEMS];
addItem("Notif 1");
addItem("Notif 2");
addItem("Notif 3");
}
notificationList[numberOfItems] = notification;
numberOfItems = numberOfItems + 1;
}
}
11
{
return new NotificationIterator(notificationList);
}
}
interface Iterator
{
boolean hasNext();
Object next();
}
class NotificationIterator implements Iterator
{
Notif[] notificationList;
int pos = 0;
{
this.notificationList = notificationList;
}
{
if (pos >= notificationList.length ||
notificationList[pos] == null)
return false;
else
return true;
}
}
class NotifBAR
{
NotifCollec notifications;
{
this.notifications = notifications;
}
System.out.println(n.getNotification());
}
}
}
12
class Main
{
public static void main(String args[])
{
NotifCollec nc = new NotifCollec();
NotifBAR nb = new NotifBAR(nc);
nb.PrintNotif();
}
OUTPUT
-------NOTIFICATION BAR------------
Notification 1
Notification 2
Notification 3
13