You are here
Home > java > Core Java >

Java Design Patterns

Java Design PatternsIf you want to learn ‘Java Design Patterns’ in less time without missing any concept, you are at the right place. Moreover, if you want to appear in a Java interview, go through this article once. Additionally, If you want to have a quick revision of your concepts on Java Design Patterns which you had learnt a log time before, then also you are at the right place. However, if you are looking for one liner definition of each Design Pattern to memorize them, then also you are at the right place. Then, visit the last section of this article (Design Pattern’s Cheat Sheet).

In this article, we will discuss about all GoF (Gang of Four) design patterns. The 23 design patterns by GOF are well known, and more are to be discovered on the way. We will start with some basic concepts that are also expected from a developer. Let’s start with our topic ‘Java Design Patterns’ and other concepts related to it.

What is a Design Pattern?

In a software industry, Design Pattern is a description or guideline to solve a problem that occurs repeatedly while developing a software Application. Some people in the industry also call it a template as it solves the problems while developing the application. When we talk about these types of solutions to recurring problems in Java language, they become ‘Design Patterns in Java’ or ‘Java Design Patterns’.

Why these Design Patterns named as GoF?

It was 21 October 1994, when four authors Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides, with a foreword by Grady Booch, published a book titled as Design Patterns – Elements of Reusable Object-Oriented Software which launched the concept of Design Pattern in Software design. These four authors are altogether known as the Gang of Four (GoF). Since then, these are called GoF Design Patterns.

Why do we need to learn Design Patterns?

Design Patterns help us to solve a problem during project development. Moreover, Design Patterns will help us in fixing performance and memory related issues. If you have strong concepts in Java Design Patterns, your confidence level will also be high because design patterns itself provide you solutions for many common use cases. Also, you will never feel low while developing a project. Even you will be able to suggest a better solution to a problem instead of the existing one.

If you are strong in design patterns, you will complete your development earlier than the expected time and number of bugs will also be very negligible. In contrast, if you are weak in design patterns, you will face a lot of problems while developing, even sometimes you may need to modify your code from the beginning, that can result in pain & frustration.

If you are planning to apply for a job as an experienced developer, keep in mind that your interview will not be completed without  questions on Design Patterns.

Please note that Design Patterns are not closely related to Java Application Design. However, they help to to design a better software. Particularly, they are useful to us while we think about a solution to a problem during project development.

What are the advantages/benefits of using Design Patterns?

1) Design Patterns help in finding the solution of a complex problem.
2) Using design patterns, we can make our code loosely-coupled.
3) Furthermore, it will have the option of reusable codes, which reduce the total development cost of the application.
4) Additionally, future developers feel the code more user-friendly.
5) It has all the standard approaches to find out solutions to the common problem of a software.
6) We can use the same pattern repeatedly in multiple projects.
7) Moreover, It helps in refactoring our code in a better way.

How are Design Patterns useful in your Professional Career?

If you know Design Patterns, it will lead to better code structure, better communication, and, best of all, it will save you a lot of time that you can spend on cooler things. Moreover, Design Patterns will also be useful in your Professional Career in multiple places.

♦ In Code Comments and Naming Conventions: When you’re writing code, clearly identify the patterns you’re using in the comments. Also, choose class and method names that follow any patterns. As a result, other developers who have to read your code will thank you for allowing them to quickly understand your implementation.

♥ In Architecture Documentation: When you write architectural documentation, using patterns will reduce the amount of documentation you need to write and gives the reader a clearer picture of the design.

♦ In Groups of Interested Developers: Share your knowledge as much as possible. Generally, many developers have heard about patterns, but don’t have a good understanding of what they are. Volunteer to give a brown-bag lunch on patterns or a talk at your local user group.

♥ In Project Meetings: When you meet with your team to discuss a software design, use design patterns to help stay “in the design” longer.

Click on the links below to go through the topics: Step by Step Tutorials

Links to Each Design Patterns in Detail With Examples

Creational Design Patterns

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

Structural Design Patterns

  1. Adapter Pattern
  2. Composite Pattern
  3. Proxy Pattern
  4. Flyweight Pattern
  5. Façade Pattern
  6. Bridge Pattern
  7. Decorator Pattern

Behavioral Design Patterns

  1. Template Method Pattern
  2. Mediator Pattern
  3. Chain of Responsibility Pattern
  4. Observer Pattern
  5. Strategy Pattern
  6. Command Pattern
  7. State Pattern
  8. Visitor Pattern
  9. Iterator Pattern
  10. Interpreter Pattern
  11. Memento Pattern

Design Pattern’s Cheat Sheet

In fact, our target is to make you remember the Design Patterns in Java as a vocabulary. So, now it’s time to provide the short descriptions of each design pattern so that you can memorize it easily. Below is the cheat sheet of Design patterns in Java as a one liner definition of each patterns, just to memorize while revising the concepts. However, keep in mind that patterns are not kept in sequence to make you exercise in a better way…

Table#1

Pattern
Description
Decorator
Wraps an object to provide new behaviors
State
Encapsulate state-based behaviors and uses delegation to switch between behaviors
Iterator
Provides a way to traverse a collection of objects without exposing its implementation
Façade
Simplifies the interface of a set of classes
Strategy
Encapsulate interchangeable behavior and uses delegation to decide which one to use
Proxy
Wraps an object to control access to it
Factory Method
Subclasses decide which concrete class to create
Adapter
Wraps an object and provides a different interface to it
Observer
Allows objects to be notified when state changes
Template Method
Subclasses decide how to implement steps in an algorithm
Composite
Clients treat collection of objects an individual object uniformly
Singleton
Ensures one and only one object is created
Abstract Factory
Allows a client to create families of objects without specifying their concrete classes

Table#2

Pattern
Description
Command
Encapsulates a request as an object
Bridge
Allows abstractions & implementations to be extended independently
Builder
Encapsulate the construction of a product & allows it to be constructed in steps
Chain of Responsibility
Allows to add or remove responsibilities dynamically by changing the members or order of the members in the chain
Flyweight
Reduces the number of object instances at runtime, saving memory
Interpreter
Builds an interpreter for a language
Mediator
Centralizes complex communications and controls between related objects
Memento
Returns an object to one of its previous state
Prototype
Creates an instance of a given class when creation is either expensive or complicated
Visitor
Adds capabilities to a composite of Objects when encapsulation is not important

Links to OOPs Design Principles & SOLID Principles

OOPs Design Principles

SOLID Design Principles

close

Leave a Reply

Top