Creational patterns are design patterns used in object-oriented programming to help simplify object creation processes. They encapsulate object creation processes, provide flexibility in object creation, and promote code reuse.
There are five creational patterns, including the Abstract Factory pattern, Singleton pattern, Factory Method pattern, Builder pattern, and Prototype pattern.
An example of a creational pattern is the Singleton pattern. It is used to ensure a class has only one instance, while providing a global point of access. This pattern is useful in situations where only one instance of a class is needed throughout the application—for example, a logging class, a database connection class, or a configuration class.
Here is an example of a Singleton pattern in Java:
public class SingletonExample {
private static SingletonExample instance;
private SingletonExample() {}
public static SingletonExample getInstance() {
if (instance == null) {
instance = new SingletonExample();
}
return instance;
}
}
This code ensures that only one instance of the SingletonExample class is created, and provides a global access point to that instance through the getInstance() method.
What is the Singleton pattern and when would you use it?
Answer: The Singleton pattern is a creational pattern that restricts the instantiation of a class to one object. You would use it when you need to ensure that only one instance of a class is ever created and you want a global point of access to it.
What is the Builder pattern and when would you use it?
Answer: The Builder pattern is a creational pattern that separates the construction of a complex object from its representation so that the same construction process can create different representations. You would use it when you need to create complex objects that are composed of smaller objects that need to be created in a specific order.
What is the Factory Method pattern and when would you use it?
Answer: The Factory Method pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. You would use it when you have a superclass or interface that defines a common set of methods, but subclasses need to implement those methods differently.
What is the Abstract Factory pattern and when would you use it?
Answer: The Abstract Factory pattern is a creational pattern that provides an interface for creating families of related or dependent objects without specifying their concrete classes. You would use it when you need to create objects that belong to a particular group or family and you want to ensure that those objects are compatible with each other.
What is the Prototype pattern and when would you use it?
Answer: The Prototype pattern is a creational pattern that allows you to create new objects by cloning existing ones, without specifying their concrete classes. You would use it when you need to create new objects that are similar to existing ones, but with some variations, and you want to avoid the cost of creating those objects from scratch.