What's a fluent interface?

前端 未结 4 1705
无人及你
无人及你 2020-12-15 07:29

I recently came across this expression - but reading up on Wikipedia did not clarify it much for me - I still don\'t get it:

  1. What\'s the point of it
  2. H
4条回答
  •  無奈伤痛
    2020-12-15 07:46

    There are different interpretations of the term "fluent interface". A common way to create one in C++ is method chaining, which is commonly used in for example the iostream library:

    Object.MethodA().MethodB();
    cout << "a = " << a;
    

    The Named Parameter Idiom is another nice example of a fluent interface:

    Window w = CreateWindow()
                   .Width(400)
                   .Height(300)
                   .OnTop();
    

    The benefits? Code that's better readable and more flexible, although that still depends on the implementation of course.

提交回复
热议问题