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:
- Core Java (or JSE) Design Patterns.
- 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
- Factory Pattern
- Abstract Factory Pattern
- Singleton Pattern
- Prototype Pattern
- Builder Pattern.
2. Structural Design Pattern
- Adapter Pattern
- Bridge Pattern
- Composite Pattern
- Decorator Pattern
- Facade Pattern
- Flyweight Pattern
- Proxy Pattern
3. Behavioral Design Pattern
- Chain Of Responsibility Pattern
- Command Pattern
- Interpreter Pattern
- Iterator Pattern
- Mediator Pattern
- Memento Pattern
- Observer Pattern
- State Pattern
- Strategy Pattern
- Template Pattern
- 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
- Intercepting Filter Pattern
- Front Controller Pattern
- View Helper Pattern
- Composite View Pattern
2. Business Layer Design Pattern
- Business Delegate Pattern
- Service Locator Pattern
- Session Facade Pattern
- Transfer Object Pattern
3. Integration Layer Design Pattern
- Data Access Object Pattern
- 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/
Comments
Post a Comment