Singleton Pattern
The Singleton pattern ensures that a class has only one instance and provides a global point of access to it. This is useful in cases where a single instance of a class is needed to coordinate actions across a system.
Key Points
- Only one instance of the class can exist.
- Provides a global point of access to the instance.
Implementation Example
private static Singleton instance;
// Private constructor to prevent instantiation from outside the class
// Public static method to provide access to the instance
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
// Example method
public void showMessage() {
System.out.println("Hello World from Singleton!");
}
}
Usage
public static void main(String[] args) {
// Get the only object available
Singleton singleton = Singleton.getInstance();
// Show the message
singleton.showMessage();
}
}
Factory Method Pattern
The Factory Method pattern defines an interface for creating an object but allows subclasses to alter the type of objects that will be created. This pattern is used when a class cannot anticipate the type of objects it needs to create beforehand.
Key Points
- Defines an interface for creating an object.
- Subclasses decide which class to instantiate.
Implementation Example
public interface Product {
void use();
}
// Concrete Products
public class ConcreteProductA implements Product {
public void use() {
System.out.println("Using Product A");
}
}
public class ConcreteProductB implements Product {
public void use() {
System.out.println("Using Product B");
}
}
// Creator class
public abstract class Creator {
// Factory method
public abstract Product factoryMethod();
public void someOperation() {
Product product = factoryMethod();
product.use();
}
}
// Concrete Creators
public class ConcreteCreatorA extends Creator {
public Product factoryMethod() {
return new ConcreteProductA();
}
}
public class ConcreteCreatorB extends Creator {
public Product factoryMethod() {
return new ConcreteProductB();
}
}
Usage
public static void main(String[] args) {
// Create a creator for Product A
Creator creatorA = new ConcreteCreatorA();
creatorA.someOperation(); // Outputs: Using Product A
// Create a creator for Product B
Creator creatorB = new ConcreteCreatorB();
creatorB.someOperation(); // Outputs: Using Product B
}
}
Comparison
- Singleton Pattern:
- Ensures a class has only one instance.
- Provides a global point of access to the instance.
- Useful for managing shared resources like a database connection or configuration settings.
- Factory Method Pattern:
- Provides an interface for creating objects in a superclass but allows subclasses to alter the type of created objects.
- Helps in decoupling the code that creates the object from the code that uses the object.
- Useful for scenarios where a class cannot anticipate the type of objects it needs to create.
Both patterns are part of the creational design patterns family, but they solve different types of problems in object creation. The Singleton pattern focuses on managing a single instance of a class, while the Factory Method pattern focuses on creating objects without specifying the exact class of the object that will be created.
Nenhum comentário:
Postar um comentário