├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── xbhog
│ ├── BaseDemo
│ ├── Order.java
│ └── OrderTotal.java
│ ├── DesignApplication.java
│ ├── SOLID5大原则
│ ├── DependencyInversionPrinciple
│ │ ├── BudgetReport.java
│ │ ├── MySQLDatabase.java
│ │ └── optimization
│ │ │ └── MySqlDataBase.java
│ ├── SinglePrincipleFile
│ │ ├── VideoUserService.java
│ │ └── impl
│ │ │ ├── GuestVideoUserService.java
│ │ │ ├── OrdinaryVideoUserService.java
│ │ │ └── VipVideoUserService.java
│ └── openClosedPrincipleFile
│ │ ├── ICalculationArea.java
│ │ └── impl
│ │ ├── CalculationArea.java
│ │ └── CalculationAreaExt.java
│ ├── chainresponsibility
│ ├── Config
│ │ └── SignConfig.java
│ ├── ContractCall.java
│ ├── ContractChain.java
│ ├── ContractSignProcessor.java
│ ├── Enum
│ │ └── ContractSignEnum.java
│ ├── annotations
│ │ └── ContractSign.java
│ ├── channel
│ │ ├── ContractSignChannel.java
│ │ └── ContractSignChannelImpl.java
│ ├── impl
│ │ ├── ContractSignCompactInitImpl.java
│ │ ├── ContractSignGenerateImpl.java
│ │ ├── ContractSignMockImpl.java
│ │ ├── ContractSignMqImpl.java
│ │ ├── ContractSignSaveUploadImpl.java
│ │ ├── ContractSignSerialImpl.java
│ │ └── ContractSignTradeImpl.java
│ ├── inter
│ │ ├── Call.java
│ │ ├── Chain.java
│ │ ├── Interceptor.java
│ │ └── Processor.java
│ └── pojo
│ │ ├── ContractRequest.java
│ │ └── ContractResponse.java
│ └── combatCode
│ ├── 工厂模式
│ └── design
│ │ ├── Coupon
│ │ ├── CouponInfo.java
│ │ ├── CouponResult.java
│ │ └── CouponService.java
│ │ ├── ICommodity.java
│ │ ├── StoreFactory.java
│ │ ├── card
│ │ ├── IQiYiCard.java
│ │ └── IQiYiCardService.java
│ │ ├── goods
│ │ ├── DeliverReq.java
│ │ ├── GoodsInfo.java
│ │ └── GoodsService.java
│ │ └── impl
│ │ ├── CardCommodityService.java
│ │ ├── CouponCommodityService.java
│ │ └── GoodsCommodityService.java
│ ├── 抽象工厂模式
│ └── design
│ │ ├── CacheService.java
│ │ ├── cache
│ │ └── CacheFactory.java
│ │ ├── factory
│ │ ├── ICacheAdapter.java
│ │ ├── JDKInvoicationHandler.java
│ │ ├── JDKProxy.java
│ │ └── impl
│ │ │ ├── EGMCacheAdapter.java
│ │ │ └── IIRCacheAdapter.java
│ │ ├── impl
│ │ └── CacheServiceImpl.java
│ │ ├── matter
│ │ ├── EGM.java
│ │ ├── IIR.java
│ │ └── RedisUtils.java
│ │ └── utils
│ │ └── ClassLoaderUtils.java
│ ├── 模板模式
│ └── design
│ │ ├── HttpClient.java
│ │ ├── NetMall.java
│ │ └── group
│ │ ├── DangDangNetMall.java
│ │ ├── JDNetMall.java
│ │ └── TaoBaoNetMall.java
│ └── 策略模式
│ └── design
│ ├── Context.java
│ ├── ICouponDiscount.java
│ └── impl
│ ├── MJCouponDiscount.java
│ ├── NYGCouponDiscount.java
│ ├── ZKCouponDiscount.java
│ └── ZjCouponDiscount.java
└── test
└── java
├── com
└── xbhog
│ └── signTest.java
└── openClosedPrincipleFile
├── TestDemo.java
└── TestDemo2.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Project exclude paths
2 | /target/
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 设计模式
2 |
3 | ## 学习内容:
4 | 1. [策略模式](https://github.com/xbhog/DesignPatternsStudy/tree/master/src/main/java/combatCode/%E7%AD%96%E7%95%A5%E6%A8%A1%E5%BC%8F/design)(优惠卷实例:满减,自减,N元购等):减少if else的实现,方便后续的扩展
5 | 2. [模板模式](https://github.com/xbhog/DesignPatternsStudy/tree/master/src/main/java/combatCode/%E6%A8%A1%E6%9D%BF%E6%A8%A1%E5%BC%8F/design)(爬取各网站数据,并生成对应海报):构建流程模板,子类继承父类实现
6 | 3. [工厂方法模式](https://github.com/xbhog/DesignPatternsStudy/tree/master/src/main/java/combatCode/%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/design)(模拟发奖多种商品):优惠卷,第三方兑换卡,实物商品,创建工厂,服务统一在工厂选举
7 | 4. [抽象工厂模式](https://github.com/xbhog/DesignPatternsStudy/tree/master/src/main/java/combatCode/%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/design)(模拟缓存服务,创建代理类,通过控制不同的入参对象,控制缓存的使用)
8 | ## 功能实现:
9 | 1. [责任链模式+组合实现合同签章功能](https://github.com/xbhog/DesignPatternsStudy/tree/master/src/main/java/combatCode/%E6%8A%BD%E8%B1%A1%E5%B7%A5%E5%8E%82%E6%A8%A1%E5%BC%8F/design)
10 | 
11 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.springframework.boot
8 | spring-boot-starter-parent
9 | 2.7.13
10 |
11 |
12 | org.example
13 | DesignPatterns
14 | 1.0-SNAPSHOT
15 |
16 |
17 | 8
18 | 8
19 |
20 |
21 |
22 | org.springframework.boot
23 | spring-boot-starter
24 |
25 |
26 | org.springframework.boot
27 | spring-boot-starter-test
28 | test
29 |
30 |
31 | org.projectlombok
32 | lombok
33 | 1.18.24
34 | provided
35 |
36 |
37 | junit
38 | junit
39 | 4.12
40 | test
41 |
42 |
43 | com.alibaba
44 | fastjson
45 | 1.2.78
46 |
47 |
48 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/BaseDemo/Order.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.BaseDemo;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | /**
8 | * @author xbhog
9 | * @describe:
10 | * @date 2022/6/7
11 | */
12 | @Data
13 | @AllArgsConstructor
14 | @NoArgsConstructor
15 | public class Order {
16 | private Long price;
17 | private Long quantity;
18 | private String country;
19 | private String demo;
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/BaseDemo/OrderTotal.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.BaseDemo;
2 |
3 | import java.util.List;
4 |
5 | /**
6 | * describe:订单总额
7 | * @author xbhog
8 | * @date 2022/6/7
9 | */
10 | public class OrderTotal {
11 | /**
12 | * 第一版:税率计算代码和方法的其他代码混杂在一起
13 | * @param orders
14 | * @return
15 | */
16 | public double getOrderToal(List orders){
17 | double total = 0;
18 | for(Order order : orders){
19 | total += order.getPrice() * order.getQuantity();
20 | if("US".equals(order.getCountry())){
21 | total += total * 0.07;
22 | } else if ("EU".equals(order.getCountry())) {
23 | total += total * 0.20;
24 | }
25 | }
26 | return total;
27 | }
28 |
29 | /**
30 | * 第二版:将计算税金的逻辑抽取到一个单独的方法中,并对原
31 | * 始方法隐藏该逻辑
32 | * @param orders
33 | * @return
34 | */
35 | public double getOrderToalTwo(List orders){
36 | double total = 0;
37 | for(Order order : orders){
38 | total += order.getPrice() * order.getQuantity();
39 | total += getTaxRate(order.getCountry());
40 | }
41 | return total;
42 | }
43 | public double getTaxRate(String country){
44 | if("US".equals(country)){
45 | return 0.07;
46 | } else if ("EU".equals(country)) {
47 | return 0.20;
48 | }
49 | return 0;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/DesignApplication.java:
--------------------------------------------------------------------------------
1 | package com.xbhog;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | /**
7 | * @author xbhog
8 | * @describe:
9 | * @date 2023/7/15
10 | */
11 | @SpringBootApplication
12 | public class DesignApplication {
13 | public static void main(String[] args) {
14 | SpringApplication.run(DesignApplication.class,args);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/DependencyInversionPrinciple/BudgetReport.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.DependencyInversionPrinciple;
2 |
3 | /**
4 | * @author xbhog
5 | * @describe:高层次的预算报告类
6 | * @date 2022/6/13
7 | */
8 | public class BudgetReport {
9 | private MySQLDatabase mySQLDatabase;
10 |
11 | public void open(String data){
12 | System.out.println("读取数据");
13 | }
14 | public void save(){
15 | System.out.println("保存数据");
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/DependencyInversionPrinciple/MySQLDatabase.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.DependencyInversionPrinciple;
2 |
3 | import com.xbhog.SOLID5大原则.DependencyInversionPrinciple.optimization.MySqlDataBase;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe:低层次的数据库类
8 | * @date 2022/6/13
9 | */
10 | public class MySQLDatabase {
11 | private MySqlDataBase mySqlDataBase;
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/DependencyInversionPrinciple/optimization/MySqlDataBase.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.DependencyInversionPrinciple.optimization;
2 |
3 | /**
4 | * @author xbhog
5 | * @describe:
6 | * @date 2022/6/13
7 | */
8 | public interface MySqlDataBase {
9 | void select();
10 | void insert();
11 | void update();
12 | void delete();
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/SinglePrincipleFile/VideoUserService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.SinglePrincipleFile;
2 |
3 | /**
4 | * @author xbhog
5 | * @describe: 单一原则例子
6 | * @date 2022/6/8
7 | */
8 | public interface VideoUserService {
9 | /**
10 | * 清晰度
11 | */
12 | void definition();
13 |
14 | /**
15 | * 广告
16 | */
17 | void advertisement();
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/SinglePrincipleFile/impl/GuestVideoUserService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.SinglePrincipleFile.impl;
2 |
3 | import com.xbhog.SOLID5大原则.SinglePrincipleFile.VideoUserService;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe: 访客用户
8 | * @date 2022/6/8
9 | */
10 | public class GuestVideoUserService implements VideoUserService {
11 | @Override
12 | public void definition() {
13 | System.out.println("视频不清楚");
14 | }
15 |
16 | @Override
17 | public void advertisement() {
18 | System.out.println("有广告");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/SinglePrincipleFile/impl/OrdinaryVideoUserService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.SinglePrincipleFile.impl;
2 |
3 | import com.xbhog.SOLID5大原则.SinglePrincipleFile.VideoUserService;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe: 普通用户
8 | * @date 2022/6/8
9 | */
10 | public class OrdinaryVideoUserService implements VideoUserService {
11 | @Override
12 | public void definition() {
13 | System.out.println("720P");
14 | }
15 |
16 | @Override
17 | public void advertisement() {
18 | System.out.println("50s广告");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/SinglePrincipleFile/impl/VipVideoUserService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.SinglePrincipleFile.impl;
2 |
3 | import com.xbhog.SOLID5大原则.SinglePrincipleFile.VideoUserService;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe: Vip用户
8 | * @date 2022/6/8
9 | */
10 | public class VipVideoUserService implements VideoUserService {
11 | @Override
12 | public void definition() {
13 | System.out.println("爷,里边请!氪金yyds");
14 | }
15 |
16 | @Override
17 | public void advertisement() {
18 | System.out.println("您随便看");
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/openClosedPrincipleFile/ICalculationArea.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.openClosedPrincipleFile;
2 |
3 | /**
4 | * 面积计算
5 | * @author xbhog
6 | */
7 | public interface ICalculationArea {
8 |
9 | /**
10 | * 计算面积,长方形
11 | *
12 | * @param x 长
13 | * @param y 宽
14 | * @return 面积
15 | */
16 | double rectangle(double x, double y);
17 |
18 | /**
19 | * 计算面积,三角形
20 | * @param x 边长x
21 | * @param y 边长y
22 | * @param z 边长z
23 | * @return 面积
24 | *
25 | * 海伦公式:S=√[p(p-a)(p-b)(p-c)] 其中:p=(a+b+c)/2
26 | */
27 | double triangle(double x, double y, double z);
28 |
29 | /**
30 | * 计算面积,圆形
31 | * @param r 半径
32 | * @return 面积
33 | *
34 | * 圆面积公式:S=πr²
35 | */
36 | double circular(double r);
37 |
38 | }
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/openClosedPrincipleFile/impl/CalculationArea.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.openClosedPrincipleFile.impl;
2 |
3 | import com.xbhog.SOLID5大原则.openClosedPrincipleFile.ICalculationArea;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe: 圆面积计算:标准
8 | * @date 2022/6/8
9 | */
10 | public class CalculationArea implements ICalculationArea {
11 | private final static double π = 3.14D;
12 | @Override
13 | public double rectangle(double x, double y) {
14 | return x * y;
15 | }
16 | @Override
17 | public double triangle(double x, double y, double z) {
18 | double p = (x + y + z) / 2;
19 | return Math.sqrt(p * (p - x) * (p - y) * (p - z));
20 | }
21 | @Override
22 | public double circular(double r) {
23 | return π * r * r;
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/SOLID5大原则/openClosedPrincipleFile/impl/CalculationAreaExt.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.SOLID5大原则.openClosedPrincipleFile.impl;
2 |
3 |
4 | /**
5 | * @author xbhog
6 | * @describe:圆面积扩展,采用精度更高Π实现,继承标准类,并做出有别于父类的行为
7 | * @date 2022/6/8
8 | */
9 | public class CalculationAreaExt extends CalculationArea {
10 | private final static double π = 3.141592653D;
11 |
12 | @Override
13 | public double circular(double r) {
14 | return π * r * r;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/Config/SignConfig.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.Config;
2 |
3 |
4 |
5 | import com.xbhog.chainresponsibility.annotations.ContractSign;
6 | import com.xbhog.chainresponsibility.inter.Interceptor;
7 | import org.springframework.core.annotation.AnnotationUtils;
8 | import org.springframework.stereotype.Component;
9 |
10 | import javax.annotation.PostConstruct;
11 | import javax.annotation.Resource;
12 | import java.util.List;
13 | import java.util.Map;
14 | import java.util.Objects;
15 | import java.util.concurrent.ConcurrentHashMap;
16 |
17 | /**
18 | * @author xbhog
19 | * @describe:
20 | * @date 2023/7/15
21 | */
22 |
23 | public class SignConfig {
24 | @Resource
25 | protected List contractSignList;
26 |
27 | protected static final Map CONTRACT_SIGN_MAP = new ConcurrentHashMap<>();
28 |
29 | @PostConstruct
30 | public void init(){
31 | contractSignList.forEach(interceptor -> {
32 | //查找这个接口的实现类上有没有ContractSign注解
33 | ContractSign sign = AnnotationUtils.findAnnotation(interceptor.getClass(), ContractSign.class);
34 | if(!Objects.isNull(sign)){
35 | CONTRACT_SIGN_MAP.put(sign.SIGN_CHANNEL().getCode(),interceptor);
36 | }
37 | });
38 | }
39 |
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/ContractCall.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility;
2 |
3 |
4 |
5 |
6 |
7 | import com.xbhog.chainresponsibility.inter.Call;
8 | import com.xbhog.chainresponsibility.inter.Interceptor;
9 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
10 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
11 |
12 | import java.util.List;
13 |
14 | /**
15 | * @author xbhog
16 | * @describe: 合同数据请求、节点的实例化及方法执行
17 | * @date 2023/7/11
18 | */
19 | public class ContractCall implements Call {
20 | private final T originalRequest;
21 | private final List> interceptorList;
22 |
23 | public ContractCall(T originalRequest, List> interceptorList) {
24 | this.originalRequest = originalRequest;
25 | this.interceptorList = interceptorList;
26 | }
27 |
28 | @Override
29 | public T request() {
30 | return this.originalRequest;
31 | }
32 |
33 | @Override
34 | public ContractResponse exectue() {
35 | //实例化流程节点
36 | ContractChain chain = new ContractChain(0,this.originalRequest,this.interceptorList);
37 | return chain.proceed(this.originalRequest);
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/ContractChain.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility;
2 |
3 |
4 |
5 |
6 |
7 | import com.xbhog.chainresponsibility.inter.Chain;
8 | import com.xbhog.chainresponsibility.inter.Interceptor;
9 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
10 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
11 | import lombok.extern.slf4j.Slf4j;
12 |
13 | import java.util.List;
14 | import java.util.Objects;
15 |
16 | /**
17 | * @author xbhog
18 | * @describe: 合同节点
19 | * @date 2023/7/11
20 | */
21 | @Slf4j
22 | public class ContractChain implements Chain {
23 | private final Integer index;
24 |
25 | private final T request;
26 |
27 | private final List> interceptors;
28 |
29 | public ContractChain(Integer index, T request, List> interceptors) {
30 | this.index = index;
31 | this.request = request;
32 | this.interceptors = interceptors;
33 | }
34 |
35 | @Override
36 | public T request() {
37 | return this.request;
38 | }
39 |
40 | @Override
41 | public ContractResponse proceed(T request) {
42 | //控制节点流程
43 | if(this.index >= this.interceptors.size()){
44 | throw new IllegalArgumentException("index越界");
45 | }
46 | //下一个节点参数设置
47 | Chain nextChain = new ContractChain(this.index + 1, request, this.interceptors);
48 | //获取节点信息
49 | Interceptor interceptor = this.interceptors.get(this.index);
50 | Class extends Interceptor> aClass = interceptor.getClass();
51 | log.info("下一个节点类:{}",aClass.getSimpleName());
52 | ContractResponse response = interceptor.process(nextChain);
53 | if(Objects.isNull(response)){
54 | throw new NullPointerException("intercetor"+interceptor+"return null");
55 | }
56 | return response;
57 | }
58 | }
59 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/ContractSignProcessor.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility;
2 |
3 |
4 |
5 | import com.xbhog.chainresponsibility.Config.SignConfig;
6 | import com.xbhog.chainresponsibility.inter.Interceptor;
7 | import com.xbhog.chainresponsibility.inter.Processor;
8 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
9 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
10 | import lombok.extern.slf4j.Slf4j;
11 | import org.springframework.stereotype.Component;
12 |
13 | import javax.annotation.Resource;
14 | import java.util.ArrayList;
15 | import java.util.Collection;
16 | import java.util.List;
17 |
18 | /**
19 | * @author xbhog
20 | * @describe: 责任链+组合+过滤器实现合同签章
21 | * @date 2023/7/11
22 | */
23 | @Slf4j
24 | @Component
25 | public class ContractSignProcessor extends SignConfig implements Processor {
26 |
27 | /* @Resource(name = "contractSignCompactInitImpl")
28 | private Interceptor contractCompactInitImpl;
29 |
30 | @Resource(name = "contractSignGenerateImpl")
31 | private Interceptor contractGenerateImpl;
32 |
33 | @Resource(name = "contractSignMockImpl")
34 | private Interceptor contractSignMockImpl;
35 |
36 | @Resource(name = "contractSignMqImpl")
37 | private Interceptor contractSignMqImpl;
38 |
39 | @Resource(name = "contractSignSaveUploadImpl")
40 | private Interceptor contractSignSaveUploadImpl;
41 |
42 | @Resource(name = "contractSignSerialImpl")
43 | private Interceptor contractSignSerialImpl;
44 |
45 | @Resource(name = "contractSignTradeImpl")
46 | private Interceptor ContractSignTradeImpl;*/
47 | public ContractSignProcessor() {
48 | }
49 |
50 | @Override
51 | public ContractResponse process(T paramter) {
52 | //获取所有的监听器
53 | List> interceptorList = new ArrayList<>();
54 | /*interceptorList.add(contractCompactInitImpl);
55 | interceptorList.add(contractGenerateImpl);
56 | interceptorList.add(contractSignMockImpl);
57 | interceptorList.add(contractSignMqImpl);
58 | interceptorList.add(contractSignSaveUploadImpl);
59 | interceptorList.add(contractSignSerialImpl);
60 | interceptorList.add(ContractSignTradeImpl);*/
61 | //获取排序后的结果,保证责任链的顺序,hashmap中key如果是数字的话,通过hashcode编码后后是有序的
62 | for(Integer key : CONTRACT_SIGN_MAP.keySet()){
63 | interceptorList.add(CONTRACT_SIGN_MAP.get(key));
64 | }
65 | //开始签章
66 | log.info("签章开始");
67 | return new ContractCall(paramter,interceptorList).exectue();
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/Enum/ContractSignEnum.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.Enum;
2 |
3 | /**
4 | * @author xbhog
5 | * @describe:
6 | * @date 2023/7/15
7 | */
8 | public class ContractSignEnum {
9 | public enum SignChannel {
10 |
11 | SIGN_INIT(1, "合同文本初始化"),
12 | SIGN_GENERATE(2, "合同文本生成"),
13 | SIGN_MOCK(3, "签章挡板"),
14 | SIGN_MQ(4, "合同签章完成发送MQ"),
15 | SIGN_TABLE(5, "合同签章表处理"),
16 | SIGN_UPLOAD(6, "合同签章完成上传服务器"),
17 | SIGN_TRADE(7, "签章渠道实际调用");
18 |
19 | private Integer code;
20 | private String info;
21 |
22 | SignChannel(Integer code, String info) {
23 | this.code = code;
24 | this.info = info;
25 | }
26 |
27 | public Integer getCode() {
28 | return code;
29 | }
30 |
31 | public void setCode(Integer code) {
32 | this.code = code;
33 | }
34 |
35 | public String getInfo() {
36 | return info;
37 | }
38 |
39 | public void setInfo(String info) {
40 | this.info = info;
41 | }
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/annotations/ContractSign.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.annotations;
2 |
3 |
4 |
5 | import com.xbhog.chainresponsibility.Enum.ContractSignEnum;
6 |
7 | import java.lang.annotation.ElementType;
8 | import java.lang.annotation.Retention;
9 | import java.lang.annotation.RetentionPolicy;
10 | import java.lang.annotation.Target;
11 |
12 | /**
13 | * @author xbhog
14 | * @describe:
15 | * @date 2023/7/15
16 | */
17 | @Target(ElementType.TYPE)
18 | @Retention(RetentionPolicy.RUNTIME)
19 | public @interface ContractSign {
20 | ContractSignEnum.SignChannel SIGN_CHANNEL();
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/channel/ContractSignChannel.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.channel;
2 |
3 |
4 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
5 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
6 |
7 | /**
8 | * @author xbhog
9 | * @describe:
10 | * @date 2023/7/15
11 | */
12 | public interface ContractSignChannel {
13 | /**
14 | * 节点值
15 | * @return 字符串
16 | */
17 | String channelCode();
18 |
19 | /**
20 | * 签章具体方法
21 | * @param request 请求
22 | * @return 合同结果处理
23 | * @throws Exception 捕获
24 | */
25 | ContractResponse contractSign(ContractRequest request) throws Exception;
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/channel/ContractSignChannelImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.channel;
2 |
3 |
4 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
5 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
6 | import lombok.extern.slf4j.Slf4j;
7 | import org.springframework.stereotype.Component;
8 |
9 | /**
10 | * @author xbhog
11 | * @describe:
12 | * @date 2023/7/15
13 | */
14 | @Slf4j
15 | @Component
16 | public class ContractSignChannelImpl implements ContractSignChannel{
17 | @Override
18 | public String channelCode() {
19 | return "contractChannel";
20 | }
21 |
22 | @Override
23 | public ContractResponse contractSign(ContractRequest request) throws Exception {
24 | log.info("签章处理开始");
25 |
26 | return ContractResponse.builder().status("success").mas("处理成功").build();
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/impl/ContractSignCompactInitImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.impl;
2 |
3 |
4 |
5 |
6 | import com.xbhog.chainresponsibility.Enum.ContractSignEnum;
7 | import com.xbhog.chainresponsibility.annotations.ContractSign;
8 | import com.xbhog.chainresponsibility.inter.Chain;
9 | import com.xbhog.chainresponsibility.inter.Interceptor;
10 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
11 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
12 | import lombok.extern.slf4j.Slf4j;
13 | import org.springframework.stereotype.Component;
14 |
15 | import java.util.Objects;
16 |
17 | /**
18 | * @author xbhog
19 | * @describe: 合同文本初始化
20 | * @date 2023/7/12
21 | */
22 | @Slf4j
23 | @ContractSign(SIGN_CHANNEL = ContractSignEnum.SignChannel.SIGN_INIT)
24 | @Component
25 | public class ContractSignCompactInitImpl implements Interceptor {
26 | public ContractSignCompactInitImpl() {
27 | }
28 |
29 | @Override
30 | public ContractResponse process(Chain chain) {
31 | log.info("=============执行合同文本初始化拦截器开始");
32 | //获取处理的请求参数
33 | T request = chain.request();
34 | request.setStatus("1");
35 | log.info("=============执行合同文本初始化拦截器结束");
36 | //进入下一个责任链节点
37 | ContractResponse response = chain.proceed(request);
38 | if(Objects.isNull(response)){
39 | log.error("返回值的为空");
40 | response = ContractResponse.builder().status("fail").mas("处理失败").build();
41 | }
42 | //其他处理
43 | return response;
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/impl/ContractSignGenerateImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.impl;
2 |
3 |
4 | import com.xbhog.chainresponsibility.Enum.ContractSignEnum;
5 | import com.xbhog.chainresponsibility.annotations.ContractSign;
6 | import com.xbhog.chainresponsibility.inter.Chain;
7 | import com.xbhog.chainresponsibility.inter.Interceptor;
8 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
9 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
10 | import lombok.extern.slf4j.Slf4j;
11 | import org.springframework.stereotype.Component;
12 |
13 | import java.util.Objects;
14 |
15 | /**
16 | * @author xbhog
17 | * @describe: 合同文本生成
18 | * @date 2023/7/15
19 | */
20 | @Slf4j
21 | @ContractSign(SIGN_CHANNEL = ContractSignEnum.SignChannel.SIGN_GENERATE)
22 | @Component
23 | public class ContractSignGenerateImpl implements Interceptor {
24 |
25 | @Override
26 | public ContractResponse process(Chain chain) {
27 | log.info("=============执行合同文本生成拦截器开始");
28 | //获取处理的请求参数
29 | T request = chain.request();
30 | log.info("=============执行合同文本生成拦截器结束");
31 | //进入下一个责任链节点
32 | ContractResponse response = chain.proceed(request);
33 | if(Objects.isNull(response)){
34 | log.error("返回值的为空");
35 | response = ContractResponse.builder().status("fail").mas("处理失败").build();
36 | }
37 | //其他处理
38 | return response;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/impl/ContractSignMockImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.impl;
2 |
3 |
4 | import com.xbhog.chainresponsibility.Enum.ContractSignEnum;
5 | import com.xbhog.chainresponsibility.annotations.ContractSign;
6 | import com.xbhog.chainresponsibility.inter.Chain;
7 | import com.xbhog.chainresponsibility.inter.Interceptor;
8 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
9 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
10 | import lombok.extern.slf4j.Slf4j;
11 | import org.springframework.beans.factory.annotation.Value;
12 | import org.springframework.stereotype.Component;
13 |
14 | /**
15 | * @author xbhog
16 | * @describe: 签章挡板
17 | * @date 2023/7/15
18 | */
19 | @Slf4j
20 | @ContractSign(SIGN_CHANNEL = ContractSignEnum.SignChannel.SIGN_MOCK)
21 | @Component
22 | public class ContractSignMockImpl implements Interceptor {
23 | @Value("signMock")
24 | private String signMock;
25 | @Override
26 | public ContractResponse process(Chain chain) {
27 | log.info("=============执行签章挡板拦截器开始");
28 | //获取处理的请求参数
29 | T request = chain.request();
30 | //挡板设置可通过配置中心配置
31 | if("true".equals(signMock)){
32 | return ContractResponse.builder().status("success").mas("挡板开啦").build();
33 | }
34 | //其他处理
35 | log.info("=============执行签章挡板拦截器结束");
36 | return chain.proceed(request);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/impl/ContractSignMqImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.impl;
2 |
3 |
4 | import com.xbhog.chainresponsibility.Enum.ContractSignEnum;
5 | import com.xbhog.chainresponsibility.annotations.ContractSign;
6 | import com.xbhog.chainresponsibility.inter.Chain;
7 | import com.xbhog.chainresponsibility.inter.Interceptor;
8 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
9 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
10 | import lombok.extern.slf4j.Slf4j;
11 | import org.springframework.stereotype.Component;
12 |
13 | import java.util.Objects;
14 |
15 | /**
16 | * @author xbhog
17 | * @describe: 合同签章完成发送mq
18 | * @date 2023/7/15
19 | */
20 | @Slf4j
21 | @ContractSign(SIGN_CHANNEL = ContractSignEnum.SignChannel.SIGN_MQ)
22 | @Component
23 | public class ContractSignMqImpl implements Interceptor {
24 | @Override
25 | public ContractResponse process(Chain chain) {
26 | log.info("=============执行合同签章完成发送mq拦截器开始");
27 | //获取处理的请求参数
28 | T request = chain.request();
29 | //进入下一个责任链节点
30 | ContractResponse response = chain.proceed(request);
31 | if(Objects.isNull(response)){
32 | log.error("返回值的为空");
33 | response = ContractResponse.builder().status("fail").mas("处理失败").build();
34 | }
35 | if(response.getStatus().equals("success")){
36 | log.info("发送MQ给下游处理数据");
37 | }
38 | //其他处理
39 | log.info("=============执行合同签章完成发送mq拦截器结束");
40 | return response;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/impl/ContractSignSaveUploadImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.impl;
2 |
3 |
4 | import com.xbhog.chainresponsibility.Enum.ContractSignEnum;
5 | import com.xbhog.chainresponsibility.annotations.ContractSign;
6 | import com.xbhog.chainresponsibility.inter.Chain;
7 | import com.xbhog.chainresponsibility.inter.Interceptor;
8 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
9 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
10 | import lombok.extern.slf4j.Slf4j;
11 | import org.springframework.stereotype.Component;
12 |
13 | import java.util.Objects;
14 |
15 | /**
16 | * @author xbhog
17 | * @describe: 合同签章完成上传服务器
18 | * @date 2023/7/15
19 | */
20 | @Slf4j
21 | @ContractSign(SIGN_CHANNEL = ContractSignEnum.SignChannel.SIGN_UPLOAD)
22 | @Component
23 | public class ContractSignSaveUploadImpl implements Interceptor {
24 | @Override
25 | public ContractResponse process(Chain chain) {
26 | log.info("=============执行合同签章完成上传服务器拦截器开始");
27 | //获取处理的请求参数
28 | T request = chain.request();
29 | //进入下一个责任链节点
30 | ContractResponse response = chain.proceed(request);
31 | if(Objects.isNull(response)){
32 | log.error("返回值的为空");
33 | response = ContractResponse.builder().status("fail").mas("处理失败").build();
34 | }
35 | //处理合同表的流水等
36 | log.info("开始上传服务器");
37 | log.info(".............");
38 | log.info("上传服务器完成");
39 | //其他处理
40 | log.info("=============执行合同签章完成上传服务器拦截器结束");
41 | return response;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/impl/ContractSignSerialImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.impl;
2 |
3 |
4 | import com.xbhog.chainresponsibility.Enum.ContractSignEnum;
5 | import com.xbhog.chainresponsibility.annotations.ContractSign;
6 | import com.xbhog.chainresponsibility.inter.Chain;
7 | import com.xbhog.chainresponsibility.inter.Interceptor;
8 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
9 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
10 | import lombok.extern.slf4j.Slf4j;
11 | import org.springframework.stereotype.Component;
12 |
13 | import java.util.Objects;
14 |
15 | /**
16 | * @author xbhog
17 | * @describe: 合同签章表处理
18 | * @date 2023/7/15
19 | */
20 | @Slf4j
21 | @ContractSign(SIGN_CHANNEL = ContractSignEnum.SignChannel.SIGN_TABLE)
22 | @Component
23 | public class ContractSignSerialImpl implements Interceptor {
24 | @Override
25 | public ContractResponse process(Chain chain) {
26 | log.info("=============执行合同签章流水处理拦截器开始");
27 | //获取处理的请求参数
28 | T request = chain.request();
29 | //进入下一个责任链节点
30 | ContractResponse response = chain.proceed(request);
31 | if(Objects.isNull(response)){
32 | log.error("返回值的为空");
33 | response = ContractResponse.builder().status("fail").mas("处理失败").build();
34 | }
35 | //处理合同表的流水等
36 | //其他处理
37 | log.info("=============执行合同签章流水处理拦截器结束");
38 | return response;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/impl/ContractSignTradeImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.impl;
2 |
3 |
4 | import com.xbhog.chainresponsibility.Enum.ContractSignEnum;
5 | import com.xbhog.chainresponsibility.annotations.ContractSign;
6 | import com.xbhog.chainresponsibility.channel.ContractSignChannel;
7 | import com.xbhog.chainresponsibility.inter.Chain;
8 | import com.xbhog.chainresponsibility.inter.Interceptor;
9 | import com.xbhog.chainresponsibility.pojo.ContractRequest;
10 | import com.xbhog.chainresponsibility.pojo.ContractResponse;
11 | import lombok.extern.slf4j.Slf4j;
12 | import org.springframework.stereotype.Component;
13 |
14 | import javax.annotation.Resource;
15 | import java.util.Objects;
16 |
17 | /**
18 | * @author xbhog
19 | * @describe: 签章渠道实际调用
20 | * @date 2023/7/15
21 | */
22 | @Slf4j
23 | @ContractSign(SIGN_CHANNEL = ContractSignEnum.SignChannel.SIGN_TRADE)
24 | @Component
25 | public class ContractSignTradeImpl implements Interceptor {
26 | @Resource
27 | private ContractSignChannel channel;
28 |
29 | @Override
30 | public ContractResponse process(Chain chain) {
31 | log.info("=============执行签章渠道实际调用拦截器开始");
32 | //获取处理的请求参数
33 | T request = chain.request();
34 | if(request.getStatus().equals("1")){
35 | try {
36 | return channel.contractSign(request);
37 | } catch (Exception e) {
38 | throw new RuntimeException(e);
39 | }
40 | }
41 | //进入下一个责任链节点
42 | ContractResponse response = chain.proceed(request);
43 | if(Objects.isNull(response)){
44 | log.error("返回值的为空");
45 | response = ContractResponse.builder().status("fail").mas("处理失败").build();
46 | }
47 | //其他处理
48 | log.info("=============执行签章渠道实际调用拦截器结束");
49 | return response;
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/inter/Call.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.inter;
2 |
3 | /**
4 | * @author xbhog
5 | * @describe:
6 | * @date 2023/7/11
7 | */
8 | public interface Call {
9 | /**
10 | * 请求
11 | * @return T
12 | */
13 | T request();
14 |
15 | /**
16 | * 执行
17 | * @return R
18 | */
19 | R exectue();
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/inter/Chain.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.inter;
2 |
3 | /**
4 | * @author xbhog
5 | */
6 | public interface Chain{
7 | /**
8 | * 请求
9 | */
10 | T request();
11 |
12 | /**
13 | * 节点处理
14 | * @param request 数据请求
15 | * @return 处理结果
16 | */
17 | R proceed(T request);
18 | }
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/inter/Interceptor.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.inter;
2 |
3 | /**
4 | * @author xbhog
5 | * @describe: 合同拦截器
6 | * @date 2023/7/11
7 | */
8 | public interface Interceptor {
9 | /**
10 | * 合同拦截器的处理流程
11 | * @param chain 节点信息
12 | * @return 处理结果
13 | */
14 | R process(Chain chain);
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/inter/Processor.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.inter;
2 |
3 | /**
4 | * @author xbhog
5 | * @describe:
6 | * @date 2023/7/11
7 | */
8 | public interface Processor {
9 | /**
10 | * 合同流程
11 | * @param paramter 请求参数
12 | * @return 处理结果
13 | */
14 | R process(T paramter);
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/pojo/ContractRequest.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.pojo;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Builder;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author xbhog
10 | * @describe:
11 | * @date 2023/7/11
12 | */
13 | @Data
14 | @NoArgsConstructor
15 | @AllArgsConstructor
16 | @Builder
17 | public class ContractRequest {
18 |
19 | private String name;
20 |
21 | private String age;
22 |
23 | private String status;
24 | }
25 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/chainresponsibility/pojo/ContractResponse.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.chainresponsibility.pojo;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Builder;
5 | import lombok.Data;
6 | import lombok.NoArgsConstructor;
7 |
8 | /**
9 | * @author xbhog
10 | * @describe:
11 | * @date 2023/7/12
12 | */
13 | @Data
14 | @NoArgsConstructor
15 | @AllArgsConstructor
16 | @Builder
17 | public class ContractResponse {
18 | private String status;
19 |
20 | private String mas;
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/Coupon/CouponInfo.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.Coupon;
2 |
3 | public class CouponInfo {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/Coupon/CouponResult.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.Coupon;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | /**
8 | * @author xbhog
9 | */
10 | @Data
11 | @NoArgsConstructor
12 | @AllArgsConstructor
13 | public class CouponResult {
14 | // 编码
15 | private String code;
16 |
17 | // 描述
18 | private String info;
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/Coupon/CouponService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.Coupon;
2 |
3 | /**
4 | * 模拟优惠卷服务
5 | * @author xbhog
6 | */
7 | public class CouponService {
8 | public CouponResult sendCoupon(String uId,String conponNumber,String uuid){
9 | System.out.println("模拟发放优惠卷一张:"+uId+","+conponNumber+","+uuid);
10 | return new CouponResult("0000","发放成功");
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/ICommodity.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design;
2 |
3 | import java.util.Map;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe:定义发奖接口
8 | */
9 | public interface ICommodity {
10 | /**
11 | *
12 | * @param uId 用户ID
13 | * @param commodityId 奖品ID
14 | * @param bizId 业务ID
15 | * @param extMap 扩展字段
16 | * @throws Exception
17 | */
18 | void sendCommodity(String uId, String commodityId, String bizId, Map extMap) throws Exception;
19 | }
20 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/StoreFactory.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design;
2 |
3 | import com.xbhog.combatCode.工厂模式.design.impl.CardCommodityService;
4 | import com.xbhog.combatCode.工厂模式.design.impl.CouponCommodityService;
5 | import com.xbhog.combatCode.工厂模式.design.impl.GoodsCommodityService;
6 |
7 | public class StoreFactory {
8 | public ICommodity getCommodityService(Integer commodityType){
9 | if(null == commodityType) {
10 | return null;
11 | }
12 | if(1 == commodityType){
13 | return new CouponCommodityService();
14 | }
15 | if(2 == commodityType){
16 | return new GoodsCommodityService();
17 | }
18 | if(3 == commodityType) {
19 | return new CardCommodityService();
20 | }
21 | throw new RuntimeException("不存在的商品服务类型");
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/card/IQiYiCard.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.card;
2 |
3 | /**
4 | * @author xbhog
5 | * @Describe:
6 | */
7 | public class IQiYiCard {
8 | }
9 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/card/IQiYiCardService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.card;
2 |
3 | /**
4 | * @author xbhog
5 | */
6 | public class IQiYiCardService {
7 | /**
8 | * @param bindMobileNumber 手机号
9 | * @param cardId 会员卡号
10 | */
11 | public void granToken(String bindMobileNumber,String cardId){
12 | System.out.println("模拟发方爱奇艺会员卡一张:"+bindMobileNumber+","+cardId);
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/goods/DeliverReq.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.goods;
2 |
3 | import lombok.AllArgsConstructor;
4 | import lombok.Data;
5 | import lombok.NoArgsConstructor;
6 |
7 | /**
8 | * @author xbhog
9 | */
10 | @Data
11 | @NoArgsConstructor
12 | @AllArgsConstructor
13 | public class DeliverReq {
14 | // 用户姓名
15 | private String userName;
16 | // 用户手机
17 | private String userPhone;
18 | // 商品SKU
19 | private String sku;
20 | // 订单ID
21 | private String orderId;
22 | // 收货人姓名
23 | private String consigneeUserName;
24 | // 收货人手机
25 | private String consigneeUserPhone;
26 | // 收获人地址
27 | private String consigneeUserAddress;
28 | }
29 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/goods/GoodsInfo.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.goods;
2 |
3 | public class GoodsInfo {
4 | }
5 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/goods/GoodsService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.goods;
2 |
3 | import com.alibaba.fastjson.JSON;
4 |
5 | /**
6 | * @author xbhog
7 | * @Describe:模拟实物商品服务
8 | */
9 | public class GoodsService {
10 | public Boolean deliverGoods(DeliverReq req){
11 | System.out.println("模拟发货实物商品一个"+ JSON.toJSONString(req));
12 | return true;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/impl/CardCommodityService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.impl;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.xbhog.combatCode.工厂模式.design.ICommodity;
5 | import com.xbhog.combatCode.工厂模式.design.card.IQiYiCardService;
6 | import org.slf4j.Logger;
7 | import org.slf4j.LoggerFactory;
8 |
9 | import java.util.Map;
10 |
11 | /**
12 | * @author xbhog
13 | * 第三方兑换卡
14 | */
15 | public class CardCommodityService implements ICommodity {
16 | private final Logger logger = LoggerFactory.getLogger(CardCommodityService.class);
17 | //模拟注入
18 | private final IQiYiCardService iQiYiCardService = new IQiYiCardService();
19 |
20 | @Override
21 | public void sendCommodity(String uId, String commodityId, String bizId,
22 | Map extMap) throws Exception {
23 | String userMobile = queryUserMobile(uId);
24 | iQiYiCardService.granToken(userMobile,bizId);
25 | logger.info("请求参数[爱奇艺兑换卡] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
26 | logger.info("测试结果[爱奇艺兑换卡]:success");
27 | }
28 | //查询手机号
29 | private String queryUserMobile(String uId){
30 | return "123456789";
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/impl/CouponCommodityService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.impl;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.xbhog.combatCode.工厂模式.design.Coupon.CouponResult;
5 | import com.xbhog.combatCode.工厂模式.design.Coupon.CouponService;
6 | import com.xbhog.combatCode.工厂模式.design.ICommodity;
7 | import lombok.extern.slf4j.Slf4j;
8 |
9 | import java.util.Map;
10 |
11 | /**
12 | * @author xbhog
13 | * 优惠卷服务
14 | */
15 | @Slf4j
16 | public class CouponCommodityService implements ICommodity {
17 | private final CouponService couponService = new CouponService();
18 |
19 | @Override
20 | public void sendCommodity(String uId, String commodityId,
21 | String bizId, Map extMap) throws Exception {
22 | CouponResult couponResult = couponService.sendCoupon(uId, commodityId, bizId);
23 | log.info("请求参数[优惠券] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
24 | log.info("测试结果[优惠券]:{}", JSON.toJSON(couponResult));
25 | if(!("0000".equals(couponResult.getCode()))) {
26 | throw new RuntimeException(couponResult.getInfo());
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/工厂模式/design/impl/GoodsCommodityService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.工厂模式.design.impl;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.xbhog.combatCode.工厂模式.design.ICommodity;
5 | import com.xbhog.combatCode.工厂模式.design.goods.DeliverReq;
6 | import com.xbhog.combatCode.工厂模式.design.goods.GoodsService;
7 | import lombok.extern.slf4j.Slf4j;
8 |
9 | import java.util.Map;
10 |
11 | /**
12 | * @author xbhog
13 | * 实物商品服务
14 | */
15 | @Slf4j
16 | public class GoodsCommodityService implements ICommodity {
17 | private final GoodsService goodsService = new GoodsService();
18 |
19 | @Override
20 | public void sendCommodity(String uId, String commodityId, String bizId, Map extMap) throws Exception {
21 | DeliverReq deliverReq = new DeliverReq();
22 | deliverReq.setUserName(queryUserName(uId));
23 | deliverReq.setUserPhone(queryUserPhoneNumber(uId));
24 | deliverReq.setSku(commodityId);
25 | deliverReq.setOrderId(bizId);
26 | deliverReq.setConsigneeUserName(extMap.get("consigneeUserName"));
27 | deliverReq.setConsigneeUserPhone(extMap.get("consigneeUserPhone"));
28 | deliverReq.setConsigneeUserAddress(extMap.get("consigneeUserAddress"));
29 | Boolean isSuccess = goodsService.deliverGoods(deliverReq);
30 | log.info("请求参数[优惠券] => uId:{} commodityId:{} bizId:{} extMap:{}", uId, commodityId, bizId, JSON.toJSON(extMap));
31 | log.info("测试结果[优惠券]:{}", isSuccess);
32 |
33 | if (!isSuccess) {
34 | throw new RuntimeException("实物商品发放失败");
35 | }
36 | }
37 | private String queryUserName(String uId) {
38 | return "花花";
39 | }
40 |
41 | private String queryUserPhoneNumber(String uId) {
42 | return "15200101232";
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/CacheService.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | public interface CacheService {
6 |
7 | String get(final String key);
8 |
9 | void set(String key, String value);
10 |
11 | void set(String key, String value, long timeout, TimeUnit timeUnit);
12 |
13 | void del(String key);
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/cache/CacheFactory.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.cache;
2 |
3 | import com.xbhog.combatCode.抽象工厂模式.design.factory.ICacheAdapter;
4 | import com.xbhog.combatCode.抽象工厂模式.design.factory.impl.EGMCacheAdapter;
5 | import com.xbhog.combatCode.抽象工厂模式.design.factory.impl.IIRCacheAdapter;
6 | import com.xbhog.combatCode.抽象工厂模式.design.matter.RedisUtils;
7 |
8 | /**
9 | * @author xbhog
10 | * @describe:代理类2(采用的简单工厂模式)
11 | * @date 2022/9/3
12 | */
13 | public class CacheFactory {
14 | public ICacheAdapter getCacheMode(Integer modeyType){
15 | if(1 == modeyType){
16 | return new EGMCacheAdapter();
17 | }else if(2 == modeyType){
18 | return new IIRCacheAdapter();
19 | }
20 | return new RedisUtils();
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/factory/ICacheAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.factory;
2 |
3 | import java.util.concurrent.TimeUnit;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe: 定义接口适配器
8 | * 主要作用是让所有集群的提供方,能在统一的方法名称下进行操作。也方面后续的拓展
9 | * @date 2022/9/3
10 | */
11 | public interface ICacheAdapter {
12 | String get(String key);
13 |
14 | void set(String key, String value);
15 |
16 | void set(String key, String value, long timeout, TimeUnit timeUnit);
17 |
18 | void del(String key);
19 |
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/factory/JDKInvoicationHandler.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.factory;
2 |
3 | import com.xbhog.combatCode.抽象工厂模式.design.utils.ClassLoaderUtils;
4 |
5 | import java.lang.reflect.InvocationHandler;
6 | import java.lang.reflect.Method;
7 |
8 | /**
9 | * @author xbhog
10 | * @describe:
11 | * @date 2022/9/3
12 | */
13 | public class JDKInvoicationHandler implements InvocationHandler {
14 | private ICacheAdapter cacheAdapter;
15 |
16 | public JDKInvoicationHandler(ICacheAdapter cacheAdapter) {
17 | this.cacheAdapter = cacheAdapter;
18 | }
19 |
20 | @Override
21 | public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
22 | return ICacheAdapter.class.
23 | getMethod(method.getName(), ClassLoaderUtils.getClazzByArgs(args))
24 | .invoke(cacheAdapter,args);
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/factory/JDKProxy.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.factory;
2 |
3 | import java.lang.reflect.InvocationHandler;
4 | import java.lang.reflect.Proxy;
5 |
6 | /**
7 | * @author xbhog
8 | * @describe:完成代理类,同时对于使用哪个集群有外部通过入参进行传递
9 | * @date 2022/9/3
10 | */
11 | public class JDKProxy {
12 |
13 | public static T getProxy(Class interfaceClass,ICacheAdapter cacheAdapter) throws Exception{
14 | //将方法调用分派到的调用处理程序,这里指传递的EGMCacheAdapter/IIRCacheAdapter
15 | InvocationHandler handler = new JDKInvoicationHandler(cacheAdapter);
16 | //获得类加载器
17 | ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
18 | //获取对象的类或者接口
19 | Class>[] classes = interfaceClass.getInterfaces();
20 | Class> currentClass = classes[0];
21 | return (T) Proxy.newProxyInstance(classLoader,new Class[]{currentClass},handler);
22 | }
23 | }
24 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/factory/impl/EGMCacheAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.factory.impl;
2 |
3 | import com.xbhog.combatCode.抽象工厂模式.design.factory.ICacheAdapter;
4 | import com.xbhog.combatCode.抽象工厂模式.design.matter.EGM;
5 |
6 | import java.util.concurrent.TimeUnit;
7 |
8 | /**
9 | * @author xbhog
10 | * @describe:服务方法命名统一
11 | * @date 2022/9/3
12 | */
13 | public class EGMCacheAdapter implements ICacheAdapter {
14 | /**
15 | * 模拟注入
16 | */
17 | private EGM egm = new EGM();
18 |
19 | @Override
20 | public String get(String key) {
21 | return egm.gain(key);
22 | }
23 |
24 | @Override
25 | public void set(String key, String value) {
26 | egm.set(key,value);
27 | }
28 |
29 | @Override
30 | public void set(String key, String value, long timeout, TimeUnit timeUnit) {
31 | egm.setEx(key, value,timeout,timeUnit);
32 | }
33 |
34 | @Override
35 | public void del(String key) {
36 | egm.delete(key);
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/factory/impl/IIRCacheAdapter.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.factory.impl;
2 |
3 | import com.xbhog.combatCode.抽象工厂模式.design.factory.ICacheAdapter;
4 | import com.xbhog.combatCode.抽象工厂模式.design.matter.IIR;
5 | import java.util.concurrent.TimeUnit;
6 |
7 | /**
8 | * 服务方法命名统一
9 | */
10 | public class IIRCacheAdapter implements ICacheAdapter {
11 | /**
12 | * 模拟注入服务
13 | */
14 | private IIR iir = new IIR();
15 | @Override
16 | public String get(String key) {
17 | return iir.get(key);
18 | }
19 | @Override
20 | public void set(String key, String value) {
21 | iir.set(key, value);
22 | }
23 | @Override
24 | public void set(String key, String value, long timeout, TimeUnit timeUnit) {
25 | iir.setExpire(key, value, timeout, timeUnit);
26 | }
27 | @Override
28 | public void del(String key) {
29 | iir.del(key);
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/impl/CacheServiceImpl.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.impl;
2 |
3 | import com.xbhog.combatCode.抽象工厂模式.design.CacheService;
4 | import com.xbhog.combatCode.抽象工厂模式.design.matter.RedisUtils;
5 |
6 | import java.util.concurrent.TimeUnit;
7 |
8 | /**
9 | * @author xbhog
10 | * @describe:
11 | * @date 2022/9/3
12 | */
13 | public class CacheServiceImpl implements CacheService {
14 |
15 | private RedisUtils redisUtils = new RedisUtils();
16 |
17 | @Override
18 | public String get(String key) {
19 | return redisUtils.get(key);
20 | }
21 |
22 | @Override
23 | public void set(String key, String value) {
24 | redisUtils.set(key, value);
25 | }
26 |
27 | @Override
28 | public void set(String key, String value, long timeout, TimeUnit timeUnit) {
29 | redisUtils.set(key, value, timeout, timeUnit);
30 | }
31 |
32 | @Override
33 | public void del(String key) {
34 | redisUtils.del(key);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/matter/EGM.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.matter;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import java.util.Map;
7 | import java.util.concurrent.ConcurrentHashMap;
8 | import java.util.concurrent.TimeUnit;
9 |
10 |
11 | public class EGM {
12 |
13 | private Logger logger = LoggerFactory.getLogger(EGM.class);
14 |
15 | private Map dataMap = new ConcurrentHashMap();
16 |
17 | public String gain(String key) {
18 | logger.info("EGM获取数据 key:{}", key);
19 | return dataMap.get(key);
20 | }
21 |
22 | public void set(String key, String value) {
23 | logger.info("EGM写入数据 key:{} val:{}", key, value);
24 | dataMap.put(key, value);
25 | }
26 |
27 | public void setEx(String key, String value, long timeout, TimeUnit timeUnit) {
28 | logger.info("EGM写入数据 key:{} val:{} timeout:{} timeUnit:{}", key, value, timeout, timeUnit.toString());
29 | dataMap.put(key, value);
30 | }
31 |
32 | public void delete(String key) {
33 | logger.info("EGM删除数据 key:{}", key);
34 | dataMap.remove(key);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/matter/IIR.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.matter;
2 |
3 | import org.slf4j.Logger;
4 | import org.slf4j.LoggerFactory;
5 |
6 | import java.util.Map;
7 | import java.util.concurrent.ConcurrentHashMap;
8 | import java.util.concurrent.TimeUnit;
9 |
10 | public class IIR {
11 |
12 | private Logger logger = LoggerFactory.getLogger(IIR.class);
13 |
14 | private Map dataMap = new ConcurrentHashMap();
15 |
16 | public String get(String key) {
17 | logger.info("IIR获取数据 key:{}", key);
18 | return dataMap.get(key);
19 | }
20 |
21 | public void set(String key, String value) {
22 | logger.info("IIR写入数据 key:{} val:{}", key, value);
23 | dataMap.put(key, value);
24 | }
25 |
26 | public void setExpire(String key, String value, long timeout, TimeUnit timeUnit) {
27 | logger.info("IIR写入数据 key:{} val:{} timeout:{} timeUnit:{}", key, value, timeout, timeUnit.toString());
28 | dataMap.put(key, value);
29 | }
30 |
31 | public void del(String key) {
32 | logger.info("IIR删除数据 key:{}", key);
33 | dataMap.remove(key);
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/matter/RedisUtils.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.matter;
2 |
3 | import com.xbhog.combatCode.抽象工厂模式.design.factory.ICacheAdapter;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 |
7 | import java.util.Map;
8 | import java.util.concurrent.ConcurrentHashMap;
9 | import java.util.concurrent.TimeUnit;
10 |
11 | public class RedisUtils implements ICacheAdapter {
12 |
13 | private Logger logger = LoggerFactory.getLogger(RedisUtils.class);
14 |
15 | private Map dataMap = new ConcurrentHashMap();
16 | @Override
17 | public String get(String key) {
18 | logger.info("Redis获取数据 key:{}", key);
19 | return dataMap.get(key);
20 | }
21 | @Override
22 | public void set(String key, String value) {
23 | logger.info("Redis写入数据 key:{} val:{}", key, value);
24 | dataMap.put(key, value);
25 | }
26 | @Override
27 | public void set(String key, String value, long timeout, TimeUnit timeUnit) {
28 | logger.info("Redis写入数据 key:{} val:{} timeout:{} timeUnit:{}", key, value, timeout, timeUnit.toString());
29 | dataMap.put(key, value);
30 | }
31 | @Override
32 | public void del(String key) {
33 | logger.info("Redis删除数据 key:{}", key);
34 | dataMap.remove(key);
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/抽象工厂模式/design/utils/ClassLoaderUtils.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.抽象工厂模式.design.utils;
2 |
3 | import java.util.*;
4 | import java.util.concurrent.TimeUnit;
5 |
6 | /**
7 | * @author xbhog
8 | * @describe:
9 | * @date 2022/9/3
10 | */
11 | public class ClassLoaderUtils {
12 | public static Class>[] getClazzByArgs(Object[] args) {
13 | Class>[] parameterTypes = new Class[args.length];
14 | for (int i = 0; i < args.length; i++) {
15 | if (args[i] instanceof ArrayList) {
16 | parameterTypes[i] = List.class;
17 | continue;
18 | }
19 | if (args[i] instanceof LinkedList) {
20 | parameterTypes[i] = List.class;
21 | continue;
22 | }
23 | if (args[i] instanceof HashMap) {
24 | parameterTypes[i] = Map.class;
25 | continue;
26 | }
27 | if (args[i] instanceof Long){
28 | parameterTypes[i] = long.class;
29 | continue;
30 | }
31 | if (args[i] instanceof Double){
32 | parameterTypes[i] = double.class;
33 | continue;
34 | }
35 | if (args[i] instanceof TimeUnit){
36 | parameterTypes[i] = TimeUnit.class;
37 | continue;
38 | }
39 | parameterTypes[i] = args[i].getClass();
40 | }
41 | return parameterTypes;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/模板模式/design/HttpClient.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.模板模式.design;
2 |
3 | import java.io.BufferedReader;
4 | import java.io.IOException;
5 | import java.io.InputStream;
6 | import java.io.InputStreamReader;
7 | import java.net.HttpURLConnection;
8 | import java.net.MalformedURLException;
9 | import java.net.URL;
10 |
11 | public class HttpClient {
12 |
13 | public static String doGet(String httpurl) {
14 | HttpURLConnection connection = null;
15 | InputStream is = null;
16 | BufferedReader br = null;
17 | String result = null;// 返回结果字符串
18 | try {
19 | // 创建远程url连接对象
20 | URL url = new URL(httpurl);
21 | // 通过远程url连接对象打开一个连接,强转成httpURLConnection类
22 | connection = (HttpURLConnection) url.openConnection();
23 | // 设置连接方式:get
24 | connection.setRequestMethod("GET");
25 | // 设置连接主机服务器的超时时间:15000毫秒
26 | connection.setConnectTimeout(15000);
27 | // 设置读取远程返回的数据时间:60000毫秒
28 | connection.setReadTimeout(60000);
29 | // 发送请求
30 | connection.connect();
31 | // 通过connection连接,获取输入流
32 | if (connection.getResponseCode() == 200) {
33 | is = connection.getInputStream();
34 | // 封装输入流is,并指定字符集
35 | br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
36 | // 存放数据
37 | StringBuilder sbf = new StringBuilder();
38 | String temp = null;
39 | while ((temp = br.readLine()) != null) {
40 | sbf.append(temp);
41 | sbf.append("\r\n");
42 | }
43 | result = sbf.toString();
44 | }
45 | } catch (MalformedURLException e) {
46 | e.printStackTrace();
47 | } catch (IOException e) {
48 | e.printStackTrace();
49 | } finally {
50 | // 关闭资源
51 | if (null != br) {
52 | try {
53 | br.close();
54 | } catch (IOException e) {
55 | e.printStackTrace();
56 | }
57 | }
58 |
59 | if (null != is) {
60 | try {
61 | is.close();
62 | } catch (IOException e) {
63 | e.printStackTrace();
64 | }
65 | }
66 |
67 | assert connection != null;
68 | connection.disconnect();// 关闭远程连接
69 | }
70 |
71 | return result;
72 | }
73 |
74 | }
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/模板模式/design/NetMall.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.模板模式.design;
2 |
3 | import lombok.extern.slf4j.Slf4j;
4 | import org.slf4j.Logger;
5 | import org.slf4j.LoggerFactory;
6 | import java.util.Map;
7 |
8 | /**
9 | * @author xbhog
10 | * @describe:基础电商推广服务
11 | * 1. 生成最优价商品海报
12 | * 2. 海报含带推广邀请码
13 | * @date 2022/8/20
14 | */
15 | @Slf4j
16 | public abstract class NetMall {
17 |
18 | protected Logger logger = LoggerFactory.getLogger(NetMall.class);
19 |
20 |
21 | String uId;
22 | String upwd;
23 |
24 | public NetMall(String uId, String upwd) {
25 | this.uId = uId;
26 | this.upwd = upwd;
27 | }
28 |
29 | /**
30 | * 生成商品推广海报
31 | * @param skuUrl 商品地址(京东、淘宝、当当)
32 | * @return 海报图片base64位信息
33 | */
34 | public String generateGoodsPoster(String skuUrl){
35 | if(!login(uId,upwd)) return null;
36 | Map reptile = reptile(skuUrl);
37 | return createBase64(reptile);
38 | }
39 |
40 | /**
41 | * 模拟用户登录
42 | * @param uId
43 | * @param upwd
44 | * @return
45 | */
46 | protected abstract boolean login(String uId, String upwd);
47 | /**
48 | * 爬虫提取商品信息(登录后的价格优惠)
49 | * @param skuUrl
50 | * @return
51 | */
52 | protected abstract Map reptile(String skuUrl);
53 |
54 |
55 | /**
56 | * 生成商品海报信息
57 | * @param reptile
58 | * @return
59 | */
60 | protected abstract String createBase64(Map reptile);
61 |
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/模板模式/design/group/DangDangNetMall.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.模板模式.design.group;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.xbhog.combatCode.模板模式.design.HttpClient;
5 | import com.xbhog.combatCode.模板模式.design.NetMall;
6 |
7 | import java.util.Base64;
8 | import java.util.Map;
9 | import java.util.concurrent.ConcurrentHashMap;
10 | import java.util.regex.Matcher;
11 | import java.util.regex.Pattern;
12 |
13 | public class DangDangNetMall extends NetMall {
14 |
15 | public DangDangNetMall(String uId, String uPwd) {
16 | super(uId, uPwd);
17 | }
18 |
19 | @Override
20 | public boolean login(String uId, String uPwd) {
21 | logger.info("模拟当当用户登录 uId:{} uPwd:{}", uId, uPwd);
22 | return true;
23 | }
24 |
25 | @Override
26 | public Map reptile(String skuUrl) {
27 | String str = HttpClient.doGet(skuUrl);
28 | Pattern p9 = Pattern.compile("(?<=title\\>).*(?= map = new ConcurrentHashMap();
31 | if (m9.find()) {
32 | map.put("name", m9.group());
33 | }
34 | map.put("price", "4548.00");
35 | logger.info("模拟当当商品爬虫解析:{} | {} 元 {}", map.get("name"), map.get("price"), skuUrl);
36 | return map;
37 | }
38 |
39 | @Override
40 | public String createBase64(Map goodsInfo) {
41 | Base64.Encoder encoder = Base64.getEncoder ();
42 | logger.info("模拟生成当当商品base64海报");
43 | return encoder.encodeToString(JSON.toJSONString(goodsInfo).getBytes());
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/模板模式/design/group/JDNetMall.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.模板模式.design.group;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.xbhog.combatCode.模板模式.design.HttpClient;
5 | import com.xbhog.combatCode.模板模式.design.NetMall;
6 |
7 | import java.util.Base64;
8 | import java.util.Map;
9 | import java.util.concurrent.ConcurrentHashMap;
10 | import java.util.regex.Matcher;
11 | import java.util.regex.Pattern;
12 |
13 |
14 |
15 | public class JDNetMall extends NetMall {
16 |
17 | public JDNetMall(String uId, String uPwd) {
18 | super(uId, uPwd);
19 | }
20 | @Override
21 | public boolean login(String uId, String uPwd) {
22 | logger.info("模拟京东用户登录 uId:{} uPwd:{}", uId, uPwd);
23 | return true;
24 | }
25 | @Override
26 | public Map reptile(String skuUrl) {
27 | String str = HttpClient.doGet(skuUrl);
28 | Pattern p9 = Pattern.compile("(?<=title\\>).*(?= map = new ConcurrentHashMap();
31 | if (m9.find()) {
32 | map.put("name", m9.group());
33 | }
34 | map.put("price", "5999.00");
35 | logger.info("模拟京东商品爬虫解析:{} | {} 元 {}", map.get("name"), map.get("price"), skuUrl);
36 | return map;
37 | }
38 | @Override
39 | public String createBase64(Map goodsInfo) {
40 | Base64.Encoder encoder = Base64.getEncoder ();
41 | logger.info("模拟生成京东商品base64海报");
42 | return encoder.encodeToString(JSON.toJSONString(goodsInfo).getBytes());
43 | }
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/模板模式/design/group/TaoBaoNetMall.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.模板模式.design.group;
2 |
3 | import com.alibaba.fastjson.JSON;
4 | import com.xbhog.combatCode.模板模式.design.HttpClient;
5 | import com.xbhog.combatCode.模板模式.design.NetMall;
6 |
7 |
8 | import java.util.Base64;
9 | import java.util.Map;
10 | import java.util.concurrent.ConcurrentHashMap;
11 | import java.util.regex.Matcher;
12 | import java.util.regex.Pattern;
13 |
14 | /**
15 | * @author xbhog
16 | * @describe:
17 | * @date 2022/8/21
18 | */
19 | public class TaoBaoNetMall extends NetMall {
20 |
21 |
22 | public TaoBaoNetMall(String uId, String upwd) {
23 | super(uId, upwd);
24 | }
25 |
26 | @Override
27 | protected boolean login(String uId, String upwd) {
28 | logger.info("登录成功,{},{}",uId,upwd);
29 | return true;
30 | }
31 |
32 | @Override
33 | protected Map reptile(String skuUrl) {
34 | String str = HttpClient.doGet(skuUrl);
35 | Pattern p9 = Pattern.compile("(?<=title\\>).*(?= map = new ConcurrentHashMap();
38 | if (m9.find()) {
39 | map.put("name", m9.group());
40 | }
41 | map.put("price", "4799.00");
42 | logger.info("模拟淘宝商品爬虫解析:{} | {} 元 {}", map.get("name"), map.get("price"), skuUrl);
43 | return map;
44 | }
45 |
46 | @Override
47 | protected String createBase64(Map goodsInfo) {
48 | Base64.Encoder encoder = Base64.getEncoder ();
49 | logger.info("模拟生成淘宝商品base64海报");
50 | return encoder.encodeToString(JSON.toJSONString(goodsInfo).getBytes());
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/策略模式/design/Context.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.策略模式.design;
2 |
3 | import java.math.BigDecimal;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe:策略控制器
8 | * @date 2022/8/21
9 | */
10 | public class Context {
11 |
12 | private ICouponDiscount couponDiscount;
13 |
14 | public Context(ICouponDiscount couponDiscount) {
15 | this.couponDiscount = couponDiscount;
16 | }
17 | public BigDecimal discountAmount(T couponInfo,BigDecimal skuPrice){
18 | return couponDiscount.discountAmount(couponInfo,skuPrice);
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/策略模式/design/ICouponDiscount.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.策略模式.design;
2 |
3 | import java.math.BigDecimal;
4 |
5 | /**
6 | * @author xbhog
7 | * @describe:优惠卷折扣计算接口
8 | * 1. 直减卷
9 | * 2. 满减卷
10 | * 3. 折扣卷
11 | * 4. N元购
12 | * @date 2022/8/21
13 | */
14 | public interface ICouponDiscount {
15 | /**
16 | *
17 | * @param couponInfo 券折扣信息;直减、满减、折扣、N元购
18 | * @param skuPrice 商品价格
19 | * @return 优惠后金额
20 | */
21 | BigDecimal discountAmount(T couponInfo, BigDecimal skuPrice);
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/xbhog/combatCode/策略模式/design/impl/MJCouponDiscount.java:
--------------------------------------------------------------------------------
1 | package com.xbhog.combatCode.策略模式.design.impl;
2 |
3 | import com.xbhog.combatCode.策略模式.design.ICouponDiscount;
4 |
5 | import java.math.BigDecimal;
6 | import java.util.Map;
7 |
8 | /**
9 | * @author xbhog
10 | * @describe:满减
11 | * @date 2022/8/21
12 | */
13 | public class MJCouponDiscount implements ICouponDiscount