Java Design Patterns


Design patterns are programming language independent strategies for solving the common object-oriented design problems. That means a design pattern represents an idea, not a particular implementation.

By using the design patterns you can make your code more flexible, reusable, and maintainable. It is the most important part because java internally follows design patterns.


Categorization of design patterns:

Basically, design patterns are categorized into two parts:

  1. Core Java (or JSE) Design Patterns.
  2. JEE Design Patterns.

Core Java Design Patterns

In core java, there are mainly three types of design patterns, which are further divided into their sub-parts:

1.Creational Design Pattern

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Singleton Pattern
  4. Prototype Pattern
  5. Builder Pattern.

2. Structural Design Pattern

  1. Adapter Pattern
  2. Bridge Pattern
  3. Composite Pattern
  4. Decorator Pattern
  5. Facade Pattern
  6. Flyweight Pattern
  7. Proxy Pattern

3. Behavioral Design Pattern

  1. Chain Of Responsibility Pattern
  2. Command Pattern
  3. Interpreter Pattern
  4. Iterator Pattern
  5. Mediator Pattern
  6. Memento Pattern
  7. Observer Pattern
  8. State Pattern
  9. Strategy Pattern
  10. Template Pattern
  11. Visitor Pattern

JEE or J2EE Design Patterns

J2EE design patterns are built for developing Enterprise Web-based Applications.

In J2EE , there are mainly three types of design patterns, which are further divided into their sub-parts:

1. Presentation Layer Design Pattern

  1. Intercepting Filter Pattern
  2. Front Controller Pattern
  3. View Helper Pattern
  4. Composite View Pattern

2. Business Layer Design Pattern

  1. Business Delegate Pattern
  2. Service Locator Pattern
  3. Session Facade Pattern
  4. Transfer Object Pattern

3. Integration Layer Design Pattern

  1. Data Access Object Pattern
  2. Web Service Broker Pattern



Factory Pattern

Factory pattern is one of the most used design patterns in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.

In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.



https://www.tutorialspoint.com/design_pattern/factory_pattern.htm


Singleton:


package com.vasanth.designPattern.singleton;

public class SingleTon implements Cloneable {

private static SingleTon Obj;

// private constructor to force use of
// getInstance() to create Singleton object
private SingleTon(){}

@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}

//we can synchronize this method to make it as Thread safe
public static SingleTon getInstance(){
//synchronized (SingleTon.class) { To make it as Thread safe
if (Obj == null) {
System.out.println("Creating object for Singleton !!!");
Obj = new SingleTon();
}
//}
System.out.println(" returning Singleton Obj!!!");
return Obj;
}

}


package com.vasanth.designPattern.singleton;

public class SingletonDemo {

public static void main(String args[]) throws CloneNotSupportedException {

SingleTon.getInstance();
SingleTon.getInstance();
SingleTon.getInstance();
SingleTon.getInstance().clone();
}
}

https://www.geeksforgeeks.org/prevent-singleton-pattern-reflection-serialization-cloning/


Below 3 features will destroy the singlton patter, however, it can be prevented.

1. Cloning - Override clone() and throw the Clone not supported exception
2. Reflection - using Enums
3. Serialization   - using readResolve()


Comments

Popular posts from this blog

Java 8 Concepts with Examples