简单工厂设计模式
前言
在日常开发中,当我们需要根据不同参数创建不同对象时,代码很容易变成一堆 if-else 的“面条代码”。
**简单工厂模式(Static Factory Method)**正是为了解决这个问题而生:它把所有对象的创建逻辑集中到一个“大工厂”里,让调用方只需要传入一个类型参数,就能拿到统一的接口对象,无需关心具体是哪一个实现类。
本文从简单工厂模式的核心定义与UML结构出发,以两个场景对比“面条代码”与“简单工厂”两种写法:
- LLM模型网关路由;
- 营销发奖系统(优惠券、实物商品、爱奇艺卡三种不同发放逻辑)。
一、 核心定义
简单工厂模式又叫做静态工厂方法模式(Static Factory Method)。
它的核心思想是:由一个专门的“大工厂”类,根据传入的参数,集中包含所有具体对象的创建逻辑,并返回它们的通用接口。
打个比方:简单工厂就像是一个全能的“自动售货机”,你按 1,它给你掉下来可乐;你按 2,它给你掉下来雪碧。售货机(工厂)内部自己搞定了所有的判断逻辑。
二、 标准体系结构图 (UML)
角色说明:
#mermaid-svg-lq1zH69lpA4Yn3IP{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-lq1zH69lpA4Yn3IP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-lq1zH69lpA4Yn3IP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-lq1zH69lpA4Yn3IP .error-icon{fill:#552222;}#mermaid-svg-lq1zH69lpA4Yn3IP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-lq1zH69lpA4Yn3IP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-lq1zH69lpA4Yn3IP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-lq1zH69lpA4Yn3IP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-lq1zH69lpA4Yn3IP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-lq1zH69lpA4Yn3IP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-lq1zH69lpA4Yn3IP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-lq1zH69lpA4Yn3IP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-lq1zH69lpA4Yn3IP .marker.cross{stroke:#333333;}#mermaid-svg-lq1zH69lpA4Yn3IP svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-lq1zH69lpA4Yn3IP p{margin:0;}#mermaid-svg-lq1zH69lpA4Yn3IP g.classGroup text{fill:#9370DB;stroke:none;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-lq1zH69lpA4Yn3IP g.classGroup text .title{font-weight:bolder;}#mermaid-svg-lq1zH69lpA4Yn3IP .cluster-label text{fill:#333;}#mermaid-svg-lq1zH69lpA4Yn3IP .cluster-label span{color:#333;}#mermaid-svg-lq1zH69lpA4Yn3IP .cluster-label span p{background-color:transparent;}#mermaid-svg-lq1zH69lpA4Yn3IP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-lq1zH69lpA4Yn3IP .cluster text{fill:#333;}#mermaid-svg-lq1zH69lpA4Yn3IP .cluster span{color:#333;}#mermaid-svg-lq1zH69lpA4Yn3IP .nodeLabel,#mermaid-svg-lq1zH69lpA4Yn3IP .edgeLabel{color:#131300;}#mermaid-svg-lq1zH69lpA4Yn3IP .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-lq1zH69lpA4Yn3IP .label text{fill:#131300;}#mermaid-svg-lq1zH69lpA4Yn3IP .labelBkg{background:#ECECFF;}#mermaid-svg-lq1zH69lpA4Yn3IP .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-lq1zH69lpA4Yn3IP .classTitle{font-weight:bolder;}#mermaid-svg-lq1zH69lpA4Yn3IP .node rect,#mermaid-svg-lq1zH69lpA4Yn3IP .node circle,#mermaid-svg-lq1zH69lpA4Yn3IP .node ellipse,#mermaid-svg-lq1zH69lpA4Yn3IP .node polygon,#mermaid-svg-lq1zH69lpA4Yn3IP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-lq1zH69lpA4Yn3IP .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP g.clickable{cursor:pointer;}#mermaid-svg-lq1zH69lpA4Yn3IP g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-lq1zH69lpA4Yn3IP g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-lq1zH69lpA4Yn3IP .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-lq1zH69lpA4Yn3IP .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-lq1zH69lpA4Yn3IP .dashed-line{stroke-dasharray:3;}#mermaid-svg-lq1zH69lpA4Yn3IP .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-lq1zH69lpA4Yn3IP #compositionStart,#mermaid-svg-lq1zH69lpA4Yn3IP .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #compositionEnd,#mermaid-svg-lq1zH69lpA4Yn3IP .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #dependencyStart,#mermaid-svg-lq1zH69lpA4Yn3IP .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #dependencyStart,#mermaid-svg-lq1zH69lpA4Yn3IP .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #extensionStart,#mermaid-svg-lq1zH69lpA4Yn3IP .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #extensionEnd,#mermaid-svg-lq1zH69lpA4Yn3IP .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #aggregationStart,#mermaid-svg-lq1zH69lpA4Yn3IP .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #aggregationEnd,#mermaid-svg-lq1zH69lpA4Yn3IP .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #lollipopStart,#mermaid-svg-lq1zH69lpA4Yn3IP .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP #lollipopEnd,#mermaid-svg-lq1zH69lpA4Yn3IP .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-lq1zH69lpA4Yn3IP .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-lq1zH69lpA4Yn3IP .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-lq1zH69lpA4Yn3IP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-lq1zH69lpA4Yn3IP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-lq1zH69lpA4Yn3IP :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}#mermaid-svg-lq1zH69lpA4Yn3IP .Product>*{fill:#E8F0FE!important;stroke:#1A73E8!important;stroke-width:1px!important;}#mermaid-svg-lq1zH69lpA4Yn3IP .Product span{fill:#E8F0FE!important;stroke:#1A73E8!important;stroke-width:1px!important;}#mermaid-svg-lq1zH69lpA4Yn3IP .ConcreteProductA>*{fill:#E6F4EA!important;stroke:#1E8E3E!important;stroke-width:1px!important;}#mermaid-svg-lq1zH69lpA4Yn3IP .ConcreteProductA span{fill:#E6F4EA!important;stroke:#1E8E3E!important;stroke-width:1px!important;}#mermaid-svg-lq1zH69lpA4Yn3IP .ConcreteProductB>*{fill:#E6F4EA!important;stroke:#1E8E3E!important;stroke-width:1px!important;}#mermaid-svg-lq1zH69lpA4Yn3IP .ConcreteProductB span{fill:#E6F4EA!important;stroke:#1E8E3E!important;stroke-width:1px!important;}#mermaid-svg-lq1zH69lpA4Yn3IP .SimpleFactory>*{fill:#FCE8E6!important;stroke:#D93025!important;stroke-width:1px!important;}#mermaid-svg-lq1zH69lpA4Yn3IP .SimpleFactory span{fill:#FCE8E6!important;stroke:#D93025!important;stroke-width:1px!important;}
实现
实现
依赖抽象
实例化 (内部通过 if/switch)
实例化 (内部通过 if/switch)
«Interface»
Product
+execute() : void
ConcreteProductA
+execute() : void
ConcreteProductB
+execute() : void
SimpleFactory
+createProduct(type: String) : Product
三、 场景推演:LLM 模型网关路由
在开发大模型 Agent 或自进化框架时,底层通常需要接入不同的 LLM 提供商(如 OpenAI, Anthropic, DeepSeek)。
如果使用简单工厂模式来构建这个网关,代码会是这样的:
1. 定义抽象接口
public interface ILLMClient {
String generateText(String prompt);
}
2. 实现具体产品
public class OpenAIClient implements ILLMClient {
@Override
public String generateText(String prompt) {
return "Calling GPT-4 API: " + prompt;
}
}
public class DeepSeekClient implements ILLMClient {
@Override
public String generateText(String prompt) {
return "Calling DeepSeek-Coder API: " + prompt;
}
}
3. 构建简单工厂(核心所在)
public class LLMFactory {
// 通常设计为静态方法,或者像之前的 StoreFactory 一样提供一个实例方法
public static ILLMClient createClient(String providerType) {
// 核心特征:所有的实例化逻辑都集中在这个 if-else / switch 结构中
if ("openai".equalsIgnoreCase(providerType)) {
return new OpenAIClient();
} else if ("deepseek".equalsIgnoreCase(providerType)) {
return new DeepSeekClient();
} else {
throw new IllegalArgumentException("Unsupported LLM provider: " + providerType);
}
}
}
4. 客户端调用
public class AgentSystem {
public void run() {
// 调用方只需要知道名字,不需要自己去 new 对象
ILLMClient client = LLMFactory.createClient("deepseek");
String response = client.generateText("Analyze this SWE-bench issue…");
}
}
四、 核心优缺点
理解设计模式的关键在于理解它的妥协。
4.1 核心优势
4.2 缺陷
简单工厂最大的问题在于它违背了“开闭原则(OCP)”。
- 假设你的框架现在需要接入一个新的模型 ClaudeClient,你除了要写一个新的类之外,还必须回头去修改 LLMFactory 里面的 if-else 代码。
- 如果系统非常庞大,有 50 种具体产品,这个工厂类的代码将会变成一个巨大的 switch 炼狱,每次修改都有可能不小心改错其他分支,导致线上故障。
五、实战演练:模拟发奖多种商品

