diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/GoodsFactory.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/GoodsFactory.java new file mode 100644 index 0000000..3aa8715 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/GoodsFactory.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.factory._01; + +import cc.niushuai.demo.designpattern.creatormodel.factory._01.service.ClothService; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.service.FoodService; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.service.FruitService; +import lombok.extern.slf4j.Slf4j; + +/** + * goods factory + * + * @author niushuai233 + * @date 2023/12/20 9:28 + * @since 0.0.1 + */ +@Slf4j +public class GoodsFactory { + + public IGoodsService getGoodsService(Integer goodsType) { + + switch (goodsType) { + case 1: + return new ClothService(); + case 2: + return new FoodService(); + case 3: + return new FruitService(); + } + throw new RuntimeException("Bad goodsType value " + goodsType); + } + +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/IGoodsService.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/IGoodsService.java new file mode 100644 index 0000000..1ed7808 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/IGoodsService.java @@ -0,0 +1,34 @@ +/* + * 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.factory._01; + +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsReq; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsResponse; + +/** + * goods service interface + * + * @author niushuai233 + * @date 2023/12/20 9:51 + * @since 0.0.1 + */ +public interface IGoodsService { + + GoodsResponse sell(GoodsReq goodsReq); + + GoodsResponse buy(GoodsReq goodsReq); +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/entity/GoodsReq.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/entity/GoodsReq.java new file mode 100644 index 0000000..35209e7 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/entity/GoodsReq.java @@ -0,0 +1,37 @@ +/* + * 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.factory._01.entity; + +import lombok.Data; + +import java.math.BigDecimal; + +/** + * 商品 request body + * + * @author niushuai233 + * @date 2023/12/20 9:53 + * @since 0.0.1 + */ +@Data +public class GoodsReq { + + private String id; + private String name; + private Integer goodsType; + private BigDecimal price; +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/entity/GoodsResponse.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/entity/GoodsResponse.java new file mode 100644 index 0000000..5ce851c --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/entity/GoodsResponse.java @@ -0,0 +1,34 @@ +/* + * 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.factory._01.entity; + +import lombok.Data; + +/** + * 商品 response body + * + * @author niushuai233 + * @date 2023/12/20 9:55 + * @since 0.0.1 + */ +@Data +public class GoodsResponse { + + private Integer code; + + private String message; +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/ClothService.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/ClothService.java new file mode 100644 index 0000000..8634d57 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/ClothService.java @@ -0,0 +1,49 @@ +/* + * 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.factory._01.service; + +import cc.niushuai.demo.designpattern.creatormodel.factory._01.IGoodsService; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsReq; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsResponse; +import cn.hutool.core.util.StrUtil; +import lombok.extern.slf4j.Slf4j; + +/** + * 衣服品类 + * + * @author niushuai233 + * @date 2023/12/20 9:49 + * @since 0.0.1 + */ +@Slf4j +public class ClothService implements IGoodsService { + @Override + public GoodsResponse sell(GoodsReq goodsReq) { + GoodsResponse response = new GoodsResponse(); + response.setCode(200); + response.setMessage(StrUtil.format("之前买的一件{}衣服, 商品编号: {},不满意,又给卖了, 卖了¥{}", goodsReq.getName(), goodsReq.getId(), goodsReq.getPrice())); + return response; + } + + @Override + public GoodsResponse buy(GoodsReq goodsReq) { + GoodsResponse response = new GoodsResponse(); + response.setCode(200); + response.setMessage(StrUtil.format("使用¥{} 买了一件{}衣服, 商品编号: {}", goodsReq.getPrice(), goodsReq.getName(), goodsReq.getId())); + return response; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/FoodService.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/FoodService.java new file mode 100644 index 0000000..b18e990 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/FoodService.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.factory._01.service; + +import cc.niushuai.demo.designpattern.creatormodel.factory._01.IGoodsService; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsReq; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsResponse; +import cn.hutool.core.util.StrUtil; + +/** + * 食物品类 + * + * @author niushuai233 + * @date 2023/12/20 9:49 + * @since 0.0.1 + */ +public class FoodService implements IGoodsService { + @Override + public GoodsResponse sell(GoodsReq goodsReq) { + GoodsResponse response = new GoodsResponse(); + response.setCode(200); + response.setMessage(StrUtil.format("之前买的一件{}食物, 商品编号: {},不满意,还没卖呢都吃完了", goodsReq.getName(), goodsReq.getId(), goodsReq.getPrice())); + return response; + } + + @Override + public GoodsResponse buy(GoodsReq goodsReq) { + GoodsResponse response = new GoodsResponse(); + response.setCode(200); + response.setMessage(StrUtil.format("使用¥{} 买了一件{}食物, 商品编号: {}", goodsReq.getPrice(), goodsReq.getName(), goodsReq.getId())); + return response; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/FruitService.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/FruitService.java new file mode 100644 index 0000000..3526c70 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/FruitService.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.factory._01.service; + +import cc.niushuai.demo.designpattern.creatormodel.factory._01.IGoodsService; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsReq; +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsResponse; +import cn.hutool.core.util.StrUtil; + +/** + * 水果品类 + * + * @author niushuai233 + * @date 2023/12/20 9:51 + * @since 0.0.1 + */ +public class FruitService implements IGoodsService { + @Override + public GoodsResponse sell(GoodsReq goodsReq) { + GoodsResponse response = new GoodsResponse(); + response.setCode(200); + response.setMessage(StrUtil.format("之前买的一件{}水果, 商品编号: {},不满意, 卖不出去了...", goodsReq.getName(), goodsReq.getId())); + return response; + } + + @Override + public GoodsResponse buy(GoodsReq goodsReq) { + GoodsResponse response = new GoodsResponse(); + response.setCode(200); + response.setMessage(StrUtil.format("使用¥{} 买了一件{}水果, 商品编号: {}", goodsReq.getPrice(), goodsReq.getName(), goodsReq.getId())); + return response; + } +} diff --git a/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/package-info.java b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/package-info.java new file mode 100644 index 0000000..82e2513 --- /dev/null +++ b/src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/package-info.java @@ -0,0 +1,8 @@ +/** + * 工厂模式 + * + * @author niushuai233 + * @date 2023/12/20 11:41 + * @since 0.0.1 + */ +package cc.niushuai.demo.designpattern.creatormodel.factory; \ No newline at end of file diff --git a/src/test/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/GoodsFactoryTest.java b/src/test/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/GoodsFactoryTest.java new file mode 100644 index 0000000..e223ce6 --- /dev/null +++ b/src/test/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/GoodsFactoryTest.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.factory._01; + +import cc.niushuai.demo.designpattern.creatormodel.factory._01.entity.GoodsReq; +import cn.hutool.core.util.IdUtil; +import cn.hutool.core.util.RandomUtil; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; + +/** + * TODO + * + * @author niushuai233 + * @date 2023/12/20 13:55 + * @since 0.0.1 + */ +@Slf4j +public class GoodsFactoryTest { + + @Test + public void testGoodsFactory() { + + GoodsReq clothReq = new GoodsReq(); + clothReq.setGoodsType(1); + clothReq.setName("皮草大衣"); + clothReq.setPrice(RandomUtil.randomBigDecimal()); + clothReq.setId(IdUtil.nanoId()); + IGoodsService clothService = new GoodsFactory().getGoodsService(clothReq.getGoodsType()); + log.info("{}", clothService.buy(clothReq)); + log.info("{}", clothService.sell(clothReq)); + + GoodsReq foodReq = new GoodsReq(); + foodReq.setGoodsType(2); + foodReq.setName("面包"); + foodReq.setPrice(RandomUtil.randomBigDecimal()); + foodReq.setId(IdUtil.nanoId()); + IGoodsService foodService = new GoodsFactory().getGoodsService(foodReq.getGoodsType()); + log.info("{}", foodService.buy(foodReq)); + log.info("{}", foodService.sell(foodReq)); + + } +}