Fluent interfaces and leaky abstractions

前端 未结 8 2235
攒了一身酷
攒了一身酷 2021-02-13 14:30

What is a fluent interface? I can\'t find a good definition of this, but all I get are long code examples in a language I am not very familiar with (e.g. C++).

Also, wha

8条回答
  •  北荒
    北荒 (楼主)
    2021-02-13 15:01

    A fluent interface is an API that allows you to write code that reads more or less like normal English. For example:

    Find.All.Questions(Where.IsAnswered == true);
    

    Method-chaining is usually used as part of the implementation, but there is more to it than that. To quote Fowler:

    I've also noticed a common misconception - many people seem to equate fluent interfaces with Method Chaining. Certainly chaining is a common technique to use with fluent interfaces, but true fluency is much more than that.

    It is also often called an internal DSL, since the syntax resembles that of a DSL, but it is implemented inside the host language instead of being processed by a parser.

提交回复
热议问题