woniu
2 years ago
19 changed files with 304 additions and 0 deletions
@ -0,0 +1,8 @@ |
|||||
|
# Default ignored files |
||||
|
/shelf/ |
||||
|
/workspace.xml |
||||
|
# Datasource local storage ignored files |
||||
|
/../../../:\builderdemo\.idea/dataSources/ |
||||
|
/dataSources.local.xml |
||||
|
# Editor-based HTTP Client requests |
||||
|
/httpRequests/ |
@ -0,0 +1,14 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="CompilerConfiguration"> |
||||
|
<annotationProcessing> |
||||
|
<profile default="true" name="Default" enabled="true" /> |
||||
|
<profile name="Maven default annotation processors profile" enabled="true"> |
||||
|
<sourceOutputDir name="target/generated-sources/annotations" /> |
||||
|
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" /> |
||||
|
<outputRelativeToContentRoot value="true" /> |
||||
|
<module name="builderdemo" /> |
||||
|
</profile> |
||||
|
</annotationProcessing> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,25 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="RemoteRepositoriesConfiguration"> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="central" /> |
||||
|
<option name="name" value="Central Repository" /> |
||||
|
<option name="url" value="https://repo.maven.apache.org/maven2" /> |
||||
|
</remote-repository> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="central" /> |
||||
|
<option name="name" value="Central Repository" /> |
||||
|
<option name="url" value="http://maven.aliyun.com/nexus/content/repositories/central/" /> |
||||
|
</remote-repository> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="central" /> |
||||
|
<option name="name" value="Maven Central repository" /> |
||||
|
<option name="url" value="https://repo1.maven.org/maven2" /> |
||||
|
</remote-repository> |
||||
|
<remote-repository> |
||||
|
<option name="id" value="jboss.community" /> |
||||
|
<option name="name" value="JBoss Community repository" /> |
||||
|
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" /> |
||||
|
</remote-repository> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,14 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project version="4"> |
||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" /> |
||||
|
<component name="MavenProjectsManager"> |
||||
|
<option name="originalFiles"> |
||||
|
<list> |
||||
|
<option value="$PROJECT_DIR$/pom.xml" /> |
||||
|
</list> |
||||
|
</option> |
||||
|
</component> |
||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK"> |
||||
|
<output url="file://$PROJECT_DIR$/out" /> |
||||
|
</component> |
||||
|
</project> |
@ -0,0 +1,2 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<module type="JAVA_MODULE" version="4" /> |
@ -0,0 +1,26 @@ |
|||||
|
<?xml version="1.0" encoding="UTF-8"?> |
||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" |
||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> |
||||
|
<modelVersion>4.0.0</modelVersion> |
||||
|
|
||||
|
<groupId>com.woniu.builderdemo</groupId> |
||||
|
<artifactId>builderdemo</artifactId> |
||||
|
<version>1.0-SNAPSHOT</version> |
||||
|
|
||||
|
<properties> |
||||
|
<maven.compiler.source>8</maven.compiler.source> |
||||
|
<maven.compiler.target>8</maven.compiler.target> |
||||
|
</properties> |
||||
|
|
||||
|
<dependencies> |
||||
|
<dependency> |
||||
|
<groupId>org.projectlombok</groupId> |
||||
|
<artifactId>lombok</artifactId> |
||||
|
<version>1.18.24</version> |
||||
|
</dependency> |
||||
|
|
||||
|
</dependencies> |
||||
|
|
||||
|
|
||||
|
</project> |
@ -0,0 +1,63 @@ |
|||||
|
package com.woniu.builderdemo; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.function.Consumer; |
||||
|
import java.util.function.Supplier; |
||||
|
|
||||
|
/** |
||||
|
* 通用的 Builder 模式构建器 |
||||
|
* |
||||
|
*/ |
||||
|
public class Builder<T> { |
||||
|
private final Supplier<T> instantiator; |
||||
|
private List<Consumer<T>> modifiers = new ArrayList<>(); |
||||
|
public Builder(Supplier<T> instantiator) { |
||||
|
this.instantiator = instantiator; |
||||
|
} |
||||
|
public static <T> Builder<T> of(Supplier<T> instantiator) { |
||||
|
return new Builder<>(instantiator); |
||||
|
} |
||||
|
public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) { |
||||
|
Consumer<T> c = instance -> consumer.accept(instance, p1); |
||||
|
modifiers.add(c); |
||||
|
return this; |
||||
|
} |
||||
|
public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) { |
||||
|
Consumer<T> c = instance -> consumer.accept(instance, p1, p2); |
||||
|
modifiers.add(c); |
||||
|
return this; |
||||
|
} |
||||
|
public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) { |
||||
|
Consumer<T> 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 参数 Consumer |
||||
|
*/ |
||||
|
@FunctionalInterface |
||||
|
public interface Consumer3<T, P1, P2, P3> { |
||||
|
void accept(T t, P1 p1, P2 p2, P3 p3); |
||||
|
} |
||||
|
} |
@ -0,0 +1,32 @@ |
|||||
|
package com.woniu.builderdemo; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
@Data |
||||
|
public class GirlFriend { |
||||
|
private String name; |
||||
|
private int age; |
||||
|
|
||||
|
public String getName() { |
||||
|
return name; |
||||
|
} |
||||
|
|
||||
|
public void setName(String name) { |
||||
|
this.name = name; |
||||
|
} |
||||
|
|
||||
|
public int getAge() { |
||||
|
return age; |
||||
|
} |
||||
|
|
||||
|
public void setAge(int age) { |
||||
|
this.age = age; |
||||
|
} |
||||
|
|
||||
|
// 省略 getter & setter ...
|
||||
|
public static void main(String[] args) { |
||||
|
GirlFriend myGirlFriend = new GirlFriend(); |
||||
|
myGirlFriend.setName("赵丽颖"); |
||||
|
myGirlFriend.setAge(18); |
||||
|
} |
||||
|
} |
@ -0,0 +1,50 @@ |
|||||
|
package com.woniu.builderdemo; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
@Data |
||||
|
public class GirlFriendNew { |
||||
|
|
||||
|
private String name; |
||||
|
private int age; |
||||
|
private int bust; |
||||
|
private int waist; |
||||
|
private int hips; |
||||
|
private List<String> hobby; |
||||
|
private String birthday; |
||||
|
private String address; |
||||
|
private String mobile; |
||||
|
private String email; |
||||
|
private String hairColor; |
||||
|
private Map<String, String> gift; |
||||
|
// 等等等等 ...
|
||||
|
// 省略 getter & setter ...
|
||||
|
public static void main(String[] args) { |
||||
|
GirlFriendNew myGirlFriend = new GirlFriendNew(); |
||||
|
myGirlFriend.setName("迪丽热巴"); |
||||
|
myGirlFriend.setAge(18); |
||||
|
myGirlFriend.setBust(33); |
||||
|
myGirlFriend.setWaist(23); |
||||
|
myGirlFriend.setHips(33); |
||||
|
myGirlFriend.setBirthday("2001-10-26"); |
||||
|
myGirlFriend.setAddress("公众号:woniuxgg"); |
||||
|
myGirlFriend.setMobile("18688888888"); |
||||
|
myGirlFriend.setEmail("pretty-xiaomei@qq.com"); |
||||
|
myGirlFriend.setHairColor("浅棕色带点微卷"); |
||||
|
List<String> hobby = new ArrayList<>(); |
||||
|
hobby.add("逛街"); |
||||
|
hobby.add("购物"); |
||||
|
hobby.add("买东西"); |
||||
|
myGirlFriend.setHobby(hobby); |
||||
|
Map<String, String> gift = new HashMap<>(); |
||||
|
gift.put("情人节礼物", "LBR 1912女王时代"); |
||||
|
gift.put("生日礼物", "迪奥烈焰蓝金"); |
||||
|
gift.put("纪念日礼物", "阿玛尼红管唇釉"); |
||||
|
myGirlFriend.setGift(gift); |
||||
|
// 等等等等 ...
|
||||
|
} |
||||
|
} |
@ -0,0 +1,58 @@ |
|||||
|
package com.woniu.builderdemo; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
|
||||
|
import java.util.*; |
||||
|
|
||||
|
@Data |
||||
|
public class GirlFriendNewN { |
||||
|
// 省略属性 ...
|
||||
|
private String name; |
||||
|
private int age; |
||||
|
private int bust; |
||||
|
private int waist; |
||||
|
private int hips; |
||||
|
private List<String> hobby; |
||||
|
private String birthday; |
||||
|
private String address; |
||||
|
private String mobile; |
||||
|
private String email; |
||||
|
private String hairColor; |
||||
|
private Map<String, String> gift; |
||||
|
// 省略 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) { |
||||
|
GirlFriendNewN girlFriendNewN = |
||||
|
Builder.of(GirlFriendNewN::new) |
||||
|
.with(GirlFriendNewN::setName, "小美") |
||||
|
.with(GirlFriendNewN::setAge, 18) |
||||
|
.with(GirlFriendNewN::setVitalStatistics, 33, 23, 33) |
||||
|
.with(GirlFriendNewN::setBirthday, "2001-10-26") |
||||
|
.with(GirlFriendNewN::setAddress, "上海浦东") |
||||
|
.with(GirlFriendNewN::setMobile, "18688888888") |
||||
|
.with(GirlFriendNewN::setEmail, "pretty-xiaomei@qq.com") |
||||
|
.with(GirlFriendNewN::setHairColor, "浅棕色带点微卷") |
||||
|
.with(GirlFriendNewN::addHobby, "逛街") |
||||
|
.with(GirlFriendNewN::addHobby, "购物") |
||||
|
.with(GirlFriendNewN::addHobby, "买东西") |
||||
|
.with(GirlFriendNewN::addGift, "情人节礼物", "LBR 1912女王时代") |
||||
|
.with(GirlFriendNewN::addGift, "生日礼物", "迪奥烈焰蓝金") |
||||
|
.with(GirlFriendNewN::addGift, "纪念日礼物", "阿玛尼红管唇釉") |
||||
|
// 等等等等 ...
|
||||
|
.build(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,12 @@ |
|||||
|
package com.woniu.builderdemo; |
||||
|
|
||||
|
/** |
||||
|
* 程序员经常会遇到灵魂拷问:你有对象吗? |
||||
|
* |
||||
|
* 没有,但我可以 new 一个! |
||||
|
*/ |
||||
|
public class MainApplication { |
||||
|
public static void main(String[] args) { |
||||
|
|
||||
|
} |
||||
|
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in new issue