This article was automatically translated from the original Turkish version.
The Factory Method pattern delegates the object creation process to subclasses, allowing the superclass to operate without knowing exactly which class the object belongs to. By abstracting the class used for object creation, it provides flexibility and extensibility. The following code example shows a Java implementation of this design pattern.
Let us define an interface named Araba and add a common sur() method to it for all car types.
Now let us write the Sedan and SUV classes that implement the Araba interface. These classes will implement the sur() method according to their specific characteristics.
Let us define an abstract class named ArabaFabrikasi. The arabaOlustur() method will be left abstract here and will be implemented by subclasses.
Create two classes, SedanFabrikasi and SUVFabrikasi, that extend the ArabaFabrikasi class. These classes will implement the arabaOlustur() method according to their own logic.
SedanFabrikasi will create a Sedan object, while SUVFabrikasi will create an SUV object.
This structure prevents the client (the class containing the main method) from directly calling new Sedan() or new SUV(). Instead, the client decides which factory class to invoke and uses the arabaOlustur() method to create the object. This reduces dependencies and increases code flexibility. In summary:
This pattern is particularly useful when object types vary. For example, in a system that creates different car types, adding a new car type does not require modifying existing code.
The Abstract Factory provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern ensures that products are compatible with each other. The following code example shows a Java implementation of this design pattern.
First, an interface named Button is defined. This interface contains a paint() method that every button type must implement. Then, two classes implementing this interface are defined: WindowsButton and MacOSButton. These classes specify which operating system the button belongs to.
The Checkbox interface and its operating system-specific implementations are defined similarly. This allows the creation of visual components tailored to different operating systems.
The GUIFactory interface serves as a common factory for the product family. This interface defines methods such as createButton() and createCheckbox() to create the respective components.
The WindowsFactory and MacOSFactory classes implement the GUIFactory interface to produce appropriate buttons and checkboxes for each operating system. Thus, the application obtains visual components suitable for the current operating system.
The Application class obtains components through the abstract factory and uses them. The key point here is that the application does not need to be aware of the existence of classes such as WindowsButton or MacOSButton. All object creation is performed via the GUIFactory interface.
Factory Method and Abstract Factory are two important approaches among creational design patterns that abstract the object creation process. Both aim to manage object creation through abstract interfaces rather than directly using the new operator. However, they differ in purpose and the level of flexibility they offer.
Factory Method focuses on creating a single type of product. The superclass can operate without knowing which specific type of object will be created, and the object creation process is delegated to subclasses. This allows the system to be extended with new object types. However, a new subclass must be defined for each new product.
Abstract Factory enables the creation of multiple related or dependent objects (a product family) together. For example, in a user interface library, components such as buttons, checkboxes, and menus belonging to the same theme can be produced through a single abstract factory. This approach ensures consistency within the product family.
In summary:
The key differences between these two patterns are as follows:
The Builder design pattern enables the step-by-step and controlled construction of complex objects. Instead of defining all parameters in a single location during object creation, the object is built incrementally. This allows different types of objects to be created using the same construction process. The following code example shows a Java implementation of this design pattern.
Although both the Factory Method and Builder design patterns deal with object creation, they manage this process differently and are preferred in different scenarios. The key difference lies in their focal points and levels of flexibility.
Factory Method delegates the responsibility of deciding which type of object to create to subclasses. This pattern is typically suitable for creating single-step and relatively simple objects. For example, if an application needs to produce either a Sedan or an SUV, this choice can be handled through the Factory. The main advantage of Factory Method is that client code (the part using the object) can work with the object without knowing its exact type.
Builder, on the other hand, provides a much more detailed object creation process. This pattern is used to construct objects step by step, especially for complex objects with many parameters or multiple components. The same construction process can be used to create different types of objects. For example, a computer manufacturer can use the Builder pattern to combine components such as RAM, processor, and storage in different ways to produce both gaming computers and office computers.
In summary:
These two patterns can sometimes be used together. A Factory can select the appropriate Builder and then initiate the complex object creation process through that Builder.
The Prototype pattern allows a new object to be created by copying an existing object. Instead of relying on the new operator to create new objects, this pattern is based on cloning an already existing object.
When is it used?
The following code example shows a Java implementation of this design pattern.
The Singleton design pattern ensures that a class has only one instance and provides a global point of access to it. This pattern is ideal for situations where a single point of access is required throughout the application. For example:
The following code example shows a Java implementation of this design pattern.
private static Singleton instance;
private Singleton()
public static Singleton getInstance()
1. Factory Method (Factory Method)
-> Interface Definition
-> Implementing Classes
-> Abstract Factory Class
-> Concrete Factory Classes
-> Usage Scenario
2. Abstract Factory (Abstract Factory) Method
-> Product Interfaces and Products
-> Abstract Factory and Sub-Factories
-> Application Class
-> Differences Between Factory Method and Abstract Factory
3. Builder Design Pattern
-> Product – Araba Class
-> Builder Interface – ArabaBuilder
-> Concrete Builder – SedanBuilder
-> Director – ArabaDirektoru
-> Usage – Main Class
-> Difference Between Factory and Builder Design Patterns
4. Prototype Design Pattern
-> Abstract Class Named Shape
-> Rectangle Class (Subclass of Shape)
-> Circle Class (Another Subclass of Shape)
-> Usage Example
5. Singleton
-> Singleton Class Definition