Skip to content

Commit f4e2c7f

Browse files
committed
docs: update tolerant reader
1 parent a6458c4 commit f4e2c7f

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

tolerant-reader/README.md

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Allows a system to be more resilient to changes in the data structures it consum
1919

2020
## Explanation
2121

22-
Real world example
22+
Real-world example
2323

2424
> 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.
2525
@@ -49,7 +49,9 @@ public class RainbowFish implements Serializable {
4949
private final int lengthMeters;
5050
private final int weightTons;
5151
}
52+
```
5253

54+
```java
5355
@Getter
5456
public class RainbowFishV2 extends RainbowFish {
5557

@@ -77,7 +79,6 @@ public class RainbowFishV2 extends RainbowFish {
7779
Next we introduce the `RainbowFishSerializer`. This is the class that implements the Tolerant Reader pattern.
7880

7981
```java
80-
8182
@NoArgsConstructor
8283
public final class RainbowFishSerializer {
8384

@@ -133,23 +134,30 @@ public final class RainbowFishSerializer {
133134
And finally, here's the full example in action.
134135

135136
```java
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());
137+
public static void main(String[] args) throws IOException, ClassNotFoundException {
138+
// Write V1
139+
var fishV1 = new RainbowFish("Zed", 10, 11, 12);
140+
LOGGER.info("fishV1 name={} age={} length={} weight={}", fishV1.getName(),
141+
fishV1.getAge(), fishV1.getLengthMeters(), fishV1.getWeightTons());
142+
RainbowFishSerializer.writeV1(fishV1, "fish1.out");
143+
// Read V1
144+
var deserializedRainbowFishV1 = RainbowFishSerializer.readV1("fish1.out");
145+
LOGGER.info("deserializedFishV1 name={} age={} length={} weight={}",
146+
deserializedRainbowFishV1.getName(), deserializedRainbowFishV1.getAge(),
147+
deserializedRainbowFishV1.getLengthMeters(), deserializedRainbowFishV1.getWeightTons());
148+
// Write V2
149+
var fishV2 = new RainbowFishV2("Scar", 5, 12, 15, true, true, true);
150+
LOGGER.info(
151+
"fishV2 name={} age={} length={} weight={} sleeping={} hungry={} angry={}",
152+
fishV2.getName(), fishV2.getAge(), fishV2.getLengthMeters(), fishV2.getWeightTons(),
153+
fishV2.isHungry(), fishV2.isAngry(), fishV2.isSleeping());
154+
RainbowFishSerializer.writeV2(fishV2, "fish2.out");
155+
// Read V2 with V1 method
156+
var deserializedFishV2 = RainbowFishSerializer.readV1("fish2.out");
157+
LOGGER.info("deserializedFishV2 name={} age={} length={} weight={}",
158+
deserializedFishV2.getName(), deserializedFishV2.getAge(),
159+
deserializedFishV2.getLengthMeters(), deserializedFishV2.getWeightTons());
160+
}
153161
```
154162

155163
Program output:
@@ -161,10 +169,6 @@ Program output:
161169
15:38:00.619 [main] INFO com.iluwatar.tolerantreader.App -- deserializedFishV2 name=Scar age=5 length=12 weight=15
162170
```
163171

164-
## Class diagram
165-
166-
![Tolerant Reader](./etc/tolerant_reader_urm.png "Tolerant Reader")
167-
168172
## Applicability
169173

170174
* Use when a system needs to consume data from external sources that may change over time.
@@ -200,4 +204,4 @@ Trade-offs:
200204
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
201205
* [Patterns of Enterprise Application Architecture](https://amzn.to/3WfKBPR)
202206
* [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)
207+
* [Tolerant Reader (Martin Fowler)](http://martinfowler.com/bliki/TolerantReader.html)

0 commit comments

Comments
 (0)