Saturday, August 19, 2023

Demystifying Design Patterns in Java

 It all began when a famous group called the "Gang of Four" wrote a book about Design Patterns. The Gang of Four included Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. They came up with 23 different patterns to solve problems that often appear when designing software.

These patterns are divided into three groups:

  1. Creational Patterns: These help us figure out the best way to create objects for different situations.

    • Singleton: Use this when you only want one instance of a class.
    • Factory Method: Creates objects from a group of classes based on certain conditions.
    • Abstract Factory: It's like a higher level of the factory method. It picks a factory based on certain conditions.
    • Builder: Builds complex objects step by step. It keeps the process of creating an object separate from how it's shown.
    • Prototype: Makes a copy of an existing object.

  2. Structural Patterns: These help us put classes and objects together to make new functions.


    • Adapter: Helps two different things work together even if they don't fit well at first.
    • Decorator: Adds or changes things about an object without changing it too much.
    • Composite: Helps build structures with parts inside other parts.
    • Facade: Makes a simple way to use a complicated system with many parts.
    • Flyweight: Saves time and memory by using objects that already exist.
    • Proxy: Acts as a stand-in for another object, often for controlling access or adding extra features.
    • Bridge: Separates how something works from how it looks so they can change without affecting each other.

  3. Behavioral Patterns: These are about how objects communicate with each other.


    • Chain of Responsibility: Keeps the sender of a request separate from who gets it.
    • Memento: Lets an object go back to an older state.
    • Template: A main class gives the basic steps of a plan, and special classes add the details.
    • Observer: Sets up a way for one object to tell many others about changes.
    • State: Makes an object behave differently when its situation changes.
    • Iterator: Helps go through a group of things without knowing how they're stored.
    • Mediator: Helps objects communicate without being too connected.
    • Command: Sets up a way to ask for things without knowing what happens behind the scenes.
    • Strategy: Chooses one way of doing something from a group of ways.
    • Visitor: Lets you make a new action without changing the things it works on. It moves the action to a different class.
    • Interpreter: Does things with a special language. It has rules for the language and a way to understand and do the rules.

These design patterns are like tools in a programmer's toolbox. Each pattern helps you solve a certain kind of problem in a smart and organized way. Just like following a recipe when cooking, using these patterns can make your code easier to understand and maintain.

Must Read Design Pattern Articles: 

Saturday, August 12, 2023

Java Collection Hierarchy: Overview

The Java Collection Framework provides a unified architecture to store, manipulate, and retrieve groups of objects. It offers a set of interfaces, classes, and algorithms that simplify the process of handling collections of data. Whether you're dealing with lists, sets, maps, or queues, the Collection Framework has you covered. In this guide, we'll delve into the details of the Collection Framework, its components, and how to effectively use them in your Java applications.

Hierarchy of Collection Interfaces

The Java Collection Framework consists of a hierarchy of interfaces that define various types of collections. This hierarchy provides a unified way to work with different types of data structures, allowing you to choose the appropriate collection type based on your needs. Here's the hierarchy of key collection interfaces:

  1. Collection Interface:

    • The root interface of the collection hierarchy.
    • Represents a group of objects, also known as elements.
    • Doesn't specify any particular order for elements.
    • Extends the Iterable interface, allowing iteration over its elements.
  2. List Interface:

    • Represents an ordered collection (sequence) of elements.
    • Allows duplicate elements and maintains the order of insertion.
    • Provides methods for positional access, search, and manipulation.
    • Main implementations: ArrayList, LinkedList, Vector.
  3. Set Interface:

    • Represents an unordered collection of unique elements.
    • Ensures that no duplicate elements are allowed.
    • Doesn't define the concept of positional access.
    • Main implementations: HashSet, LinkedHashSet, TreeSet.
  4. Queue Interface:

    • Represents a collection designed for holding elements prior to processing.
    • Typically supports operations like insertion, removal, and inspection.
    • Provides methods for inserting, removing, and inspecting elements.
    • Main implementations: LinkedList, PriorityQueue, ArrayDeque.
  5. Deque Interface:

    • Extends the Queue interface to support double-ended queues (dequeues).
    • Allows insertion and removal of elements from both ends.
    • Supports stack-like and queue-like behavior.
    • Main implementations: ArrayDeque, LinkedList.
  6. Map Interface:

    • Represents a collection of key-value pairs, where each key maps to a value.
    • Doesn't allow duplicate keys (each key is unique).
    • Provides methods for insertion, retrieval, and removal of key-value pairs.
    • Main implementations: HashMap, LinkedHashMap, TreeMap.
  7. SortedSet Interface:

    • Extends the Set interface to support a sorted set of elements.
    • Elements are ordered according to their natural ordering or a specified comparator.
    • Main implementation: TreeSet.
  8. SortedMap Interface:

    • Extends the Map interface to support a sorted map of key-value pairs.
    • Entries are ordered according to the keys' natural ordering or a specified comparator.
    • Main implementation: TreeMap.
  9. NavigableSet Interface:

    • Extends the SortedSet interface with additional navigation methods.
    • Allows the retrieval of elements based on specific criteria.
    • Main implementation: TreeSet.
  10. NavigableMap Interface:

    • Extends the SortedMap interface with additional navigation methods.
    • Allows the retrieval of key-value pairs based on specific criteria.
    • Main implementation: TreeMap.
Related Articles:




Spring Framework: A Comprehensive Guide

 The Spring Framework is a powerful and versatile framework for building robust and scalable Java applications. Over the years, it has gaine...