演化架构与紧急设计: 语言、表达性与设计:第 1 部分
2009-09-12 00:00:00 来源:WEB开发网清单 5. 使用代码块支持可撤销的操作
class Command {
def cmd, uncmd
Command(doCommand, undoCommand) {
cmd = doCommand
uncmd = undoCommand
}
def doCommand() {
cmd()
}
def undoCommand() {
uncmd()
}
}
def count = 0
def commands = []
1.upto(10) { i ->
commands.add(new Command({count++}, {count--}))
}
println "count is initially ${count}"
commands.each { c -> c.doCommand() }
commands.reverseEach { c -> c.undoCommand() }
println "undid all commands, count is ${count}"
commands.each { c -> c.doCommand() }
println "redid all command, count is ${count}"
将代码块作为参数传入比较麻烦,但它支持简洁但仍然可读的 commands.add(new Command({count++}, {count--})) 语法。
代码块、表达性和惯用模式
尽管代码块和匿名内部类之间的不同看似只是语义不同,但实际上它对代码的可读性以及积累惯用模式的容易程度会有很大影响。考虑一下这里我称为 Unit of Work 的惯用模式示例。首先是 Java 版本(使用匿名内部类),如清单 6 所示:
清单 6. 使用匿名内部类的 Unit of Work 模式
public void wrapInTransaction(Command c) throws SQLException {
setupDataInfrastructure();
try {
c.execute();
completeTransaction();
} catch (RuntimeException ex) {
rollbackTransaction();
throw ex;
} finally {
cleanUp();
}
}
public void addOrderFrom(final ShoppingCart cart, final String userName,
final Order order) throws SQLException {
wrapInTransaction(new Command() {
public void execute() throws SQLException{
add(order, userKeyBasedOn(userName));
addLineItemsFrom(cart, order.getOrderKey());
}
});
}
更多精彩
赞助商链接