Lecture thumbnail 0:00 / 0:00 Building a singleton isn’t particularly difficult.

So that’s what we’re going to do first.

Now what I’m going to do is I’m going to define an interface called I database.

So this interface is going to define a single operation for getting the population of a capital city.

So we’re going to have get population of some city.

All right.

So having this interface, what we can do is we can make a database and I’ll call it singleton database,

just to show that it’s a singleton.

And this database is going to actually read data from some external stores.

So what I’m going to do is I’m going to make a new file.

Let’s go ahead and make a new file here.

I’ll just add a plain text file, which is going to store a bunch of capital cities.

So let me just do that quickly.

All right.

Visual Studio is taking its time, as always.

So go into general and then I’ll go into text file.

So we’re going to have a file called Capitals dot TXT.

And this is where I’ll put a bunch of capital names as well as their population numbers.

Okay.

So having this text file, what I’m going to do is I’m going to just go into properties to make sure

it gets copied to the output directory.

So let’s do that.

Copy if newer.

Let’s pick.

Okay, so now we have this text file and we can actually read it in our database and you can think of

our database as a real life database access component.

So I’m going to have a singleton database.

I could have just called it a database, but I want to show that it’s a singleton implementation, which

is an AI database.

So it has to implement this interface.

And what we’re going to do in the constructor to this database is we’re actually going to read that

file.

So we’re going to initialize the database by reading the names of all the capitals.

So we need a variable here somewhere like a private dictionary from string to INT.

So that would be from the capital name to the population, we’ll call it capitals like so.

And then in here I’ll just write line that we’re initializing database database like so it seemed to

have some sort of error here.

Let’s see why.

Oh yeah, we’re in the wrong location.

So this has to go into and of course Visual Studio once again with this formatting selection.

Oh look, it’s like almost crashing now.

Okay, so let’s stick that in here.

And then of course, the right line goes into a constructor of some kind.

So let’s make a constructor so we’re initializing the database.

What I’m going to do is just read all the lines from the file, then take them pairwise.

And I’ve got a couple of I’m actually using a couple of nugget packages here, so I’m using more Linq,

for example, so I can buffer the elements together.

So I’m going to say capitals equals and then I’ll file dot read all lines.

Okay, So I’m going to read all the lines.

And what we’re reading is capitals dot txt and then of course I’m going to batch them in pairs.

So batch two and then I’m going to call two dictionary because that’s what we want.

We want a dictionary of some kind.

So here I’m getting a list of elements with two elements in it.

So I make a key.

So if I have a list and the key is the first element trimmed, I think I have a bunch of spurious spaces

in there.

So I’ll say list dot element, element at zero dot trim.

So that’s the name of the capital.

And then for the value of the dictionary, I’ll say list dot element at one and then that has to be

parsed as an integer.

So in dot parse, I’m going to catch any exceptions here.

I assume that all the data is correct.

So that’s the initialization of our capitals.

So you can think of it like a read from a normal database.

And then what we’re going to do is we’re going to provide some sort of public API for actually getting

the population of a particular city.

So I’m going to have public int get population given the name of a city and we simply return capitals

at this particular name.

Once again, I’m not doing any kind of error checking.

Okay, so let’s get rid of this since I’ve just done it right.

So we have this interface for a database and what we can now do is we can now maybe start using it so

we can say var DB equals new and then the Singleton database.

Now of course, the whole point of this exercise is to make a singleton.

So we’re arguing that we don’t need multiple instances of the database.

Why?

Well, because every time you make a database, you make a read from this file and there is no point,

the file isn’t changing.

So why not just read it once?

And so you want to prevent anyone.

And that includes both your developers as well as anybody consuming this library externally from making

more than one singleton database.

You don’t want people to call the constructor on the Singleton database.

So what do you do?

Well, one obvious solution is you simply make the constructor private so you hide the constructor completely.

So just don’t enter too private.

Okay?

So now you cannot make a singleton database.

Another question is, well, how do you get it then?

And of course, the typical implementation of a singleton and the one that we’re going to do is by simply

making a static field of some kind and then exposing that field as a static public property.

So you can write something like the following.

For example, you can make a private singleton database instance.

And of course it has to be static, private, static singleton database instance equals new singleton

database, something like this, for example.

And then whenever somebody wants it, you have a public static, static instance which returns instance,

something like this.

And then of course, I forgot the name once again.

So that has to be a singleton database.

There we go.

So this is how you can both initialize and expose a single instance.

So now nobody can make more than one instance because there is no way of actually calling the constructor

because the constructor is private.

So there’s no way of constructing a singleton data.

Base, but you can get it by using something like this.

Var db equals singleton database instance and then you can operate on DB because that gives you the

appropriate reference and you can get the population of some city, for example.

So this works and it works to some degree.

The only thing which we can do to improve this situation is we can make the construction of the Singleton

database lazy.

Now the question is why would you want to do that?

And the answer is, well, because you might not even need the list of capital.

So let’s suppose you don’t need any of this.

You’re still going to be paying the price of actually performing the initialization here, performing

the static initialization, because this constructor causes you to read a file or in the real world,

it will cause you to read the database maybe.

And that’s something you want to avoid if the client doesn’t actually need it.

So what you can do here is you can change things a little bit.

So instead of having a singleton database, you can have something like a lazy singleton database.

Remember, we have the system lazy class, which instead of taking the assignment, what it does is

it lets you initialize a new lazy construct where you pass in a lambda for the initialization.

So you pass in the lambda, which makes a new singleton database.

So this way, instead of returning instance, you return instance dot value.

And this construct allows you to only create the singleton database when somebody accesses the instance,

because that’s when you get the value and that’s where the lambda that you passed in actually gets invoked.

So that’s a lazy way of creating the database.

And now what we can do is we can actually start consuming it.

So here I have var DB equals singleton database instance.

I can make a city, for example, Tokyo, and then of course we can write line its population.

So we’ll have city has population and then database dot get population and provide the city.

All right, so let’s see what the output is.

All right.

So as you can see, we initialized the database in Tokyo has population, what is it, 33 million.

That’s.

That’s a lot of people.

Okay.

So this is a rather good implementation of the singleton.

It’s lazy, it’s threadsafe.

It’s, you know, everything that you want it to be.

So the question is, what is wrong with it?

Why is Singleton such a bad idea?

And that’s what we’re going to see in the next clip.

Play Stop Play Play Play Play Play Start Play