From 5d97f29cfab2c32346f0a4286775c586173e3a4d Mon Sep 17 00:00:00 2001 From: niushuai233 Date: Wed, 20 Dec 2023 16:56:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20:green=5Fapple:=20=20=E5=BB=BA=E9=80=A0?= =?UTF-8?q?=E8=80=85=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 6 ++ .../creatormodel/builder/_01/Address.java | 47 +++++++++++ .../creatormodel/builder/_01/People.java | 79 +++++++++++++++++++ .../creatormodel/builder/_01/Sex.java | 28 +++++++ .../creatormodel/builder/package-info.java | 8 ++ .../builder/_01/PeopleBuilderTest.java | 57 +++++++++++++ 6 files changed, 225 insertions(+) create mode 100644 src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Address.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/People.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Sex.java create mode 100644 src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/package-info.java create mode 100644 src/test/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/PeopleBuilderTest.java diff --git a/pom.xml b/pom.xml index a55b765..8c7c7c8 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,12 @@ 5.10.1 test + + + net.datafaker + datafaker + 1.9.0 + org.slf4j diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Address.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Address.java new file mode 100644 index 0000000..c50affc --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Address.java @@ -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; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/People.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/People.java new file mode 100644 index 0000000..acc5554 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/People.java @@ -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; + } + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Sex.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Sex.java new file mode 100644 index 0000000..555b562 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/Sex.java @@ -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; +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/package-info.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/package-info.java new file mode 100644 index 0000000..383a143 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/builder/package-info.java @@ -0,0 +1,8 @@ +/** + * 建造者模式 + * + * @author niushuai233 + * @date 2023/12/20 16:18 + * @since 0.0.1 + */ +package cc.niushuai.demo.designpattern.creatormodel.builder; \ No newline at end of file diff --git a/src/test/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/PeopleBuilderTest.java b/src/test/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/PeopleBuilderTest.java new file mode 100644 index 0000000..fd56006 --- /dev/null +++ b/src/test/java/cc/niushuai/demo/designpattern/creatormodel/builder/_01/PeopleBuilderTest.java @@ -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()); + + } +}