Before we look at what is the canonical implementation of the bridge design pattern I want to show you
something else I want to show you the simple idiom and the reason why we're discussing the simple idiom
is because it is also a manifestation of the bridge design pattern albeit a slightly different one.
So essentially the simple idiom is all about hiding the implementation details of a particular class
by sticking it into its implementation just as the name suggests.
Let me show you how this works.
So we go and make a new class I'm going to call this class person.
So we've got a header file and we've got to see what we can do is we can start implementing the class
person right here.
I'm going to keep everything public for the purposes of this demonstration so let's suppose that the
person has a name.
So here is the name and then we might want to for example have a constructor and destructor in the US.
By the way you need the include four string if you are using a string Sorrells typically has to be in
here.
So we might want to have a constructor as well as a destructor.
We're going to fill in those gaps later on.
So now let us suppose that I also want to have some actual behavior so maybe one person to produce a
greeting.
So I'm going to have a function called Greed's like so once again I'm not going to implement this here.
So the idea of the pimpled idiom is that the implementation details which could be right inside person
for example they are actually deferred or relayed or bridged into a separate class.
So what ends up happening is you have an inner class which is declared inside person but its definition
is not inside person.
So here you would have a class called let's say person input like so and then you would have a pointer
to that class so you would have person input pointer called in pool like so.
OK so let me actually get rid of some of the extra stuff and I want to split the editor vertically so
that we get to see both sides so we're going to have a person who should be here and person that OCB
be here.
So person not CBP is where the fun stuff happens so that's where you would be expected to implement
person.
But the way it's implemented is not as straightforward as what we've seen previously because essentially
what happens is the actual behaviors of person are sort of delegated into this bridge which is person.
So let's go ahead and let's actually put in the implementations.
So essentially as you can see we have a pointer to the implementation which means we have to construct
it somewhere and it seems like the constructor is the best place to do that so I'm going to generate
the constructor here and here what I'm going to do is I'm going to initialize import with a new instance
of person in.
And then similarly for the destructor we can delete that instance or generate the destructor here.
And this is where we get to delete imports.
So that's the way that we actually create the bridge and subsequently destroyed.
Now the question is Well how do we actually use it.
Now we have this grid function here and we can actually have its implementation as part of a person
simple.
But interestingly enough notice that we have a class person here without any information about what
person import actually does so we're kind of hiding the implementation details behind this bridge that
we're using.
And when we want to implement greed.
So let's actually generate the implementation for it.
All we do is we really the request to the implementation which we have hidden.
So the implementation isn't even here but I guess now is a good time to actually define it.
So up here what we do is we put in the body of the person import.
So you have a class person poll and notice it's part of person so it's going to be person colon colon
person ample life so and then we'll have a single function which is called greed.
So it's going to take maybe a person pointer p and they might for example print some information about
the person maybe print a greeting.
So we're actually going to implement this right now.
And I notice I'm doing the implementation in the CBP file.
I'm not doing it in the header file so whoever is actually consuming the header file does not get exposed
to the details of the implementation.
So let's generate the implementation off person greed.
And here all I'm going to do is I'm just going to print hello followed by the name all file whatever
the person's name actually is.
So let me get an ice cream in here and we're going to use the S to the prefix.
So we're going to see out.
Hello my name is.
And then the name which we get from the person.
So here you would say the dash greater than name like so maybe an end line at the end of it as well.
So here is the hidden implementation of the greeting whereas the original greeting is kind of exposed
just as a function signature effectively.
So you get to see greed.
You don't get to see any information about what green actually does and so behind the scenes we have
the greed implementation of a person actually just being relayed to the implementation.
So you say impled greet passing this pointer.
OK so this is the pimple idiom in it's kind of concise form shall we say.
And the question is Well why would you want to do this in the first place.
Well one advantage is you're really hiding the implementation details you know exposing the inner mechanics
of the person class to whoever is actually consuming your library because remember you have to ship
the head or you have to give the consumer of your API at least this header file.
Now imagine if you had lots of private implementation details on how to put together a greeting and
lots of other stuff what would typically happen if you're not doing the pimple The AM is you would have
a private section here and you would have lots of you know secret functions here which would do something.
Now the problem is that even though these functions are private and they're supposed to be secret you're
still giving them to the consumer of the API.
So whoever is using your header file they can go into the header file and they will be able to see all
these secret functions that you might not even want to expose because you might have I don't know trade
secrets in here.
So instead what you do is all this private implementation details you don't even give them to the consumer.
Instead they go into the simple and the great thing about this pimple idiom is that all of the details
can go into this.
Now the great thing about the CBP file of course is you don't have to ship it.
You don't have to ship it to the client so here inside person all you can have a private section and
you can have lots of interesting stuff in here whether it's in line or side by side.
But it's all in the CBP file.
So you never actually expose the consumer to these functions so essentially you can build a very nice
looking internal implementation with plenty of details but you don't expose those details to whoever
is actually consuming those AP eyes.
So that's one reason to use the PEMBELI And basically the idea of hiding certain implementation details
and making sure that they don't appear as part of the header.
Another improvement is compilation speed.
Now this is becoming less and less relevant as the compilers become more and more incremental and they
are really fantastic nowadays.
But essentially what the pimple Adium allows you to do is it allows you to change the internal implementations
without changing the surface API of the signatures and as a result you don't introduce binary incompatibilities
because remember if you start adding members here for example you start adding private members.
You would have to recompile the whole thing and the consumer or whoever is actually using the header
file they would have to recompile their program to take advantage of all the new stuff whereas if you
use the pimple idiom you are carefully hiding all the implementation details behind the actual simple
pointer you have everything in this GPP so you can recompile and you can ship the new binary to the
consumer but they don't have to recompile their code because essentially nothing has changed in terms
of the function signatures.
So you have that same API with that same person call and call and agreed that they can call.
But the details have not been exposed to them.
And so the pimple idiom is or at least they used to be a way of actually improving compilation speeds
quite simply because you don't have to rebuild so much of your application.