Lecture thumbnail 0:00 / 0:00 I wanted to share yet another example of a composite proxy.

And this is going to take us back to the discussion we had in the composite design pattern, where we

looked at array backed properties.

So now we’re going to take a look at is a situation which is very strongly tied to user interfaces.

So in user interfaces, sometimes you have a bunch of checkboxes that you want to switch on or off,

and then you have a grouping checkbox which if you tick it, it sets or unsets every single one of the

related checkboxes.

So the question is how do you actually implement this in code?

And the answer is that you use a composite proxy property.

I know it’s getting really complicated, so let me show you an example that I actually have in one of

my applications.

I’m going to try and simplify this a little bit, but essentially I have a bunch of different Boolean

flags for the kinds of masonry that I want to generate for a house.

So I have a bunch of booleans for generating pillars and walls and floors of a particular house.

So in addition to having these variables bound to checkboxes in the user interface, I also want a grouping

checkbox where I can tick this checkbox and it will set or unset every single one of these properties

or fields.

So how do you do it?

Well, one approach is to simply use a composite proxy property.

Nothing particularly complicated here.

You just make a public boolean property called all.

So when you want to set every single property to a particular value, you simply do it.

You say pillars equals value and you duplicate this well, obviously duplicate it like this.

And then you also set the walls and the floors as well.

So this is easy.

If somebody checks a single checkbox, they set every single other checkbox automatically.

But what about the getter?

What about the getter?

Because obviously if all of these values are true, then you should return a true.

If all of these values are false, you should return a false.

But what if they are different?

What if pillars is true and walls is false?

For example, what do you return then?

And the answer is, Well, you are supposed to return null.

And that’s one of the reasons why we have nullable and tri state logic.

So let me show you how this works.

So if you want a getter, you basically have to change this property from a boolean to a nullable boolean,

which automatically invalidates all of this stuff.

Because essentially now what you have to do is you first of all, have to make sure that somebody is

passing in a non null value.

So if the value doesn’t have any actual value, if it’s null, then we return.

Otherwise, instead of setting value, we set value dot value.

I know it sounds like repetition, but that’s the way things are set up here.

So this is how you would implement this.

And then of course for the getter, you basically have to make sure that every single property has the

same value.

If they all have the same value, then you can return that value, otherwise you return null.

So if pillars is equal to walls and walls is equal to floors, then all the variables are the same and

you return pillars.

Otherwise you return null.

So this is the approach you would take if you wanted to have a property like all, which would be bound

to a user interface component, a checkbox, for example, which would control whether all of these

properties or fields are actually set or unset.

So we can actually take this approach and we can marry it with the approach of array backed properties

that we talked about when we talked about the composite design pattern.

So now we can put these two together and we can change this implementation because to be honest, this

part is particularly fragile.

And if you add additional Boolean values, for example, you have to make sure you have the additional

checks here.

You have to make sure that you have the appropriate assignment here.

How do you make this code safer?

And the answer is array backed properties, the approach that we’ve talked about in the composite design

pattern.

So let me comment everything out and comment this out.

And we’re going to implement this example, but we’re going to do it using array backed property.

So what I’m going to have is I’m going to have a private boolean array for the fields called flags,

and I’m just going to put three booleans in there.

And then when I want to expose the pillar property or the walls property or the floors property, I

just do it using a property.

So I say public bool pillars.

And here, for example, let me just undo this.

Obviously there is an error somewhere.

Let me just try to find it.

I think I’ve commented out the entire class.

Yeah, that’s that’s an obvious problem.

So let’s uncomment this and do it like this.

Okay.

So now we can, we can actually implement this.

So pillars, for example, for the setter, for instance, you would simply say flags at position zero

equals value for the getter.

You would return flags at zero.

And you would do the same for the floors and the walls.

Let me just copy over some code here, like so.

So this is how you would implement array back properties essentially for walls.

You return flags at one for floors.

We operate on flags at two.

And now if you want to re implement the composite property called O, you can do this knowing that flags

is in fact an array.

In actual fact, we may as well make it read only.

So let’s implement all once again.

So it’s once again going to be a boolean nullable boolean called All but now the Getter and the Setter

are more reliable, shall we say.

So for the getter, you say if flags dot skip one.

If all of these are equal to flags at zero, then you return flags at zero.

Otherwise you return null.

So basically you compare the value of the first element.

So you skip the first element and you make sure that the first element is equal to all of the other

elements.

And if that’s the case, then then you return that element, otherwise you return null.

And for the setter, it’s a simple for loop.

So once again we check that the value has a value.

If not, then we simply return and then we just make a for loop with I going from zero to flags dot

length.

And here what we do is we say flags at position I equals value dot value and that’s pretty much it.

So this is how you can implement a UI centric in this case a composite property using the array backed

properties approach.

Play Play Play Play Play Stop Play Play Play Start Play Play