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

在C++中实现规格模式(Specification Pattern)可以有效封装业务规则,提升代码的可读性、可维护性和可复用性。该模式通过将复杂的判断逻辑拆解为独立的规格对象,使得业务规则可以像积木一样组合使用。
什么是规格模式
规格模式是一种行为设计模式,它将业务规则封装成独立的布尔判断对象(即“规格”),每个规格实现一个接口,提供一个方法来判断某个对象是否满足条件。多个规格可以通过逻辑操作(如与、或、非)组合成更复杂的规则。
核心思想是:将“判断是否满足条件”的逻辑从主业务流程中剥离出来,使代码更清晰、更易于测试和扩展。
基本结构与接口设计
定义一个抽象基类 Specification,所有具体规格继承自它:
立即学习“C++免费学习笔记(深入)”;
template class Specification {public: virtual ~Specification() = default; virtual bool isSatisfiedBy(const T& candidate) const = 0;// 组合操作:与、或、非Specification* operator&&(const Specification& other) { return new AndSpecification(*this, other);}Specification* operator||(const Specification& other) { return new OrSpecification(*this, other);}Specification* operator!() { return new NotSpecification(*this);}
};
由于C++不支持直接返回临时对象的引用用于操作符重载,实际中更推荐使用组合函数或智能指针管理生命周期。下面是一个更实用的实现方式:
具体实现:订单折扣规则示例
假设我们要根据用户订单金额、会员等级、是否节假日等条件决定是否应用折扣。
#include #include// 订单类struct Order {double amount;std::string membership;bool isHoliday;};
// 规格基类template class Specification {public:virtual ~Specification() = default;virtual bool isSatisfiedBy(const T& candidate) const = 0;
// 工厂方法返回组合规格template std::shared_ptr<Specification> andSpec(const Specification& other) { return std::make_shared<AndSpecification>(*this, other);}template std::shared_ptr<Specification> orSpec(const Specification& other) { return std::make_shared<OrSpecification>(*this, other);}std::shared_ptr<Specification> notSpec() { return std::make_shared<NotSpecification>(*this);}
};
// 金额大于指定值的规格class AmountGreaterThan : public Specification {double threshold;public:AmountGreaterThan(double t) : threshold(t) {}bool isSatisfiedBy(const Order& order) const override {return order.amount > threshold;}};
// 会员等级为VIPclass IsVIPMember : public Specification {public:bool isSatisfiedBy(const Order& order) const override {return order.membership == "VIP";}};
// 是否节假日class IsHoliday : public Specification {public:bool isSatisfiedBy(const Order& order) const override {return order.isHoliday;}};
// 与规格class AndSpecification : public Specification {std::shared_ptr> left, right;public:AndSpecification(const Specification& l, const Specification& r): left(std::make_shared>(l)), right(std::make_shared>(r)) {}
bool isSatisfiedBy(const Order& order) const override { return left->isSatisfiedBy(order) && right->isSatisfiedBy(order);}
};
// 或规格class OrSpecification : public Specification {std::shared_ptr> left, right;public:OrSpecification(const Specification& l, const Specification& r): left(std::make_shared>(l)), right(std::make_shared>(r)) {}
bool isSatisfiedBy(const Order& order) const override { return left->isSatisfiedBy(order) || right->isSatisfiedBy(order);}
};
// 非规格class NotSpecification : public Specification {std::shared_ptr> spec;public:explicit NotSpecification(const Specification& s): spec(std::make_shared>(s)) {}
bool isSatisfiedBy(const Order& order) const override { return !spec->isSatisfiedBy(order);}
};
使用示例
组合多个规则判断是否满足折扣条件:
int main() { Order order{1200.0, "VIP", true};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++规格模式 业务规则封装实现的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1473433.html
微信扫一扫
支付宝扫一扫