0

I'm working on a tree-structured storage and we currently use transaction methods to modify the tree-structure. I always thought using the Command Pattern would be appropriate. However I just changed a small behaviour and I like it (returning the transaction instance located at the inserted node (except for attributes)):

wtx.insertText(EInsert.ASRIGHTSIBLING, "value").insertElement(EInsert.ASRIGHTSIBLING, new QName("bla").insertElement(EInsert.ASFIRSTCHILD, new QName("blubb")).insertAttribute(new QName("foo"), "bar").insertAttribute(new QName("bar"), "foo"));

I think chaining the operations this way is very nice, but our transactions provide cursor like methods on the tree (moveTo(long), moveToParent(), moveToFirstChild()...) which return boolean values instead of the current transaction instance, but I think this can't be avoided. Otherwise we could even do moves between without the cumbersome

wtx.method();wtx.method();wtx.method();

However I thought about the command pattern which would be

new InsertText(EInsert.ASRIGHTSIBLING, "value").execute(wtx);
new InsertElement(EInsert.ASRIGHTSIBLING, new QName("bla")).execute(wtx);
...

which is a bit more verbose but well, it would "support" the open/closed principle which is really nice.

So, what do you think?

4

1 回答 1

1

对我来说,树结构听起来像是一个巨大的复合对象。由于您还要处理构建事务,我认为使用命令模式是合适的,但如果可能,您应该考虑将您的 execute() 调用包装在某种导向器中,以便尽可能将命令和构建器模式混合在一起.

public void directorMethod(Object wtx) {
  InsertText(EInsert.ASRIGHTSIBLING, "value").execute(wtx);
  InsertText(EInsert.ASRIGHTSIBLING, "blah").execute(wtx);
}

请注意,void 返回可能是您的树结构,而 wtx 参数可以是事务和/或树结构,我不一定确定您想要如何做。使用 Builder 的想法是从底层实现中抽象出 Composite 事务的构建。这样,如果您需要更改底层命令,您应该能够这样做并进行原子测试。

于 2012-04-30T12:18:34.043 回答