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
3 changes: 1 addition & 2 deletions abstract-document/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ folder: abstract-document
permalink: /patterns/abstract-document/
categories: Structural
tags:
- Java
- Difficulty-Intermediate
- Extensibility
---

## Intent
Expand Down
4 changes: 1 addition & 3 deletions abstract-factory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ folder: abstract-factory
permalink: /patterns/abstract-factory/
categories: Creational
tags:
- Java
- Gang Of Four
- Difficulty-Intermediate
- Gang of Four
---

## Also known as
Expand Down
3 changes: 1 addition & 2 deletions acyclic-visitor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ folder: acyclic-visitor
permalink: /patterns/acyclic-visitor/
categories: Behavioral
tags:
- Java
- Difficulty-Intermediate
- Extensibility
---

## Intent
Expand Down
4 changes: 1 addition & 3 deletions adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ folder: adapter
permalink: /patterns/adapter/
categories: Structural
tags:
- Java
- Gang Of Four
- Difficulty-Beginner
- Gang of Four
---

## Also known as
Expand Down
3 changes: 1 addition & 2 deletions aggregator-microservices/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ folder: aggregator-microservices
permalink: /patterns/aggregator-microservices/
categories: Architectural
tags:
- Java
- Spring
- Cloud distributed
---

## Intent
Expand Down
3 changes: 1 addition & 2 deletions ambassador/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ folder: ambassador
permalink: /patterns/ambassador/
categories: Structural
tags:
- Java
- Difficulty-Intermediate
- Decoupling
---

## Intent
Expand Down
5 changes: 2 additions & 3 deletions api-gateway/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ folder: api-gateway
permalink: /patterns/api-gateway/
categories: Architectural
tags:
- Java
- Difficulty-Intermediate
- Spring
- Cloud distributed
- Decoupling
---

## Intent
Expand Down
3 changes: 0 additions & 3 deletions async-method-invocation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ folder: async-method-invocation
permalink: /patterns/async-method-invocation/
categories: Concurrency
tags:
- Java
- Difficulty-Intermediate
- Functional
- Reactive
---

Expand Down
3 changes: 1 addition & 2 deletions balking/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ folder: balking
permalink: /patterns/balking/
categories: Concurrency
tags:
- Java
- Difficulty-Beginner
- Decoupling
---

## Intent
Expand Down
4 changes: 1 addition & 3 deletions bridge/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ folder: bridge
permalink: /patterns/bridge/
categories: Structural
tags:
- Java
- Gang Of Four
- Difficulty-Intermediate
- Gang of Four
---

## Also known as
Expand Down
4 changes: 1 addition & 3 deletions builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ folder: builder
permalink: /patterns/builder/
categories: Creational
tags:
- Java
- Gang Of Four
- Difficulty-Intermediate
- Gang of Four
---

## Intent
Expand Down
5 changes: 2 additions & 3 deletions business-delegate/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ layout: pattern
title: Business Delegate
folder: business-delegate
permalink: /patterns/business-delegate/
categories: Business Tier
categories: Structural
tags:
- Java
- Difficulty-Intermediate
- Decoupling
---

## Intent
Expand Down
3 changes: 1 addition & 2 deletions bytecode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ folder: bytecode
permalink: /patterns/bytecode/
categories: Behavioral
tags:
- Java
- Difficulty-Beginner
- Game programming
---

## Intent
Expand Down
4 changes: 1 addition & 3 deletions caching/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ layout: pattern
title: Caching
folder: caching
permalink: /patterns/caching/
categories: Other
categories: Behavioral
tags:
- Java
- Difficulty-Intermediate
- Performance
---

Expand Down
7 changes: 2 additions & 5 deletions callback/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ layout: pattern
title: Callback
folder: callback
permalink: /patterns/callback/
categories: Other
categories: Idiom
tags:
- Java
- Difficulty-Beginner
- Functional
- Idiom
- Reactive
---

## Intent
Expand Down
4 changes: 1 addition & 3 deletions chain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ folder: chain
permalink: /patterns/chain/
categories: Behavioral
tags:
- Java
- Gang Of Four
- Difficulty-Intermediate
- Gang of Four
---

## Intent
Expand Down
6 changes: 3 additions & 3 deletions circuit-breaker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ layout: pattern
title: Circuit Breaker
folder: circuit-breaker
permalink: /patterns/circuit-breaker/
categories: Other
categories: Behavioral
tags:
- Java
- Performance
- Difficulty-Intermediate
- Decoupling
---

