pimpl成语与桥梁设计模式 [英] pimpl idiom vs. bridge design pattern

查看:126
本文介绍了pimpl成语与桥梁设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚注意到一个新的术语pimpl成语,这个成语与桥梁设计模式有什么区别?我很困惑。



我也注意到pimpl成语总是用于交换功能,那是什么?有人可以给我一个例子吗?

解决方案

PIMPL是一种隐藏实现的方式,主要是打破编译依赖关系。 >

另一方面,Bridge模式是支持多种实现的一种方式。



交换是用于交换两个对象的值的标准C ++函数。如果您将指针交换到不同实现的实现,那么您实际上是在运行时更改类的机制。



但是,它的基本和通用形式是一个类使用PIMPL指向单个实现,因此不存在具有不同子类的抽象类 - 只有一个类,前向声明和其他地方编译。更改实现类不需要重新编译包含主标题的源。



例如,假设你有很多私有成员函数,私有枚举和私有的数据。而这些私人的位随着课程的开发和维护而相当频繁地变化。如果 #include 依赖关系是这样的,触摸这个头文件导致大量的源重新编译,你有一个很好的候选人PIMPL。



所以Bridge模式是关于面向对象的设计,而PIMPL成语是关于文件的物理设计。



(关于物理设计的更多信息,我推荐这本书大型C ++软件设计由John Lakos。)


I just noticed a new term pimpl idiom, what's the difference between this idiom with Bridge design pattern? I am confused about that.

I also noticed the pimpl idiom is always used for swap function, what's that? Could anybody give me an example?

解决方案

PIMPL is a way of hiding the implementation, primarily to break compilation dependencies.

The Bridge pattern, on the other hand, is a way of supporting multiple implementations.

swap is a standard C++ function for exchanging the values of two objects. If you swap the pointer to the implementation for a different implementation, you are essentially changing the mechanism of the class at runtime.

But in its basic and common form, a class using PIMPL points to a single implementation, so there is no abstract class with distinct subclasses — just one class, forward declared, and compiled elsewhere. Changing the implementation class does not require any recompilation of sources that include the main header.

For example, say you have a lot of private member functions, private enums, and private data. And these private "bits" change fairly frequently as the class is developed and maintained. If the #include dependencies are such that touching this header file causes a large number of sources to be recompiled, you have a good candidate for PIMPL.

So the Bridge pattern is about object-oriented design, while the PIMPL idiom is about physical design of files.

(For more on physical design, I recommend the book Large-Scale C++ Software Design by John Lakos.)

这篇关于pimpl成语与桥梁设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