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
- GOF Patterns
- Presentation Tier Patterns
- Business Tier Patterns
- 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
- Intercepting Filter
- Front Controller
- Application Controller
- Composite View
- Context Object
Business Tier Patterns: Business Tier provides business services required by the clients
Type of Business Tiers
- Session Facade
- Transfer Object
- Application Service
- Business Object
- Service Locator
Integration Tier Patterns: Integration Tier provides context for communicating with external resources and systems like database etc.
Type of Integration Tier
- Data Access Object
- Service Activator
- 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 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
- Abstract Factory: Abstract Factory can be used as a super factory, by using abstract factory design pattern we abstract the creation of another class.
- Builder: It separates object creation from its representation.
- Factory Method: Factory Method creates an instance of many derived classes.
- 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
- Make Class Constructor private - When we mark the constructor of the class as private, other classes cannot create an instance of the class Directly.
- 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.
- Make a static member of the same class type in the class.
Let us take an example to understand it better.
- public class Singletonpattern {
- private static Singletonpattern instance;
- private Singletonpattern() {}
- public static Singletonpattern getInstance() {
- if (instance == null) {
- instance = new Singletonpattern();
- }
- return instance;
- }
- }
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
- public static Singletonpattern getInstance(){
- if(instance == null){
- instance = new Singletonpattern();
- }
- return instance;
- }
There are many different ways to implement a Singleton Pattern,
- Eager Initialization
- Lazy Initialization
- Implementation using double-check locking
- Lazy <T> type instantiation
- Implementing Singleton using Generics
- Override clone method and threw ClonenotsupportedExecption
Differences between Static class and Singleton Pattern
Static class | Singleton Pattern |
We cannot create an object of Static Class. | We can create an object of a singleton class |
Static class is generally loaded automatically | Singleton can Lazy Load. |
Static object stores in stack memory | Singleton object stores in Heap Memory |
A static class cannot implement an interface | But 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.