我想对下面的域驱动设计提出一些建议和批评。我在下面包含了伪代码。真正的代码将具有封装的属性。域名驱动设计评判
问题
我唯一担心的是,它似乎是贫血。
步骤
- 创建与价值观的新请求
- 被请求批准?
- 如果是的话,显示值
- 如果没有,表明没有批准 原因
类
enum UnitedStatesState {
ALABAMA,
//...
CALIFORNIA,
//...
MAINE,
//...
WASHINGTON
}
class License {
int id;
String name;
//enum of state that the license is applicable in
UnitedStatesState state;
}
class LicenseRequest {
//the name of the person making the request
String name;
//enum of state to which the user is requesting a license in
UnitedStatesState state;
LicenseResponse submit()
{
//TODO: move creation of the rules out of this class
RuleGroup<LicenseRequest> ruleGroup = new RuleGroup<>();
ruleGroup.add(new StateExclusionLicenseRequestRule(UnitedStatesState.MAINE));
boolean approved = ruleGroup.execute(this);
if(approved) {
License license = createLiscense(request);
return new ApprovedLicenseResponse(license);
} else {
DeniedLicenseResponse response = new DeniedLicenseResponse();
response.rules = newArrayList(ruleGroup);
return response;
}
}
//TODO: move create license out of Request. maybe a factory class?
private License createLicense()
{
License license = LicenseIdGenerator.generate(this.state);
license.name = this.name;
license.state = this.state;
save(license);
return license;
}
}
//visitor for the rule
interface Rule<T> {
public boolean execute(T o);
public List<String> getMessages();
}
//rule that auto denies when the request is made in an excluded state
class StateExclusionLicenseRequestRule : Rule<LicenseRequest> {
public List<String> getMessages();
UnitedStatesState excludedState;
public boolean execute(LicenseRequest request) {
if(request.state == excludedState)
{
messages.add("No license for " + request.state + " is available at this time.");
return false;
}
return true;
}
}
//rule that groups all other rules
class RuleGroup<T> : Rule<T> {
public void addRule(Rule<T> rule);
public List<Rule<T>> getFailedRules();
public List<String> getMessages() {
List<String> messages = new ArrayList<>();
for(Rule<T> rule : rules) {
messages.addAll(rule.getMessages());
}
return messages;
}
public boolean execute(T o) {
List<Rule<T>> failedRules = new ArrayList<>(rules.size());
for(Rule<T> rule : rules) {
boolean approve = rule.execute(o);
if(!approve) {
failedRules.add(rule);
}
}
return !failedRules.isEmpty();
}
}
interface LicenseResponse {
boolean approved;
}
class ApprovedLicenseResponse : LicenseResponse {
License license;
}
class DeniedLicenseResponse : LicenseResponse {
private List<Rule<LicenseRequest>> rules;
public List<String> getMessages()
{
List<String> messages = new ArrayList<>();
for(Rule<LicenseRequest> rule : rules) {
messages.addAll(rule.getMessages());
}
return messages;
}
}
示例代码
request = new Request(name: 'Test', state: UnitedStatesState.CALIFORNIA)
response = request.submit()
if(response.approved)
{
out('Your request is approved');
out('license id = ' + reponse.id);
}
else
{
out('Your request was denied');
for(String message : response.messages)
{
out(message);
}
}
更新1:背景
这只是我想要实现的一个模拟。这是一个简单的系统,用户将信息输入到自己的表格中,并且他们被批准或拒绝许可证。批准后,可以打印证书。
为了举例,唯一的规则是缅因州的许可证申请被拒绝。
更新2:重构规则和删除处理程序
我做了一些修改,以上面的例子。删除处理程序并将所有代码移动到LicenseRequest。我也将批准/拒绝的规则提交给实施vistor模式的课程。
看来,我认为您正在更换缺少的重要经营理念/ s的程序, “域之外的”Handler“概念。 – 2011-04-01 00:17:36
给我们一些关于您尝试解决的问题的简短介绍,ddd在没有上下文的情况下无法正常工作。给我们一些洞察什么使用这个和为什么。 – 2011-04-02 07:40:11
@ kyri-sarantakos - 我已经添加了一些背景。如果您需要更多信息,请告诉我。 – 2011-04-04 13:42:50