1
Software Design
Lecture : 31
2
Problem Statement of Singleton
Example
3
In Chocolate manufacturing industry, there are computer controlled
chocolate boilers. The job of boiler is to take in milk and chocolate,
bring them to boil and then pass it on to the next phase of chocolate
manufacturing process. We have to make sure that bad things don’t
happen like filling the filled boiler or boiling empty boiler or
draining out unboiled mixture. We have to make sure that there
should be no simultaneous boiler activity taking place.
4
public class ChocolateBoiler {
private boolean empty;
private boolean boiled;
private static ChocolateBoiler uniqueins;
private ChocolateBoiler()
{
empty=true;
boiled=false;
}
5
public static ChocolateBoiler getInstance()
{
if(uniqueins==null)
{
new ChocolateBoiler();
public static ChocolateBoiler getInstance()
{
if(uniqueins==null)
{
uniqueins=new ChocolateBoiler();
getInstance().fill();
getInstance().boil();
getInstance().drain();
}
return uniqueins;
}
6
public void fill()
{
if(isempty())
{
empty=false;
empty=true;
}}
public void drain()
{
if(!isempty()&&isboiled())
{
empty=true;
}}
7
public void boil()
{
if(!isempty() && !isboiled())
{
boiled=true;
}}
public boolean isempty()
{
return empty;
}
public boolean isboiled()
{
return boiled;
}}
8
Problem With the Code
There is a possibility that while batch of milk
and chocolate is boiling, filling method starts to
fill the boiler.
While one object is calling the boiling method
other object has called the fill method.
This is possible due to simultaneous access of
object
9
Solution
The problem lies with simultaneous threads
executing at the same time and this can happen
and this is what has happen
We need to make our code Thread safe ie we
need to add code to handle multithreading
10
public static synchronized ChocolateBoiler getInstance()
{
if(uniqueins==null)
{
uniqueins=new ChocolateBoiler();
getInstance().fill();
getInstance().boil();
getInstance().drain();
}
return uniqueins;
}
11
Practice Questions
Assuming Database is not providing Referential
Integrity Constraints support ie Primary key,
foreign key and Unique key, your task is to
design a database engine with your own built in
Referential integrity rules implementation and
you need that only one database connection is
maintained which an application should access.
12
Prototype Design Pattern
When we are trying to create objects that are expensive
both in term of time and resource like remote host
connection; then concept of object reusing should be
used.
We reuse already instantiated objects what have
performed timeconsuming instantiation process
13
Object Cloning
When we talk about object creation we can find
a better way to have new objects: cloning
If the cost of creating a new object is large and
creation is resource intensive, we clone the
object.
14
What is cloning in Java
In Java, objects are manipulated through reference
variables, and there is no operator for copying an object
—the assignment operator duplicates the reference, not
the object.
clone() is
a method in
the Java
programming
language for object duplication.
Clone method available in base class Object
15
Clone Method
One
disadvantage
with
the
design
of
the clone() method is that the return type
of clone() is Object, and needs to be explicitly
cast back into the appropriate type.