Skip to content

Commit 6ed6b1c

Browse files
committed
docs: update tolerant reader
1 parent 628ec15 commit 6ed6b1c

File tree

6 files changed

+147
-154
lines changed

6 files changed

+147
-154
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,5 @@ build/
5757
#################### Java Design Patterns #######
5858
etc/Java Design Patterns.urm.puml
5959
serialized-entity/output.txt
60+
fish1.out
61+
fish2.out

tolerant-reader/README.md

+138-150
Original file line numberDiff line numberDiff line change
@@ -1,215 +1,203 @@
11
---
22
title: Tolerant Reader
3-
category: Integration
3+
category: Resilience
44
language: en
55
tag:
6-
- Decoupling
6+
- API design
7+
- Decoupling
8+
- Fault tolerance
9+
- Integration
710
---
811

12+
## Also known as
13+
14+
* Lenient Consumer
15+
916
## Intent
1017

11-
Tolerant Reader is an integration pattern that helps creating robust communication systems. The idea
12-
is to be as tolerant as possible when reading data from another service. This way, when the
13-
communication schema changes, the readers must not break.
18+
Allows a system to be more resilient to changes in the data structures it consumes by ignoring elements that it does not recognize.
1419

1520
## Explanation
1621

1722
Real world example
1823

19-
> We are persisting rainbowfish objects to file and later on they need to be restored. What makes it
20-
> problematic is that rainbowfish data structure is versioned and evolves over time. New version of
21-
> rainbowfish needs to be able to restore old versions as well.
24+
> Imagine a postal system that delivers letters and packages to recipients. In this system, postal workers deliver mail regardless of additional information or stickers that might be present on the envelopes or packages. If a package has extra labels or instructions that the postal system does not recognize, the postal worker ignores these and focuses only on the essential information like the address. This approach ensures that the delivery process remains functional even when senders use different formats or include unnecessary details, similar to how the Tolerant Reader pattern works in software by ignoring unrecognized data elements to maintain functionality and compatibility.
2225
2326
In plain words
2427

