Now, one question you might have is how can we use this functionality of factories?

What additional benefits do factories provide?

Should factories always be static or should factories actually have some sort of member variables?

Well, let’s take a look at two examples where a factory brings additional benefits, and the things

we’re going to talk about are object tracking and bulk replacement.

So here I have a simple scenario.

I have an interface I theme which specifies that objects should return a certain text color and background

color.

And we have two themes.

We have a light theme which returns a text color of black and a background color of white.

And we have a dark theme which does the opposite.

It uses the text color, white and background color as dark gray.

Now what we want to be able to do is we want to be able to have a sort of factory which, depending

on a Boolean value, specifying whether we want something dark or not, we would return the appropriate

theme.

But in addition, what we’re going to do is we’re going to keep a weak reference to every single object

created inside the factory.

So we’ll have a class called Tracking Theme Factory.

Now this is going to have a factory method which returns an eye theme, and it’s going to be called

Create Theme, which takes a Boolean value specifying whether or not we want a dark theme.

Now, in terms of storage, in terms of what we want to keep internally inside the factory, we want

to keep a list of weak references to every single class that we construct.

So we’ll have a private read only list of weak reference of theme.

Let’s call it themes new like so fix the errors here.

So now when we create a theme, not only do we return it, but we also wrap it with a weak reference

and add it to this particular list.

So here I say I theme theme equals, and then we check whether or not the user wants a dark theme.

If they do, we create a dark theme.

Otherwise we create a light theme.

Then we wrap it with a reference and add it to the set of themes.

So we say themes, dot add, and here we make a weak reference of I theme and we add the theme here

and then we return the theme as well.

There we go.

So this is something that allows us to output diagnostic information about all the objects that have

been constructed by this particular factory so we can have some sort of let’s have a public string info

property which is going to have a getter for returning some information about every single object that

has been constructed.

I’m going to keep things simple here.

Just having a string builder where we’re going to add information about every single type that has been

constructed.

So for each of the weak references, so for each of our reference in themes, we’re going to first of

all, check whether or not the reference is actually valid.

So we’re going to say if reference dot try get Target out var theme.

So we’re going to try getting the theme that the weak reference points to.

And if we do have it, we can check whether or not it’s dark so we can say Bool, dark equals theme

is dark theme, and if it is a dark theme, then we add the word dark.

Otherwise we add the word light.

So here we can say SB dot append sb dot append.

We check for dark and if it’s dark then we start with the word dark.

Otherwise we start with the word light and then we can append line.

The rest.

We can say theme, for example.

So it’s going to be either a dark theme or a light theme.

And then at the end of it all we return SB to string like so and that’s pretty much it.

Now this is something we can start using straight away to show how to work with the factory.

So we can say var factory equals new tracking theme factory, and then we can make a bunch of themes.

So I can say I can have theme one equals factory dot, create theme with a value of false, for example,

and have another theme.

Let’s call it theme two with a value of true.

So one of them is going to be light and one of them is going to be dark and we can actually output the

information so we can take the factory, we can say.info and get the information about the objects that

this factory has constructed throughout its lifetime.

So if we run this now, you’ll see fairly predictable output.

We have light theme and dark theme as the two themes that we have constructed so far.

If we had constructed the additional themes that would appear here in the list.

And of course, having such a list means that you can group things together.

So, for example, you can simply make a count of the number of themes that you’ve constructed.

So it doesn’t maybe you don’t even need a list of weak references such maybe you just want a dictionary

with some diagnostic information.

It doesn’t really matter.

But here we’re keeping the references.

And one question you might have is, well, what is the benefit of keeping these references?

Can we do anything?

Else with them.

And in actual fact, if you’re prepared to add one level of indirection, it’s possible to perform a

bulk replacement.

So imagine the situation where you’re using this factory in your application and all the themes you’re

returning are dark themes everywhere throughout the application.

Everybody that tries to get the theme gets a dark theme.

Now imagine that later on you want to take every single instance of a dark theme and replace it with

a light theme without without the user having to do anything, just doing it automatically.

Well, in actual fact, if you use a factory, it is possible.

So here is how you can do it.

First of all, we’re going to introduce a new class called Ref of T, so public class ref of T, where

T is a class.

It’s going to work with reference types only.

So here we’ll have public t value and we’ll also have some sort of constructor for initializing it.

Now this class is completely useless.

I mean, it’s just a wrapper.

It doesn’t do anything.

And if you want to work with it, you end up having to go into value to get the actual type.

