Browse Source

feat: 🍎 工厂模式

master
niushuai233 1 year ago
parent
commit
38b48e3559
  1. 47
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/GoodsFactory.java
  2. 34
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/IGoodsService.java
  3. 37
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/entity/GoodsReq.java
  4. 34
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/entity/GoodsResponse.java
  5. 49
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/ClothService.java
  6. 47
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/FoodService.java
  7. 47
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/service/FruitService.java
  8. 8
      src/main/java/cc/niushuai/demo/designpattern/creatormodel/factory/package-info.java
  9. 57
      src/test/java/cc/niushuai/demo/designpattern/creatormodel/factory/_01/GoodsFactoryTest.java

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

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

37
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;
}

34
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;
}

49
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;
}
}

47
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;
}
}

47
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;
}
}

8
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;

57
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));
}
}
Loading…
Cancel
Save