Builder object

New Object general we are direct new look

GirlFriend class {public
Private String name;
Private int Age;
// ... omitted setter & getter
public static void main (String [] args) {
GirlFriend GirlFriend new new myGirlFriend = ();
myGirlFriend.setName ( "Mel");
myGirlFriend.setAge (18);
}
}
no problem, the old iron! However, if the properties of an object too much, are we supposed to?

public class GirlFriend {
private String name;
private int age;
private int bust;
private int waist;
private int hips;
private List hobby;
// 等等等等 ...
// 省略 getter & setter ...
public static void main(String[] args) {
GirlFriend myGirlFriend = new GirlFriend();
myGirlFriend.setName("小美");
myGirlFriend.setAge(18);
myGirlFriend.setBust(33);
myGirlFriend.setWaist(23);
// 等等等等 ...
}
}

The Java8 General Builder applies to all classes, no alteration of the original class, without lombok plug-in support.

Take a look at the use of gestures:

class of GirlFriend {public
// ... omitted attribute
// omitted getter & setter ...

// 为了演示方便,加几个聚合方法
public void addHobby(String hobby) {
    this.hobby = Optional.ofNullable(this.hobby).orElse(new ArrayList<>());
    this.hobby.add(hobby);
}
public void addGift(String day, String gift) {
    this.gift = Optional.ofNullable(this.gift).orElse(new HashMap<>());
    this.gift.put(day, gift);
}
public void setVitalStatistics(int bust, int waist, int hips) {
    this.bust = bust;
    this.waist = waist;
    this.hips = hips;
}
public static void main(String[] args) {
    GirlFriend myGirlFriend = Builder.of(GirlFriend::new)
            .with(GirlFriend::setName, "小美")
            .with(GirlFriend::setAge, 18)
            .with(GirlFriend::setVitalStatistics, 33, 23, 33)
            .with(GirlFriend::setBirthday, "2001-10-26")
            .with(GirlFriend::setAddress, "上海浦东")
            .with(GirlFriend::setMobile, "18688888888")
            .with(GirlFriend::setEmail, "[email protected]")
            .with(GirlFriend::setHairColor, "浅棕色带点微卷")
            .with(GirlFriend::addHobby, "逛街")
            .with(GirlFriend::addHobby, "购物")
            .with(GirlFriend::addHobby, "买东西")
            .with(GirlFriend::addGift, "情人节礼物", "LBR 1912女王时代")
            .with(GirlFriend::addGift, "生日礼物", "迪奥烈焰蓝金")
            .with(GirlFriend::addGift, "纪念日礼物", "阿玛尼红管唇釉")
            // 等等等等 ...
            .build();
}

}
See it! Instantiation and execution property is set in the same statement, the chain operation, all the way little point, refreshing!

Talk is cheap, show me the code:

/**

  • Common mode builder Builder
  • @author: CipherCui
  • @since 2019/8/29
    */
    public class Builder {
    private final Supplier instantiator;
    private List<Consumer > modifiers = new ArrayList<>();
    public Builder(Supplier instantiator) {
    this.instantiator = instantiator;
    }
    public static Builder of(Supplier instantiator) {
    return new Builder<>(instantiator);
    }
    public Builder with(Consumer1<T, P1> consumer, P1 p1) {
    Consumer c = instance -> consumer.accept(instance, p1);
    modifiers.add(c);
    return this;
    }
    public <P1, P2> Builder with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
    Consumer c = instance -> consumer.accept(instance, p1, p2);
    modifiers.add(c);
    return this;
    }
    public <P1, P2, P3> Builder with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
    Consumer c = instance -> consumer.accept(instance, p1, p2, p3);
    modifiers.add(c);
    return this;
    }
    public T build() {
    T value = instantiator.get();
    modifiers.forEach(modifier -> modifier.accept(value));
    modifiers.clear();
    return value;
    }
    /**
    • 1 参数 Consumer
      */
      @FunctionalInterface
      public interface Consumer1<T, P1> {
      void accept(T t, P1 p1);
      }
      /**
    • 2 参数 Consumer
      */
      @FunctionalInterface
      public interface Consumer2<T, P1, P2> {
      void accept(T t, P1 p1, P2 p2);
      }
      /**
    • 3 Parameter Consumer
      * /
      @FunctionalInterface
      public interface Consumer3 <T, Pl, P2, P3> {
      void Accept (T T, Pl P1, P2 P2, P3 P3);
      }
      }
      This example supports up to three parameters set property method, also completely sufficient. If you want to expand is also very easy, Yihuhuhuapiao, add multiple parameters Consumer.

Reference: http: //www.ciphermagic.cn/java8-builder.html

Guess you like

Origin www.cnblogs.com/chen-chen-chen/p/12201556.html