this is an assignement question from software engineering course and its a text book question..need help with the assignement as soon as possible!! please!and the textbook is fundamentals of software engineering second addition by carlo ghezzi and mehdi

profileJenifer-bore_45
SingletonPattern.pdf

Singleton Pattern

CSI 5390

Department of Computer Science and Engineering

Oakland University

Dae-Kyoo Kim

Intent

• Ensure a class has only one instance, and

provide a global point of access to it

Motivation

• Sometimes we want just a single instance

of a class to exist in the system

– E.g., one window manager, or just one factory

for a family of products

• To have the single instance easily

accessible

• To ensure that additional instances of the

class can not be created

Generic Solution: Structure

Consequences

• Benefits

– Controlled access to sole instance

– Permits a variable number of instances

Implementation

• Use a private constructor and a static

method to allow clients to get a reference

single instance

Implementation (continued)

public class Singleton

{

private static Singleton uniqueInstance = null;

private int data = 0;

private Singleton() {}

public static Singleton instance()

{

if(uniqueInstance == null)

uniqueInstance = new Singleton();

return uniqueInstance;

}

}

Implementation (continued)

public class TestSingleton

{

public static void main(String args[])

{

Singleton s = Singleton.instance();

s.setData(34);

System.out.println("First reference: " + s);

System.out.println("Singleton data value is: " +

s.getData());

s = null;

s = Singleton.instance();

System.out.println("\nSecond reference: " + s);

System.out.println("Singleton data value is: " +

s.getData());

}

}

Implementation (continued)

First reference: Singleton@1cc810

Singleton data value is: 34

Second reference: Singleton@1cc810

Singleton data value is: 34

Lazy Instantiation vs. Eager

Instantiation

• Lazy Instantiation – the singleton instance is created only when needed.

• What if two threads concurrently invoke the instance() method? – Two instances could be created

• Solutions – Make the instance() synchronized, but expensive and

needed only the first time

– Do an eager instantiation • Create the singleton instance in a static initializer

• Guaranteed to be thread safe

Eager Instantiation

public class Singleton

{

private static Singleton uniqueInstance = new Singleton();

private Singleton() {}

private int data = 0;

public static Singleton instance()

{

return uniqueInstance;

}

}

Hierarchical Singleton

• What if the Singleton class has subclasses?

– E.g., MazeFactory has subclasses of

EnchantedMazeFactory and

AgentMazeFactory

• Solutions

– Have the static instance() method of the

parent class determine the particular subclass

instance to instantiate

Hierarchical Singleton

public abstract class MazeFactory

{

private static MazeFactory uniqueInstance = null;

protected MazeFactory() {}

public static MazeFactory instance()

{

if (uniqueInstance == null)

return instance("enchanted");

else

return uniqueInstance;

}

public static MazeFactory instance(String name)

{

if(uniqueInstance == null)

if (name.equals("enchanted"))

uniqueInstance = new EnchantedMazeFactory();

else if (name.equals("agent"))

uniqueInstance = new AgentMazeFactory();

return uniqueInstance;

}

}

overloaded

calls

Violating open-closed

principle

Hierarchical Singleton (continued)

• The instance(String) methods violates the

Open-Closed Principle, since it must be

modified for each new MazeFactory

subclass.

• Solution

– use the “name” argument of the instance

method to determine the class

Must be open for extension, but closed for

modification

Hierarchical Singleton (continued)

public static MazeFactory instance(String

name)

{

if (uniqueInstance == null)

uniqueInstance =

Class.forName(name).newInstance();

return uniqueInstance;

}

Hierarchical Singleton (continued)

• Constructors of the subclasses cannot be

private, since MazeFactory needs to

instantiate them.

– Problem: Clients could instantiate other

instances of these subclasses

– Solution: Have each subclass provide a static

instance() method

Solution

public abstract class MazeFactory

{

protected static MazeFactory

uniqueInstance = null;

protected MazeFactory() {}

public static MazeFactory instance()

{return uniqueInstance;}

}

Solution

public class EnchantedMazeFactory extends MazeFactory

{

private EnchantedMazeFactory() {}

public static MazeFactory instance()

{

if(uniqueInstance == null)

uniqueInstance = new EnchantedMazeFactory();

return uniqueInstance;

}

}

overriding

Solution

• Create in the child class and access in the

parent class

• Client code to create factory the first time

– MazeFactory factory =

EnchantedMazeFactory.instance();

• Client code to access the factory

– MazeFactory factory =

MazeFactory.instance();