Singleton Pattern

12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€66.45
List Price:  €94.93
You save:  €28.48
£55.32
List Price:  £79.03
You save:  £23.71
CA$98.04
List Price:  CA$140.06
You save:  CA$42.02
A$107.72
List Price:  A$153.89
You save:  A$46.17
S$94.09
List Price:  S$134.42
You save:  S$40.33
HK$544.68
List Price:  HK$778.15
You save:  HK$233.46
CHF 61.92
List Price:  CHF 88.46
You save:  CHF 26.54
NOK kr774.79
List Price:  NOK kr1,106.89
You save:  NOK kr332.10
DKK kr495.67
List Price:  DKK kr708.13
You save:  DKK kr212.46
NZ$118.82
List Price:  NZ$169.76
You save:  NZ$50.93
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,339.40
List Price:  ৳11,913.94
You save:  ৳3,574.54
₹5,914.23
List Price:  ₹8,449.26
You save:  ₹2,535.03
RM311.24
List Price:  RM444.65
You save:  RM133.41
₦118,083.62
List Price:  ₦168,698.12
You save:  ₦50,614.50
₨19,391.36
List Price:  ₨27,703.14
You save:  ₨8,311.77
฿2,410.17
List Price:  ฿3,443.25
You save:  ฿1,033.08
₺2,423.71
List Price:  ₺3,462.60
You save:  ₺1,038.88
B$418.75
List Price:  B$598.25
You save:  B$179.49
R1,273.04
List Price:  R1,818.71
You save:  R545.66
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩97,732.12
List Price:  ₩139,623.30
You save:  ₩41,891.18
₪255.55
List Price:  ₪365.09
You save:  ₪109.53
₱4,110.61
List Price:  ₱5,872.56
You save:  ₱1,761.94
¥10,630.43
List Price:  ¥15,186.98
You save:  ¥4,556.55
MX$1,418.07
List Price:  MX$2,025.91
You save:  MX$607.83
QR254.36
List Price:  QR363.39
You save:  QR109.02
P953.37
List Price:  P1,362.02
You save:  P408.64
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.89
List Price:  E£4,970.06
You save:  E£1,491.16
ብር8,820.94
List Price:  ብር12,601.88
You save:  ብር3,780.94
Kz63,759.63
List Price:  Kz91,089.09
You save:  Kz27,329.46
CLP$68,404.72
List Price:  CLP$97,725.22
You save:  CLP$29,320.50
CN¥507.53
List Price:  CN¥725.08
You save:  CN¥217.54
RD$4,206.74
List Price:  RD$6,009.89
You save:  RD$1,803.14
DA9,351.13
List Price:  DA13,359.34
You save:  DA4,008.20
FJ$158.85
List Price:  FJ$226.94
You save:  FJ$68.09
Q538.41
List Price:  Q769.20
You save:  Q230.78
GY$14,600.65
List Price:  GY$20,858.96
You save:  GY$6,258.31
ISK kr9,615.92
List Price:  ISK kr13,737.62
You save:  ISK kr4,121.70
DH699.24
List Price:  DH998.96
You save:  DH299.72
L1,281.52
List Price:  L1,830.82
You save:  L549.30
ден4,084.06
List Price:  ден5,834.63
You save:  ден1,750.56
MOP$559.24
List Price:  MOP$798.95
You save:  MOP$239.71
N$1,266.15
List Price:  N$1,808.86
You save:  N$542.71
C$2,568.06
List Price:  C$3,668.81
You save:  C$1,100.75
रु9,427.87
List Price:  रु13,468.97
You save:  रु4,041.09
S/262.66
List Price:  S/375.25
You save:  S/112.58
K281.39
List Price:  K402.01
You save:  K120.61
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,903.42
List Price:  ZK2,719.29
You save:  ZK815.86
L330.70
List Price:  L472.45
You save:  L141.75
Kč1,679.39
List Price:  Kč2,399.23
You save:  Kč719.84
Ft27,552.80
List Price:  Ft39,362.83
You save:  Ft11,810.03
SEK kr765.72
List Price:  SEK kr1,093.94
You save:  SEK kr328.21
ARS$70,549.59
List Price:  ARS$100,789.45
You save:  ARS$30,239.85
Bs482.26
List Price:  Bs688.98
You save:  Bs206.71
COP$307,907.55
List Price:  COP$439,886.78
You save:  COP$131,979.23
₡35,643.95
List Price:  ₡50,922.11
You save:  ₡15,278.16
L1,765.02
List Price:  L2,521.57
You save:  L756.54
₲545,488.80
List Price:  ₲779,303.12
You save:  ₲233,814.31
$U2,997.83
List Price:  $U4,282.79
You save:  $U1,284.96
zł286.31
List Price:  zł409.03
You save:  zł122.72
Already have an account? Log In

