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.
Located in CreationalDesignPattern/ (see its dedicated Readme.md for full details).
Implemented patterns include:
- Abstract Factory –
AbstractFactory/* - Builder –
Builder/* - Factory Method –
Factory/* - Prototype –
Prototype/* - Singleton –
Singelton/*(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.
Located in StructuralDesignPatterns/.
Implemented patterns include:
-
Adapter –
Adapter/*
Adapts incompatible interfaces so that classes can collaborate (e.g.,SquarePegAdapterto fit a square peg into a round hole). -
Composite –
Composite/*
Treats individual objects and compositions uniformly (e.g.,Shape,Circle,Rectangle,CompoundShape, with anImageEditorclient). -
Decorator –
Decorators/Decorators/*
Dynamically adds behavior to objects by wrapping them (e.g.,DataSourcewrapped byEncryptionDecoratorandCompressionDecorator). -
Facade –
Facade/*
Provides a simplified interface (VideoConversionFacade) to a complex subsystem (some_complex_media_library). -
Proxy –
Proxy/*
Represents another object and controls access to it (e.g.,YouTubeCacheProxyin front ofThirdPartyYouTubeClass).
Located in BehavioralDesignPattern/.
Implemented patterns include:
-
Chain of Responsibility –
ChainOfResponsibility/*
Passes requests along a chain of handlers (LoggerandConcreteHandlers) until one of them handles the request. -
Command –
Command/*
Encapsulates requests as objects (Commandimplementations likeCopyCommand,CutCommand,PasteCommand) with anEditorandCommandHistory. -
Iterator –
Iterator/*
Provides a way to sequentially access elements of a collection without exposing its internal structure (social network example withProfileIterator,Facebook,LinkedIn,SocialSpammer). -
Observer –
Observer/*
Defines a one‑to‑many dependency between objects (e.g.,EditorusesEventManagerto notifyEmailNotificationListener,LogOpenListenerobservers). -
Strategy –
Strategy/*
Defines a family of algorithms (PayByCreditCard,PayByPayPalimplementingPayStrategy) encapsulated and interchangeable at runtime viaOrderandDemo.
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 anyDemo(or othermain) 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.