So that’s a bit of an inconvenience.

But what it does give us is an ability to create a factory which not only tracks the object it has constructed,

but is able to perform bulk replacement.

So we’re going to have a new class called Replaceable Theme Factory.

So here we’re going to have another private list of weak references, but those are going to be different

weak references.

So we’ll have a private read only list of weak reference of ref of I theme.

So it’s quite a mouthful.

Unfortunately there is no way around this, so let’s call it themes.

I’ll put the new on the next line.

So the idea here is once again that we’re going to construct the different themes using a factory method,

but we’re going to return them as a ref as opposed to a nice theme.

So first of all, I’ll make a utility method which returns an eye theme on the basis of a boolean value,

create theme impl, which takes bool dark and simply returns.

If it’s dark, it returns a new dark theme.

Otherwise it returns a new light theme.

It’s just a utility method that we’re going to be using later on.

So what we can do is we can create a factory method.

So public ref of theme, ref of eye theme here, create theme, bool dark.

Now, first of all, we create the underlying object, but we need to wrap it with a reference.

So I’m going to say var equals new ref of eye theme like so.

Obviously minding the spelling here, ref of theme where I call create theme impl with the boolean flag.

Now what I do now is I add it to the set of themes.

So I say themes dot add, I add a new wrapper around the reference.

And by the way, notice I’m using the shorthand new here because the full specified type looks like

this, but I don’t want to keep typing all of that.

So let’s just keep this simple and then I return the reference.

So now we’re getting a ref whenever we actually use this.

And this is well, it becomes a bit annoying because now you have to unwrap the ref, you have to go

into its value.

But there is a major benefit to this and the benefit is that we can perform a bulk replacement, replacing

every single theme that has been created and given out to the user with a different theme.

So to do this we can have a method called replace theme where you specify whether or not you want all

the themes replaced with a dark one or a light one.

And here we can go through each of the weak references.

So for each var weak reference in themes, we can try getting at the underlying object so we can say

if w r dot w r not Volkswagen w r dot try get target out var reference.

So if we’re able to get the reference then we say reference dot value equals create theme info.

And by the way, this should be a capital V here.

Create theme info with the flag as before.

So what we’re going is we’re going into each of the refs that have been given out from the factory and

we’re changing its value, therefore affecting every single object in the system that has been given

out through this factory.

Now, there is a bit of a catch here because somebody can go ahead and they can get a ref, an eye theme,

they can unwrap this, get the value and then store the value elsewhere, in which case our ID is a

bit broken.

So we are relying on people to actually store a reference to an eye theme as opposed to the raw eye

theme itself.

Now let’s take a look at how we can perform the bulk replacement.

So let’s make another factory.

This is going to be a replaceable theme factory.

Now, in this factory, I’ll make a magic theme.

It’s magic because we can replace the type.

So here I can say Factory two dot create theme and.

Well, let’s make a dark theme, for example.

So I’m making a dark theme.

Let’s actually output the value.

So magic theme, dot value dot background color.

So this is the background color of a dark theme.

Now what I can do is I can take the factory and I can say, Can you please make sure that every single

object that you’ve ever given out becomes a light theme?

So I can say replace theme and I can specify a value of false here.

So now everything is a light theme, and then we can repeat the process.

We can output the background color once again and take a look at what these values actually are.

So as I run this, as you can see, we have the output dark gray, Then I perform the bulk replacement

and now I have the value of white.

So this has been a demonstration of some of the effective uses, some of the additional benefits, if

you will, of factories.

So a factory can keep track of every object that creates.

Obviously you want to use a weak reference so as not to extend the lifetime of the constructed object

because otherwise objects will unfortunately live for as long as the factory lives, which might not

necessarily be what you want because you’re you’re sort of interfering with the garbage collector here.

And in addition, what you can do with an extra level of indirection is you can also make these objects

replaceable.

So after you’ve given out a ref from a factory, you can go into the ref and you can change the underlying

object.

Of course, this is easy to sabotage by the end user because they can unwrap the ref themselves and

keep a reference that’s inside the ref, which unfortunately there isn’t much you can do about.

But on the other hand, it allows you this flexibility of performing bulk replacements, which in certain

situations are actually useful, particularly when you’re doing any sort of dynamic programming or scripting.

When the user has updated their script and you want to compile their changes and make a modification

inside the system, this is one of the ways that you can actually implement this.

Play Play Play Play Play Stop Play Play Play Start Play