Transcript

Now it's time to learn our next creational pattern. And I can say that the singleton pattern is the simplest one. And I think the most famous one in the all creational design factors, as well, my past experience, guys, whenever I asked someone that if I met a developer and I asked him that, do you know any design pattern, I'm damn sure that 90% of the people are already familiar with the singleton pattern. And that's the reason that in VHDL design patterns, I'm covering this thing in in this course, the singleton pattern is the simplest one, but we just have to take care of one or two things inside this code. And that's where I want you to concentrate. If you see the singleton pattern, simply use a private constructor and a static read only instance variable.

Now this is that key line which I want you To remember always, somewhere, if someone asked you that what is a singleton pattern and how I can achieve that thing, that's a secondary thing. The first thing is what I have to keep in mind to create my sample code with the singleton pattern. And that is this key line, you need a private constructor and a static read only instance variable. Now we know what is the purpose of the constructor constructor is going to be called whenever we create an object of a class. And that's why somewhere inside this we need a private constructor. And then we have a static read only instance variable.

So this static read only instance variable is somewhere going to help me to make sure that we are going to have only one particular instance available. The advantage of the singleton pattern is going to be that this application code is going to be thread safe. We know that when we have a multi threaded application. This is a very common and famous issue, that when more than one thread Trying to access the same instance of a particular application, it creates a problem, we will make sure that when any number of instances, or any number of clients are going to use my application, and that's the reason they are going to access the same instance of an application aces. And that reason groundbreaking works. Now you can assume that somewhere we have a service, which is hosted on the server, you can keep in mind that this may be a web service or web service or any kind of service, which is motion on the server.

And I have two clients who are going to access the service, I want to make sure that when the communication is going to happen, the service is going to have always one instance this should not vary the instance based on the client. You know that when we have a communication between client and service, it's going to be done by endpoints. So obviously, there are chances that my client is using one endpoint and client two is using the other endpoint. But the instance of the service should be saved. And that for the goal we are going to achieve in our this application sample. Start with single impact.

And I will create a new project management studio, which is going to be a console application. And this console application is having a name, maybe light souls. In fact, this line service console application to have a program.cs file. We will write some logic inside this main method but not right now literal. The first thing is I won't get an idea about a service. So assume that this is going to be a service which is showing an online cricket school.

I hope some of you are cricket fans, and we check the schools which are the life schools and we check this thing online in some websites. Assume that this service is going to provide that live cricket school to my audience and we need to make sure that Anybody who is going to see the live cricket school, they should have the one thing instance of that service because maintaining multiple instance and this kind of value is a bad habit. So we are going to create a new C sharp class and I'm giving a name of this C sharp law ticket service. Cricut service is going to be like sealed class for amplifying that this is going to be a sealed class, which is also a public class. It's a public party class, I hope you know that sealed class is the class did not inherit so we cannot inherit this particular class and it cannot have any child anywhere.

So that's what the Rudolph Singleton design pattern we need a private stem circle. So the first thing is I'm creating one private constructor destructor is going to be the method which is having a same name. I can across to the private sector and then we One more thing, which is going to be an instance variable. But as we know that my instance variable is going to be stated as well as read only. So I'm creating a static read only is a variable, you can keep this under cursors is actually static and read only, and it is actually preventing the instance of in class. And that's why we're using a new keyword.

Inside this constructor, we have to add some logic. But before I do that thing, I want to utilize multiple endpoints into service. So I'm assuming that maybe we're going to have one particular endpoint, which can have an address and the binding associated with that, to implement that code. I'm having a simple endpoint C sharp path, just having public properties inside that address and binding. Both are of type string, so I can pass some values inside this. And using this endpoint feature class, I'm actually going to create one collection of endpoints.

