Design Patterns

By: Ivan García 

What are design patterns?

They are a collection of practices used to address and resolve typical recurring problems of Object Oriented Programming (OOP).

Types of Patterns

  • Creational: Used to separate objects created and encapsulate them objects, thereby separating client implementations from objects used.
  • Behavioral: used to define how the interaction between object and classes will be carried out
  • Structural Patterns: They target definition of objects and classes. The concept of inheritance is used to define interfaces and ways to create objects with new features

Why use them?

Although use of design patterns is not mandatory, it is highly recommended when developing robust and easy to maintain applications, as it ensures scalability, interoperability and reusability.

Examples of Design Patterns

There is a wide range of patterns used for specific cases variety, in this post we’ll start with one of the simplest design patterns: Singleton, and we will advance to other patterns in future posts.

SINGLETON
This pattern corresponds to the creational type, because it provides one of the best ways to create an object.

This pattern consists of a class, which is responsible for creating an object while ensuring that this object is created only once, this will allow to make calls to only this object  throughout the system, without the need to create the object again, ensuring that in any other class we work with is associated with the singleton object. This design pattern is very useful. For example, if you want to have, at all times, the object that carried out the connection to an SQL database, as we only the will make the connection to the database once, the object will have the same information all times and be accessible from any other class.

Implementation

We will create a class ClaseSingleton, this has its constructor defined as private and has a static instance of itself.

This class provides a static method to obtain the mentioned static instance from any other class, if the instance was not created, a new one is created and the new instance is returned.

We will create another class SingletonDemo, which use our ClaseSingleton class for an object SingletonDemo

Implementation ClaseSingleton (Java):

     

public class ClaseSingleton {
//define an object ClaseSingleton
private static ClaseSingleton instance = null;
//specify the constructor as private, this way the class cannot
//be instantiated by another class
private ClaseSingleton (){}
// Returns the only available object created, if it was not instantiated
//create a new instance
public static ClaseSingleton getInstance(){
    if(instance == null){
       instance = new ClaseSingleton ();
   }
  return instance;
}
public void showMessage(){
   System.out.println("Hello World!");
  }
}

Implementation of class SingletonDemo:

public class SingletonDemo {   
public static void main(String[] args) {       
   //Invalid call to the constructor      
   //Compile Time Error: The constructor ClaseSingleton () is not visible      
   // ClaseSingleton object = new ClaseSingleton ();       
   //Obtain the only available object      
   //When making this definition, the new instance is created if it was      
   //not created before, in later calls the instance will be return the first       
   // instance created      
ClaseSingleton singletonObject = ClaseSingleton.getInstance();       
  
//show message      
  singletonObject.showMessage();   
 }
}

After executing the method showMessage of the singleton object, the output should be: Hello World! .

This way, when calling from another class to the method getInstance of the class ClaseSingleton, you get the same instance every time, this way variables and methods that have a singleton class can be reused from other classes.

CONCLUSION

Using design patterns helps standardizing code, making it more understandable for other developers. The possible applications may vary for each particular case and according to the creativity of the developer, in later tutorials we will be looking at other design patters. Stay tuned!




Leave a Reply

Your email address will not be published. Required fields are marked *

Posts de Prensa Recientes