开发者

Looking for the name of a design pattern

VBScript guarantees that the GC will run after every line, so if you create an object and don't keep a reference, its destructor will be called at the end of the line. This allows you to do a number of interesting things, one of which is simulating optional arguments:

with foo(mandatoryArg)
    .optArg = 42
end

Another is allowing for a convenient builder syntax:

with Schema.define("Foo")
    .attr "name", String

    with .attr "key", String
        .lengthEquals(10)
    end
end

In this example, define and attr return objects that finalize the schema and attribute definitions in the destructor.

I've been call开发者_如何学运维ing the temporary object an ephemeron, but I was wondering if there was a preexisting term for such constructions. Anyone seen this elsewhere?


According to Builder pattern in wikipedia this is the builder or more specifically the concrete builder. In practice these objects are usually short lived. The builder pattern has a few components including the director, etc. But this short lived object you are calling an ephemeron seems to be precisely the builder object.


You're definitely using a builder pattern. The abuse of GC is new, though. The more general name for the type of syntax you're building is a fluent interface.

You could achieve something similar in C# with the "using" syntax, but in practice people use lambda expressions for this, which are more clear.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