【Java】你还在 new 对象吗?Java8 通用 Builder 了解一下?

你还在 new 对象吗?Java8 通用 Builder 了解一下?

码农突围发布于 今天 10:28

程序员经常会遇到灵魂拷问:你有对象吗?
没有,但我可以 new 一个!

public class GirlFriend {

private String name;

private int age;

// 省略 getter & setter ...

public static void main(String[] args) {

GirlFriend myGirlFriend = new GirlFriend();

myGirlFriend.setName("小美");

myGirlFriend.setAge(18);

}

}

没问题,老铁!但如果对象的属性太多,咋办?

public class GirlFriend {

private String name;

private int age;

private int bust;

private int waist;

private int hips;

private Listhobby;

private String birthday;

private String address;

private String mobile;

private String email;

private String hairColor;

private Mapgift;

// 等等等等 ...

// 省略 getter & setter ...

public static void main(String[] args) {

GirlFriend myGirlFriend = new GirlFriend();

myGirlFriend.setName("小美");

myGirlFriend.setAge(18);

myGirlFriend.setBust(33);

myGirlFriend.setWaist(23);

myGirlFriend.setHips(33);

myGirlFriend.setBirthday("2001-10-26");

myGirlFriend.setAddress("上海浦东");

myGirlFriend.setMobile("18688888888");

myGirlFriend.setEmail("[email protected]");

myGirlFriend.setHairColor("浅棕色带点微卷");

Listhobby = new ArrayList<>();

hobby.add("逛街");

hobby.add("购物");

hobby.add("买东西");

myGirlFriend.setHobby(hobby);

Mapgift = new HashMap<>();

gift.put("情人节礼物", "LBR 1912女王时代");

gift.put("生日礼物", "迪奥烈焰蓝金");

gift.put("纪念日礼物", "阿玛尼红管唇釉");

myGirlFriend.setGift(gift);

// 等等等等 ...

}

}

GirlFriend{name='小美'

, age=18

, bust=33

, waist=23

, hips=33

, hobby=[逛街, 购物, 买东西]

, birthday='2001-10-26'

, address='上海浦东'

, mobile='18688888888'

, email='[email protected]'

, hairColor='浅棕色带点微卷'

, gift={情人节礼物=LBR 1912女王时代, 生日礼物=迪奥烈焰蓝金, 纪念日礼物=阿玛尼红管唇釉}

}

GirlFriend 是很美,但写起来也太麻烦了吧。

说说缺点:实例化和设置属性分开,不好维护;变量名重复写。

莫慌,看法宝~

这里不再介绍其他 Builder 实现方式,直接祭出最实用的通用Builder:

适用于所有类,不需要改造原来类,不需要 lombok 插件支持。

先看看使用姿势:

public class GirlFriend {

// 省略属性 ...

// 省略 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();

}

}

看到了吗!实例化和属性设置在同一条语句执行,链式操作,一路点点点,清爽!

Talk is cheap, show me the code:

/**

* 通用的 Builder 模式构建器

*

* @author: CipherCui

* @since 2019/8/29

*/