So to access a service, you're going to have more than one endpoint, but also All of these endpoints are going to be using one theme instance. To keep this thing of type faith, we are just checking one thing, we have one collection, which is a generic list collection. And it's going to represent a collection of type endpoints. And I'm giving the name of the collection underscore endpoints, we need some number of endpoints, and we'll store that in in this collection. And somewhere to call this endpoints at runtime, I'm going to use a predefined C sharp class, which is random. This random class is going to be used to filter my number of endpoints and from number of endpoints, I'm going to choose one of them before calling the method of the service.

Inside the constructor, I'm writing a simple logic when I'm actually in a new collection of underscore endpoint, and I'm initializing the collection and while initializing the collection and parsing of values to the properties which I have inside the endpoint class, so you can see this is purely object initializer code, I'm creating object of the endpoint class. And while initializing the object, I'm passing a windows to the address and binding. Because I'm referring something similar to the Volusia service, I have a first endpoint which is going to have an HTTP address with the Ws HTTP binding. The second one is also having HTTP address with the basic ht providing. And the third one is having some TCP address with the TCP packet, which is net TCP binding. Once we have this, we have three endpoints created with the help of the endpoint C sharp class.

And then we have one collection, which is having a name underscore endpoint. And because we have created this connection inside this create service constructor, one thing is sure that this private constructor is going to be called only once and that is first time. And at that time is going to create three endpoints. After this, now is the time to use this endpoint. Recall one of the methods of our service. And for that, I need to create some levels exactly below the constructor I have created one method, which is the name get endpoint.

And you can see this get endpoint method is actually a static method, which returns a cricket service instance. So this is that method, which is actually going to give me an instance. And we are returning underscore instance, which is declared on the top, which is our static read only instance variable. So it's something like this, then we are not going to directly going to create an object of this class because this is a class. But this class is going to have one private static read only instance variable, which are going to access whenever I'm going to call this method which is get endpoint. And that's what the key strategy behind our Singleton design pattern.

I have one simple property, which is next endpoint. I'm going to use this next endpoint property to generate that randomized endpoint and I can choose this stuff In different endpoints, and we can see whether it is working or not at runtime. To use this properly, I need to go to my program.cs class. Inside the main method cluster, which I'm checking is, I'm going to create more than one instances of my cricket service class, you can see that c one, C, two, C three, all three are actually three different objects. And in all three different objects, we are using TRICARE service class dot get endpoint. The moment I do this, this is going to call this get endpoint method, which is going to return my underscore instance instance variable.

Now, if I want to check whether those three instances are same or not, I'm just going to make sure that if we see one equals two equals to C two and C two is equal to C three, ensure these three objects are having the same instance or not. If this is true, it's going to print it in the console dot write line, that the same is just as other otherwise, is going to show me some exceptions or something like that. Because I do not have any insight. Now let me quickly run this and see whether we are getting famous or not. You can see showing the same single incidence, it means right now we are not creating any new Easter's because we do not have any new keyword in this, we are going to make sure that it's always good to have single term means single instance.

Even though if I implement my logic of multiple endpoint like this, you can see I'm creating a new object of a cricket service, which is C s. And then I'm running a for loop, which is going to start from zero to 15. And every time inside this for loop, we're just going to use that property next endpoint, which is going to generate an random number between one to 15. And based on that, we are going to generate a new address which is going to be stored inside the string new endpoint. And I'm just printing the address which is going to be used by that. Now, this time also been running this whole loop. If I check my face is stuff that is going to be C. So I'm going to make a new new endpoint.

Every time but if I run this thing you can see it's going to show me 15 different addresses. So sometimes it's using that net TCP sometime is using that ABCD. But if I check all this 15 instances, this all 15 instances are going to be saved. Because it's not that we are creating a new new instance every time we are making sure that we are going to return the same instance every time when somebody is going to call the service. I hope you understood this Singleton pattern, which is the simplest one, any number of business applications like the cricket service, which is online or some of the Sensex is that site and all at that time when we need to make sure that we are going to represent a same amount of instance to all losers. So that same data indivisible at that time, the singleton is this useful.

Thank you.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.