项目结构:

python
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fail-Fast Pattern 快速失败模式
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2024.3.6 python 3.11
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/6/30 7:03
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : fail_fast.py
class ResourceCheckFailedError(Exception):
"""
快速失败:资源不可用"
"""
pass
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fail-Fast Pattern 快速失败模式
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2024.3.6 python 3.11
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/6/30 7:04
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : inventory_repo.py
class InventoryRepository:
"""
"""
def __init__(self):
self.inventory = {
"DIA001": 5,
"DIA002": 0,
"GOLD001": 20
}
def get_stock(self, product_id: str)->int|None:
"""
:param product_id:
:return:
"""
return self.inventory.get(product_id, -1)
def deduct_stock(self, product_id: str, quantity: int)->int|None:
"""
:param product_id:
:param quantity:
:return:
"""
self.inventory[product_id] -= quantity
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fail-Fast Pattern 快速失败模式
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2024.3.6 python 3.11
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/6/30 7:05
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : external_services_repo.py
class ExternalServices:
"""
"""
def __init__(self):
self.inventory_service = True
self.payment_gateway = True
self.quality_inspection = True
self.invoice_service = True
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fail-Fast Pattern 快速失败模式
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2024.3.6 python 3.11
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/6/30 7:05
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : validation_service.py
from FailFastPattern.exceptions.fail_fast import ResourceCheckFailedError
from FailFastPattern.repository.external_services_repo import ExternalServices
from FailFastPattern.repository.inventory_repo import InventoryRepository
class ValidationService:
"""
"""
def __init__(
self,
inventory_repo: InventoryRepository,
ext_services: ExternalServices
):
self.inventory_repo = inventory_repo
self.ext_services = ext_services
def fail_fast_check(self, customer_id: str, product_id: str, quantity: int):
"""
Fail-Fast 核心:全部资源一次性检查,不满足立即失败
:param customer_id:
:param product_id:
:param quantity:
:return:
"""
# 1. 顾客检查
if not customer_id or len(customer_id) < 3:
raise ResourceCheckFailedError("顾客信息无效")
# 2. 商品存在检查
stock = self.inventory_repo.get_stock(product_id)
if stock < 0:
raise ResourceCheckFailedError(f"商品 {product_id} 不存在")
# 3. 库存足够检查
if stock < quantity:
raise ResourceCheckFailedError(f"商品 {product_id} 库存不足")
# 4. 外部服务检查
if not self.ext_services.inventory_service:
raise ResourceCheckFailedError("库存服务不可用")
if not self.ext_services.payment_gateway:
raise ResourceCheckFailedError("支付网关不可用")
if not self.ext_services.quality_inspection:
raise ResourceCheckFailedError("质检系统不可用")
if not self.ext_services.invoice_service:
raise ResourceCheckFailedError("发票系统不可用")
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fail-Fast Pattern 快速失败模式
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2024.3.6 python 3.11
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/6/30 7:06
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : order_service.py
from FailFastPattern.exceptions.fail_fast import ResourceCheckFailedError
from FailFastPattern.repository.external_services_repo import ExternalServices
from FailFastPattern.repository.inventory_repo import InventoryRepository
from FailFastPattern.service.validation_service import ValidationService
from ParallelismPattern.models.entities import Order
class OrderService:
"""
"""
def __init__(
self,
validation_service: ValidationService,
inventory_repo: InventoryRepository,
ext_services: ExternalServices
):
self.validation = validation_service
self.inventory_repo = inventory_repo
self.ext_services = ext_services
def create_order(self, customer_id: str, product_id: str, quantity: int)->dict:
"""
:param customer_id:
:param product_id:
:param quantity:
:return:
"""
try:
# ========== 第一步:快速失败检查 ==========
self.validation.fail_fast_check(customer_id, product_id, quantity)
# ========== 检查通过:执行业务 ==========
self.inventory_repo.deduct_stock(product_id, quantity)
return {
"order_id": "ORD-JWL-9999",
"product_id": product_id,
"quantity": quantity,
"remaining_stock": self.inventory_repo.get_stock(product_id),
"status": "success"
}
except ResourceCheckFailedError as e:
raise e
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fail-Fast Pattern 快速失败模式
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2024.3.6 python 3.11
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/6/30 7:06
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : order_controller.py
from FailFastPattern.service.order_service import OrderService
class OrderController:
"""
"""
def __init__(self, order_service: OrderService):
self.order_service = order_service
def create_jewelry_order(self, customer_id: str, product_id: str, quantity: int)->dict:
"""
:param customer_id:
:param product_id:
:param quantity:
:return:
"""
try:
result = self.order_service.create_order(customer_id, product_id, quantity)
return {"success": True, "data": result}
except Exception as e:
return {"success": False, "error": f"快速失败: {str(e)}"}
调用:
python
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Fail-Fast Pattern 快速失败模式
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2024.3.6 python 3.11
# os : windows 10
# database : mysql 9.0 sql server 2019, postgreSQL 17.0 Oracle 21c Neo4j
# Datetime : 2026/6/30 7:15
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : FailFastBll.py
from FailFastPattern.api.order_controller import OrderController
from FailFastPattern.repository.external_services_repo import ExternalServices
from FailFastPattern.repository.inventory_repo import InventoryRepository
from FailFastPattern.service.order_service import OrderService
from FailFastPattern.service.validation_service import ValidationService
class FailFastBll(object):
"""
"""
def demo(self):
"""
:return:
"""
# 初始化依赖
inventory_repo = InventoryRepository()
ext_services = ExternalServices()
validation_service = ValidationService(inventory_repo, ext_services)
order_service = OrderService(validation_service, inventory_repo, ext_services)
controller = OrderController(order_service)
print("==== 场景1:正常下单 ===")
print(controller.create_jewelry_order("C1001", "DIA001", 1))
print("\n==== 场景2:库存不足 ===")
print(controller.create_jewelry_order("C1001", "DIA002", 1))
print("\n==== 场景3:顾客无效 ===")
print(controller.create_jewelry_order("C", "DIA001", 1))
print("\n==== 场景4:支付服务挂了 ===")
ext_services.payment_gateway = False
print(controller.create_jewelry_order("C1001", "DIA001", 1))
输出:
