Fluent interfaces and leaky abstractions

前端 未结 8 2236
攒了一身酷
攒了一身酷 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:04

    Neal Ford does a nice job of explaining and giving Fluent Interface examples in his book the 'Productive Programmer'.

    Traditional Object or 'bean' with getters/setters:

    Car car = new CarImpl();
    MarketingDescription des = new MarketingDescriptionImpl();
    desc.setType("Box");
    desc.setSubtype("Insulated");
    desc.setAttribute("length", "50.5");
    desc.setAttribute("ladder", "yes");
    desc.setAttribute("lining type", "cork");
    car.setDescription(desc);
    

    Meet the same need with a fluent interface:

    Car car = Car.describedAs()
      .box()
      .length(50.5)
      .type(Type.INSULATED)
      .includes(Equipment.LADDER)
      .lining(Lining.CORK);
    

提交回复
热议问题