0% found this document useful (0 votes)
58 views

Why Do We Need Singleton Pattern ? Answer

The document provides answers about design patterns, U

Uploaded by

imtiaz eemel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Why Do We Need Singleton Pattern ? Answer

The document provides answers about design patterns, U

Uploaded by

imtiaz eemel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

1. Why do we need singleton pattern ?

Answer:

OR Describe applicability of Singleton Pattern?


Answer:
Use the Singleton pattern when there must be only one instance of a class, and it
must be accessible to clients from a well- known access point.

When the sole instance should be extensible by sub classing, and clients should
be able to use an extended instance without modifying their code.

 Why do we need Factory pattern?


Answer: Factory method is a creational design pattern, i.e., related to object creation.

In Factory pattern, we create object without exposing the creation logic to client
and the client use the same common interface to create new type of object.

The idea is to use a static member-function (static factory method) which creates
& returns instances, hiding the details of class modules from user.

May be the most used pattern in modern languages like java.


OR Describe applicability of Factory Pattern?
Answer: Use the Factory Method pattern when
 A class can’t anticipate which kind of class of objects it must create.
 A class uses its subclasses to specify which objects it creates.
 You want to localize the knowledge of which class gets created.

Why do we need Composite Pattern?

Answer:

Compose objects into tree structures to represent part-whole hierarchies.


Composite lets clients treat individual objects and compositions of objects
uniformly.

File system: folder, file operation

Describe applicability of Composite Pattern?

Answer: Use the Composite pattern when

You want to represent part-whole hierarchies of objects.


You want clients to be able to ignore the difference between compositions of
objects and individual objects. Clients will treat all objects in the composite
structure uniformly.

2 . Write simple java code for Singleton with explanation?

Answer: Implementing Singleton class with method name as that of class name

// Java program implementing Singleton class


// with method name as that of class
class Singleton
{
// static variable single_instance of type Singleton
private static Singleton single_instance=null;

// variable of type String


public String s;

// private constructor restricted to this class itself


private Singleton()
{
s = "Hello I am a string part of Singleton class";
}
// static method to create instance of Singleton class
public static Singleton Singleton()
{
// To ensure only one instance is created
if (single_instance == null)
{
single_instance = new Singleton();
}
return single_instance;
}
}

// Driver Code
class Main
{
public static void main(String args[])
{
// instantiating Singleton class with variable x
Singleton x = Singleton.Singleton();

// instantiating Singleton class with variable y


Singleton y = Singleton.Singleton();

// instantiating Singleton class with variable z


Singleton z = Singleton.Singleton();

// changing variable of instance x


x.s = (x.s).toUpperCase();

System.out.println("String from x is " + x.s);


System.out.println("String from y is " + y.s);
System.out.println("String from z is " + z.s);
System.out.println("\n");

// changing variable of instance x


z.s = (z.s).toLowerCase();

System.out.println("String from x is " + x.s);


System.out.println("String from y is " + y.s);
System.out.println("String from z is " + z.s);
}
}

Explanation: In the Singleton class, when we first time call Singleton() method, it
creates an object of class Singleton with name single_instance and return it to the
variable. Since single_instance is static, it is changed from null to some object. Next
time if we try to call Singleton() method, since single_instance is not null, it is returned to
the variable, instead of instantiating the Singleton class again.
 Write simple java code for Factory with explanation?

Answer:

class Namer {

protected String last; //store last name here

protected String first; //store first name here

public String getFirst() {

return first;

public String getLast() {

return last;

class FirstLast extends Namer {

public FirstLast(String s) {

int i = s.lastIndexOf(" "); //find sep space

if (i > 0) {

first = s.substring(0, i).trim();

last =s.substring(i+1).trim();

else {

first = “”; // put all in last name if no space

last = s;

}}
class LastFirst extends Namer {

public LastFirst(String s) {

int i = s.indexOf(","); //find comma

if (i > 0) {

last = s.substring(0, i).trim();

first = s.substring(i + 1).trim();

else {

last = s; // put all in last name if no comma

first = "";

class NameFactory {

public Namer getNamer(String entry) {

int i = entry.indexOf(",");

if (i>0)

return new LastFirst(entry); //return one class

else

return new FirstLast(entry); //or the other

NameFactory nfactory = new NameFactory();

private void computeName() {


//send the text to the factory and get a class back

namer = nfactory.getNamer(entryField.getText());

txFirstName.setText(namer.getFirst());

txLastName.setText(namer.getLast());

 Write a simple java code for composite pattern?

Answer:
3. Describe Class Diagram Machine?

Answer: Class diagrams are the main building block in object-oriented modeling. They are
used to show the different objects in a system, their attributes, their operations and the
relationships among them.

The following figure is an example of a simple class:

In the example, a class called “loan account” is depicted. Classes in class diagrams are
represented by boxes that are partitioned into three:

1. The top partition contains the name of the class.


2. The middle part contains the class’s attributes.
3. The bottom partition shows the possible operations that are associated with the class.

The example shows how a class can encapsulate all the relevant data of a particular object in a
very systematic and clear way. A class diagram is a collection of classes similar to the one
above.

Describe Sequence Diagram Machine?

Answer: This sequence diagram tutorial is to help you understand sequence diagrams better;
to explain everything you need to know, from how to draw a sequence diagram to the common
mistakes you should avoid when drawing one.

There are 3 types of Interaction diagrams; Sequence diagrams, communication diagrams, and
timing diagrams.
Describe Behavioral State Diagram Machine?

 Answer: A behavioral state machine diagram is a dynamic model that shows the
different states that a single class passes through during its life in response to events,
along with its responses and actions.

Typically, behavioral state machine diagrams are not used for all classes, but just to further
define complex classes to help simplify the design of algorithms for their methods
#4# What is temporal event? How many “Actors” can be in use case modeling?
Describe briefly.

Answer: An event that occurs as a result of a reaching a point in time.


#5# How many “Associations” can be in use case modeling? Describe briefly.
Answer:

You might also like