Browse Source

builderdemo

pull/1/head
woniu 2 years ago
parent
commit
f2aeba1ed9
  1. 8
      builderdemo/.idea/.gitignore
  2. 14
      builderdemo/.idea/compiler.xml
  3. 25
      builderdemo/.idea/jarRepositories.xml
  4. 14
      builderdemo/.idea/misc.xml
  5. 2
      builderdemo/builderdemo.iml
  6. 26
      builderdemo/pom.xml
  7. 63
      builderdemo/src/main/java/com/woniu/builderdemo/Builder.java
  8. 32
      builderdemo/src/main/java/com/woniu/builderdemo/GirlFriend.java
  9. 50
      builderdemo/src/main/java/com/woniu/builderdemo/GirlFriendNew.java
  10. 58
      builderdemo/src/main/java/com/woniu/builderdemo/GirlFriendNewN.java
  11. 12
      builderdemo/src/main/java/com/woniu/builderdemo/MainApplication.java
  12. BIN
      builderdemo/target/classes/com/woniu/builderdemo/Builder$Consumer1.class
  13. BIN
      builderdemo/target/classes/com/woniu/builderdemo/Builder$Consumer2.class
  14. BIN
      builderdemo/target/classes/com/woniu/builderdemo/Builder$Consumer3.class
  15. BIN
      builderdemo/target/classes/com/woniu/builderdemo/Builder.class
  16. BIN
      builderdemo/target/classes/com/woniu/builderdemo/GirlFriend.class
  17. BIN
      builderdemo/target/classes/com/woniu/builderdemo/GirlFriendNew.class
  18. BIN
      builderdemo/target/classes/com/woniu/builderdemo/GirlFriendNewN.class
  19. BIN
      builderdemo/target/classes/com/woniu/builderdemo/MainApplication.class

8
builderdemo/.idea/.gitignore

@ -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/

14
builderdemo/.idea/compiler.xml

@ -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>

25
builderdemo/.idea/jarRepositories.xml

@ -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>

14
builderdemo/.idea/misc.xml

@ -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>

2
builderdemo/builderdemo.iml

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />

26
builderdemo/pom.xml

@ -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>

63
builderdemo/src/main/java/com/woniu/builderdemo/Builder.java

@ -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);
}
}

32
builderdemo/src/main/java/com/woniu/builderdemo/GirlFriend.java

@ -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);
}
}

50
builderdemo/src/main/java/com/woniu/builderdemo/GirlFriendNew.java

@ -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);
// 等等等等 ...
}
}

58
builderdemo/src/main/java/com/woniu/builderdemo/GirlFriendNewN.java

@ -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();
}
}

12
builderdemo/src/main/java/com/woniu/builderdemo/MainApplication.java

@ -0,0 +1,12 @@
package com.woniu.builderdemo;
/**
* 程序员经常会遇到灵魂拷问你有对象吗
*
* 没有但我可以 new 一个
*/
public class MainApplication {
public static void main(String[] args) {
}
}

BIN
builderdemo/target/classes/com/woniu/builderdemo/Builder$Consumer1.class

Binary file not shown.

BIN
builderdemo/target/classes/com/woniu/builderdemo/Builder$Consumer2.class

Binary file not shown.

BIN
builderdemo/target/classes/com/woniu/builderdemo/Builder$Consumer3.class

Binary file not shown.

BIN
builderdemo/target/classes/com/woniu/builderdemo/Builder.class

Binary file not shown.

BIN
builderdemo/target/classes/com/woniu/builderdemo/GirlFriend.class

Binary file not shown.

BIN
builderdemo/target/classes/com/woniu/builderdemo/GirlFriendNew.class

Binary file not shown.

BIN
builderdemo/target/classes/com/woniu/builderdemo/GirlFriendNewN.class

Binary file not shown.

BIN
builderdemo/target/classes/com/woniu/builderdemo/MainApplication.class

Binary file not shown.
Loading…
Cancel
Save