Design Patterns From The Beginning - Day Two

In the second part of my design patterns articles series, we are going to learn about the classification of design patterns according to the nature of the design problem they solve. In the previous articles, we already discussed the basics about GOF design patterns. If you want to read the previous article of this series, check this link.

In this series, we will also discuss Presentation Tier Patterns, Business, and Integration Tier Patterns.

Types of Design Patterns

  1. GOF Patterns
  2. Presentation Tier Patterns
  3. Business Tier Patterns
  4. Integration Tier Patterns

Presentation Tier Patterns: Presentation Tier contains all the presentation tier logic to provide services to clients who access the software.

Type of Presentation Tier Patterns

  1. Intercepting Filter
  2. Front Controller
  3. Application Controller
  4. Composite View
  5. Context Object

Business Tier Patterns: Business Tier provides business services required by the clients

Type of Business Tiers

  1. Session Facade
  2. Transfer Object
  3. Application Service
  4. Business Object
  5. Service Locator

Integration Tier Patterns: Integration Tier provides context for communicating with external resources and systems like database etc.

Type of Integration Tier

  1. Data Access Object
  2. Service Activator
  3. Web service Broker

What is GOF?

Gang of Four is not a design pattern, it is the newsy name for the book "Design Patterns: Elements of Reusable Object-Oriented Software" written by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides (thus called Gang of Four).

 
Creational Patterns

Creational design patterns are all about classes and objects. These patterns are also divided into class creation patterns and object creation patterns. 

 
Type of Creational Design Patterns

 

  1. Abstract Factory: Abstract Factory can be used as a super factory, by using abstract factory design pattern we abstract the creation of another class.
  2. Builder: It separates object creation from its representation.
  3. Factory Method: Factory Method creates an instance of many derived classes.
  4. Singleton: The singleton pattern is one of the most popular and used patterns in Software Development. Singleton pattern describes  a class that has only a single instance and provides a global access point for this.
Singleton Patterns

The singleton pattern is one of the most popular and used patterns in Software Development. The Singleton design pattern is a part of the creational design pattern. Creational design patterns are all about classes and object creation. If you use singleton in your application, it allows creating only a single instance of this class. We use a class as singleton when we want global access point of that instance.

UML Diagram of Singleton 
 
How we create a class as Singleton,

Rules to make a class as a singleton

  1. Make Class Constructor private - When we mark the constructor of the class as private, other classes cannot create an instance of the class Directly.

  2. Make a static method - When we make a private constructor, no one from outside the class can call the constructor to create an instance. The function of the same class can call the constructor to the objects, so in the function, we can write the code to check and return only a single object of the class.

    But if the method is a member method, to call it we need an instance of the class. So to use the method without instance marks this method as a static method.

  3. Make a static member of the same class type in the class.
Let us take an example to understand it better.
  1. public class Singletonpattern {  
  2.     private static Singletonpattern instance;  
  3.     private Singletonpattern() {}  
  4.     public static Singletonpattern getInstance() {  
  5.         if (instance == null) {  
  6.             instance = new Singletonpattern();  
  7.         }  
  8.         return instance;  
  9.     }  
  10. }  
Code Explanation
  • Create a class with a name Singletonpattern.
    public class Singletonpattern{

  • Declare a static member of a class with same class type
    private static Singletonpattern instance;

  • Declare a private constructor
    private Singletonpattern() {}

  • Declare a static method to create single instance
    1. public static Singletonpattern getInstance(){  
    2. if(instance == null){  
    3.    instance = new Singletonpattern();  
    4.    }  
    5. return instance;  
    6. }  

There are many different ways to implement a Singleton Pattern,

  1. Eager Initialization
  2. Lazy Initialization
  3. Implementation using double-check locking
  4. Lazy <T> type instantiation
  5. Implementing Singleton using Generics
  6. Override clone method and threw ClonenotsupportedExecption

Differences between Static class and Singleton Pattern

Static classSingleton Pattern
We cannot create an object of Static Class.We can create an object of a singleton class
Static class is generally loaded automaticallySingleton can Lazy Load.
Static object stores in stack memorySingleton object stores in Heap Memory
A static class cannot implement an interfaceBut when a class with a single instance needs to implement an interface we can use the Singleton pattern.
 
Summary

I hope that you understood the classification of design patterns. In the next article, we will discuss the Singleton Design patterns and Abstract Factory in detail with real time examples.