Aarti 1123269 4H
Mishra 1 1
Practical No 5
Aim: Write program to understand The Singleton Pattern, the Command Pattern.
Singleton Pa ern is probably the most widely used design pa ern. It is a simple pa
ern, easy to understand and to use. Some mes it is used in excess and in
scenarios where it is not required. In such cases, the disadvantages of using it
outweigh the advantages it brings. For this reason, the singleton pa ern is some
mes considered an an pa ern or pa ern singleton. /*package whatever //do not
write package name here */ import java.io.*; class
Singleton { // static class private static Singleton instance; private
Singleton(){ System.out.println("Singleton is
Instantiated.");
public static Singleton
getInstance(){ if
(instance == null)
instance = new Singleton();
return instance;
public static void doSomething()
{ System.out.println("Somethong is
Done.");
} class GFG { public static void main(String[] args){
Singleton.getInstance().doSomething(); }}
Aarti 1123269 4H
Mishra 1 1
Command Pattern
Definition: The command pattern encapsulates a request as an object,
thereby letting us parameterize other objects with different requests, queue
or log requests, and support undoable operations.
The definition is a bit confusing at first but let’s step through it. In
analogy to our problem above remote control is the client and stereo,
lights, etc. are the receivers. In a command pattern, there is a
Command object that encapsulates a request by binding together a set
of actions on a specific receiver. It does so by exposing just one method
execute() that causes some actions to be invoked on the receiver.
// An interface for command interface
Command
{
public void execute();
}
// Light class and its corresponding command
// classes
class Light
{
public void on()
{
System.out.println("Light is on");
}
public void off()
{
System.out.println("Light is off");
}
}
Aarti 1123269 4H
Mishra 1 1
class LightOnCommand implements Command
{ Light light;
// The constructor is passed the
light it // is going to control. public
LightOnCommand(Light light)
{ this.light = light;
}
public void execute()
{
light.on();
}
}
class LightOffCommand implements Command
{
Light light; public
LightOffCommand(Light
light)
{ this.light = light;
}
public void execute()
{ light.off();
}
}
// Stereo and its command classes
class Stereo
{
public void on()
{
System.out.println("Stereo is on");
}
public void off()
{
System.out.println("Stereo is off");
}
public void setCD()
{
System.out.println("Stereo is set " +
Aarti 1123269 4H
Mishra 1 1
"for CD input");
}
public void setDVD()
{
System.out.println("Stereo is set"+
" for DVD input");
}
public void setRadio()
{
System.out.println("Stereo is set" +
" for Radio");
}
public void setVolume(int volume)
{
// code to set the volume
System.out.println("Stereo volume set"
+ " to " + volume);
}
}
class StereoOffCommand implements Command
{
Stereo stereo; public
StereoOffCommand(Stereo
stereo)
{
this.stereo = stereo;
}
public void execute()
{
stereo.off();
}
}
class StereoOnWithCDCommand implements Command
{
Stereo stereo;
public StereoOnWithCDCommand(Stereo stereo)
{
this.stereo = stereo;
Aarti 1123269 4H
Mishra 1 1
} public void execute() {
stereo.on();
stereo.setCD();
stereo.setVolume(11);
}
}
// A Simple remote control with one
button class SimpleRemoteControl
{
Command slot; // only one button
public SimpleRemoteControl()
{
}
public void setCommand(Command command)
{
// set the command the remote will
// execute slot = command;
}
public void buttonWasPressed()
{ slot.execute();
}
}
// Driver class class
RemoteControlTest
{
public static void main(String[] args)
{
SimpleRemoteControl remote = new
SimpleRemoteControl();
Light light = new Light();
Stereo stereo = new Stereo();
// we can change command dynamically
remote.setCommand(new LightOnCommand(light));
remote.buttonWasPresse
d(); remote.setCommand(new
StereoOnWithCDCommand(stereo));
Aarti 1123269 4H
Mishra 1 1
remote.buttonWasPresse
d();
remote.setCommand(ne
w
StereoOffCommand(stereo));
remote.buttonWasPressed();
}
}
Aarti 1123269 4H
Mishra 1 1
Practical No 6
Aim: Write program to understand Prototype Patterns.
Prototype allows us to hide the complexity of making new instances from the client.
The concept is to copy an existing object rather than creating a new instance from
scratch, something that may include costly operations. The existing object acts as a
prototype and contains the state of the object. The newly copied object may change
same properties only if required. This approach saves costly resources and time,
especially when object creation is a heavy process.
// A Java program to demonstrate working of //
Prototype Design Pattern with example // of a
ColorStore class to store existing objects.
import java.util.HashMap;
import java.util.Map;
// Cloneable interface allows the implementing
class to // have its objects to be cloned instead of
using a new operator.
// It is available in Java.lang.Cloneable.
abstract class Color implements Cloneable
{
protected String colorName;
abstract void addColor();
public Object clone()
{
Object clone =
null; try
{
clone = super.clone();
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
return clone;
}
}
class blueColor extends Color
{
Aarti 1123269 4H
Mishra 1 1
public blueColor()
{
this.colorName = "blue";
}
@Override
void
addColor()
{
System.out.println("Blue color added");
}
}
class blackColor extends Color{ public
blackColor()
{
this.colorName = "black";
}
@Override
void
addColor()
{
System.out.println("Black color added");
}
}
class ColorStore { private static Map<String, Color> colorMap =
new HashMap<String, Color>(); static
{
colorMap.put("blue", new
blueColor()); colorMap.put("black", new
blackColor());
}
public static Color getColor(String colorName)
{
return (Color) colorMap.get(colorName).clone();
}
}
// Driver class class
Prototype {
public static void main (String[] args)
{
ColorStore.getColor("blue").addColo
r();
ColorStore.getColor("black").addCol
or();
ColorStore.getColor("black").addCol
Aarti 1123269 4H
Mishra 1 1
or();
ColorStore.getColor("blue").addColo
r();
Aarti 1123269 4H
Mishra 1 1
}
}