Technical Exchange, DH explanation.
In Singleton mode, a variable is usually stored in the global variable area. When creating a global variable, check whether the global variable is assigned a value,
If not, the global variable is directly returned.
UML diagram:
If you do not want to create an instance of an object, you can set the constructor to a private domain.
Let's take a look at how the code is implemented.
1 <? PHP
2 Class Phpsinglton {
3 Private $ Huangjacky ;
4 Private Static $ Singleinstance ;
5 Private Function _ Construct (){
6 $ Huangjacky = ' Thefiend ' ;
7 }
8 Static Function Getinstance (){
9 If ( ! Isset ( $ Singleinstance ))
10 $ Singleinstance = New Phpsinglton ();
11 Return $ Singleinstance ;
12 }
13 }
14 ?>
This is the PHP Singleton mode.Code. We use a static variable to act as a global variable.
The following is an example of C:
1 Public Class Csharpsingleton {
2 Private Static Csharpsingleton instance;
3 Private Csharpsingleton (){
4 }
5 Public Static Csharpsingleton getinstance (){
6 If (Instance = Null )
7 Instance = New Csharpsingleton ();
8 Return Instance;
9 }
10 }
11 Private Void Button#click ( Object Sender, eventargs E)
12 {
13 Csharpsingleton A, B;
14 // A = new csharpsingleton (); this will report an error
15 A = Csharpsingleton. getinstance ();
16 B = Csharpsingleton. getinstance ();
17 If ( = B)
18 {MessageBox. Show ( " Equals " );}
19 }
20
Now PHP and C # code are available.
What should we do? Hey, it must be implemented using our favorite Delphi:
In Delphi, if we set constructor to private, but Delphi will quietly change it back to public
And we still cannot find...
If you have any problems, you need to solve them.
Tsingleton = Class
Private
Class VaR Instance: tsingleton;
Public
Class Function Getinstance: tsingleton;
Class Function Newinstance: tobject; Override ;
Procedure Freeinstance; Override ;
End ;
We will find that the two methods in tobject are reloaded here.
Procedure Tsingleton. freeinstance;
Begin
Inherited ;
Instance: =Nil ;
End ;
Class Function Tsingleton. getinstance: tsingleton;
Begin
If Not Assigned (Instance) Then
Instance: = tsingleton. Create;
Result: = instance;
End ;
Class Function Tsingleton. newinstance: tobject;
Begin
If Not Assigned (Instance)Then
Instance: = tsingleton ( Inherited Newinstance );
Result: = instance;
End ;
In fact, it is here to determine whether static variables have been created.
Test:
Procedure Tform2.button1click (Sender: tobject );
VaR
A, B: tsingleton;
Begin
A: = tsingleton. Create;
B: = tsingleton. Create;
Showmessagefmt ('a: % 8x, B: % 8x', [INTEGER (A), INTEGER (B)]);
End ;
We can see that the object addresses after create are the same for A, B, and getinstance.
We can see that to implement the singleton mode in Delphi, The newinstance method is reloaded. Article As you can see, createinstance is the most important step in object construction.
Applicability:
- When a class can only have one instance and the customer can access it from a well-known access point.
- When this unique instance should be extensible through subclass, and the customer should be able to use an extended instance without changing the code.
Okay, so let's talk about it today. I'm DH.