Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
iluwatar committed Aug 29, 2020
1 parent 2bb2134 commit e8b42bd
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions converter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,28 @@ tags:
---

## Intent
The purpose of the Converter Pattern is to provide a generic, common way of bidirectional

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
need to be aware of each other. Moreover, the Converter pattern introduces bidirectional collection
mapping, reducing a boilerplate code to minimum.

## Explanation

Real world example

> 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.
> 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.
In plain words

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

We need a generic solution for the mapping problem. To achieve this, let's introduce a generic converter.
We need a generic solution for the mapping problem. To achieve this, let's introduce a generic
converter.

```java
public class Converter<T, U> {
Expand Down Expand Up @@ -77,7 +81,7 @@ public class UserConverter extends Converter<UserDto, User> {
}
```

Now mapping between User and UserDto becomes trivial.
Now mapping between `User` and `UserDto` becomes trivial.

```java
var userConverter = new UserConverter();
Expand All @@ -86,14 +90,18 @@ var user = userConverter.convertFromDto(dtoUser);
```

## Class diagram

![alt text](./etc/converter.png "Converter Pattern")

## Applicability

Use the Converter Pattern in the following situations:

* When you have types that logically correspond which other and you need to convert entities between them
* When you want to provide different ways of types conversions depending on a context
* Whenever you introduce a DTO (Data transfer object), you will probably need to convert it into the domain equivalence
* When you have types that logically correspond with each other and you need to convert entities
between them.
* When you want to provide different ways of types conversions depending on the context.
* Whenever you introduce a DTO (Data transfer object), you will probably need to convert it into the
domain equivalence.

## Credits

Expand Down

0 comments on commit e8b42bd

Please sign in to comment.