Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/images/abstractfactory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/adapter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/decorator.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/factory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/factorymethod.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/observer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/producerconsumer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/singleton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/strategy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/templatemethod.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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
```
23 changes: 23 additions & 0 deletions src/main/java/com/penapereira/example/constructs/adapter/README.md
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
22 changes: 22 additions & 0 deletions src/main/java/com/penapereira/example/constructs/factory/README.md
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
@@ -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
```
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down