Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Design Patterns – Detailed Java Implementations

This repository contains hands-on Java implementations of the Gang of Four (GoF) design patterns, organized into three main groups:

  • Creational patterns under src/main/java/CreationalDesignPattern
  • Structural patterns under src/main/java/StructuralDesignPatterns
  • Behavioral patterns under src/main/java/BehavioralDesignPattern

Each pattern comes with small, focused examples and usually a Demo class that you can run directly.


Pattern Groups & Implementations

Creational Design Patterns

Located in CreationalDesignPattern/ (see its dedicated Readme.md for full details).

Implemented patterns include:

  • Abstract FactoryAbstractFactory/*
  • BuilderBuilder/*
  • Factory MethodFactory/*
  • PrototypePrototype/*
  • SingletonSingelton/* (naive single-threaded, naive multi-threaded, and thread-safe variants)

Each package has a Demo class (for example, CreationalDesignPattern.AbstractFactory.Demo) that shows how the pattern is used in practice.

Structural Design Patterns

Located in StructuralDesignPatterns/.

Implemented patterns include:

  • AdapterAdapter/*
    Adapts incompatible interfaces so that classes can collaborate (e.g., SquarePegAdapter to fit a square peg into a round hole).

  • CompositeComposite/*
    Treats individual objects and compositions uniformly (e.g., Shape, Circle, Rectangle, CompoundShape, with an ImageEditor client).

  • DecoratorDecorators/Decorators/*
    Dynamically adds behavior to objects by wrapping them (e.g., DataSource wrapped by EncryptionDecorator and CompressionDecorator).

  • FacadeFacade/*
    Provides a simplified interface (VideoConversionFacade) to a complex subsystem (some_complex_media_library).

  • ProxyProxy/*
    Represents another object and controls access to it (e.g., YouTubeCacheProxy in front of ThirdPartyYouTubeClass).

Behavioral Design Patterns

Located in BehavioralDesignPattern/.

Implemented patterns include:

  • Chain of ResponsibilityChainOfResponsibility/*
    Passes requests along a chain of handlers (Logger and ConcreteHandlers) until one of them handles the request.

  • CommandCommand/*
    Encapsulates requests as objects (Command implementations like CopyCommand, CutCommand, PasteCommand) with an Editor and CommandHistory.

  • IteratorIterator/*
    Provides a way to sequentially access elements of a collection without exposing its internal structure (social network example with ProfileIterator, Facebook, LinkedIn, SocialSpammer).

  • ObserverObserver/*
    Defines a one‑to‑many dependency between objects (e.g., Editor uses EventManager to notify EmailNotificationListener, LogOpenListener observers).

  • StrategyStrategy/*
    Defines a family of algorithms (PayByCreditCard, PayByPayPal implementing PayStrategy) encapsulated and interchangeable at runtime via Order and Demo.


How to Run the Examples

From the project root:

  • Using Maven (if configured):
    mvn exec:java -Dexec.mainClass="<FullyQualifiedMainClass>"

    For example:
    mvn exec:java -Dexec.mainClass="CreationalDesignPattern.AbstractFactory.Demo"

  • Using an IDE (IntelliJ, Eclipse, VS Code, etc.):
    Open the project, navigate to any Demo (or other main) class for a pattern and run it directly.

Refer to src/main/java/CreationalDesignPattern/Readme.md for more detailed notes on the creational patterns and their structure.