Search code examples
design-patternsclojurebuilder

What's the clojure way to builder pattern?


We usually use builder pattern in java, like this:

UserBuilder userBuilder = new UserBuilder();
User John = userBuiler.setName("John")
                      .setPassword("1234")
                      .isVip(true)
                      .visableByPublic(false)
                      .build();

Some of the attributes have default value, and some haven't.

Passing attributes in a map may be a solution, but it makes the argument really longer:

(def john (make-user {:name "John" :pass "1234" :vip true :visible false}))

So, my question is, is there a elegant way to achieve this?


Solution

  • If you want to construct some clojure structure, you can use destructuring pattern in function arguments. You will then achieve the similar thing you have already wrote.

    (defn make-user [& {:keys [name pass vip visible]}]
      ; Here name, pass, vip and visible are regular variables
      ; Do what you want with them
    )
    
    (def user (make-user :name "Name" :pass "Pass" :vip false :visible true))
    

    I doubt that you can do something in less code than this.

    If you want to construct Java object (using its setters), you can use the approach Nicolas suggested.