Browse Source

feat: 🍏 建造者模式

master
niushuai233 1 year ago
parent
commit
5d97f29cfa
  1. 6
      pom.xml
  2. 47
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Address.java
  3. 79
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/People.java
  4. 28
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Sex.java
  5. 8
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/package-info.java
  6. 57
      src/test/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/PeopleBuilderTest.java

6
pom.xml

@ -28,6 +28,12 @@ @@ -28,6 +28,12 @@
<version>5.10.1</version>
<scope>test</scope>
</dependency>
<!--虚假数据生成器-->
<dependency>
<groupId>net.datafaker</groupId>
<artifactId>datafaker</artifactId>
<version>1.9.0</version>
</dependency>
<!--log api-->
<dependency>
<groupId>org.slf4j</groupId>

47
src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Address.java

@ -0,0 +1,47 @@ @@ -0,0 +1,47 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.builder._01;
import lombok.Data;
/**
* 简单地址生成器
*
* @author niushuai233
* @date 2023/12/20 16:20
* @since 0.0.1
*/
@Data
public class Address {
private String country;
private String province;
private String city;
private String street;
public Address(String country, String province, String city, String street) {
this.country = country;
this.province = province;
this.city = city;
this.street = street;
}
@Override
public String toString() {
return this.country + this.province + this.city + this.street;
}
}

79
src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/People.java

@ -0,0 +1,79 @@ @@ -0,0 +1,79 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.builder._01;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
/**
* people creator
*
* @author niushuai233
* @date 2023/12/20 16:19
* @since 0.0.1
*/
@Data
public class People {
private String id;
private String name;
private String idCard;
private Sex sex;
private Address address;
@Override
public String toString() {
return StrUtil.format("id: {}, 姓名: {}, 身份证号: {} 性别: {}, 地址: {}", id, name, idCard, sex, address);
}
public static class Builder {
private People people;
public Builder() {
this.people = new People();
}
public Builder id(String id) {
people.id = id;
return this;
}
public Builder name(String name) {
people.name = name;
return this;
}
public Builder idCard(String idCard) {
people.idCard = idCard;
return this;
}
public Builder sex(Sex sex) {
people.sex = sex;
return this;
}
public Builder address(Address address) {
people.address = address;
return this;
}
public People build() {
return people;
}
}
}

28
src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Sex.java

@ -0,0 +1,28 @@ @@ -0,0 +1,28 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.builder._01;
/**
* 简单性别
*
* @author niushuai233
* @date 2023/12/20 16:20
* @since 0.0.1
*/
public enum Sex {
Boy, Girl;
}

8
src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/package-info.java

@ -0,0 +1,8 @@ @@ -0,0 +1,8 @@
/**
* 建造者模式
*
* @author niushuai233
* @date 2023/12/20 16:18
* @since 0.0.1
*/
package cc.niushuai.demo.designpattern.creatormodel.builder;

57
src/test/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/PeopleBuilderTest.java

@ -0,0 +1,57 @@ @@ -0,0 +1,57 @@
/*
* Copyright (C) 2023 niushuai233 niushuai.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cc.niushuai.demo.designpattern.creatormodel.builder._01;
import cn.hutool.core.util.IdUtil;
import lombok.extern.slf4j.Slf4j;
import net.datafaker.Faker;
import org.junit.jupiter.api.Test;
import java.util.Locale;
/**
* people builder test
*
* @author niushuai233
* @date 2023/12/20 16:25
* @since 0.0.1
*/
@Slf4j
public class PeopleBuilderTest {
@Test
public void testPeople() {
Faker faker = new Faker(Locale.CHINA);
net.datafaker.providers.base.Address address = faker.address();
log.info("{}", new People.Builder()
.id(IdUtil.nanoId())
.name(faker.name().fullName())
.idCard(faker.idNumber().validZhCNSsn())
.sex(Sex.Boy)
.address(new Address(address.countryCode(), address.state(), address.cityName(), address.streetAddress()))
.build());
log.info("{}", new People.Builder()
.id(IdUtil.nanoId())
.name(faker.name().fullName())
.idCard(faker.idNumber().validZhCNSsn())
.sex(Sex.Girl)
.address(new Address(address.countryCode(), address.state(), address.cityName(), address.streetAddress()))
.build());
}
}
Loading…
Cancel
Save