You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: converter/README.md
+43-7Lines changed: 43 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,10 @@ language: en
5
5
tag:
6
6
- Compatibility
7
7
- Data transformation
8
+
- Decoupling
9
+
- Interface
8
10
- Object mapping
11
+
- Wrapping
9
12
---
10
13
11
14
## Also known as
@@ -21,14 +24,16 @@ The purpose of the Converter pattern is to provide a generic, common way of bidi
21
24
22
25
Real world example
23
26
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.
25
28
26
29
In plain words
27
30
28
31
> Converter pattern makes it easy to map instances of one class into instances of another class.
29
32
30
33
**Programmatic Example**
31
34
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
+
32
37
We need a generic solution for the mapping problem. To achieve this, let's introduce a generic converter.
33
38
34
39
```java
@@ -82,14 +87,40 @@ public class UserConverter extends Converter<UserDto, User> {
82
87
Now mapping between `User` and `UserDto` becomes trivial.
83
88
84
89
```java
85
-
var userConverter=newUserConverter();
86
-
var dtoUser=newUserDto("John","Doe",true,"whatever[at]wherever.com");
0 commit comments