Let's take a look at a different example of the implementation of the flyway design pattern.
And this time around what we're going to do is we're going to print formatted text.
So what I wanted to do is I want to get a chunk of plain text and then I want to add some formatting
to it and we're going to see the different ways in which you can do that.
So let's imagine that we have a class called formatted text.
Ok so this class is going to be initialized with a chunk of plain text so I'll have plain text here
and then I'll have a public constructor which actually initializes the plain text but then I'm going
to customize this by for example allowing capitalization.
So let's suppose that there is a chunk of this formatted text that I want to capitalize.
So the question is Well how do you actually specify which parts of this text to capitalize.
Now one of the really kind of straightforward but very inefficient ways of doing it is to just make
a boolean array for every single character.
So here you just make a boolean array called cops and whenever you initialize this object what you do
is you also initialize this boolean array.
So you say caps equals and you bool and you specify the size of the plain text so you take the plain
text and you get its length and that's your array and subsequently when you go and look up a particular
character you also check in this boolean array whether it should be capitalized and yes.
By the way we need to make the structure where we delete gaps.
OK so how would you actually go about capitalizing a particular range of characters what you could make
utility function something like capitalize which takes the start and and positions that you want to
capitalize and then you have a simple for loop where you just have some counter going from let's say
going from zero to what I should be going from start to end.
Actually I should be going from start to end.
And in this particular case all you do is you say caps.
And I think what is true.
And that's how you capitalize the whole thing.
OK so having this utility function what you can do is you can implement the stream output operator and
that's not going to be pretty because we're going to be printing each of the characters in turn.
So we'll just generate the signature here remove everything because what we're going to be doing is
we're going to be putting everything into a single string so we're going to go through each of the characters
so I'll just make an ordinary for a loop where I will have the index i going from zero to the plain
text dot length.
And here what we're going to be doing and yes I want to call this BGA just for the sake of simplicity.
So what we want to be doing is we want to be going through each of the characters and basically checking
whether or not this character should be capitalized so I get the character.
So that's Avijit plain text and index.
And then I check whether I want to capitalize it so I appended to a string.
Here is my string as an I appended to the string.
But first of all I check whether I want to capitalize this particular location or if I do want to capitalize
it I call to Opper on the character and I don't think this should be an underscore here.
Otherwise I just add the character as is.
And that's how you build up a string and then we can just return the stream output operator actually
outputting this string.
So we are done.
This is a very inefficient limitation but it is an implementation that's going to work and we're actually
going to use it here.
So what I'm going to do is I'm going to make formatted text F.T. and I'm going to have a sentence.
This is a brave new world.
And let's suppose I want to capitalize the world.
Brave.
So I say T-Dog to capitalize from 10 to 15.
That's my range.
That's precisely these letters here.
And then I can out of words F T and line so that it should hopefully give me the formatted text so let's
actually run this and see what we get.
OK so we somehow managed to capitalize the whole thing basically.
Now let's think about why this actually happened.
And the reason why it happened is because of the initialization of the whole array essentially when
we kind of constructed the array we never planned on having being full of zeros.
Basically So here we should have some sort of meme set where you would maybe have the destination which
has caps and then you would have the value and then the size of Kaps would be essentially once again
plain text plain text cut length like so because that's the count in bytes in actual fact.
So if we now go ahead and we kind of approach this once again hopefully this time around we get the
right capitalization because obviously getting this right is very important for us.
OK so this time round we're getting the right results we're getting.
This is a brave new world so only the brave is capitalized.
So far so good.
OK.
But we know that we are wasting memory because there is no point in actually specifying every single
character whether it should be capitalized or not.
We should just be able to operate on this idea of ranges.
So let's see if we can actually use the flyway pattern to provide this idea of a range and we're going
to make a new class called better formatted text format text.
There we go.
So what I'm going to do is I'm basically going to still keep some information in by the plain text and
so on.
So let's have the string plain text but now instead of having the specifiers as is a boolean array what
I'm going to do is I'm going to have a vector of text range objects which I'm going to call formatted.
OK so this is going to be a private section.
And now what we're going to do is we're going to build up a public section where we actually define
this text range and provide some operations on it.
So each of the text ranges is going to be a nested class or nested structure text range like so.
And in here we're going to define the starting and ending positions of the range starting and ending
as well as whether we want to capitalize this particular chunk and here you can add other options to
the range like if this was a proper textedit or you would have things like baldie italic and so on and
so forth.
So you would have all sorts of things in here.
Now one thing we want to determine is whether the text range covers a particular point.
So we'll have a returning function called covers which takes a position and returns whether or not this
position is between the start and the end of the range.
So we return position greater than or equal to start and position less than or equal to end.
Now we go OK.
So this is the implementation of the TX range and now what we want is we want to be able to get a range
for a particular kind of slice of our string so we're going to have a function which is going to return
a text range reference.
Now the reason why we're going to return a reference is because every single range that we generate
on request from the user We're going to put it into this vector and so it's very valid to return a reference
to something inside a vector.
So I'm going to call this get range will have the start and coordinates for the range.
And what we're going to do is we're going to first of all get the formatting vector and we're going
to placed back a constructed text range with the start and end values and then we just going to return
a reference to it.
So we're going to return star formatting to our begin again.
That's all that we're going to do.
So now the use it can actually get one of these ranges.
Now let's not forget that we want a constructor which actually initializes the plain text string so
I'm going to add it in here.
And we do want some sort of stream output.
Operator but it's going to operate differently.
So now the stream out with the operator is going to use this idea of ranges and basically check every
single range that we have and whether or not it's formatting actually affects the system here.
So I'm going to generate the stream map with operator signature.
I'll get everything that's in here and we're going to go through this again.
So we're building a string.
And let me just rename this till again.
We're building a string s.
Now we're going to go through every single character as before.
So for the index i going from zero to plain text length from index I here we have RBJ dot plain text
length.
Now we go and we're going to first of all get the characters always.
Or C equals object of plaintext at position.
And then we go through every single range and we verify whether or not this range covers this particular
point.
And if it does we check that it has the capitalization Flack's said.
And if it does we apply the capitalization.
So it looks something like this for Konst order orangy in Abi Jadot formatting so we go through each
of the Texas Rangers and we say if the range covers the point at all and we want capitalization orangeade
or counter the lies then please set seed to oper see like so and then we append C to the string obviously
so we say x plus equals C like so.
So what this gives us is it gives us the ability to basically append the character and modify it as
well.
Now this isn't the perfect solution.
For example if you have two ranges affecting a character it's going to be duplicated but it's going
to be OK for us because we're just going to be applying capitalization and nothing else.
So here I'm going to return O S S.
So return the overall string and we can actually experiment with this particular approach so the guy
has changed a little bit but it's more or less the same.
So here is a better formatted text BFC.
And once again I'm going to pick this string here so stick it in here.
And then when I'm going to do is I'm going to say B F T b f t don't get range and I'm going to get the
range from 10 to 15 and then I've got this reference to a range and I can for example capitalize it
by setting capitalized to true.
And then I can see how the.
And hopefully get precisely the same result.
That's actually you see that it is in fact the case
all writers you can see for both of these cases we're getting exactly the same output string which is
what we wanted in the first place.
So the takeaway from this is that in this particular example it is the text range which is a flyweight.
So instead of doing this huge duplication of having lots of these boolean variables or a boolean array
in fact what we have is we have this flyweight and we keep a set of these flyweights inside the object
so that we know what kind of formatting to apply.