规格模式通过将业务规则封装为可组合的布尔判断对象,提升代码可读性与可维护性。在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<Order> { double threshold; public: AmountGreaterThan(double t) : threshold(t) {} bool isSatisfiedBy(const Order& order) const override { return order.amount > threshold; } };
// 会员等级为VIP class IsVIPMember : public Specification<Order> { public: bool isSatisfiedBy(const Order& order) const override { return order.membership == "VIP"; } };
// 是否节假日 class IsHoliday : public Specification<Order> { public: bool isSatisfiedBy(const Order& order) const override { return order.isHoliday; } };
// 与规格 class AndSpecification : public Specification<Order> { std::shared_ptr<Specification<Order>> left, right; public: AndSpecification(const Specification<Order>& l, const Specification<Order>& r) : left(std::make_shared<Specification<Order>>(l)), right(std::make_shared<Specification<Order>>(r)) {}
bool isSatisfiedBy(const Order& order) const override {
return left->isSatisfiedBy(order) && right->isSatisfiedBy(order);
}};
// 或规格 class OrSpecification : public Specification<Order> { std::shared_ptr<Specification<Order>> left, right; public: OrSpecification(const Specification<Order>& l, const Specification<Order>& r) : left(std::make_shared<Specification<Order>>(l)), right(std::make_shared<Specification<Order>>(r)) {}
bool isSatisfiedBy(const Order& order) const override {
return left->isSatisfiedBy(order) || right->isSatisfiedBy(order);
}};
// 非规格 class NotSpecification : public Specification<Order> { std::shared_ptr<Specification<Order>> spec; public: explicit NotSpecification(const Specification<Order>& s) : spec(std::make_shared<Specification<Order>>(s)) {}
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++中虽不如动态语言那样灵活,但通过模板和智能指针仍能实现优雅的业务规则封装。关键在于避免内存泄漏,推荐使用共享指针管理组合对象的生命周期。
以上就是C++规格模式 业务规则封装实现的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号