This repository contains examples of various design patterns implemented in Java. Each design pattern is provided as a separate Java file and can be run individually.
-
Factory Pattern
- Location: FactoryPattern.java
- Description: Demonstrates the Factory pattern, which provides an interface for creating objects but lets subclasses decide which class to instantiate.
-
Builder Pattern
- Location: BuilderPattern.java
- Description: Demonstrates the Builder pattern, which separates the construction of an object from its representation, allowing the same construction process to create various representations.
-
Singleton Pattern
- Location: Singleton.java
- Description: Demonstrates the Singleton pattern, which ensures a class has only one instance and provides a global point of access to that instance.
-
Pub/Sub Pattern
- Location: Publisher.java, Subscriber.java, ExampleSubscriber.java
- Description: Demonstrates the Pub/Sub (Publish/Subscribe) pattern, which allows decoupling between publishers and subscribers, enabling one-to-many communication.
-
Iterator Pattern
- Location: Item.java, ItemCollection.java
- Description: Demonstrates the Iterator pattern, which provides a way to access the elements of an aggregate object sequentially without exposing its underlying structure.
-
Strategy Pattern
- Location: SortingStrategy.java, BubbleSortStrategy.java, QuickSortStrategy.java, SortingContext.java
- Description: Demonstrates the Strategy pattern, which enables the selection of an algorithm dynamically at runtime by encapsulating interchangeable algorithms in separate classes.
-
Facade Pattern
- Location: SubsystemA.java, SubsystemB.java, SubsystemC.java, Facade.java
- Description: Demonstrates the Facade pattern, which provides a simplified interface to a complex subsystem, making it easier to use and decoupling clients from the subsystem's implementation details.
-
Adapter Pattern
- Location: Adaptee.java, Target.java, Adapter.java
- Description: Demonstrates the Adapter pattern, which allows objects with incompatible interfaces to work together by providing a common interface that clients can use.
To run the examples, you can compile each Java file individually and execute the compiled class using the java
command. For example:
javac FactoryPattern.java
java FactoryPattern