Skip to content

Commit 9890579

Browse files
committed
docs: update converter
1 parent a4f3f8a commit 9890579

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

converter/README.md

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ language: en
55
tag:
66
- Compatibility
77
- Data transformation
8+
- Decoupling
9+
- Interface
810
- Object mapping
11+
- Wrapping
912
---
1013

1114
## Also known as
@@ -21,14 +24,16 @@ The purpose of the Converter pattern is to provide a generic, common way of bidi
2124

2225
Real world example
2326

24-
> In real world applications it is often the case that database layer consists of entities that need to be mapped into DTOs for use on the business logic layer. Similar mapping is done for potentially huge amount of classes, and we need a generic way to achieve this.
27+
> In a real-world scenario, consider a scenario where a library system needs to interact with a third-party book database. The library system uses its own internal book format, while the third-party database provides book information in a different format. To facilitate communication between the two systems, a Converter design pattern can be employed. This pattern will define a converter class that transforms the third-party book data format into the library's internal book format and vice versa. This ensures that the library system can seamlessly integrate with the third-party database without altering its own internal structure or the third-party system's format.
2528
2629
In plain words
2730

2831
> Converter pattern makes it easy to map instances of one class into instances of another class.
2932
3033
**Programmatic Example**
3134

35+
In real world applications it is often the case that database layer consists of entities that need to be mapped into DTOs for use on the business logic layer. Similar mapping is done for potentially huge amount of classes, and we need a generic way to achieve this.
36+
3237
We need a generic solution for the mapping problem. To achieve this, let's introduce a generic converter.
3338

3439
```java
@@ -82,14 +87,40 @@ public class UserConverter extends Converter<UserDto, User> {
8287
Now mapping between `User` and `UserDto` becomes trivial.
8388

8489
```java
85-
var userConverter=new UserConverter();
86-
var dtoUser=new UserDto("John","Doe",true,"whatever[at]wherever.com");
87-
var user=userConverter.convertFromDto(dtoUser);
90+
public static void main(String[] args) {
91+
Converter<UserDto, User> userConverter = new UserConverter();
92+
93+
UserDto dtoUser = new UserDto("John", "Doe", true, "whatever[at]wherever.com");
94+
User user = userConverter.convertFromDto(dtoUser);
95+
LOGGER.info("Entity converted from DTO: {}", user);
96+
97+
var users = List.of(
98+
new User("Camile", "Tough", false, "124sad"),
99+
new User("Marti", "Luther", true, "42309fd"),
100+
new User("Kate", "Smith", true, "if0243")
101+
);
102+
LOGGER.info("Domain entities:");
103+
users.stream().map(User::toString).forEach(LOGGER::info);
104+
105+
LOGGER.info("DTO entities converted from domain:");
106+
List<UserDto> dtoEntities = userConverter.createFromEntities(users);
107+
dtoEntities.stream().map(UserDto::toString).forEach(LOGGER::info);
108+
}
88109
```
89110

90-
## Class diagram
111+
Program output:
91112

92-
![alt text](./etc/converter.png "Converter Pattern")
113+
```
114+
08:28:27.019 [main] INFO com.iluwatar.converter.App -- Entity converted from DTO: User[firstName=John, lastName=Doe, active=true, userId=whatever[at]wherever.com]
115+
08:28:27.035 [main] INFO com.iluwatar.converter.App -- Domain entities:
116+
08:28:27.035 [main] INFO com.iluwatar.converter.App -- User[firstName=Camile, lastName=Tough, active=false, userId=124sad]
117+
08:28:27.035 [main] INFO com.iluwatar.converter.App -- User[firstName=Marti, lastName=Luther, active=true, userId=42309fd]
118+
08:28:27.035 [main] INFO com.iluwatar.converter.App -- User[firstName=Kate, lastName=Smith, active=true, userId=if0243]
119+
08:28:27.036 [main] INFO com.iluwatar.converter.App -- DTO entities converted from domain:
120+
08:28:27.037 [main] INFO com.iluwatar.converter.App -- UserDto[firstName=Camile, lastName=Tough, active=false, email=124sad]
121+
08:28:27.037 [main] INFO com.iluwatar.converter.App -- UserDto[firstName=Marti, lastName=Luther, active=true, email=42309fd]
122+
08:28:27.037 [main] INFO com.iluwatar.converter.App -- UserDto[firstName=Kate, lastName=Smith, active=true, email=if0243]
123+
```
93124

94125
## Applicability
95126

@@ -100,6 +131,10 @@ Use the Converter Pattern in the following situations:
100131
* For legacy systems integration where data models differ significantly from newer systems.
101132
* When aiming to encapsulate conversion logic to promote single responsibility and cleaner code.
102133

134+
## Tutorials
135+
136+
* [Converter Pattern in Java 8 (Boldare)](http://www.xsolve.pl/blog/converter-pattern-in-java-8/)
137+
103138
## Known Uses
104139

105140
* Data Transfer Objects (DTOs) conversions in multi-layered applications.
@@ -129,4 +164,5 @@ Trade-offs:
129164

130165
## Credits
131166

132-
* [Converter Pattern in Java 8](http://www.xsolve.pl/blog/converter-pattern-in-java-8/)
167+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
168+
* [Effective Java](https://amzn.to/4cGk2Jz)

converter/src/main/java/com/iluwatar/converter/App.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,5 @@ public static void main(String[] args) {
5959
LOGGER.info("DTO entities converted from domain:");
6060
List<UserDto> dtoEntities = userConverter.createFromEntities(users);
6161
dtoEntities.stream().map(UserDto::toString).forEach(LOGGER::info);
62-
6362
}
6463
}

0 commit comments

Comments
 (0)