## Intent
Expand Down Expand Up @@ -179,6 +178,7 @@ Use the Circuit Breaker pattern when
- [Retry Pattern](https://github.com/iluwatar/java-design-patterns/tree/master/retry)

## Real world examples

* [Spring Circuit Breaker module](https://spring.io/guides/gs/circuit-breaker)
* [Netflix Hystrix API](https://github.com/Netflix/Hystrix)

Expand Down
8 changes: 3 additions & 5 deletions collection-pipeline/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ layout: pattern
title: Collection Pipeline
folder: collection-pipeline
permalink: /patterns/collection-pipeline/
categories: Other
categories: Functional
tags:
- Java
- Difficulty-Beginner
- Functional
- Reactive
---

## Intent
Expand All @@ -28,4 +26,4 @@ Use the Collection Pipeline pattern when

* [Function composition and the Collection Pipeline pattern](https://www.ibm.com/developerworks/library/j-java8idioms2/index.html)
* [Martin Fowler](https://martinfowler.com/articles/collection-pipeline/)
* [Java8 Streams](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html)
* [Java8 Streams](https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html)
9 changes: 5 additions & 4 deletions combinator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ layout: pattern
title: Combinator
folder: combinator
permalink: /patterns/combinator/
categories: Behavioral
categories: Idiom
tags:
- Functional
- Reactive
- Idiom
---

## Also known as
Expand All @@ -23,13 +21,16 @@ and some "combinators" which can combine values of type T in various ways to bui

## Applicability
Use the combinator pattern when:

- You are able to create a more complex value from more plain values but having the same type(a combination of them)

## Real world examples

- java.util.function.Function#compose
- java.util.function.Function#andThen

## Credits

- [Example for java](https://gtrefs.github.io/code/combinator-pattern/)
- [Combinator pattern](https://wiki.haskell.org/Combinator_pattern)
- [Combinatory logic](https://wiki.haskell.org/Combinatory_logic)
- [Combinatory logic](https://wiki.haskell.org/Combinatory_logic)
5 changes: 1 addition & 4 deletions command/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ folder: command
permalink: /patterns/command/
categories: Behavioral
tags:
- Java
- Gang Of Four
- Difficulty-Intermediate
- Functional
- Gang of Four
---

## Also known as
Expand Down
6 changes: 3 additions & 3 deletions commander/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ layout: pattern
title: Commander
folder: commander
permalink: /patterns/commander/
categories: Other
categories: Concurrency
tags:
- Java
- Difficulty-Intermediate
- Cloud distributed
---

## Intent
Expand All @@ -24,4 +23,5 @@ Handling distributed transactions can be tricky, but if we choose to not handle
We need a mechanism in place which can handle these kinds of situations. We have to direct the order to either one of the services (in this example, shipping) and then add the order into the database of the other service (in this example, payment), since two databses cannot be updated atomically. If currently unable to do it, there should be a queue where this request can be queued, and there has to be a mechanism which allows for a failure in the queueing as well. All this needs to be done by constant retries while ensuring idempotence (even if the request is made several times, the change should only be applied once) by a commander class, to reach a state of eventual consistency.

## Credits

* [https://www.grahamlea.com/2016/08/distributed-transactions-microservices-icebergs/]
4 changes: 1 addition & 3 deletions composite/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ folder: composite
permalink: /patterns/composite/
categories: Structural
tags:
- Java
- Gang Of Four
- Difficulty-Intermediate
- Gang of Four
---

## Intent
Expand Down
25 changes: 15 additions & 10 deletions converter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ layout: pattern
title: Converter
folder: converter
permalink: /patterns/converter/
categories: Business Tier
categories: Creational
tags:
- Java
- Difficulty-Beginner
- Decoupling
---

## Intent
Expand Down Expand Up @@ -64,20 +63,26 @@ The specialized converters inherit from this base class as follows.
public class UserConverter extends Converter<UserDto, User> {

public UserConverter() {
super(userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
userDto.getEmail()),
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(),
user.getUserId()));
super(UserConverter::convertToEntity, UserConverter::convertToDto);
}

private static UserDto convertToDto(User user) {
return new UserDto(user.getFirstName(), user.getLastName(), user.isActive(), user.getUserId());
}

private static User convertToEntity(UserDto dto) {
return new User(dto.getFirstName(), dto.getLastName(), dto.isActive(), dto.getEmail());
}

}
```

Now mapping between User and UserDto becomes trivial.

```java
Converter<UserDto, User> userConverter = new UserConverter();
UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
User user = userConverter.convertFromDto(dtoUser);
var userConverter = new UserConverter();
var dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
var user = userConverter.convertFromDto(dtoUser);
```

## Class diagram
Expand Down
7 changes: 5 additions & 2 deletions converter/src/main/java/com/iluwatar/converter/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public static void main(String[] args) {
User user = userConverter.convertFromDto(dtoUser);
LOGGER.info("Entity converted from DTO:" + user);

var users = List.of(new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"), new User("Kate", "Smith", true, "if0243"));
var users = List.of(
new User("Camile", "Tough", false, "124sad"),
new User("Marti", "Luther", true, "42309fd"),
new User("Kate", "Smith", true, "if0243")
);
LOGGER.info("Domain entities:");
users.stream().map(User::toString).forEach(LOGGER::info);

Expand Down
2 changes: 1 addition & 1 deletion converter/src/main/java/com/iluwatar/converter/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
User user = (User) o;
var user = (User) o;
return isActive == user.isActive && Objects.equals(firstName, user.firstName) && Objects
.equals(lastName, user.lastName) && Objects.equals(userId, user.userId);
}
Expand Down
17 changes: 10 additions & 7 deletions converter/src/main/java/com/iluwatar/converter/UserConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,16 @@
*/
public class UserConverter extends Converter<UserDto, User> {

/**
* Constructor.
*/
public UserConverter() {
super(userDto -> new User(userDto.getFirstName(), userDto.getLastName(), userDto.isActive(),
userDto.getEmail()),
user -> new UserDto(user.getFirstName(), user.getLastName(), user.isActive(),
user.getUserId()));
super(UserConverter::convertToEntity, UserConverter::convertToDto);
}

private static UserDto convertToDto(User user) {
return new UserDto(user.getFirstName(), user.getLastName(), user.isActive(), user.getUserId());
}

private static User convertToEntity(UserDto dto) {
return new User(dto.getFirstName(), dto.getLastName(), dto.isActive(), dto.getEmail());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) {
return false;
}
UserDto userDto = (UserDto) o;
var userDto = (UserDto) o;
return isActive == userDto.isActive && Objects.equals(firstName, userDto.firstName) && Objects
.equals(lastName, userDto.lastName) && Objects.equals(email, userDto.email);
}
Expand Down
Loading