Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pimpl idiom vs. bridge design pattern

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?

like image 646
skydoor Avatar asked Feb 27 '10 03:02

skydoor


People also ask

Is Pimpl a Bridge pattern?

The bridge pattern can also be thought of as two layers of abstraction. When there is only one fixed implementation, this pattern is known as the Pimpl idiom in the C++ world.

What is the difference between Bridge pattern and adapter pattern?

Adapter pattern is used after the application components are designed so that we can use them without modifying the source code. This is in contrast to the Bridge pattern, which is used before the components are designed.

What is Pimpl in design pattern?

The pimpl idiom is a modern C++ technique to hide implementation, to minimize coupling, and to separate interfaces. Pimpl is short for "pointer to implementation." You may already be familiar with the concept but know it by other names like Cheshire Cat or Compiler Firewall idiom.

What is the difference between strategy and Bridge pattern?

While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure. The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.


2 Answers

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.)

like image 166
Jon Reid Avatar answered Oct 04 '22 07:10

Jon Reid


I have used pointer impl to hide implementation details from public interface/include file Basically interface look like this:

struct Interface { private: class Impl; Impl *pImpl; }; 

Then somewhere in inside Interface::Impl is defined and implemented, but details not exposed.

as far as swap, this is the first time I hear it. Can you elaborate on it?

like image 39
Anycorn Avatar answered Oct 04 '22 07:10

Anycorn