KEMBAR78
Short Review - Singleton Vs Factory Design Patterns | PDF | Class (Computer Programming) | Software Design Pattern
0% found this document useful (0 votes)
6 views3 pages

Short Review - Singleton Vs Factory Design Patterns

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

Short Review - Singleton Vs Factory Design Patterns

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

Short Review: Singleton vs Factory Design

Patterns
Introduction to Design Patterns
Design patterns are proven, reusable solutions to common software design problems 1 . They serve as
“pre-made blueprints” that promote modular, maintainable and scalable code 1 2 . By providing a
standard way to solve recurring issues, patterns improve readability and ease collaboration among
developers 1 2 . Two common Java creational patterns are Singleton and Factory, each addressing
object creation in different ways.

Singleton Pattern
The Singleton pattern ensures only one instance of a class is created and provides a global point of access
to that instance 3 . It is useful for managing shared resources like configuration or logging (so all parts of
a program use the same instance).
- Advantages: Only one object exists, so resources (e.g. database connections) are shared and controlled
consistently. The global access point simplifies getting that instance from anywhere.
- Disadvantages: It introduces global state, which can make code harder to understand and test 4 .
Singletons can also be tricky in multithreaded code if not implemented with proper locking.
Example: A simple Java Singleton uses a private constructor and a getInstance() method. For example:

class Singleton {
private static Singleton instance;
private Singleton() { }
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}

Factory Pattern
The Factory pattern abstracts object creation so that a class delegates instantiation to a separate factory
class 5 . The client asks the factory for an object by specifying a type or key, without knowing the concrete
class.
- Advantages: It decouples client code from concrete classes, enhancing flexibility and maintainability 5 .
New product types can be added by extending the factory logic without changing client code.

1
- Disadvantages: It adds extra classes (e.g. creator and product classes) which increases complexity 6 .
Overuse can make code harder to follow if the factory logic is simple.
Example: A Java factory for creating Animal objects might look like:

interface Animal { void speak(); }


class Dog implements Animal { public void speak() {
System.out.println("Woof"); } }
class Cat implements Animal { public void speak() {
System.out.println("Meow"); } }

class AnimalFactory {
public Animal getAnimal(String type) {
if (type.equalsIgnoreCase("dog")) return new Dog();
else if (type.equalsIgnoreCase("cat")) return new Cat();
else return null;
}
}

// Usage:
Animal a = new AnimalFactory().getAnimal("dog");
a.speak(); // prints "Woof"

Comparison
• Instance Control: Singleton allows exactly one instance (e.g. a single configuration manager) 3 ,
whereas a Factory can create many instances of different classes on demand 7 5 .
• Purpose: Singleton centralizes access to a single object. Factory centralizes object creation, letting
clients work with interfaces rather than concrete classes 3 5 .
• Use-case: Singleton solves problems like having one global logger or cache. Factory solves problems
like creating a family of related objects (e.g. various Shape or Animal objects) without hard-
coding their classes.
• Trade-offs: The Singleton’s global state can hinder testing and increase coupling 4 . The Factory
pattern’s extra layers improve flexibility but at the cost of added complexity 6 .

Overall, both patterns improve code organization: Singleton by managing shared state, Factory by
organizing complex object creation. They are chosen based on the specific design problem: one-for-all
access (Singleton) versus flexible object instantiation (Factory).

Sources: Descriptions and comparisons are based on design pattern references 3 5 , with insights on
advantages and drawbacks from Java pattern analyses 4 6 .

1 The Significance of Design Patterns in Software Development | by Chandan Kumar | Medium


https://medium.com/@chandantechie/the-significance-of-design-patterns-in-software-development-c1ab78e1d9ee

2
2 3 7 Difference Between Singleton and Factory Design Pattern in Java - GeeksforGeeks
https://www.geeksforgeeks.org/java/difference-between-singleton-and-factory-design-pattern-in-java/

4 Java Patterns, Singleton: Cons & Pros - Java Code Geeks


https://www.javacodegeeks.com/java-patterns-singleton-cons-pros.html

5 6 Factory Method Design Pattern in Java - GeeksforGeeks


https://www.geeksforgeeks.org/java/factory-method-design-pattern-in-java/

You might also like