Design patterns – Singleton, Factory, Observer

D

Design patterns are general and reusable solutions to a common problem in software design. A pattern design is a description of the solution or a template that can be applied to solve the problem, not a piece of code that can be applied directly. In general, object-oriented patterns show relationships and interactions between classes or objects without specifying the final shape of the classes or objects involved.

1. Singleton Pattern
The Singleton Pattern is used to restrict the number of instances of a class to a single object, so it is a way to use a single instance of an object in the application.
The Singleton Pattern is useful in the following cases:
a. a subset of other patterns:
Together with Abstract Factory, Builder, Prototype, etc. For example, in the application we want a single factory object to create objects of a certain type. Instead of global variables, a singleton is preferred to global variables because, among other things, it does not pollute the global namespace with unnecessary variables.

Singleton is often used in situations where we have objects to access from multiple places in the application:

    -logger objects
    – objects that are shared resources (connections, sockets, etc.)
    -configuration objects for the application
    -For Factory Objects.

Applying the Singleton pattern consists of implementing a method that allows creating a new instance of the class if it does not exist, and returning a reference to it if it already exists. In Java, in order to ensure a single instantiation of the class, the builder must be private, and the instance must be provided by a static, public method.

2. Factory
Factory patterns are used for objects that generate instances of related classes (implement the same interface, inherit the same abstract class). These are used when we want to isolate the object that needs a court of a certain type, to actually create it.
Additionally, the class that uses the instance does not need to specify exactly the subclass of the object to be created, so you do not have to know all the implementations of that type, but just what features the object has to create. For this reason, Factory is part of the Creational Patterns category because it provides a solution for creating objects.
Applicability:
-In libraries / APIs, the user is separate from the actual type of implementation and must use factory methods to obtain certain objects.
-When creating objects is more complex (several stages must be done, etc.), it is more useful to separate the logic needed to instantiate the class sub-type that needs that instance.

3. Observer Pattern
Design The Observer Pattern defines a relationship of dependency 1 to n between objects so that when an object changes its status, all its addicts are notified and updated automatically. Using this pattern involves the existence of a topic object that has a list of dependent objects as an observer, which it automatically calls every time an action takes place.
This pattern is behavioral (Behavioral), because it facilitates a better organization of classroom communication according to their roles/behavior.
The observer is used if several classes (observers) depend on the behavior of another class (subject) in situations like:
A class implements/represents logic, the basic component, and other classes only use its results (monitoring). A class performs actions that can then be represented in several ways by other classes. Practically in all of these situations, the Observer classes observe the changes/actions of the Subject class. The observation is implemented through notifications initiated from the methods of the Subject class.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories