策略模式还是命令模式?
假设我有一个金融交易列表,我需要针对这些交易执行一系列验证规则。一个例子是我有一笔购买产品的交易,但是首先我需要验证交易中的帐户是否有足够的可用资金,产品没有售完等。由于这些规则,交易将是标记为拒绝,并应指定错误代码。
当然,我正在考虑用一个接口来呈现我的规则,允许执行代码滚动执行每个规则,直到第一个规则拒绝事务。
每个规则都需要配置参数(例如,ValidateMinimumBalance 需要知道minimumBalance = 30)。规则执行的结果可以很简单,只需在交易对象上设置拒绝代码和错误代码即可;或者它可能像自动修改事务的多个其他属性一样复杂。
我对设计模式的基本理解表明我要么是策略模式,要么是命令模式,但我不完全确定哪一种更适合这种情况。
命令模式
- 每个命令都将实现某种 IValidate 接口。
- 命令的构造函数将采用事务的实例作为接收者,以便能够读取/验证事务以及修改事务的各个方面。构造函数还将采用键/值对数组作为验证逻辑的参数。
当我尝试想象策略模式如何适应这种场景时,它看起来非常相似。在大多数示例中,策略是具有单个方法的简单对象,但在我的情况下,策略将需要对交易以及验证参数的引用。
Assuming I have a list of financial transactions, I have a need to execute a list of validation rules against those transactions. An example would be I have a transaction to purchase a product, however first I need to validate that the account in the transaction has enough available funds, that the product is not sold out etc. As a result of these many rules the transaction will be marked as rejected, as well as an error code should be specified.
Naturally I am thinking towards fronting my rules with an interface, allowing the executing code to roll through the rules executing each one until the first one rejects the transaction.
Each rule will require to be configured with parameters (ex. ValidateMinimumBalance will need to know that minimumBalance = 30). The result of a rule executing can be as simple as settings the rejection code on the transaction object, and the error code; or it can be as complicated as automatically modifying multiple other properties of the transaction.
My basic understanding of design patterns points to me either Strategy or Command patterns, but I am not entirely sure which one is better suited for this scenario.
Command Pattern
- Each command will implement some sort of IValidate interface
- The constructor of the command will take an instance of the transaction as the receiver in order to be able to read/validate the transaction as well as modify aspects of it. The constructor will also take an array of key/value pairs as parameters for the validation logic.
When I try to picture how the Strategy Pattern fits this scenario it looks very similar. In most examples the strategy is a simple object with a single method, however in my case the strategy will need a reference to the transaction as well as validation parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
策略更多地用于替换算法,而不是真正用于链接验证。如果您打算采用一种模式,其中每种类型只有一个验证,那么您可以使用该策略,如果您发现必须使用多个验证器,或者需要重用验证器。我认为你必须找到一种新的方法来做到这一点(又名 COR),或者在你的策略中使用 COR。
我实际上会回答其他。我认为 责任链模式 和 复合模式,或 验证器的装饰器更适合您的需求。
现在输入一个示例实现..但在较高的层次上
责任链
设计将围绕以下内容展开:
其他有用的链接:
责任链维基百科
Strategy is more used to swap out algorithms, its not really used for chaining validations. If you are going to have a pattern where you have one validation per type then you could use the strategy, if you are finding your having to use multiple validators, or the need to reuse validators. I think you are going to have to either find a new way to do it (aka COR) or within your strategy use the COR.
I actually would answer other. I think a combination chain of responsibility pattern and the composite pattern, or decorator for validators is much more suited for your needs.
Typing up an example implementation now.. but at a high level
Chain of Responsiblity
The design would revolve around something like:
Other useful links:
Chain of Responsiblity wikipedia
相似的?它看起来应该是一样的。
区别之一在于上下文和委托的工作方式。原则上,指挥是“主动”代理。将策略注入到某些活性剂中。这种区别非常微妙。
它几乎没有改变设计。改变的是期望。
命令对象(或多或少)是独立的。它们生来就是为了完成它们的工作,然后它们就会消失。没有人再关心他们了。也许他们也使用Memento模式,并有一些未来的生活,但也许不是。
策略对象(或多或少)与它们所注入的对象一起存在。策略将是某个更大对象的一部分,并且可以被不同的实现替换,而不会破坏或更改其他任何内容。
但基本界面基本相同。
这些都是糟糕的示例。
并不罕见。没有什么问题。
Similar? It should look identical.
The distinction is one of how the context and delegation works. In principle a Command is the "active" agent. A Strategy is injected into some active agent. That distinction is pretty subtle.
It barely changes the design. What does change is the expectation.
Command objects (more-or-less) stand alone. They're built to do their work, and then they can vanish. No one cares about them any more. Perhaps they also use the Memento pattern, and have some future life, but perhaps not.
Strategy objects (more-or-less) live with the object into which they're injected. A Strategy would be part of some larger object, and could be replaced by a different implementation without breaking or changing anything else.
But the essential interface is largely the same.
Those are poor examples.
Not unusual. Nothing wrong with it.
策略可用于“参数化”命令(告诉它应如何执行操作的部分)。
A Strategy would be something use to 'parameterize' a Command (telling it how parts of the operation should be executed).
两者都不是:)
我强烈建议您查看Interpreter。实际上,您的验证器规则只是为您的交易制定的谓词。很可能很快您就需要将这些规则与 AND、OR、NOT 等结合起来。
Neither :)
I strongly recommend to look at Interpreter. Actually your validator rules are just predicates formulated for your transactions. It's quite possible that soon you will need to combine these rules with AND, OR, NOT, etc.