Lecture thumbnail 0:00 / 0:00 So now I want to show you an example of a decorator that I end up building quite a lot, and that is

a decorator around a stringbuilder class.

So you might be wondering, well, why would you want a decorator around a single class?

Isn’t the whole point of decorator to build some sort of multiple inheritance scenario thing?

Well, not really, because sometimes you just get sealed classes.

So if I want to make a code builder, for example, I might want to inherit from Stringbuilder.

Notice it’s not even in the list.

And if I leave it like this, it’s kind of underlined in Wavy Red underlines and it says here that you

cannot inherit from a sealed class.

So I don’t know the reason why Stringbuilder is sealed.

Maybe it’s for security reasons so that you cannot override it and then stick it into some method where

something sensitive is happening.

Whatever the reason, we now have a problem.

We want a stringbuilder we cannot inherit from it.

So what do we do?

Well, the answer is obvious.

We build a decorator.

So here I make a private stringbuilder builder equals new stringbuilder.

Surprised I don’t get a pop up helping me here.

And then of course, you generate all the delegating members.

So you go and you generate the delegating members and there is a ton of them.

So just press return and we have to sit here and wait because there is apparently 59 of them.

So that is a very complicated operation.

But the consequence of this operation is that we end up once again with a decorator, which does everything

that a stringbuilder does.

Well, not really.

Not really.

You might think that this resharper magic actually fixes all the problems for us.

Well, no, because Stringbuilder has a fluent interface.

It returns a stringbuilder.

Now, we don’t want to return a stringbuilder anywhere.

We want to return a code builder and we’re returning a stringbuilder.

This is terrible.

This is not what we wanted.

Now, unfortunately, Resharper doesn’t know that there is this thing called fluent interface.

And if you’re building a decorator over something which has a fluent interface, you might want to expose

the outside object instead of whatever it is you’re actually decorating.

So with that in mind, we have to do it ourselves.

And unfortunately, it’s not easy because for each method, like for example, instead of returning

builder dot clear, what we have to do and this is quite tedious is two lines we have to say builder,

dot, clear builder, dot clear.

And then we have to do return this because that’s the only way we once again make a fluent interface.

And of course this return type has to change to a code builder.

So let’s try doing that.

So first of all, I’m going to grab this huge chunk of code from about here to the very end, sort of

jump to here, and then I’m going to open up the replace and I’m going to change string Builder to code

builder, and I want to do it in the selection.

I think I had a selection.

Let’s just do this again.

I think my selection got cleared, so I’m going to do this throughout this entire file and I’ll just

go in here and alt a hopefully that fixes it for us.

Okay.

So we now have lots of red code, obviously, because we’re converting the wrong thing and now we need

to do a replace using regular expressions to make sure that we get the right invocation.

So once again, coming back to kind of up here, I’m going to open up the replace once again.

And here we’re looking for something which starts with return builder dot, followed by whatever.

We’ll just make a group with one or more characters in it and it’s finished with the end line.

So I’ll put a dollar in here and we need to replace this whole thing.

We need to replace it with, first of all, calling Builder Dot and then using that group that we made.

And then we put a line break backslash n and then we return this semicolon.

So let’s try doing this.

Just fire this up.

Okay, So 53 occurrences replaced and we now need to check all those cases where we did it unnecessarily.

Like when you return an int for example.

That’s not really what we need.

We just need a proper return.

And the same goes for this one.

Okay, so return here.

I think we fixed every single error.

Have we fixed every single error?

No.

There’s one more look here it is equals.

So here, once again, we fix it back.

Okay, so we are now ready to actually perform certain operations.

Let’s just check.

The original builder is still a string builder because if this becomes a code builder, for some reason,

we’re going to have a stack overflow.

Okay, so everything is great.

We’re now having a fluent interface.

I may as well call the formatting.

Okay, so this is nicely aligned now.

So we’re returning a code builder everywhere, and this is precisely the point where we can start customizing

the code builder because it’s now a string builder plus a bit more.

So the first and most obvious thing that you would do is implement to string.

So if you go and you implement to string, that would obviously return builder dot to string.

There we go.

So.

So with all of this in mind, let’s take a look at the actual APIs.

I’m going to go here in Main and then var KB equals new code builder.

Come on.

Okay.

And then of course, I can say KB dot append line class foo dot append line curly brace dot append line

closing curly brace.

And I can I would typically customize this so that I have scopes and indentation and all those sorts

of things.

I’m not going to show them here.

But the point is I can now write line KB and this is hopefully going to give me the right output.

So let’s try executing this just to show that the whole thing actually works.

All right, so we get the right output.

So this has been a small demonstration of how you build a decorator over a single class, so everything

seems easy until you get a fluent interface and you end up correcting everything using regular expressions.

But then once you’re done, you can start customizing it and building your own implementations of methods

and properties and so on and so forth.

So a very nice demonstration of how a limitation of the dot net framework probably done for security

reasons can actually be managed using the decorator pattern.

Play Stop Play Play Play Play Play Play Play Start Play information alert