pimpl成语与桥梁设计模式 [英] pimpl idiom vs. bridge design pattern
问题描述
我只是注意到一个新的术语pimpl成语,这个成语和桥梁设计模式有什么区别?我很困惑。
I just noticed a new term pimpl idiom, what's the difference between this idiom with Bridge design pattern? I am confused about that.
我也注意到pimpl成语总是用于交换功能,这是什么?
I also noticed the pimpl idiom is always used for swap function, what's that? Could anybody give me an example?
推荐答案
PIMPL是隐藏实现的一种方式,主要是打破编译依赖。
PIMPL is a way of hiding the implementation, primarily to break compilation dependencies.
另一方面,Bridge模式是一种支持多种实现的方式。
The Bridge pattern, on the other hand, is a way of supporting multiple implementations.
swap
是一个用于交换两个对象的值的标准C ++函数。如果你将指针交换到不同实现的实现,你基本上是在运行时改变类的机制。
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.
但是在它的基本和通用形式,使用PIMPL指向单个实现,因此没有具有不同子类的抽象类 - 只有一个类,向前声明,并在别处编译。更改实现类不需要对包含主头文件的源文件进行重新编译。
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.
例如,假设你有很多私有成员函数,私有枚举和私有数据。随着类的开发和维护,这些私有的位变化相当频繁。如果 #include
依赖性是这样的,接触这个头文件导致大量的源被重新编译,你有一个很好的候选人PIMPL。
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.
所以Bridge模式是面向对象设计,而PIMPL模式是关于文件的物理设计。
(有关物理设计的更多信息,我推荐John Lakos着作的大型C ++软件设计一书。)
(For more on physical design, I recommend the book Large-Scale C++ Software Design by John Lakos.)
这篇关于pimpl成语与桥梁设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!