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
€64.99
List Price:  €92.85
You save:  €27.85
£55.77
List Price:  £79.68
You save:  £23.90
CA$95.68
List Price:  CA$136.70
You save:  CA$41.01
A$105.96
List Price:  A$151.38
You save:  A$45.42
S$94.48
List Price:  S$134.98
You save:  S$40.50
HK$546.78
List Price:  HK$781.15
You save:  HK$234.36
CHF 63.33
List Price:  CHF 90.48
You save:  CHF 27.14
NOK kr761.11
List Price:  NOK kr1,087.35
You save:  NOK kr326.23
DKK kr485.02
List Price:  DKK kr692.92
You save:  DKK kr207.89
NZ$116.44
List Price:  NZ$166.35
You save:  NZ$49.91
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,660.01
List Price:  ৳10,943.35
You save:  ৳3,283.33
₹5,835.78
List Price:  ₹8,337.18
You save:  ₹2,501.40
RM331.75
List Price:  RM473.95
You save:  RM142.20
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,492.21
List Price:  ₨27,847.21
You save:  ₨8,355
฿2,572.74
List Price:  ฿3,675.50
You save:  ฿1,102.76
₺2,264.43
List Price:  ₺3,235.04
You save:  ₺970.61
B$355.02
List Price:  B$507.19
You save:  B$152.17
R1,295.44
List Price:  R1,850.71
You save:  R555.27
Лв127.05
List Price:  Лв181.51
You save:  Лв54.46
₩94,909.58
List Price:  ₩135,590.93
You save:  ₩40,681.35
₪260.15
List Price:  ₪371.66
You save:  ₪111.51
₱3,993.87
List Price:  ₱5,705.78
You save:  ₱1,711.90
¥10,713.01
List Price:  ¥15,304.96
You save:  ¥4,591.95
MX$1,187.73
List Price:  MX$1,696.83
You save:  MX$509.10
QR254.83
List Price:  QR364.06
You save:  QR109.23
P950.82
List Price:  P1,358.38
You save:  P407.55
KSh9,247.76
List Price:  KSh13,211.65
You save:  KSh3,963.89
E£3,352.12
List Price:  E£4,788.95
You save:  E£1,436.83
ብር4,006.43
List Price:  ብር5,723.72
You save:  ብር1,717.28
Kz58,511.64
List Price:  Kz83,591.64
You save:  Kz25,080
CLP$65,950.47
List Price:  CLP$94,219
You save:  CLP$28,268.52
CN¥506.53
List Price:  CN¥723.65
You save:  CN¥217.11
RD$4,055.76
List Price:  RD$5,794.19
You save:  RD$1,738.43
DA9,420.16
List Price:  DA13,457.95
You save:  DA4,037.79
FJ$157.70
List Price:  FJ$225.30
You save:  FJ$67.59
Q542.52
List Price:  Q775.06
You save:  Q232.54
GY$14,601.52
List Price:  GY$20,860.22
You save:  GY$6,258.69
ISK kr9,773.40
List Price:  ISK kr13,962.60
You save:  ISK kr4,189.20
DH703.98
List Price:  DH1,005.73
You save:  DH301.75
L1,236.34
List Price:  L1,766.28
You save:  L529.93
ден3,998.26
List Price:  ден5,712.05
You save:  ден1,713.78
MOP$561.77
List Price:  MOP$802.57
You save:  MOP$240.79
N$1,291.99
List Price:  N$1,845.78
You save:  N$553.78
C$2,569.36
List Price:  C$3,670.67
You save:  C$1,101.31
रु9,319.09
List Price:  रु13,313.56
You save:  रु3,994.46
S/260.54
List Price:  S/372.22
You save:  S/111.67
K269.79
List Price:  K385.44
You save:  K115.64
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,882.68
List Price:  ZK2,689.66
You save:  ZK806.98
L323.40
List Price:  L462.03
You save:  L138.62
Kč1,628.77
List Price:  Kč2,326.92
You save:  Kč698.14
Ft25,334.28
List Price:  Ft36,193.38
You save:  Ft10,859.10
SEK kr755.02
List Price:  SEK kr1,078.64
You save:  SEK kr323.62
ARS$61,468.17
List Price:  ARS$87,815.44
You save:  ARS$26,347.26
Bs483.33
List Price:  Bs690.51
You save:  Bs207.17
COP$271,845.87
List Price:  COP$388,367.89
You save:  COP$116,522.02
₡35,672.25
List Price:  ₡50,962.55
You save:  ₡15,290.29
L1,724.16
List Price:  L2,463.20
You save:  L739.03
₲522,510.75
List Price:  ₲746,475.93
You save:  ₲223,965.17
$U2,674.97
List Price:  $U3,821.56
You save:  $U1,146.58
zł281.37
List Price:  zł401.98
You save:  zł120.60
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.