public class Builder<T> {

private final Supplierinstantiator;

private List<consumer> modifiers = new ArrayList<>();

public Builder(Supplierinstantiator){

this.instantiator = instantiator;

}

public staticBuilderof(Supplierinstantiator){

return new Builder<>(instantiator);

}

publicBuilderwith(Consumer1consumer, P1 p1){

Consumerc = instance -> consumer.accept(instance, p1);

modifiers.add(c);

return this;

}

publicBuilderwith(Consumer2consumer, P1 p1, P2 p2){

Consumerc = instance -> consumer.accept(instance, p1, p2);

modifiers.add(c);

return this;

}

publicBuilderwith(Consumer3consumer, P1 p1, P2 p2, P3 p3){

Consumerc = 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 参数 Consumer

*/

@FunctionalInterface

public interface Consumer3<T, P1, P2, P3> {

void accept(T t, P1 p1, P2 p2, P3 p3);

}

}

这个示例最多支持三个参数的设置属性方法,也完全够用了。如果要扩展也很容易,依葫芦画瓢,添加多个参数的Consumer。

快用你的 Builder 建个对象吧~

来自:rrd.me/gtQTp
【Java】你还在 new 对象吗?Java8 通用 Builder 了解一下?
欢迎关注我的微信公众号「码农突围」,分享Python、Java、大数据、机器学习、人工智能等技术,关注码农技术提升•职场突围•思维跃迁,20万+码农成长充电第一站,陪有梦想的你一起成长

java

本文系转载,阅读原文

https://mp.weixin.qq.com/s?__biz=MzUyNDkzNzczNQ==&mid=2247501384&idx=2&sn=125384a2388578aa047fd3227a1972fe&chksm=fa273920cd50b0363bd394abe697352454ffbc9768222e876edf759862f2b5ab82afce8927a3&scene=27#wechat_redirect

阅读 36发布于 今天 10:28

avatar

码农突围

分享音视频、Python、Java等技术,15万程序员的成长充电站。

24 声望

7 粉丝

0 条评论

得票时间

avatar

码农突围

分享音视频、Python、Java等技术,15万程序员的成长充电站。

24 声望

7 粉丝

宣传栏

程序员经常会遇到灵魂拷问:你有对象吗?
没有,但我可以 new 一个!

public class GirlFriend {

private String name;

private int age;

// 省略 getter & setter ...

public static void main(String[] args) {

GirlFriend myGirlFriend = new GirlFriend();

myGirlFriend.setName("小美");

myGirlFriend.setAge(18);

}

}

没问题,老铁!但如果对象的属性太多,咋办?

public class GirlFriend {

private String name;

private int age;

private int bust;

private int waist;

private int hips;

private Listhobby;

private String birthday;

private String address;

private String mobile;

private String email;

private String hairColor;

private Mapgift;

// 等等等等 ...

// 省略 getter & setter ...

public static void main(String[] args) {

GirlFriend myGirlFriend = new GirlFriend();

myGirlFriend.setName("小美");

myGirlFriend.setAge(18);

myGirlFriend.setBust(33);

myGirlFriend.setWaist(23);

myGirlFriend.setHips(33);

myGirlFriend.setBirthday("2001-10-26");

myGirlFriend.setAddress("上海浦东");

myGirlFriend.setMobile("18688888888");

myGirlFriend.setEmail("[email protected]");

myGirlFriend.setHairColor("浅棕色带点微卷");

Listhobby = new ArrayList<>();

hobby.add("逛街");

hobby.add("购物");

hobby.add("买东西");

myGirlFriend.setHobby(hobby);

Mapgift = new HashMap<>();

gift.put("情人节礼物", "LBR 1912女王时代");

gift.put("生日礼物", "迪奥烈焰蓝金");

gift.put("纪念日礼物", "阿玛尼红管唇釉");

myGirlFriend.setGift(gift);

// 等等等等 ...

}

}

GirlFriend{name='小美'

, age=18

, bust=33

, waist=23

, hips=33

, hobby=[逛街, 购物, 买东西]

, birthday='2001-10-26'

, address='上海浦东'

, mobile='18688888888'

, email='[email protected]'

, hairColor='浅棕色带点微卷'

, gift={情人节礼物=LBR 1912女王时代, 生日礼物=迪奥烈焰蓝金, 纪念日礼物=阿玛尼红管唇釉}

}

GirlFriend 是很美,但写起来也太麻烦了吧。

说说缺点:实例化和设置属性分开,不好维护;变量名重复写。

莫慌,看法宝~

这里不再介绍其他 Builder 实现方式,直接祭出最实用的通用Builder:

适用于所有类,不需要改造原来类,不需要 lombok 插件支持。

先看看使用姿势:

public class GirlFriend {

// 省略属性 ...

// 省略 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();

}

}

看到了吗!实例化和属性设置在同一条语句执行,链式操作,一路点点点,清爽!

Talk is cheap, show me the code:

/**

* 通用的 Builder 模式构建器

*

* @author: CipherCui

* @since 2019/8/29

*/

public class Builder<T> {

private final Supplierinstantiator;

private List<consumer> modifiers = new ArrayList<>();

public Builder(Supplierinstantiator){

this.instantiator = instantiator;

}

public staticBuilderof(Supplierinstantiator){

return new Builder<>(instantiator);

}

publicBuilderwith(Consumer1consumer, P1 p1){

Consumerc = instance -> consumer.accept(instance, p1);

modifiers.add(c);

return this;

}

publicBuilderwith(Consumer2consumer, P1 p1, P2 p2){

Consumerc = instance -> consumer.accept(instance, p1, p2);

modifiers.add(c);

return this;

}

publicBuilderwith(Consumer3consumer, P1 p1, P2 p2, P3 p3){

Consumerc = 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 参数 Consumer

*/

@FunctionalInterface

public interface Consumer3<T, P1, P2, P3> {

void accept(T t, P1 p1, P2 p2, P3 p3);

}

}

这个示例最多支持三个参数的设置属性方法,也完全够用了。如果要扩展也很容易,依葫芦画瓢,添加多个参数的Consumer。

快用你的 Builder 建个对象吧~

来自:rrd.me/gtQTp
【Java】你还在 new 对象吗?Java8 通用 Builder 了解一下?
欢迎关注我的微信公众号「码农突围」,分享Python、Java、大数据、机器学习、人工智能等技术,关注码农技术提升•职场突围•思维跃迁,20万+码农成长充电第一站,陪有梦想的你一起成长

以上是 【Java】你还在 new 对象吗?Java8 通用 Builder 了解一下? 的全部内容, 来源链接: utcz.com/a/109869.html

回到顶部