6 changed files with 225 additions and 0 deletions
@ -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; |
||||||
|
} |
||||||
|
} |
@ -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; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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; |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
/** |
||||||
|
* 建造者模式 |
||||||
|
* |
||||||
|
* @author niushuai233 |
||||||
|
* @date 2023/12/20 16:18 |
||||||
|
* @since 0.0.1 |
||||||
|
*/ |
||||||
|
package cc.niushuai.demo.designpattern.creatormodel.builder; |
@ -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…
Reference in new issue