- 图片来自:重学 Java 设计模式:实战工厂方法模式「多种类型商品不同接口,统一发奖服务搭建场景」 | 小傅哥 bugstack 虫洞栈
实战演练的代码参考小傅哥博客
本文代码仓库:https://github.com/likerhood/CodeDesignWork#
5.1 需求分析
在营销、抽奖等业务场景中,系统往往需要向用户发放多种类型的奖品(如:优惠券、实物商品、第三方兑换卡等)。
- 业务痛点:每种奖品的发放逻辑、所需的参数、以及底层调用的外部接口(可能是不同部门或不同公司提供的 RPC/HTTP 接口)完全不一致。
- 优惠券:需要 uId (用户ID), awardNumber (券码), bizId (防重流水号)。
- 实物商品:需要极度详细的收货信息(姓名、电话、SKU、防重ID、收货地址等)。
- 爱奇艺兑换卡:仅需要用户的 手机号 和 兑换码。
- 架构目标:对上层业务(如抽奖接口、前端调用)屏蔽底层的发奖差异,提供一个统一的、高内聚低耦合的发奖网关。
如图:描述了系统参与者(外部系统/用户)与发奖系统之间的功能边界。
#mermaid-svg-jykpyMPFkoyWBxb9{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-jykpyMPFkoyWBxb9 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-jykpyMPFkoyWBxb9 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-jykpyMPFkoyWBxb9 .error-icon{fill:#552222;}#mermaid-svg-jykpyMPFkoyWBxb9 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-jykpyMPFkoyWBxb9 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-jykpyMPFkoyWBxb9 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-jykpyMPFkoyWBxb9 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-jykpyMPFkoyWBxb9 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-jykpyMPFkoyWBxb9 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-jykpyMPFkoyWBxb9 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-jykpyMPFkoyWBxb9 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-jykpyMPFkoyWBxb9 .marker.cross{stroke:#333333;}#mermaid-svg-jykpyMPFkoyWBxb9 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-jykpyMPFkoyWBxb9 p{margin:0;}#mermaid-svg-jykpyMPFkoyWBxb9 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-jykpyMPFkoyWBxb9 .cluster-label text{fill:#333;}#mermaid-svg-jykpyMPFkoyWBxb9 .cluster-label span{color:#333;}#mermaid-svg-jykpyMPFkoyWBxb9 .cluster-label span p{background-color:transparent;}#mermaid-svg-jykpyMPFkoyWBxb9 .label text,#mermaid-svg-jykpyMPFkoyWBxb9 span{fill:#333;color:#333;}#mermaid-svg-jykpyMPFkoyWBxb9 .node rect,#mermaid-svg-jykpyMPFkoyWBxb9 .node circle,#mermaid-svg-jykpyMPFkoyWBxb9 .node ellipse,#mermaid-svg-jykpyMPFkoyWBxb9 .node polygon,#mermaid-svg-jykpyMPFkoyWBxb9 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-jykpyMPFkoyWBxb9 .rough-node .label text,#mermaid-svg-jykpyMPFkoyWBxb9 .node .label text,#mermaid-svg-jykpyMPFkoyWBxb9 .image-shape .label,#mermaid-svg-jykpyMPFkoyWBxb9 .icon-shape .label{text-anchor:middle;}#mermaid-svg-jykpyMPFkoyWBxb9 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-jykpyMPFkoyWBxb9 .rough-node .label,#mermaid-svg-jykpyMPFkoyWBxb9 .node .label,#mermaid-svg-jykpyMPFkoyWBxb9 .image-shape .label,#mermaid-svg-jykpyMPFkoyWBxb9 .icon-shape .label{text-align:center;}#mermaid-svg-jykpyMPFkoyWBxb9 .node.clickable{cursor:pointer;}#mermaid-svg-jykpyMPFkoyWBxb9 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-jykpyMPFkoyWBxb9 .arrowheadPath{fill:#333333;}#mermaid-svg-jykpyMPFkoyWBxb9 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-jykpyMPFkoyWBxb9 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-jykpyMPFkoyWBxb9 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jykpyMPFkoyWBxb9 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-jykpyMPFkoyWBxb9 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jykpyMPFkoyWBxb9 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-jykpyMPFkoyWBxb9 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-jykpyMPFkoyWBxb9 .cluster text{fill:#333;}#mermaid-svg-jykpyMPFkoyWBxb9 .cluster span{color:#333;}#mermaid-svg-jykpyMPFkoyWBxb9 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-jykpyMPFkoyWBxb9 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-jykpyMPFkoyWBxb9 rect.text{fill:none;stroke-width:0;}#mermaid-svg-jykpyMPFkoyWBxb9 .icon-shape,#mermaid-svg-jykpyMPFkoyWBxb9 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-jykpyMPFkoyWBxb9 .icon-shape p,#mermaid-svg-jykpyMPFkoyWBxb9 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-jykpyMPFkoyWBxb9 .icon-shape .label rect,#mermaid-svg-jykpyMPFkoyWBxb9 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-jykpyMPFkoyWBxb9 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-jykpyMPFkoyWBxb9 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-jykpyMPFkoyWBxb9 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}#mermaid-svg-jykpyMPFkoyWBxb9 .actor>*{fill:#e1f5fe!important;stroke:#01579b!important;stroke-width:2px!important;}#mermaid-svg-jykpyMPFkoyWBxb9 .actor span{fill:#e1f5fe!important;stroke:#01579b!important;stroke-width:2px!important;}#mermaid-svg-jykpyMPFkoyWBxb9 .usecase>*{fill:#fff3e0!important;stroke:#e65100!important;stroke-width:2px!important;}#mermaid-svg-jykpyMPFkoyWBxb9 .usecase span{fill:#fff3e0!important;stroke:#e65100!important;stroke-width:2px!important;}
外部依赖服务
发奖业务系统核心用例
业务客户都安
触发发奖请求
触发发奖请求
触发发奖请求
<> 调用发券接口
<> 调用发货接口
<> 调用发卡接口
触发方
发放优惠券奖品
发放实物商品
发放爱奇艺兑换卡
优惠券服务
物流商品服务
爱奇艺卡服务
5.2 发放奖品的请求体和响应体字段设计
#mermaid-svg-e1KRA4iT0KbvHkwW{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-e1KRA4iT0KbvHkwW .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-e1KRA4iT0KbvHkwW .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-e1KRA4iT0KbvHkwW .error-icon{fill:#552222;}#mermaid-svg-e1KRA4iT0KbvHkwW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-e1KRA4iT0KbvHkwW .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-e1KRA4iT0KbvHkwW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-e1KRA4iT0KbvHkwW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-e1KRA4iT0KbvHkwW .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-e1KRA4iT0KbvHkwW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-e1KRA4iT0KbvHkwW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-e1KRA4iT0KbvHkwW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-e1KRA4iT0KbvHkwW .marker.cross{stroke:#333333;}#mermaid-svg-e1KRA4iT0KbvHkwW svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-e1KRA4iT0KbvHkwW p{margin:0;}#mermaid-svg-e1KRA4iT0KbvHkwW g.classGroup text{fill:#9370DB;stroke:none;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-e1KRA4iT0KbvHkwW g.classGroup text .title{font-weight:bolder;}#mermaid-svg-e1KRA4iT0KbvHkwW .cluster-label text{fill:#333;}#mermaid-svg-e1KRA4iT0KbvHkwW .cluster-label span{color:#333;}#mermaid-svg-e1KRA4iT0KbvHkwW .cluster-label span p{background-color:transparent;}#mermaid-svg-e1KRA4iT0KbvHkwW .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-e1KRA4iT0KbvHkwW .cluster text{fill:#333;}#mermaid-svg-e1KRA4iT0KbvHkwW .cluster span{color:#333;}#mermaid-svg-e1KRA4iT0KbvHkwW .nodeLabel,#mermaid-svg-e1KRA4iT0KbvHkwW .edgeLabel{color:#131300;}#mermaid-svg-e1KRA4iT0KbvHkwW .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-e1KRA4iT0KbvHkwW .label text{fill:#131300;}#mermaid-svg-e1KRA4iT0KbvHkwW .labelBkg{background:#ECECFF;}#mermaid-svg-e1KRA4iT0KbvHkwW .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-e1KRA4iT0KbvHkwW .classTitle{font-weight:bolder;}#mermaid-svg-e1KRA4iT0KbvHkwW .node rect,#mermaid-svg-e1KRA4iT0KbvHkwW .node circle,#mermaid-svg-e1KRA4iT0KbvHkwW .node ellipse,#mermaid-svg-e1KRA4iT0KbvHkwW .node polygon,#mermaid-svg-e1KRA4iT0KbvHkwW .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-e1KRA4iT0KbvHkwW .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW g.clickable{cursor:pointer;}#mermaid-svg-e1KRA4iT0KbvHkwW g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-e1KRA4iT0KbvHkwW g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-e1KRA4iT0KbvHkwW .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-e1KRA4iT0KbvHkwW .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-e1KRA4iT0KbvHkwW .dashed-line{stroke-dasharray:3;}#mermaid-svg-e1KRA4iT0KbvHkwW .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-e1KRA4iT0KbvHkwW #compositionStart,#mermaid-svg-e1KRA4iT0KbvHkwW .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #compositionEnd,#mermaid-svg-e1KRA4iT0KbvHkwW .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #dependencyStart,#mermaid-svg-e1KRA4iT0KbvHkwW .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #dependencyStart,#mermaid-svg-e1KRA4iT0KbvHkwW .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #extensionStart,#mermaid-svg-e1KRA4iT0KbvHkwW .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #extensionEnd,#mermaid-svg-e1KRA4iT0KbvHkwW .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #aggregationStart,#mermaid-svg-e1KRA4iT0KbvHkwW .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #aggregationEnd,#mermaid-svg-e1KRA4iT0KbvHkwW .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #lollipopStart,#mermaid-svg-e1KRA4iT0KbvHkwW .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW #lollipopEnd,#mermaid-svg-e1KRA4iT0KbvHkwW .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-e1KRA4iT0KbvHkwW .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-e1KRA4iT0KbvHkwW .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-e1KRA4iT0KbvHkwW .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-e1KRA4iT0KbvHkwW .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-e1KRA4iT0KbvHkwW :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
AwardReq
-String uId %% 用户ID
-Integer awardType %% 1优惠券 2实物商品 3爱奇艺卡
-String bizId %% 幂等业务ID防重复
-String awardNumber %% 奖品编号(券号/SKU/卡ID)
-Map<String,String> extMap %% 扩展字段(收货地址等)
+getuId() : String
+getAwardType() : Integer
+getAwardNumber() : String
+getBizId() : String
+getExtMap() : Map
AwardRes
-String code %% 0000成功 0001失败
-String info %% 描述信息
+AwardRes(code, info)
+getCode() : String
+getInfo() : String
extMap 的 key 约定(发实物商品时必传):
| consigneeUserName | 收货人姓名 |
| consigneeUserPhone | 收货人手机 |
| consigneeUserAddress | 收货地址 |
5.3 简单体系结构图对比
5.3.1 面条代码

