规格模式通过将业务规则封装为可组合的布尔判断对象,提升代码可读性与可维护性。在c++中,使用模板定义规格基类,结合智能指针实现and、or、not等逻辑组合。以订单折扣为例,金额、会员等级、节假日等条件分别实现为独立规格,通过andspec、orspec等方法组合成复杂规则,最终判断是否满足折扣条件,输出“满足折扣条件!”。该模式符合开闭原则,易于扩展与测试,推荐使用shared_ptr管理生命周期,避免内存泄漏。

在C++中实现规格模式(Specification Pattern)可以有效封装业务规则,提升代码的可读性、可维护性和可复用性。该模式通过将复杂的判断逻辑拆解为独立的规格对象,使得业务规则可以像积木一样组合使用。
什么是规格模式
规格模式是一种行为设计模式,它将业务规则封装成独立的布尔判断对象(即“规格”),每个规格实现一个接口,提供一个方法来判断某个对象是否满足条件。多个规格可以通过逻辑操作(如与、或、非)组合成更复杂的规则。
核心思想是:将“判断是否满足条件”的逻辑从主业务流程中剥离出来,使代码更清晰、更易于测试和扩展。
基本结构与接口设计
定义一个抽象基类 Specification,所有具体规格继承自它:
立即学习“C++免费学习笔记(深入)”;
template <typename T>
class Specification {
public:
virtual ~Specification() = default;
virtual bool isSatisfiedBy(const T& candidate) const = 0;
<pre class='brush:php;toolbar:false;'>// 组合操作:与、或、非
Specification<T>* operator&&(const Specification<T>& other) {
return new AndSpecification<T>(*this, other);
}
Specification<T>* operator||(const Specification<T>& other) {
return new OrSpecification<T>(*this, other);
}
Specification<T>* operator!() {
return new NotSpecification<T>(*this);
}};
由于C++不支持直接返回临时对象的引用用于操作符重载,实际中更推荐使用组合函数或智能指针管理生命周期。下面是一个更实用的实现方式:
具体实现:订单折扣规则示例
假设我们要根据用户订单金额、会员等级、是否节假日等条件决定是否应用折扣。
#include <iostream>
#include <memory>
<p>// 订单类
struct Order {
double amount;
std::string membership;
bool isHoliday;
};</p><p>// 规格基类
template <typename T>
class Specification {
public:
virtual ~Specification() = default;
virtual bool isSatisfiedBy(const T& candidate) const = 0;</p><pre class='brush:php;toolbar:false;'>// 工厂方法返回组合规格
template <typename U>
std::shared_ptr<Specification<T>> andSpec(const Specification<T>& other) {
return std::make_shared<AndSpecification<T>>(*this, other);
}
template <typename U>
std::shared_ptr<Specification<T>> orSpec(const Specification<T>& other) {
return std::make_shared<OrSpecification<T>>(*this, other);
}
std::shared_ptr<Specification<T>> notSpec() {
return std::make_shared<NotSpecification<T>>(*this);
}};
// 金额大于指定值的规格
class AmountGreaterThan : public Specification
// 会员等级为VIP
class IsVIPMember : public Specification
// 是否节假日
class IsHoliday : public Specification
// 与规格
class AndSpecification : public Specification
bool isSatisfiedBy(const Order& order) const override {
return left->isSatisfiedBy(order) && right->isSatisfiedBy(order);
}};
// 或规格
class OrSpecification : public Specification
bool isSatisfiedBy(const Order& order) const override {
return left->isSatisfiedBy(order) || right->isSatisfiedBy(order);
}};
// 非规格
class NotSpecification : public Specification
bool isSatisfiedBy(const Order& order) const override {
return !spec->isSatisfiedBy(order);
}};
使用示例
组合多个规则判断是否满足折扣条件:
int main() {
Order order{1200.0, "VIP", true};
<pre class='brush:php;toolbar:false;'>AmountGreaterThan highAmount(1000);
IsVIPMember vip;
IsHoliday holiday;
// 折扣规则:高金额且(是VIP或节假日)
auto rule = highAmount.andSpec(vip.orSpec(holiday));
if (rule->isSatisfiedBy(order)) {
std::cout << "满足折扣条件!" << std::endl;
} else {
std::cout << "不满足折扣条件。" << std::endl;
}
return 0;}
输出结果为“满足折扣条件!”,因为订单金额超过1000,且是VIP会员(或节假日)。
这种方式让业务规则清晰可读,新增规则只需添加新的规格类,无需修改已有代码,符合开闭原则。
基本上就这些。规格模式在C++中虽不如动态语言那样灵活,但通过模板和智能指针仍能实现优雅的业务规则封装。关键在于避免内存泄漏,推荐使用共享指针管理组合对象的生命周期。









