Design patterns in java

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


Software Design patterns in java are a custom set of best practices that are reusable in solving common programming issues. 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 an idea about the structure of all the design patterns and their usages.

For example, 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, Creational Design patterns, Structural Patterns, and Behavioural patterns.

1. Creational Design Patterns

The Creational patterns provide solutions by class instantiation and object creations. They make use of inheritance and delegation effectively to implement a correct solution. Details of each of the creational design patterns are listed below.

  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.

An example, the most famous Spring Framework is the best example of the creational design pattern in which IOC works on a Factory pattern. Here 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.

2. Structural Design Patterns

The 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.

  1. Adapter Pattern – 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

An example, 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.

3. Behavioural Design Patterns

The 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.

  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

For example, an object guiding other objects about their responsibilities. 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.

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

By |2020-05-09T15:40:52+00:00November 8th, 2019|Categories: Java™|Tags: , |