5.3.2 简单工厂

5.4 类图对比
5.4.1 面条代码类图
对应仓库中项目模块:codedesign1.0-0
#mermaid-svg-GkFOc7v4QMB1rkzI{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-GkFOc7v4QMB1rkzI .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-GkFOc7v4QMB1rkzI .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-GkFOc7v4QMB1rkzI .error-icon{fill:#552222;}#mermaid-svg-GkFOc7v4QMB1rkzI .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-GkFOc7v4QMB1rkzI .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-GkFOc7v4QMB1rkzI .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-GkFOc7v4QMB1rkzI .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-GkFOc7v4QMB1rkzI .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-GkFOc7v4QMB1rkzI .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-GkFOc7v4QMB1rkzI .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-GkFOc7v4QMB1rkzI .marker{fill:#333333;stroke:#333333;}#mermaid-svg-GkFOc7v4QMB1rkzI .marker.cross{stroke:#333333;}#mermaid-svg-GkFOc7v4QMB1rkzI svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-GkFOc7v4QMB1rkzI p{margin:0;}#mermaid-svg-GkFOc7v4QMB1rkzI g.classGroup text{fill:#9370DB;stroke:none;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-GkFOc7v4QMB1rkzI g.classGroup text .title{font-weight:bolder;}#mermaid-svg-GkFOc7v4QMB1rkzI .cluster-label text{fill:#333;}#mermaid-svg-GkFOc7v4QMB1rkzI .cluster-label span{color:#333;}#mermaid-svg-GkFOc7v4QMB1rkzI .cluster-label span p{background-color:transparent;}#mermaid-svg-GkFOc7v4QMB1rkzI .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-GkFOc7v4QMB1rkzI .cluster text{fill:#333;}#mermaid-svg-GkFOc7v4QMB1rkzI .cluster span{color:#333;}#mermaid-svg-GkFOc7v4QMB1rkzI .nodeLabel,#mermaid-svg-GkFOc7v4QMB1rkzI .edgeLabel{color:#131300;}#mermaid-svg-GkFOc7v4QMB1rkzI .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-GkFOc7v4QMB1rkzI .label text{fill:#131300;}#mermaid-svg-GkFOc7v4QMB1rkzI .labelBkg{background:#ECECFF;}#mermaid-svg-GkFOc7v4QMB1rkzI .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-GkFOc7v4QMB1rkzI .classTitle{font-weight:bolder;}#mermaid-svg-GkFOc7v4QMB1rkzI .node rect,#mermaid-svg-GkFOc7v4QMB1rkzI .node circle,#mermaid-svg-GkFOc7v4QMB1rkzI .node ellipse,#mermaid-svg-GkFOc7v4QMB1rkzI .node polygon,#mermaid-svg-GkFOc7v4QMB1rkzI .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-GkFOc7v4QMB1rkzI .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI g.clickable{cursor:pointer;}#mermaid-svg-GkFOc7v4QMB1rkzI g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-GkFOc7v4QMB1rkzI g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-GkFOc7v4QMB1rkzI .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-GkFOc7v4QMB1rkzI .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-GkFOc7v4QMB1rkzI .dashed-line{stroke-dasharray:3;}#mermaid-svg-GkFOc7v4QMB1rkzI .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-GkFOc7v4QMB1rkzI #compositionStart,#mermaid-svg-GkFOc7v4QMB1rkzI .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #compositionEnd,#mermaid-svg-GkFOc7v4QMB1rkzI .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #dependencyStart,#mermaid-svg-GkFOc7v4QMB1rkzI .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #dependencyStart,#mermaid-svg-GkFOc7v4QMB1rkzI .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #extensionStart,#mermaid-svg-GkFOc7v4QMB1rkzI .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #extensionEnd,#mermaid-svg-GkFOc7v4QMB1rkzI .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #aggregationStart,#mermaid-svg-GkFOc7v4QMB1rkzI .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #aggregationEnd,#mermaid-svg-GkFOc7v4QMB1rkzI .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #lollipopStart,#mermaid-svg-GkFOc7v4QMB1rkzI .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI #lollipopEnd,#mermaid-svg-GkFOc7v4QMB1rkzI .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-GkFOc7v4QMB1rkzI .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-GkFOc7v4QMB1rkzI .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-GkFOc7v4QMB1rkzI .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-GkFOc7v4QMB1rkzI .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-GkFOc7v4QMB1rkzI :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
入参
出参
if awardType==1 直接 new
if awardType==2 直接 new
if awardType==3 直接 new
PrizeController
-Logger logger
+awardToUser(AwardReq req) : AwardRes
-queryUserName(uId) : String
-queryUserPhoneNumber(uId) : String
AwardReq
-String uId
-Integer awardType
-String awardNumber
-String bizId
-Map extMap
AwardRes
-String code
-String info
CouponService
+sendCoupon(uId, couponNumber, uuid) : CouponResult
GoodsService
+deliverGoods(DeliverReq) : Boolean
IQiYiCardService
+grantToken(mobile, cardId) : void
5.4.2 简单工厂类图
#mermaid-svg-Ye5X2LyXZTcIVF52{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Ye5X2LyXZTcIVF52 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Ye5X2LyXZTcIVF52 .error-icon{fill:#552222;}#mermaid-svg-Ye5X2LyXZTcIVF52 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Ye5X2LyXZTcIVF52 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Ye5X2LyXZTcIVF52 .marker.cross{stroke:#333333;}#mermaid-svg-Ye5X2LyXZTcIVF52 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Ye5X2LyXZTcIVF52 p{margin:0;}#mermaid-svg-Ye5X2LyXZTcIVF52 g.classGroup text{fill:#9370DB;stroke:none;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:10px;}#mermaid-svg-Ye5X2LyXZTcIVF52 g.classGroup text .title{font-weight:bolder;}#mermaid-svg-Ye5X2LyXZTcIVF52 .cluster-label text{fill:#333;}#mermaid-svg-Ye5X2LyXZTcIVF52 .cluster-label span{color:#333;}#mermaid-svg-Ye5X2LyXZTcIVF52 .cluster-label span p{background-color:transparent;}#mermaid-svg-Ye5X2LyXZTcIVF52 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Ye5X2LyXZTcIVF52 .cluster text{fill:#333;}#mermaid-svg-Ye5X2LyXZTcIVF52 .cluster span{color:#333;}#mermaid-svg-Ye5X2LyXZTcIVF52 .nodeLabel,#mermaid-svg-Ye5X2LyXZTcIVF52 .edgeLabel{color:#131300;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edgeLabel .label rect{fill:#ECECFF;}#mermaid-svg-Ye5X2LyXZTcIVF52 .label text{fill:#131300;}#mermaid-svg-Ye5X2LyXZTcIVF52 .labelBkg{background:#ECECFF;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edgeLabel .label span{background:#ECECFF;}#mermaid-svg-Ye5X2LyXZTcIVF52 .classTitle{font-weight:bolder;}#mermaid-svg-Ye5X2LyXZTcIVF52 .node rect,#mermaid-svg-Ye5X2LyXZTcIVF52 .node circle,#mermaid-svg-Ye5X2LyXZTcIVF52 .node ellipse,#mermaid-svg-Ye5X2LyXZTcIVF52 .node polygon,#mermaid-svg-Ye5X2LyXZTcIVF52 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Ye5X2LyXZTcIVF52 .divider{stroke:#9370DB;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 g.clickable{cursor:pointer;}#mermaid-svg-Ye5X2LyXZTcIVF52 g.classGroup rect{fill:#ECECFF;stroke:#9370DB;}#mermaid-svg-Ye5X2LyXZTcIVF52 g.classGroup line{stroke:#9370DB;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5;}#mermaid-svg-Ye5X2LyXZTcIVF52 .classLabel .label{fill:#9370DB;font-size:10px;}#mermaid-svg-Ye5X2LyXZTcIVF52 .relation{stroke:#333333;stroke-width:1;fill:none;}#mermaid-svg-Ye5X2LyXZTcIVF52 .dashed-line{stroke-dasharray:3;}#mermaid-svg-Ye5X2LyXZTcIVF52 .dotted-line{stroke-dasharray:1 2;}#mermaid-svg-Ye5X2LyXZTcIVF52 #compositionStart,#mermaid-svg-Ye5X2LyXZTcIVF52 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #compositionEnd,#mermaid-svg-Ye5X2LyXZTcIVF52 .composition{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #dependencyStart,#mermaid-svg-Ye5X2LyXZTcIVF52 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #dependencyStart,#mermaid-svg-Ye5X2LyXZTcIVF52 .dependency{fill:#333333!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #extensionStart,#mermaid-svg-Ye5X2LyXZTcIVF52 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #extensionEnd,#mermaid-svg-Ye5X2LyXZTcIVF52 .extension{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #aggregationStart,#mermaid-svg-Ye5X2LyXZTcIVF52 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #aggregationEnd,#mermaid-svg-Ye5X2LyXZTcIVF52 .aggregation{fill:transparent!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #lollipopStart,#mermaid-svg-Ye5X2LyXZTcIVF52 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 #lollipopEnd,#mermaid-svg-Ye5X2LyXZTcIVF52 .lollipop{fill:#ECECFF!important;stroke:#333333!important;stroke-width:1;}#mermaid-svg-Ye5X2LyXZTcIVF52 .edgeTerminals{font-size:11px;line-height:initial;}#mermaid-svg-Ye5X2LyXZTcIVF52 .classTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Ye5X2LyXZTcIVF52 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Ye5X2LyXZTcIVF52 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Ye5X2LyXZTcIVF52 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
入参
出参
持有
创建返回
PrizeController
-Logger logger
-StoreFactory storeFactory
+awardToUser(AwardReq req) : AwardRes
StoreFactory
+getCommodityService(Integer) : ICommodity
+getCommodityService(Class) : ICommodity
«interface»
ICommodity
+sendCommodity(uId, commodityId, bizId, extMap) : void
CouponCommodityService
-CouponService couponService
+sendCommodity(…)
GoodsCommodityService
-GoodsService goodsService
+sendCommodity(…)
-queryUserName(uId) : String
-queryUserPhoneNumber(uId) : String
CardCommodityService
-IQiYiCardService iQiYiCardService
+sendCommodity(…)
-queryUserMobile(uId) : String
AwardReq
-String uId
-Integer awardType
-String awardNumber
-String bizId
-Map extMap
AwardRes
-String code
-String info
5.5 时序图对比
5.5.1 面条代码
GoodsService
PrizeController
ApiTest
GoodsService
PrizeController
ApiTest
#mermaid-svg-bqVmfTxwqwP2ZLag{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-bqVmfTxwqwP2ZLag .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-bqVmfTxwqwP2ZLag .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-bqVmfTxwqwP2ZLag .error-icon{fill:#552222;}#mermaid-svg-bqVmfTxwqwP2ZLag .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-bqVmfTxwqwP2ZLag .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-bqVmfTxwqwP2ZLag .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-bqVmfTxwqwP2ZLag .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-bqVmfTxwqwP2ZLag .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-bqVmfTxwqwP2ZLag .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-bqVmfTxwqwP2ZLag .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-bqVmfTxwqwP2ZLag .marker{fill:#333333;stroke:#333333;}#mermaid-svg-bqVmfTxwqwP2ZLag .marker.cross{stroke:#333333;}#mermaid-svg-bqVmfTxwqwP2ZLag svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-bqVmfTxwqwP2ZLag p{margin:0;}#mermaid-svg-bqVmfTxwqwP2ZLag .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-bqVmfTxwqwP2ZLag text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-bqVmfTxwqwP2ZLag .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-bqVmfTxwqwP2ZLag .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-bqVmfTxwqwP2ZLag .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-bqVmfTxwqwP2ZLag .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-bqVmfTxwqwP2ZLag #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-bqVmfTxwqwP2ZLag .sequenceNumber{fill:white;}#mermaid-svg-bqVmfTxwqwP2ZLag #sequencenumber{fill:#333;}#mermaid-svg-bqVmfTxwqwP2ZLag #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-bqVmfTxwqwP2ZLag .messageText{fill:#333;stroke:none;}#mermaid-svg-bqVmfTxwqwP2ZLag .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-bqVmfTxwqwP2ZLag .labelText,#mermaid-svg-bqVmfTxwqwP2ZLag .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-bqVmfTxwqwP2ZLag .loopText,#mermaid-svg-bqVmfTxwqwP2ZLag .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-bqVmfTxwqwP2ZLag .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-bqVmfTxwqwP2ZLag .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-bqVmfTxwqwP2ZLag .noteText,#mermaid-svg-bqVmfTxwqwP2ZLag .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-bqVmfTxwqwP2ZLag .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-bqVmfTxwqwP2ZLag .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-bqVmfTxwqwP2ZLag .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-bqVmfTxwqwP2ZLag .actorPopupMenu{position:absolute;}#mermaid-svg-bqVmfTxwqwP2ZLag .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-bqVmfTxwqwP2ZLag .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-bqVmfTxwqwP2ZLag .actor-man circle,#mermaid-svg-bqVmfTxwqwP2ZLag line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-bqVmfTxwqwP2ZLag :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
if type==1 → false
else if type==2 → true
awardToUser(AwardReq{type=2})
new GoodsService()
new DeliverReq() 手动组装7个字段
queryUserName() / queryUserPhoneNumber()
deliverGoods(deliverReq)
true
AwardRes("0000","发放成功")
5.5.2 简单工厂
GoodsService
GoodsCommodityService
StoreFactory
PrizeController
ApiTest
GoodsService
GoodsCommodityService
StoreFactory
PrizeController
ApiTest
#mermaid-svg-PkdU7soOXtBS2rZj{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-PkdU7soOXtBS2rZj .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-PkdU7soOXtBS2rZj .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-PkdU7soOXtBS2rZj .error-icon{fill:#552222;}#mermaid-svg-PkdU7soOXtBS2rZj .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-PkdU7soOXtBS2rZj .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-PkdU7soOXtBS2rZj .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-PkdU7soOXtBS2rZj .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-PkdU7soOXtBS2rZj .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-PkdU7soOXtBS2rZj .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-PkdU7soOXtBS2rZj .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-PkdU7soOXtBS2rZj .marker{fill:#333333;stroke:#333333;}#mermaid-svg-PkdU7soOXtBS2rZj .marker.cross{stroke:#333333;}#mermaid-svg-PkdU7soOXtBS2rZj svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-PkdU7soOXtBS2rZj p{margin:0;}#mermaid-svg-PkdU7soOXtBS2rZj .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-PkdU7soOXtBS2rZj text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-PkdU7soOXtBS2rZj .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-PkdU7soOXtBS2rZj .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-PkdU7soOXtBS2rZj .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-PkdU7soOXtBS2rZj .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-PkdU7soOXtBS2rZj #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-PkdU7soOXtBS2rZj .sequenceNumber{fill:white;}#mermaid-svg-PkdU7soOXtBS2rZj #sequencenumber{fill:#333;}#mermaid-svg-PkdU7soOXtBS2rZj #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-PkdU7soOXtBS2rZj .messageText{fill:#333;stroke:none;}#mermaid-svg-PkdU7soOXtBS2rZj .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-PkdU7soOXtBS2rZj .labelText,#mermaid-svg-PkdU7soOXtBS2rZj .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-PkdU7soOXtBS2rZj .loopText,#mermaid-svg-PkdU7soOXtBS2rZj .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-PkdU7soOXtBS2rZj .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-PkdU7soOXtBS2rZj .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-PkdU7soOXtBS2rZj .noteText,#mermaid-svg-PkdU7soOXtBS2rZj .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-PkdU7soOXtBS2rZj .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-PkdU7soOXtBS2rZj .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-PkdU7soOXtBS2rZj .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-PkdU7soOXtBS2rZj .actorPopupMenu{position:absolute;}#mermaid-svg-PkdU7soOXtBS2rZj .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-PkdU7soOXtBS2rZj .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-PkdU7soOXtBS2rZj .actor-man circle,#mermaid-svg-PkdU7soOXtBS2rZj line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-PkdU7soOXtBS2rZj :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}
awardToUser(AwardReq{type=2})
getCommodityService(awardType=2)
GoodsCommodityService实例
sendCommodity(uId, sku, bizId, extMap)
组装DeliverReq(职责在这里)
deliverGoods(deliverReq)
true
正常返回
AwardRes("0000","发放成功")
总结
简单工厂模式又称静态工厂方法模式,其核心是由一个专门的工厂类(SimpleFactory / StoreFactory)根据传入的参数(如字符串或枚举),集中完成所有具体产品的实例化,并统一返回抽象产品接口(Product / ICommodity)。
核心角色包括:
- 抽象产品(Product / ICommodity)
- 具体产品(ConcreteProductA/B / CouponCommodityService、GoodsCommodityService、CardCommodityService)
- 简单工厂(负责if/switch判断与new对象)
典型应用场景:
- LLM提供商路由(OpenAI、DeepSeek、Claude等)
- 营销发奖系统(不同奖品类型对应不同外部服务)
优点:极简、职责清晰、调用方代码干净,快速实现一定程度的解耦。 最大缺陷:违背开闭原则(OCP),每新增一种产品都必须修改工厂类的判断逻辑,容易形成“巨型switch炼狱”,维护成本随产品种类增加而指数级上升。

![基于SpringBoot的企业资产借还与维修管理系统[源码免费+文档免费]-171主机测评](https://www.171host.com/wp-content/uploads/2026/08/20260825170029-6a8dca2dbb882-220x150.png)


