|
2 | 2 | title: Mute Idiom
|
3 | 3 | category: Idiom
|
4 | 4 | language: en
|
5 |
| -tag: |
6 |
| - - Decoupling |
| 5 | +tag: |
| 6 | +- Decoupling |
7 | 7 | ---
|
8 | 8 |
|
| 9 | + |
9 | 10 | ## Intent
|
10 | 11 | Provide a template to suppress any exceptions that either are declared but cannot occur or should only be logged;
|
11 | 12 | while executing some business logic. The template removes the need to write repeated `try-catch` blocks.
|
12 | 13 |
|
| 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 | + |
13 | 77 | ## Class diagram
|
14 | 78 | 
|
15 | 79 |
|
| 80 | + |
16 | 81 | ## Applicability
|
17 | 82 | Use this idiom when
|
18 |
| - |
19 | 83 | * an API declares some exception but can never throw that exception eg. ByteArrayOutputStream bulk write method.
|
20 | 84 | * you need to suppress some exception just by logging it, such as closing a resource.
|
21 | 85 |
|
| 86 | + |
| 87 | + |
| 88 | + |
22 | 89 | ## Credits
|
23 | 90 |
|
| 91 | + |
24 | 92 | * [JOOQ: Mute Design Pattern](http://blog.jooq.org/2016/02/18/the-mute-design-pattern/)
|
| 93 | + |
| 94 | + |
| 95 | + |
0 commit comments