What is the Scala equivalent to a Java builder pattern?

前端 未结 5 586
遇见更好的自我
遇见更好的自我 2020-12-23 13:31

In the work that I do on a day to day in Java, I use builders quite a lot for fluent interfaces, e.g.: new PizzaBuilder(Size.Large).onTopOf(Base.Cheesy).with(Ingredien

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 13:46

    using Scala partial applies are feasible if you are building a smallish object that you don't need to pass over method signatures. If any of those assumptions don't apply, I recommend using a mutable builder to build an immutable object. With this being scala you could implement the builder pattern with a case class for the object to build with a companion as the builder.

    Given that the end result is a constructed immutable object I don't see that it defeats any of the Scala principles.

提交回复
热议问题