25-
> Tolerant Reader pattern is used to create robust communication mechanisms between services.
28+
> Tolerant Reader pattern is used to create robust communication mechanisms between services.
2629
2730
[Robustness Principle](https://java-design-patterns.com/principles/#robustness-principle) says
2831

2932
> Be conservative in what you do, be liberal in what you accept from others.
3033
3134
**Programmatic Example**
3235

36+
We are persisting `RainbowFish` objects to file. Later on they need to be restored. What makes it problematic is that `RainbowFish` data structure is versioned and evolves over time. New version of `RainbowFish` needs to be able to restore old versions as well.
37+
3338
Here's the versioned `RainbowFish`. Notice how the second version introduces additional properties.
3439

3540
```java
41+
@Getter
42+
@RequiredArgsConstructor
3643
public class RainbowFish implements Serializable {
3744

38-
private static final long serialVersionUID = 1L;
39-
40-
private final String name;
41-
private final int age;
42-
private final int lengthMeters;
43-
private final int weightTons;
44-
45-
/**
46-
* Constructor.
47-
*/
48-
public RainbowFish(String name, int age, int lengthMeters, int weightTons) {
49-
this.name = name;
50-
this.age = age;
51-
this.lengthMeters = lengthMeters;
52-
this.weightTons = weightTons;
53-
}
54-
55-
public String getName() {
56-
return name;
57-
}
58-
59-
public int getAge() {
60-
return age;
61-
}
62-
63-
public int getLengthMeters() {
64-
return lengthMeters;
65-
}
66-
67-
public int getWeightTons() {
68-
return weightTons;
69-
}
45+
private static final long serialVersionUID = 1L;
46+
47+
private final String name;
48+
private final int age;
49+
private final int lengthMeters;
50+
private final int weightTons;
7051
}
7152

53+
@Getter
7254
public class RainbowFishV2 extends RainbowFish {
7355

74-
private static final long serialVersionUID = 1L;
75-
76-
private boolean sleeping;
77-
private boolean hungry;
78-
private boolean angry;
79-
80-
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons) {
81-
super(name, age, lengthMeters, weightTons);
82-
}
83-
84-
/**
85-
* Constructor.
86-
*/
87-
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping,
88-
boolean hungry, boolean angry) {
89-
this(name, age, lengthMeters, weightTons);
90-
this.sleeping = sleeping;
91-
this.hungry = hungry;
92-
this.angry = angry;
93-
}
94-
95-
public boolean getSleeping() {
96-
return sleeping;
97-
}
98-
99-
public boolean getHungry() {
100-
return hungry;
101-
}
102-
103-
public boolean getAngry() {
104-
return angry;
105-
}
56+
@Serial
57+
private static final long serialVersionUID = 1L;
58+
59+
private boolean sleeping;
60+
private boolean hungry;
61+
private boolean angry;
62+
63+
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons) {
64+
super(name, age, lengthMeters, weightTons);
65+
}
66+
67+
public RainbowFishV2(String name, int age, int lengthMeters, int weightTons, boolean sleeping,
68+
boolean hungry, boolean angry) {
69+
this(name, age, lengthMeters, weightTons);
70+
this.sleeping = sleeping;
71+
this.hungry = hungry;
72+
this.angry = angry;
73+
}
10674
}
10775
```
10876

109-
Next we introduce the `RainbowFishSerializer`. This is the class that implements the Tolerant Reader
110-
pattern.
77+
Next we introduce the `RainbowFishSerializer`. This is the class that implements the Tolerant Reader pattern.
11178

11279
```java
113-
public final class RainbowFishSerializer {
114-
115-
private RainbowFishSerializer() {
116-
}
11780

118-
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
119-
var map = Map.of(
120-
"name", rainbowFish.getName(),
121-
"age", String.format("%d", rainbowFish.getAge()),
122-
"lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
123-
"weightTons", String.format("%d", rainbowFish.getWeightTons())
124-
);
81+
@NoArgsConstructor
82+
public final class RainbowFishSerializer {
12583

126-
try (var fileOut = new FileOutputStream(filename);
127-
var objOut = new ObjectOutputStream(fileOut)) {
128-
objOut.writeObject(map);
84+
public static void writeV1(RainbowFish rainbowFish, String filename) throws IOException {
85+
var map = Map.of(
86+
"name", rainbowFish.getName(),
87+
"age", String.format("%d", rainbowFish.getAge()),
88+
"lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
89+
"weightTons", String.format("%d", rainbowFish.getWeightTons())
90+
);
91+
92+
try (var fileOut = new FileOutputStream(filename);
93+
var objOut = new ObjectOutputStream(fileOut)) {
94+
objOut.writeObject(map);
95+
}
12996
}
130-
}
131-
132-
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
133-
var map = Map.of(
134-
"name", rainbowFish.getName(),
135-
"age", String.format("%d", rainbowFish.getAge()),
136-
"lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
137-
"weightTons", String.format("%d", rainbowFish.getWeightTons()),
138-
"angry", Boolean.toString(rainbowFish.getAngry()),
139-
"hungry", Boolean.toString(rainbowFish.getHungry()),
140-
"sleeping", Boolean.toString(rainbowFish.getSleeping())
141-
);
142-
143-
try (var fileOut = new FileOutputStream(filename);
144-
var objOut = new ObjectOutputStream(fileOut)) {
145-
objOut.writeObject(map);
97+
98+
public static void writeV2(RainbowFishV2 rainbowFish, String filename) throws IOException {
99+
var map = Map.of(
100+
"name", rainbowFish.getName(),
101+
"age", String.format("%d", rainbowFish.getAge()),
102+
"lengthMeters", String.format("%d", rainbowFish.getLengthMeters()),
103+
"weightTons", String.format("%d", rainbowFish.getWeightTons()),
104+
"angry", Boolean.toString(rainbowFish.getAngry()),
105+
"hungry", Boolean.toString(rainbowFish.getHungry()),
106+
"sleeping", Boolean.toString(rainbowFish.getSleeping())
107+
);
108+
109+
try (var fileOut = new FileOutputStream(filename);
110+
var objOut = new ObjectOutputStream(fileOut)) {
111+
objOut.writeObject(map);
112+
}
146113
}
147-
}
148114

149-
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
150-
Map<String, String> map;
115+
public static RainbowFish readV1(String filename) throws IOException, ClassNotFoundException {
116+
Map<String, String> map;
151117

152-
try (var fileIn = new FileInputStream(filename);
153-
var objIn = new ObjectInputStream(fileIn)) {
154-
map = (Map<String, String>) objIn.readObject();
155-
}
118+
try (var fileIn = new FileInputStream(filename);
119+
var objIn = new ObjectInputStream(fileIn)) {
120+
map = (Map<String, String>) objIn.readObject();
121+
}
156122

157-
return new RainbowFish(
158-
map.get("name"),
159-
Integer.parseInt(map.get("age")),
160-
Integer.parseInt(map.get("lengthMeters")),
161-
Integer.parseInt(map.get("weightTons"))
162-
);
163-
}
123+
return new RainbowFish(
124+
map.get("name"),
125+
Integer.parseInt(map.get("age")),
126+
Integer.parseInt(map.get("lengthMeters")),
127+
Integer.parseInt(map.get("weightTons"))
128+
);
129+
}
164130
}
165131
```
166132

167-
And finally here's the full example in action.
133+
And finally, here's the full example in action.
168134

169135
```java
170-
var fishV1 = new RainbowFish("Zed", 10, 11, 12);
171-
LOGGER.info("fishV1 name={} age={} length={} weight={}", fishV1.getName(),
172-
fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons());
173-
RainbowFishSerializer.writeV1(fishV1, "fish1.out");
174-
175-
var deserializedRainbowFishV1 = RainbowFishSerializer.readV1("fish1.out");
176-
LOGGER.info("deserializedFishV1 name={} age={} length={} weight={}",
177-
deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(),
178-
deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons());
179-
180-
var fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
181-
LOGGER.info(
182-
"fishV2 name={} age={} length={} weight={} sleeping={} hungry={} angry={}",
183-
fishV2.getName(), fishV2.getAge(), fishV2.getLengthMeters(), fishV2.getWeightTons(),
184-
fishV2.getHungry(), fishV2.getAngry(), fishV2.getSleeping());
185-
RainbowFishSerializer.writeV2(fishV2, "fish2.out");
186-
187-
var deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out");
188-
LOGGER.info("deserializedFishV2 name={} age={} length={} weight={}",
189-
deserializedFishV2.getName(), deserializedFishV2.getAge(),
190-
deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons());
136+
// Write V1
137+
var fishV1 = new RainbowFish("Zed", 10, 11, 12);
138+
LOGGER.info("fishV1 name={} age={} length={} weight={}", fishV1.getName(), fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons());
139+
RainbowFishSerializer.writeV1(fishV1, "fish1.out");
140+
141+
// Read V1
142+
var deserializedRainbowFishV1 = RainbowFishSerializer.readV1("fish1.out");
143+
LOGGER.info("deserializedFishV1 name={} age={} length={} weight={}", deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(), deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons());
144+
145+
// Write V2
146+
var fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
147+
LOGGER.info("fishV2 name={} age={} length={} weight={} sleeping={} hungry={} angry={}", fishV2.getName(), fishV2.getAge(), fishV2.getLengthMeters(), fishV2.getWeightTons(), fishV2.isHungry(), fishV2.isAngry(), fishV2.isSleeping());
148+
RainbowFishSerializer.writeV2(fishV2, "fish2.out");
149+
150+
// Read V2 with V1 method
151+
var deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out");
152+
LOGGER.info("deserializedFishV2 name={} age={} length={} weight={}", deserializedFishV2.getName(), deserializedFishV2.getAge(), deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons());
191153
```
192154

193155
Program output:
194156

195157
```
196-
fishV1 name=Zed age=10 length=11 weight=12
197-
deserializedFishV1 name=Zed age=10 length=11 weight=12
198-
fishV2 name=Scar age=5 length=12 weight=15 sleeping=true hungry=true angry=true
199-
deserializedFishV2 name=Scar age=5 length=12 weight=15
158+
15:38:00.602 [main] INFO com.iluwatar.tolerantreader.App -- fishV1 name=Zed age=10 length=11 weight=12
159+
15:38:00.618 [main] INFO com.iluwatar.tolerantreader.App -- deserializedFishV1 name=Zed age=10 length=11 weight=12
160+
15:38:00.618 [main] INFO com.iluwatar.tolerantreader.App -- fishV2 name=Scar age=5 length=12 weight=15 sleeping=true hungry=true angry=true
161+
15:38:00.619 [main] INFO com.iluwatar.tolerantreader.App -- deserializedFishV2 name=Scar age=5 length=12 weight=15
200162
```
201163

202164
## Class diagram
203165

204-
![alt text](./etc/tolerant_reader_urm.png "Tolerant Reader")
166+
![Tolerant Reader](./etc/tolerant_reader_urm.png "Tolerant Reader")
205167

206168
## Applicability
207169

208-
Use the Tolerant Reader pattern when
170+
* Use when a system needs to consume data from external sources that may change over time.
171+
* Applicable when backward compatibility is required in API design.
172+
* Suitable for integration scenarios where different systems exchange data and evolve independently.
173+
174+
## Known Uses
175+
176+
* JSON or XML parsers that skip unknown elements.
177+
* API clients in microservices architectures that interact with multiple versions of a service.
178+
179+
## Consequences
180+
181+
Benefits:
182+
183+
* Increases the robustness and flexibility of the system.
184+
* Allows independent evolution of producers and consumers in a distributed system.
185+
* Simplifies versioning by enabling backward compatibility.
186+
187+
Trade-offs:
188+
189+
* May result in silent failures if important data is ignored.
190+
* Can complicate debugging and tracing of issues due to missing or unrecognized data.
191+
192+
## Related Patterns
209193

210-
* The communication schema can evolve and change and yet the receiving side should not break
194+
* [Adapter](https://java-design-patterns.com/patterns/adapter/): Both patterns deal with data transformation and integration, but the Adapter Pattern focuses on converting interfaces, while Tolerant Reader focuses on ignoring unrecognized data.
195+
* [Facade](https://java-design-patterns.com/patterns/facade/): Simplifies interactions with complex systems, similar to how Tolerant Reader simplifies data consumption by ignoring irrelevant data.
196+
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Can be used in conjunction with Tolerant Reader to dynamically switch between different data handling strategies.
211197

212198
## Credits
213199

214-
* [Martin Fowler - Tolerant Reader](http://martinfowler.com/bliki/TolerantReader.html)
215-
* [Service Design Patterns: Fundamental Design Solutions for SOAP/WSDL and RESTful Web Services](https://www.amazon.com/gp/product/032154420X/ref=as_li_tl?ie=UTF8&tag=javadesignpat-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=032154420X&linkId=94f9516e747ac2b449a959d5b096c73c)
200+
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
201+
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
202+
* [Service Design Patterns: Fundamental Design Solutions for SOAP/WSDL and RESTful Web Services](https://amzn.to/4dNIfOx)
203+
* [Tolerant Reader - Martin Fowler](http://martinfowler.com/bliki/TolerantReader.html)

tolerant-reader/src/main/java/com/iluwatar/tolerantreader/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import lombok.extern.slf4j.Slf4j;
2929

3030
/**
31-
* Tolerant Reader is an integration pattern that helps creating robust communication systems. The
31+
* Tolerant Reader is an integration pattern that helps to create robust communication systems. The
3232
* idea is to be as tolerant as possible when reading data from another service. This way, when the
3333
* communication schema changes, the readers must not break.
3434
*

tolerant-reader/src/main/java/com/iluwatar/tolerantreader/RainbowFish.java

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package com.iluwatar.tolerantreader;
2626

27+
import java.io.Serial;
2728
import java.io.Serializable;
2829
import lombok.Getter;
2930
import lombok.RequiredArgsConstructor;
@@ -35,6 +36,7 @@
3536
@RequiredArgsConstructor
3637
public class RainbowFish implements Serializable {
3738

39+
@Serial
3840
private static final long serialVersionUID = 1L;
3941

4042
private final String name;

0 commit comments

Comments
 (0)