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
+73-47
Original file line number
Diff line number
Diff line change
@@ -1,61 +1,62 @@
1
1
---
2
2
title: Converter
3
-
category: Creational
3
+
category: Structural
4
4
language: en
5
5
tag:
6
-
- Decoupling
6
+
- Compatibility
7
+
- Data transformation
8
+
- Object mapping
7
9
---
8
10
11
+
## Also known as
12
+
13
+
* Mapper
14
+
* Translator
15
+
9
16
## Intent
10
17
11
-
The purpose of the Converter pattern is to provide a generic, common way of bidirectional
12
-
conversion between corresponding types, allowing a clean implementation in which the types do not
13
-
need to be aware of each other. Moreover, the Converter pattern introduces bidirectional collection
14
-
mapping, reducing a boilerplate code to minimum.
18
+
The purpose of the Converter pattern is to provide a generic, common way of bidirectional conversion between corresponding types, allowing a clean implementation in which the types do not need to be aware of each other. Moreover, the Converter pattern introduces bidirectional collection mapping, reducing a boilerplate code to minimum.
15
19
16
20
## Explanation
17
21
18
22
Real world example
19
23
20
-
> In real world applications it is often the case that database layer consists of entities that need
21
-
> to be mapped into DTOs for use on the business logic layer. Similar mapping is done for
22
-
> potentially huge amount of classes and we need a generic way to achieve this.
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.
23
25
24
26
In plain words
25
27
26
28
> Converter pattern makes it easy to map instances of one class into instances of another class.
27
29
28
30
**Programmatic Example**
29
31
30
-
We need a generic solution for the mapping problem. To achieve this, let's introduce a generic
31
-
converter.
32
+
We need a generic solution for the mapping problem. To achieve this, let's introduce a generic converter.
Now mapping between `User` and `UserDto` becomes trivial.
83
83
84
84
```java
85
-
var userConverter=newUserConverter();
86
-
var dtoUser=newUserDto("John","Doe",true,"whatever[at]wherever.com");
87
-
var user=userConverter.convertFromDto(dtoUser);
85
+
var userConverter=newUserConverter();
86
+
var dtoUser=newUserDto("John","Doe",true,"whatever[at]wherever.com");
87
+
var user=userConverter.convertFromDto(dtoUser);
88
88
```
89
89
90
90
## Class diagram
@@ -95,11 +95,37 @@ var user = userConverter.convertFromDto(dtoUser);
95
95
96
96
Use the Converter Pattern in the following situations:
97
97
98
-
* When you have types that logically correspond with each other and you need to convert entities
99
-
between them.
100
-
* When you want to provide different ways of types conversions depending on the context.
101
-
* Whenever you introduce a DTO (Data transfer object), you will probably need to convert it into the
102
-
domain equivalence.
98
+
* When there are types that logically correspond with each other, and there is a need to convert between them.
99
+
* In applications that interact with external systems or services that require data in a specific format.
100
+
* For legacy systems integration where data models differ significantly from newer systems.
101
+
* When aiming to encapsulate conversion logic to promote single responsibility and cleaner code.
102
+
103
+
## Known Uses
104
+
105
+
* Data Transfer Objects (DTOs) conversions in multi-layered applications.
106
+
* Adapting third-party data structures or API responses to internal models.
107
+
* ORM (Object-Relational Mapping) frameworks for mapping between database records and domain objects.
108
+
* Microservices architecture for data exchange between different services.
109
+
110
+
## Consequences
111
+
112
+
Benefits:
113
+
114
+
* Separation of Concerns: Encapsulates conversion logic in a single component, keeping the rest of the application unaware of the conversion details.
115
+
* Reusability: Converter components can be reused across the application or even in different applications.
116
+
* Flexibility: Makes it easy to add new conversions without impacting existing code, adhering to the [Open/Closed Principle](https://java-design-patterns.com/principles/#open-closed-principle).
117
+
* Interoperability: Facilitates communication between different systems or application layers by translating data formats.
118
+
119
+
Trade-offs:
120
+
121
+
* Overhead: Introducing converters can add complexity and potential performance overhead, especially in systems with numerous data formats.
122
+
* Duplication: There's a risk of duplicating model definitions if not carefully managed, leading to increased maintenance.
123
+
124
+
## Related Patterns
125
+
126
+
*[Adapter](https://java-design-patterns.com/patterns/adapter/): Similar in intent to adapting interfaces, but Converter focuses on data models.
127
+
*[Facade](https://java-design-patterns.com/patterns/facade/): Provides a simplified interface to a complex system, which might involve data conversion.
128
+
*[Strategy](https://java-design-patterns.com/patterns/strategy/): Converters can use different strategies for conversion, especially when multiple formats are involved.
0 commit comments