Money (analysis pattern)

from Wikipedia, the free encyclopedia
A money object

Money is an analysis model by Martin Fowler and is used to map monetary values in a computer system . This pattern is to be used especially when monetary values ​​have to be represented in different currencies .

Money objects in different currencies can be grouped together in a money bag (a list of money objects). This has advantages when z. E.g. a single operation (e.g. the calculation of a tax ) should be applied to several objects at the same time.

implementation

Money is a special case of the value-object pattern. The value represents the amount of money in this case is, as a float and with a high accuracy, fixed decimal place and precisely defined rounding rules (z. B. is System.Decimalin .NET or BigDecimalin Java ), respectively. Floating point numbers according to IEEE 754 may not be used due to rounding errors . For applications that require particularly high accuracy, the value can also be saved and processed as a rational number .

Depending on the accuracy required for the application, the value in the user interface can be rounded to a certain number of decimal places. This occasionally has the effect that the sum of the rounded amounts displayed does not exactly correspond to the sum calculated internally with high accuracy and then rounded, which occasionally leads to confusion for the user.

The money object also needs a field to manage the currency of the amount. Alternatively, the money class can also be implemented abstractly and the individual currencies implemented as subclasses. The conversion from one currency to another should not be included in the money object itself due to the separation of concerns principle. The conversion ratio analysis pattern would be suitable for this .

Since the exchange rates between currencies change over time, it is advisable to use a web service that can also convert historical values, as well as to save currency changes in a chain of command (i.e. a list of command objects).

For simple applications, however, it may be sufficient to only deal with a single currency, which significantly reduces the complexity of the Money object, as there is no need to explicitly specify and convert a currency.

Individual evidence

  1. ^ Martin Fowler: Patterns of Enterprise Application Architecture . Addison-Wesley-Longman, Amsterdam 2002, ISBN 0-321-12742-0 .
  2. ^ Martin Fowler: Money. Retrieved April 9, 2013 .
  3. Decimal structure. In: MSDN . Microsoft , accessed April 9, 2013 .