Wednesday, August 1, 2018

Design Pattern in Java



Design Pattern in Java
  • A design pattern are well proved solution for the specific problem.
  • Independent of programming language.
  • we can make our code more flexible, usable and maintainable.
  • well proved solution for solving specific task.
  • Best practices used by experienced object oriented software developers.
  • used during the analysis of SDLC.

  • These authors are collectively known as Gang of Four (GOF). According to these authors design patterns are primarily based on the following principles of object orientated design.
    • Program to an interface not an implementation
    • Favor object composition over inheritance

Advantage:

    • They are reusable in multiple projects.
    • provide the solution for helping the system architecture.
    • provides transparency to the application.
In core java mainly 3 types of design patterns are there.

  • Creational Design Pattern

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

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

  •  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
Creational Design Pattern:

concerned with the way of creating objects. 


Factor Method Pattern:


  • Defines an interface or abstract class for creating an object, but let sub classes decides which object to instantiate.
  • Factory method lets a class defer instantiation to sub classes.




Implementation:



  • Provide an interface for creating an object. Here don't consider the word interface as java interface. it simply means to provide a contract/method to create an object.
  • let the sub class decide what exact object to instantiate. This pattern involves inheritance.
  • also known as Virtual constructor.
Example :




Abstract Factory Design Pattern :

  • It works around a super factory which create other factories.
  • This is also called factory of factories.
  • Inteface is responsible for creating a factory of related objects without explicitly specifying their classes.
Example:





Singleton Pattern:


  • Ensure that a class has only one instance and provide a global point of access to it.




Implementation:


  • Make sure that there is one instance: restrict constructor , make constructor private and let the class manage its instance.
  • provide a global point of access , a static method to get the solo instance.
Why Hard?

  • Reflection
  • Serialization/ deserialization
  • clone
  • multi threaded access
  • multiple class loader
  • Garbage Collection
How to Fix?

  • Reflection: enums are used because java ensures internally that enum value is instantiated only once. Since java Enums are globally accessible, they can be used for singletons. Its only drawback is that it is not flexible i.e it does not allow lazy initialization.
  • Serialization/ deserialization - To overcome this issue, we have to implement method readResolve() 
  • clone() : override the clone() method and throw an exception that is CloneNotSupportedException.