diff --git a/assets/images/abstractfactory.png b/assets/images/abstractfactory.png new file mode 100644 index 0000000..801ddaa Binary files /dev/null and b/assets/images/abstractfactory.png differ diff --git a/assets/images/adapter.png b/assets/images/adapter.png new file mode 100644 index 0000000..09ec674 Binary files /dev/null and b/assets/images/adapter.png differ diff --git a/assets/images/decorator.png b/assets/images/decorator.png new file mode 100644 index 0000000..33938ee Binary files /dev/null and b/assets/images/decorator.png differ diff --git a/assets/images/factory.png b/assets/images/factory.png new file mode 100644 index 0000000..8b61776 Binary files /dev/null and b/assets/images/factory.png differ diff --git a/assets/images/factorymethod.png b/assets/images/factorymethod.png new file mode 100644 index 0000000..4ea9087 Binary files /dev/null and b/assets/images/factorymethod.png differ diff --git a/assets/images/observer.png b/assets/images/observer.png new file mode 100644 index 0000000..c329387 Binary files /dev/null and b/assets/images/observer.png differ diff --git a/assets/images/producerconsumer.png b/assets/images/producerconsumer.png new file mode 100644 index 0000000..1178854 Binary files /dev/null and b/assets/images/producerconsumer.png differ diff --git a/assets/images/singleton.png b/assets/images/singleton.png new file mode 100644 index 0000000..134ee88 Binary files /dev/null and b/assets/images/singleton.png differ diff --git a/assets/images/strategy.png b/assets/images/strategy.png new file mode 100644 index 0000000..237e5fe Binary files /dev/null and b/assets/images/strategy.png differ diff --git a/assets/images/templatemethod.png b/assets/images/templatemethod.png new file mode 100644 index 0000000..c005d0b Binary files /dev/null and b/assets/images/templatemethod.png differ diff --git a/src/main/java/com/penapereira/example/constructs/abstractfactory/README.md b/src/main/java/com/penapereira/example/constructs/abstractfactory/README.md new file mode 100644 index 0000000..de6918b --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/abstractfactory/README.md @@ -0,0 +1,32 @@ +# Abstract Factory Pattern + +The abstract factory pattern provides an interface for creating families of related objects without specifying their concrete classes. This example defines two factories that each create a pair of products. + +## Class diagram + +![Class diagram](/assets/images/abstractfactory.png) + +```plantuml +@startuml +interface AbstractFactory { + +createProductA() + +createProductB() +} + +interface ProductA +interface ProductB + +class Factory1 implements AbstractFactory +class Factory2 implements AbstractFactory + +Factory1 --> ProductA1 +Factory1 --> ProductB1 +Factory2 --> ProductA2 +Factory2 --> ProductB2 + +class ProductA1 implements ProductA +class ProductA2 implements ProductA +class ProductB1 implements ProductB +class ProductB2 implements ProductB +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/adapter/README.md b/src/main/java/com/penapereira/example/constructs/adapter/README.md new file mode 100644 index 0000000..22c5669 --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/adapter/README.md @@ -0,0 +1,23 @@ +# Adapter Pattern + +The adapter pattern allows incompatible interfaces to work together. It wraps an existing class with a new interface so that it can be used as another type. + +## Class diagram + +![Class diagram](/assets/images/adapter.png) + +```plantuml +@startuml +interface Target { + +request() +} +class Adaptee { + +specificRequest() +} +class Adapter implements Target { + -adaptee : Adaptee + +request() +} +Adapter --> Adaptee +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/decorator/README.md b/src/main/java/com/penapereira/example/constructs/decorator/README.md new file mode 100644 index 0000000..2472b26 --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/decorator/README.md @@ -0,0 +1,21 @@ +# Decorator Pattern + +The decorator pattern attaches additional responsibilities to an object dynamically by wrapping it with decorator classes. + +## Class diagram + +![Class diagram](/assets/images/decorator.png) + +```plantuml +@startuml +interface ComponentIF { + +operation() +} +class ConcreteComponent implements ComponentIF +abstract class Decorator implements ComponentIF { + -component : ComponentIF +} +class ConcreteDecoratorA extends Decorator +ConcreteDecoratorA --> ComponentIF +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/factory/README.md b/src/main/java/com/penapereira/example/constructs/factory/README.md new file mode 100644 index 0000000..9c6b0a1 --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/factory/README.md @@ -0,0 +1,22 @@ +# Factory Pattern + +The factory pattern encapsulates object creation logic in a dedicated factory class. The factory decides which concrete product to instantiate. + +## Class diagram + +![Class diagram](/assets/images/factory.png) + +```plantuml +@startuml +interface Product { + +name() +} +class ConcreteProductA implements Product +class ConcreteProductB implements Product +class ProductFactory { + +createProduct(type) +} +ProductFactory --> ConcreteProductA +ProductFactory --> ConcreteProductB +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/factorymethod/README.md b/src/main/java/com/penapereira/example/constructs/factorymethod/README.md new file mode 100644 index 0000000..8040ebf --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/factorymethod/README.md @@ -0,0 +1,22 @@ +# Factory Method Pattern + +The factory method pattern defines an interface for creating an object but lets subclasses decide which class to instantiate. + +## Class diagram + +![Class diagram](/assets/images/factorymethod.png) + +```plantuml +@startuml +abstract class GenericProduct { + +factoryMethod() {abstract} + +build() +} +class ConcreteProductA extends GenericProduct { + +factoryMethod() +} +class ConcreteProductB extends GenericProduct { + +factoryMethod() +} +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/observer/README.md b/src/main/java/com/penapereira/example/constructs/observer/README.md new file mode 100644 index 0000000..f653cc0 --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/observer/README.md @@ -0,0 +1,25 @@ +# Observer Pattern + +The observer pattern defines a one-to-many dependency so that when one object changes state, its dependents are notified automatically. + +## Class diagram + +![Class diagram](/assets/images/observer.png) + +```plantuml +@startuml +interface ObservableInterface { + +getSupport() + +addPropertyChangeListener(l) + +removePropertyChangeListener(l) +} +abstract class ObservableAbstract implements ObservableInterface +class Observable extends ObservableAbstract { + +doSomethingWith(n) +} +class Observer implements PropertyChangeListener { + +propertyChange(e) +} +Observable --> Observer : notifies +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/producerconsumer/README.md b/src/main/java/com/penapereira/example/constructs/producerconsumer/README.md new file mode 100644 index 0000000..711fba0 --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/producerconsumer/README.md @@ -0,0 +1,22 @@ +# Producer / Consumer + +The Producer/Consumer is a concurrency model where producer threads generate data and place it into a shared, thread-safe buffer (like a queue), while consumer threads retrieve and process that data. It helps decouple the creation and processing of data, allowing both to operate at different speeds. Synchronization mechanisms like locks or semaphores are used to ensure safe access to the buffer, preventing race conditions and deadlocks. This pattern is commonly used in logging systems, task queues, and real-time processing pipelines to improve scalability and responsiveness in multithreaded applications. + +In this example implementation a java `BlockingQueue` is used as the thread-safe buffer while a thread pool of 2 consumers and 1 producer is launched through the `Executors` factory. + +## Class diagram + +![Class diagram](/assets/images/producerconsumer.png) + +```plantuml +@startuml +class Producer implements Runnable +class Consumer implements Runnable +class BlockingQueue { + +put() + +take() +} +Producer --> BlockingQueue +Consumer --> BlockingQueue +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/singleton/README.md b/src/main/java/com/penapereira/example/constructs/singleton/README.md new file mode 100644 index 0000000..7f99a5a --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/singleton/README.md @@ -0,0 +1,18 @@ +# Singleton Pattern + +The singleton pattern ensures a class has only one instance while providing a global point of access to it. + +## Class diagram + +![Class diagram](/assets/images/singleton.png) + +```plantuml +@startuml +class Singleton { + -uniqueInstance : Singleton + -Singleton() + +instance() : Singleton + +doSomething() +} +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/strategy/README.md b/src/main/java/com/penapereira/example/constructs/strategy/README.md new file mode 100644 index 0000000..01d1eb6 --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/strategy/README.md @@ -0,0 +1,23 @@ +# Strategy Pattern + +The strategy pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable at runtime. + +## Class diagram + +![Class diagram](/assets/images/strategy.png) + +```plantuml +@startuml +interface Strategy { + +executeAlgorithm() +} +class StrategyImpl1 implements Strategy +class StrategyImpl2 implements Strategy +class Context { + -strategy : Strategy + +operation() + +setStrategy(Strategy) +} +Context --> Strategy +@enduml +``` diff --git a/src/main/java/com/penapereira/example/constructs/templatemethod/README.md b/src/main/java/com/penapereira/example/constructs/templatemethod/README.md new file mode 100644 index 0000000..8eb2c4f --- /dev/null +++ b/src/main/java/com/penapereira/example/constructs/templatemethod/README.md @@ -0,0 +1,25 @@ +# Template Method Pattern + +The template method pattern defines the skeleton of an algorithm in a base class and lets subclasses override specific steps. + +## Class diagram + +![Class diagram](/assets/images/templatemethod.png) + +```plantuml +@startuml +abstract class AbstractClass { + +templateMethod() + #stepOne() {abstract} + #stepTwo() {abstract} +} +class ConcreteClassA extends AbstractClass { + #stepOne() + #stepTwo() +} +class ConcreteClassB extends AbstractClass { + #stepOne() + #stepTwo() +} +@enduml +``` diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 44ac060..5fb1a0c 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -12,7 +12,7 @@ app.linkColorHover=#3366BB msg.windowTitle=Java Patterns and Constructs msg.greeting=Welcome to Java Patterns and Constructs -msg.info=You can check what is implemented in the project's home: +msg.info=You can check documentation, examples and diagrams at: msg.homeUrl=https://github.com/lpenap/java-patterns-and-constructs msg.examplesFound=Implemented examples found