货币转换的设计模式? [英] design patterns for currency conversion?

查看:183
本文介绍了货币转换的设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我们是否可以将设计模式应用于编写货币转换代码,如果是,那么它们会是什么?假设转换率是静态的,我们可以对它们进行硬编码。



我想到了使用状态模式,其中每个状态代表一个特定的货币,并且封装了公式转换成相应的方法,例如'toDollars()','toPounds()'等。



另一种可能性(不确定是否是设计模式)使用从特定货币转换为的功能对象另一个,并将它们作为值存储在2级Hashtable中,其中第一级密钥是从货币,第二级密钥是到货币。



是还有其他设计模式可以应用于这个问题吗?这个问题在最抽象的层面上就是我们有m个实体,它们都是基于一些具体的规则进行互换的。示例是将时区值相互转换;日历日期从不同的日历类型(格里高利文/中文)等彼此。



感谢和问候!

解决方案

从学习/设计的角度来看,这个问题是完全落后的,因为目标不应该是应用设计模式,而是解决问题和沟通解决方案和设计模式有助于



但是,由于您问:设计模式的数量可能会根据要求而有所变化:




  • 责任链:有一个接一个咨询的转换器列表,直到可以转换手头的货币

  • 工厂方法:创建基于原始和目标货币

  • 门面/装饰员:从另一种货币和转换器创建一种货币的金额,从客户端隐藏api。

  • Singleton:如果你想确保每个Convers只有一个Converter离子

  • 价值对象
    ...



我推荐这个视频: a href =http://www.infoq.com/presentations/Value-Objects-Dan-Bergh-Johnsson =nofollow> http://www.infoq.com/presentations/Value-Objects-Dan- Bergh-Johnsson
这个问题是过度的,但它恰好以货币转换为例,为Value Objects ...


I was wondering if we could apply design patterns to writing code for currency conversion, and if yes then what would they be? The assumption is that conversion rates are static and we can hard-code them.

I thought about using 'state pattern' where every state represents a specific currency and has formulas encapsulated into corresponding methods for conversion, e.g. 'toDollars()', 'toPounds()' etc.

Another possibility (not sure if it's a design pattern) is using function objects that convert from a specific currency to another, and storing them as values in a 2-level Hashtable where the first level key is 'from' currency and the second level key is 'to' currency.

Are there any other design patterns to apply to this problem? The problem at its most abstract level is that we have 'm' entities and they are all inter-convertible based on some specific rules. Examples are converting time zone values in to each other; calendar dates from different calendar types (Gregorian/Chinese) etc. into each other.

Thanks and regards!

解决方案

From a learning/design point of view this question is totally backward since the aim should not be to apply design patterns, but to solve problems and to communicate about the solutions and design patterns help with that.

But since you asked: Tons of design patterns might make sense depending on the requirements:

  • Chain of Responsibility: Have a list of Converters which you consult one after the other, until one can convert the currencies at hand
  • Factory Method: Create a Converter based on the original and target currency
  • Facade/Decorator: Create an amount in one currency from another currency and a Converter, hiding the api from the client.
  • Singleton: In case you want to make sure you have only one Converter for each Conversion
  • Value Object ...

I recommend this video: http://www.infoq.com/presentations/Value-Objects-Dan-Bergh-Johnsson It is overkill for the question but it happens to use currency conversion as an example for Value Objects ...

这篇关于货币转换的设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