我有一个要构建的项目,它有以下要求:
钱包和用户逻辑生活在不同的服务中,所以上面只需要处理移动信贷的问题。
为了按照DDD方法对此进行建模,我想出了两个可能的解决方案:
我想到了第三种选择,那就是使用事件。然而,我认为这种方法非常接近备选案文2,而且还需要考虑到另一层复杂性。
interface IApprovable {
approve()
reject()
}
interface IPayable {
pay()
}
class Approvable implements IApprovable {
...
approve() {...}
reject() {...}
}
class NotApprovable implements IApprovable {
...
approve() {...} <-- throw an error
reject() {...} <-- throw an error
}
class Payable implements IPayable {
...
pay() {...}
}
class NotPayable implements IPayable {
...
pay() {...} <-- throw an error
}
class Transaction {
private IApprovable approvable;
private IPayable payable;
Transaction(IApprovable approvable, IPayable payable) {
this.approvable = approvable;
this.payable = payable;
}
}
public static main() {
Transaction topup = new Transaction(new Approvable(), new Payable());
Transaction transfer = new Transaction(new NotApprovable(), new NotPayable());
Transaction refund = new Transaction(new Approvable(), new NotPayable());
}是否还有其他更好的设计方法,或者,在上述三种设计中,哪一种是最好的选择?
发布于 2019-04-23 08:07:43
哎哟,看起来很乱。
我认为你需要的是一个工作流和一本规则手册。
随之而来的是一个TransferRequest,它包含一些源信息、目标信息和平衡。
enum AccountKind
{
BankTransfer,
Cashier,
Wallet,
...
}
class Account
{
AccountKind kind;
AccountIdentifier identifier;
}
enum Currency
{
Dollars,
Credits
}
class Money
{
Currency currency;
decimal amount;
}
class Approval
{
bool approved;
...
}
enum WorkflowState
{
New,
AwaitingMoney,
MoneyReceived,
AwaitingApproval,
PayMoney,
Done
...
}
class TransferRequest
{
WorkflowState
Account source;
Account destination;
Money from_source;
Money to_destination;
Approval approval;
}是的,这是一个数据结构,而不是一个成熟的对象。下一个谜题是“规则书”。
class Rule
{
Func<TransferRequest, bool> precondition;
Func<TransferRequest, TransferRequest> transition;
}
class RuleBook
{
private Rule[] rules;
TransferRequest Update(TransferRequest transferRequest)
{
return rules.single(r -> r.precondition(transferRequest)).transition(transferRequest);
}
}每条规则都知道何时可以应用它,并在事务中应用它自己。其结果是将TransferRequest移动到其旅程中的下一个适当点。
现在,当一个TransferRequest出现时:
一旦转换完成并完成了与其相关的后台任务,TransferRequest就被标记为为下一个阶段做好了准备。规则手册被重新应用,将转移转移到其旅程的下一阶段。
这允许您组成一个工作流,在该工作流中,对于某些实例可以忽略某些步骤,但在其他情况下则需要某些步骤。例如,钱包之间的转移可能进入InsufficientFunds状态(在这种状态下,不可能从银行转账或出纳)。相反,银行转账需要批准,以表明收款的地方,出纳员已经处理过的资金。
https://softwareengineering.stackexchange.com/questions/390691
复制相似问题