You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: money/README.md
+74-103
Original file line number
Diff line number
Diff line change
@@ -2,10 +2,13 @@
2
2
title: "Money Pattern in Java: Encapsulating Monetary Values with Currency Consistency"
3
3
shortTitle: Money
4
4
description: "Learn how the Money design pattern in Java ensures currency safety, precision handling, and maintainable financial operations. Explore examples, applicability, and benefits of the pattern."
5
-
category: Behavioral
5
+
category: Structural
6
6
language: en
7
7
tag:
8
-
- Encapsulation
8
+
- Business
9
+
- Domain
10
+
- Encapsulation
11
+
- Immutable
9
12
---
10
13
11
14
## Also known as
@@ -14,147 +17,115 @@ tag:
14
17
15
18
## Intent of Money Design Pattern
16
19
17
-
The Money design pattern provides a robust way to encapsulate monetary values and their associated currencies. It ensures precise calculations, currency consistency, and maintainability of financial logic in Java applications.
20
+
Encapsulate monetary values and their associated currency in a domain-specific object.
18
21
19
22
## Detailed Explanation of Money Pattern with Real-World Examples
20
23
21
-
### Real-world example
24
+
Real-world example
22
25
23
-
> Imagine an e-commerce platform where customers shop in their local currencies. The platform needs to calculate order totals, taxes, and discounts accurately while handling multiple currencies seamlessly.
26
+
> Imagine an online gift card system, where each gift card holds a specific balance in a particular currency. Instead of just using a floating-point value for the balance, the system uses a Money object to precisely track the amount and currency. Whenever someone uses the gift card, it updates the balance with accurate calculations that avoid floating-point rounding errors, ensuring the domain logic stays consistent and accurate.
24
27
25
-
In this example:
26
-
- Each monetary value (like a product price or tax amount) is encapsulated in a `Money` object.
27
-
- The `Money` class ensures that only values in the same currency are combined and supports safe currency conversion for global operations.
28
-
29
-
### In plain words
28
+
In plain words
30
29
31
30
> The Money pattern encapsulates both an amount and its currency, ensuring financial operations are precise, consistent, and maintainable.
32
31
33
-
### Wikipedia says
32
+
Wikipedia says
33
+
34
+
> The Money design pattern encapsulates a monetary value and its currency, allowing for safe arithmetic operations and conversions while preserving accuracy and consistency in financial calculations.
35
+
36
+
Mind map
34
37
35
-
> "The Money design pattern encapsulates a monetary value and its currency, allowing for safe arithmetic operations and conversions while preserving accuracy and consistency in financial calculations."
38
+

In this example, we're creating a `Money` class to demonstrate how monetary values can be encapsulated along with their currency. This approach helps avoid floating-point inaccuracies, ensures arithmetic operations are handled consistently, and provides a clear domain-centric way of working with money.
40
47
41
48
```java
42
-
43
-
/**
44
-
* Represents a monetary value with an associated currency.
45
-
* Provides operations for basic arithmetic (addition, subtraction, multiplication),
46
-
* as well as currency conversion while ensuring proper rounding.
TheMoney pattern should be used in scenarios where:
97
+
By encapsulating all money-related logic in a single class, we reduce the risk of mixing different currencies, improve clarity of the codebase, and facilitate future modifications such as adding new currencies or refining rounding rules. This pattern ultimately strengthens the domain model by treating money as a distinct concept rather than just another numeric value.
97
98
98
-
1.**Currency-safe arithmetic operations**
99
-
To ensure that arithmetic operations like addition, subtraction, and multiplication are performed only between amounts in the same currency, preventing inconsistencies or errors in calculations.
100
-
101
-
2.**Accurate rounding for financial calculations**
102
-
Precise rounding to two decimal places is critical to maintain accuracy and consistency in financial systems.
103
-
104
-
3.**Consistent currency conversion**
105
-
When handling international transactions or displaying monetary values in different currencies, the Money pattern facilitates easy and reliable conversion using exchange rates.
99
+
## When to Use the Money Pattern
106
100
107
-
4.**Encapsulation of monetary logic**
108
-
By encapsulating all monetary operations within a dedicated class, the Money pattern improves maintainability and reduces the likelihood of errors.
101
+
* When financial calculations or money manipulations are part of the business logic
102
+
* When precise handling of currency amounts is required to avoid floating-point inaccuracies
103
+
* When domain-driven design principles and strong typing are desired
109
104
110
-
5.**Preventing errors in financial operations**
111
-
Strict validation ensures that operations like subtraction or multiplication are only performed when conditions are met, safeguarding against misuse or logical errors.
105
+
## Real-World Applications of Monad Pattern in Java
112
106
113
-
6.**Handling diverse scenarios in financial systems**
114
-
Useful in complex systems like e-commerce, banking, and payroll applications where precise and consistent monetary value handling is crucial.
107
+
* JSR 354 (Java Money and Currency) library in Java
108
+
* Custom domain models in e-commerceand accounting systems
115
109
116
-
---
117
110
## Benefits and Trade-offs of Money Pattern
118
111
119
-
### Benefits
120
-
1.**Precision and Accuracy**
121
-
TheMoney pattern ensures precise handling of monetary values, reducing the risk of rounding errors.
122
-
123
-
2.**Encapsulation of BusinessLogic**
124
-
By encapsulating monetary operations, the pattern enhances maintainability and reduces redundancy in financial systems.
125
-
126
-
3.**CurrencySafety**
127
-
It ensures operations are performed only between amounts of the same currency, avoiding logical errors.
112
+
Benefits
128
113
129
-
4.**ImprovedReadability**
130
-
By abstracting monetary logic into a dedicated class, the code becomes easier to read and maintain.
114
+
* Provides a single, type-safe representation of monetary amounts and currency
115
+
* Encourages encapsulation of related operations such as addition, subtraction, and formatting
116
+
* Avoids floating-point errors by using integers or specialized decimal libraries
131
117
132
-
5.**Ease of Extension**
133
-
Addingnew operations, handling different currencies, or incorporating additional business rules is straightforward.
118
+
Trade-offs
134
119
135
-
### Trade-offs
136
-
1. **Increased Complexity**
137
-
Introducing a dedicated `Money` class can add some overhead, especially for small or simple projects.
138
-
139
-
2. **Potential for Misuse**
140
-
Without proper validation and handling, incorrect usage of the Money pattern may introduce subtle bugs.
141
-
142
-
3. **Performance Overhead**
143
-
Precision and encapsulation might slightly affect performance in systems with extremely high transaction volumes.
144
-
145
-
---
120
+
* Requires additional classes and infrastructure to handle currency conversions and formatting
121
+
* Might introduce performance overhead when performing large numbers of money operations
146
122
147
123
## Related Design Patterns
148
124
149
-
1. **Value Object**
150
-
Money is a classic example of the Value Object pattern, where objects are immutable and define equality based on their value.
0 commit comments