Lecture thumbnail 0:00 / 0:00 I found a rather interesting scenario where you end up building something which I guess you could call
an adapter slash decorator.
So a pattern which actually embodies both concepts independently.
Well, not independently because it’s all happening in the same class, but it sort of fulfills the
requirements of the decorator in that you have an embedded object and you’re proxying the calls over
to that object.
So you’re not acting as if you were that object, but you have the exact same interface, so you’re
kind of substitutable.
So that’s the decorator.
And on the other hand, I found a way where you have the adaptation of an interface.
So I know it all sounds very confusing.
So let me give you an example.
This is actually an example from real life, amazingly enough, it’s an example from something that
people do.
So let’s suppose that you decide to write a simple Hello World application and you write it by saying
Var as equals.
Hello, Going back to basics here and then adding world to that and then right line, the result.
So we write line the letter S and I hope you know what the expected output of this actually is.
Now, the point about this demonstration is we all know this is inefficient from the point of view of
concatenating strings because strings are immutable.
So when you do a plus equals, it’s not like you’re adding the word world to the word hello.
It’s not like you’re mutating strings, you’re reassigning it effectively, you’re making a new string
and thereby you’re wasting a lot of memory.
And this is all very inefficient.
However, if you try and replace this, let’s say you decide to do a string builder, you realize that
none of this is going to work.
This results in absolute failure.
I mean, it would be nice if we could do something like this because it would lead to a very nice refactoring,
sort of change your string concatenations to a string builder.
That would be nice.
Unfortunately, String Builder does not contain support for implicit conversion from string and it doesn’t
support the plus equals operator.
So we have a bit of a problem.
And this is where on the one hand, because you can’t inherit from Stringbuilder, you will be building
a decorator and on the other hand, because you’re attempting to conform an interface that stringbuilder
gives you to an interface that you want, specifically the interface which has implicit conversion from
string and the plus equals operator.
You’re building a decorator and an adapter at the same time.
So that’s what we’re going to do.
We’re going to build both of them.
And we’ve already seen how to build a decorator of the string builder.
So I’m not going to go into the whole business of fixing the cause.
But the point is, you make your own class, Let’s go with my string builder.
And of course you aggregate the ordinary string builder.
So you say Stringbuilder SB equals new string builder like so.
And then you go to generate code.
Let’s do that, generate code, and then you generate the delegating members.
So you take stringbuilder everything.
Basically it’s quite a bit of calls that you’re making like 59 different operations.
And I remember one of those operations even being unsafe and operating on a pointer.
So that’s something that we may encounter at some point in time or maybe not.
I don’t know.
Let’s actually take a look.
We do seem to have a bit of red code here, so let’s just come back.
Okay.
So I’ve aggregated the string builder and my goal is to write something like this.
My string builder.
We cannot do this yet.
It’s still all red.
But luckily if we are serious about building the adapter, we can add the appropriate members.
So we’ve already got a decorator and now all we have to do is an adapter as well.
So first of all, we need an implicit conversion from a string.
So we have public static, implicit operator, my string builder being created from a string s And here
all you have to do really is you make a new string builder, you append the string and then you return
exactly that.
So var SB equals new my string builder.
So we make my string builder.
We say msb.sb dot append.
Now string builder is private.
It’s private here, but we can access the private member from within the class.
So it’s okay for us to kind of jump into the implementation detail here and append the string S and
then we return MSb.
So this allows us to say my string builder foo equals and then a string because we have the implicit
conversion.
Now two more things that we need to do.
So the next thing that we need to do is to implement Operator Plus so that you can do things like MSb
plus equals Foo because that’s another requirement of the interface.
So once again, we continue working with the adapter.
So we have public static, my string builder operator plus.
Let’s try this again.
Operator Plus.
And we take my string builder.
My stringbuilder.
SB And we also take strings.
So the idea here is very simple.
You just do MSb dot append don’t even have to.
Actually, we didn’t have to use the Stringbuilder here either.
We could have just done it like this.
So MSb dot append you append the string and once again you return MSb So that takes care of the plus
operator and by extension the plus equals operator.
And then finally we want some sort of nice two string implementation which simply exposes what the Stringbuilder
gives us.
Now, interestingly enough, this isn’t something that Resharper generates, so Resharper doesn’t generate
a delegating call for to a string.
So we’ll do it ourselves.
So return SB to string.
Okay.
Now if we come back to the main method, you’ll see that we no longer have any red wavy underlines.
The code is completely legit now we are now using my stringbuilder and we are essentially we can.
If we started out from var, which was obviously a string, we can now substitute with a single line.
We just substitute a string for a my string builder and we’re good to go.
There is no additional changes that are required here.
So the great thing about this example and by the way, let me just show you that it does in fact work.
The great thing about this example is that with a single type change, you can build an adapter which
on the one hand it’s a decorator over a string builder because you need one and you cannot inherit from
it.
On the other hand, it’s an adapter because it satisfies the interface, and the interface requirements
are that you have implicit conversion from string to a my string builder and you have operator plus
equals.
Play Play Stop Play Play Play Play Play Play Start Play information alert