Skip to content

Commit bb8de87

Browse files
authored
docs: Update mute-idiom/README.md with explanation (iluwatar#2716)
1 parent 191d87e commit bb8de87

File tree

1 file changed

+74
-3
lines changed

1 file changed

+74
-3
lines changed

mute-idiom/README.md

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,94 @@
22
title: Mute Idiom
33
category: Idiom
44
language: en
5-
tag:
6-
- Decoupling
5+
tag:
6+
- Decoupling
77
---
88

9+
910
## Intent
1011
Provide a template to suppress any exceptions that either are declared but cannot occur or should only be logged;
1112
while executing some business logic. The template removes the need to write repeated `try-catch` blocks.
1213

14+
15+
## Explanation
16+
Real World Example
17+
> The vending machine contained in your office displays a warning when making a transaction. The issue occurs when the
18+
> customer decides to pay with physical money that is not recognized by the system. However, you and everyone in the office
19+
> only pays with the company credit card and will never encounter this issue.
20+
21+
In plain words
22+
> The Mute Idiom design pattern is used to reduce the requirement of catching exceptions when they cannot be thrown or
23+
> should be ignored when thrown. This applies in cases such as API functions, where the underlying code cannot be changed
24+
> to include individual use cases.
25+
26+
Programmatic Example
27+
28+
Converting the real-world example into a programmatic representation, we represent an API function as the
29+
office Vending machine
30+
31+
```java
32+
33+
public class VendingMachine {
34+
public void purchaseItem(int itemID, PaymentMethod paymentMethod) throws Exception {
35+
if (paymentMethod == PaymentMethod.Cash) {
36+
throw new Exception();
37+
}
38+
else {
39+
System.out.println("Here is your item");
40+
}
41+
}
42+
}
43+
```
44+
45+
```java
46+
public enum PaymentMethod {
47+
Card,Cash
48+
}
49+
```
50+
51+
We then run our office's daily routine, which involves purchasing items
52+
from the vending machine with the company card, using the mute pattern to ignore the exceptions that can't be thrown
53+
```java
54+
package com.iluwatar.mute;
55+
56+
public class Office {
57+
private PaymentMethod companyCard = PaymentMethod.Card;
58+
private VendingMachine officeVendingMachine = new VendingMachine();
59+
60+
public static void main(String[] args) {
61+
Office office = new Office();
62+
office.dailyRoutine();
63+
64+
}
65+
public void dailyRoutine() {
66+
Mute.mute(() -> {
67+
officeVendingMachine.purchaseItem(1,companyCard);
68+
officeVendingMachine.purchaseItem(1,companyCard);
69+
officeVendingMachine.purchaseItem(1,companyCard);
70+
});
71+
}
72+
}
73+
74+
```
75+
76+
1377
## Class diagram
1478
![alt text](./etc/mute-idiom.png "Mute Idiom")
1579

80+
1681
## Applicability
1782
Use this idiom when
18-
1983
* an API declares some exception but can never throw that exception eg. ByteArrayOutputStream bulk write method.
2084
* you need to suppress some exception just by logging it, such as closing a resource.
2185

86+
87+
88+
2289
## Credits
2390

91+
2492
* [JOOQ: Mute Design Pattern](http://blog.jooq.org/2016/02/18/the-mute-design-pattern/)
93+
94+
95+

0 commit comments

Comments
 (0)