Menu

Drop Down MenusCSS Drop Down Menu Pure CSS Dropdown Menu

Java Design Patterns


Design Patterns are very popular among software developers. A design pattern is a well described solution to a common software problem. I have written extensively on java design patterns.


Some of the benefits of using design patterns are:
  1. Design Patterns are already defined and provides industry standard approach to solve a recurring problem, so it saves time if we sensibly use the design pattern. There are many java design patterns that we can use in our java based projects.
  2. Using design patterns promotes reusability that leads to more robust and highly maintainable code. It helps in reducing total cost of ownership (TCO) of the software product.
  3. Since design patterns are already defined, it makes our code easy to understand and debug. It leads to faster development and new members of team understand it easily.
Java Design Patterns are divided into three categories – creationalstructural, and behavioral design patterns. This post serves as an index for all the java design patterns articles I have written so far.

      1. Creational Design Patterns
           Creational design patterns provide solution to instantiate a object in the best possible way for specific situations.Creational Design Pattern is divided into five categories:


  1. Singleton Pattern

    Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. It seems to be a very simple design pattern but when it comes to implementation, it comes with a lot of implementation concerns. The implementation of Singleton pattern has always been a controversial topic among developers. Check out Singleton Design Pattern to learn about different ways to implement Singleton pattern and pros and cons of each of the method. This is one of the most discussed java design patterns.
  2. Factory Pattern

    Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern take out the responsibility of instantiation of a class from client program to the factory class. We can apply Singleton pattern on Factory class or make the factory method static. Check out Factory Design Pattern for example program and factory pattern benefits. This is one of the most widely used java design pattern.
  3. Abstract Factory Pattern

    Abstract Factory pattern is similar to Factory pattern and it’s factory of factories. If you are familiar with factory design pattern in java, you will notice that we have a single Factory class that returns the different sub-classes based on the input provided and factory class uses if-else or switch statement to achieve this.
    In Abstract Factory pattern, we get rid of if-else block and have a factory class for each sub-class and then an Abstract Factory class that will return the sub-class based on the input factory class. Check out Abstract Factory Pattern to know how to implement this pattern with example program.
  4. Builder Pattern

    This pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes. Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object. Check out Builder Pattern for example program and classes used in JDK.
  5. Prototype Pattern

    Prototype pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. So this pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs. This pattern uses java cloning to copy the object.
    Prototype design pattern mandates that the Object which you are copying should provide the copying feature. It should not be done by any other class. However whether to use shallow or deep copy of the Object properties depends on the requirements and its a design decision. Check out Prototype Pattern for sample program.

     2. Structural Design Patterns
         Structural patterns provide different ways to create a class structure, for example using inheritance and composition to create a large object from small objects.
  
    3. Behavioral Design Patterns
         Behavioral patterns provide solution for the better interaction between objects and how to provide lose coupling and flexibility to extend easily.

No comments:

Post a Comment

Allah Is Almighty