
























项目结构:

珠宝行业适用场景:
我们实现:珠宝价格 / 属性规则解释器
支持规则:
黄金价格 = 克重 × 基础金价 + 工费黄金纯度达标 且 钻石净度达标价格 ≥ 5000 且 材质是 铂金# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 21:43
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : jewelry_context.py
from dataclasses import dataclass
@dataclass
class JewelryContext:
"""
珠宝上下文:存储所有珠宝属性
"""
name: str = ""
material: str = ""
weight: float = 0.0
purity: float = 0.0
price: float = 0.0
clarity: str = ""
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 21:50
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : base_expression.py
from abc import ABC, abstractmethod
from Interpreter.context.jewelry_context import JewelryContext
class BaseExpression(ABC):
"""
职责:抽象表达式(所有规则必须实现)
"""
@abstractmethod
def interpret(self, context: JewelryContext) -> bool | float:
"""
解释规则:返回判断结果 或 计算结果
:param context:
:return:
"""
pass
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 21:54
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : calculate_exp.py
from Interpreter.expression.base_expression import BaseExpression
from Interpreter.context.jewelry_context import JewelryContext
class GoldPriceExpression(BaseExpression):
"""
原子规则:黄金价格计算(可扩展钻石 / 宝石)
"""
def __init__(self, base_price: float, craft_fee: float):
"""
:param base_price:
:param craft_fee:
"""
self.base_price = base_price
self.craft_fee = craft_fee
def interpret(self, context: JewelryContext) -> float:
"""
:param context:
:return:
"""
total = context.weight * self.base_price + self.craft_fee
return round(total, 2)
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 21:52
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : compare_exp.py
from Interpreter.expression.base_expression import BaseExpression
from Interpreter.context.jewelry_context import JewelryContext
class GreaterThanExpression(BaseExpression):
"""
原子规则:数值比较 >=
"""
def __init__(self, field: str, threshold: float):
"""
:param field:
:param threshold:
"""
self.field = field
self.threshold = threshold
def interpret(self, context: JewelryContext) -> bool:
"""
:param context:
:return:
"""
value = getattr(context, self.field, 0.0)
return value >= self.threshold
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 21:53
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : material_exp.py
from Interpreter.expression.base_expression import BaseExpression
from Interpreter.context.jewelry_context import JewelryContext
class MaterialMatchExpression(BaseExpression):
"""
原子规则:材质匹配
"""
def __init__(self, material: str):
"""
:param material:
"""
self.material = material
def interpret(self, context: JewelryContext) -> bool:
"""
:param context:
:return:
"""
return context.material == self.material
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 21:55
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : and_exp.py
from Interpreter.expression.base_expression import BaseExpression
from Interpreter.context.jewelry_context import JewelryContext
class AndExpression(BaseExpression):
"""
组合规则:AND
"""
def __init__(self, exp1: BaseExpression, exp2: BaseExpression):
"""
:param exp1:
:param exp2:
"""
self.exp1 = exp1
self.exp2 = exp2
def interpret(self, context: JewelryContext) -> bool:
"""
:param context:
:return:
"""
return self.exp1.interpret(context) and self.exp2.interpret(context)
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 21:55
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : or_exp.py
from Interpreter.expression.base_expression import BaseExpression
from Interpreter.context.jewelry_context import JewelryContext
class OrExpression(BaseExpression):
"""
组合规则:OR
"""
def __init__(self, exp1: BaseExpression, exp2: BaseExpression):
"""
:param exp1:
:param exp2:
"""
self.exp1 = exp1
self.exp2 = exp2
def interpret(self, context: JewelryContext) -> bool:
"""
:param context:
:return:
"""
return self.exp1.interpret(context) or self.exp2.interpret(context)
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 21:56
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : jewelry_rule_service.py
from Interpreter.context.jewelry_context import JewelryContext
from Interpreter.expression.base_expression import BaseExpression
class JewelryRuleService:
"""
业务服务层:对外提供规则解释能力(解耦调用方)
"""
@staticmethod
def evaluate(expression: BaseExpression, context: JewelryContext) -> bool | float:
"""
执行规则解释
:param expression:
:param context:
:return:
"""
return expression.interpret(context)
调用:
# encoding: utf-8
# 版权所有 2026 ©涂聚文有限公司™ ®
# 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎
# 描述:Interpreter 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/4/28 22:01
# User : geovindu
# Product : PyCharm
# Project : pydesginpattern
# File : InterpreterBll.py
'''
'''
from Interpreter.context.jewelry_context import JewelryContext
from Interpreter.expression.terminal.compare_exp import GreaterThanExpression
from Interpreter.expression.terminal.material_exp import MaterialMatchExpression
from Interpreter.expression.terminal.calculate_exp import GoldPriceExpression
from Interpreter.expression.non_terminal.and_exp import AndExpression
from Interpreter.service.jewelry_rule_service import JewelryRuleService
class InterpreterBll(object):
"""
"""
def demo(self):
"""
:return:
"""
# 1. 构建珠宝上下文
jewelry = JewelryContext(
name="999足金戒指",
material="黄金",
weight=5.2,
purity=99.9,
clarity="VS1"
)
# 2. 规则服务
service = JewelryRuleService()
# ------------------- 规则1:计算黄金价格 -------------------
price_exp = GoldPriceExpression(base_price=450, craft_fee=150)
price = service.evaluate(price_exp, jewelry)
print(f"售价:{price} 元")
# ------------------- 规则2:高品质黄金 -------------------
high_purity = GreaterThanExpression("purity", 99.9)
is_gold = MaterialMatchExpression("黄金")
high_quality_exp = AndExpression(high_purity, is_gold)
result = service.evaluate(high_quality_exp, jewelry)
print(f"是否高品质黄金:{result}")
# ------------------- 规则3:高端珠宝 -------------------
platinum = JewelryContext(material="铂金", price=6800)
high_price = GreaterThanExpression("price", 5000)
is_platinum = MaterialMatchExpression("铂金")
filter_exp = AndExpression(high_price, is_platinum)
print(f"是否高端珠宝:{service.evaluate(filter_exp, platinum)}")
输出:

哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。