Design patterns in java.

Home/Java™/Design patterns in java.
  • Design pattern in Java

Software Design patterns in java are a custom set of practices that are reusable and used in programming. They solve the most common design-related problems in software development. The basic idea for creating the Design patterns is creating the relationship between classes and objects in a different way resulting in fast development along with a more flexible, reusable and maintainable set of code.

Design patterns are always implemented for a need in project development which solves the most common problems with in the project. To decide when to go with which pattern one must have the idea about the structure of all the design patterns and the uses for the same.


For instance, We do not want to create multiple duplicated objects as it can lead to some serious issues like memory leaks and low-performance in the application. As a solution to this serious and most-common problem, we can implement the prototype pattern which uses the clone() method to duplicate the objects that can be used as a prototype.

Types of Design Patterns

There’s plenty of design patterns available and we are also familiar with some of them already. Being more related to them can help us to increase the speed and quality of the work.

Most of these patterns apply to multiple languages, not just Java, but some, like the J2EE Design Patterns, is applicable mostly to Java.

There are three main design pattern categories as follow:

  • Creational Patterns: Creational patterns provide solutions by class instantiation and object creations. They use of inheritance effectively and delegation effectively to implement a correct solution, We will look at all the creational patterns available and the descriptions of those in further related posts.

    Use Case

    The most famous framework spring is the best example for the creational design pattern in which IOC works on a Factory pattern, where the creation logic of the objects is not known to the client itself. The client also uses the common interface for the creation of a new object.
  • Structural Patterns: Structural patterns provide solutions by composing the classes and objects resulting to form larger structures. This helps to ease the design by identifying the simplest way to understand the relationships between entities.

    Use Case:

    To add new functionality within an existing object without changing its original structure by acting as a wrapper if the existing class. This pattern is called the decorator pattern which wraps the original class and provides the additional functionality.
  • Behavioral Patterns:  Behavioral Patterns provide solutions by creating object interactions like how object communicates and how they are dependent on others along with how to segregate them to be both dependent and independent and provide both flexibility and testing capabilities.

    Use Case:

    An object guiding other objects about their responsibilities. For example, A linked list of handlers, which is able to process the requests. once the request is processed to the list, it is automatically transferred to the first handler available in the list which will process the request further. This is an example of a chain of responsibility design pattern.

List of Design Patterns

1. Creational Patterns

  1. Abstract Factory – Sets of methods to make various objects.
  2. Builder – Make and return one object in various ways.
  3. Factory Method – Methods to make and return components of one object in various ways.
  4. Prototype – Make new objects by cloning the objects which you set as prototypes.
  5. Singleton Pattern – A class distributes the only instance of itself.
  6. Object Pool Design Pattern – Use for a significant performance boost.

2. Structural patterns

  1. Adapter – Match interfaces of different classes
  2. Bridge – Separates an object’s interface from its implementation
  3. Composite – A tree structure of simple and composite objects
  4. Decorator – Add responsibilities to objects dynamically
  5. Facade – A single class that represents an entire subsystem
  6. Flyweight – A fine-grained instance used for efficient sharing Private Class Data Restricts accessor/mutator access
  7. Proxy – An object representing another object

3. Behavioral patterns

  1. Chain of responsibility – A way of passing a request between a chain of objects
  2. Command – Encapsulate a command request as an object
  3. Interpreter – A way to include language elements in a program
  4. Iterator – Sequentially access the elements of a collection
  5. Mediator – Defines simplified communication between classes
  6. Memento – Capture and restore an object’s internal state
  7. Null Object – Designed to act as a default value of an object
  8. Observer – A way of notifying a change to a number of classes
  9. State – Alter an object’s behavior when its state changes
  10. Strategy – Encapsulates an algorithm inside a class
  11. Template method – Defer the exact steps of an algorithm to a subclass
  12. Visitor – Defines a new operation to a class without change

You will learn all the above discussed Designed Patterns in Java. All the code examples are available on GitHub.

By |2020-03-18T15:53:07+00:00November 8th, 2019|Categories: Java™|Tags: , |