Home > Net >  How to design the singleton pattern?
How to design the singleton pattern?

Time:12-23

Want to be a winform version of Toast,
The preliminary design singleton

Private Toast () {
LBL=new Label ();
LBL. Name="_lblTips";
}
Public static Toast getInstance ()
{
If (toast==null) {
Toast=new toast ();
The timer=new timer ();
The timer. The Interval=duration;
The timer. Tick +=timer_Tick;
The timer. The Start ();
}
Return toast;
}
If using a static instance, members also need static? Such as this timer is designed to be static or ordinary?

CodePudding user response:

The singleton design step 1: give to private constructor.
Step 2: set the static Instance all attribute Instance
 
Public class Toast
{
Private Toast ()
{
}
Public static Toast Instance {get; }=new Toast ();

Public string Name {get; The set; }
}

Of course, if you have a multithreaded, want to consider concurrent create brings the problems such as multiple instantiation.

CodePudding user response:

Public static Toast Instance {get; }=new Toast ();
This method can also be written, the static instance set invisible, unified call getInstance method after packaging

CodePudding user response:

reference 1st floor ziqi0716 response:
the first step design singleton: give to private constructor.
Step 2: set the static Instance all attribute Instance
 
Public class Toast
{
Private Toast ()
{
}
Public static Toast Instance {get; }=new Toast ();

Public string Name {get; The set; }
}

And, of course, if you have multiple threads, want to consider concurrent create brings the problems such as multiple instantiation.


Brother, you and I asked what relation ah...

CodePudding user response:

Aniseed Hui word of 9 kinds of writing

CodePudding user response:

reference weixin_42406391 reply: 3/f
Quote: refer to 1st floor ziqi0716 response:
the first step design singleton: give to private constructor.
Step 2: set the static Instance all attribute Instance
 
Public class Toast
{
Private Toast ()
{
}
Public static Toast Instance {get; }=new Toast ();

Public string Name {get; The set; }
}

And, of course, if you have multiple threads, want to consider concurrent create brings the problems such as multiple instantiation.


Brother, you and I asked what relation ah...

This is the case, and use it every time you Toast. The Instance is the Instance you want

CodePudding user response:

reference weixin_42406391 reply: 3/f
Quote: refer to 1st floor ziqi0716 response:
the first step design singleton: give to private constructor.
Step 2: set the static Instance all attribute Instance
 
Public class Toast
{
Private Toast ()
{
}
Public static Toast Instance {get; }=new Toast ();

Public string Name {get; The set; }
}

And, of course, if you have multiple threads, want to consider concurrent create brings the problems such as multiple instantiation.


Brother, you and I asked what relation ah...

Your timer, as the Name, is a member variable, no static, because only one instance

CodePudding user response:

reference 4 floor JHDXHJ response:
writing of aniseed Hui word

Can't, too volume in a garden, so look at the garden articles are too volume, and you can't point out problems, you want to point out the problem, will definitely attack by a crowd,
Whether I said here, or somebody else in the garden himself said, will have to attack by a crowd of
https://www.cnblogs.com/willick/p/14152115.html
Have a look at the comments here, hey hey, want to say as a people, will be beaten,

As for the post, itself, you don't reply, I didn't answer, let them roll die well,
Since the reply, we still say, did not discuss what context you, if you like to see the garden, that garden is looking for people to play, the ans here only focus on context

So, regardless of what a multithreaded problem, said the problem first, static variables and instance you have relations? Static variables and instance has nothing to do, so if you want to static variables, don't need what "singleton", singleton===full name globally unique instance, discussed is the instance -- -- -- -- -- -- -- -- -- -- - a design pattern in three categories: the first kind of creational pattern, to discuss how to create and how new

So -- -- -- -- -- -- -- -- -- -- static variables need new an instance?

CodePudding user response:

Use static variables + private constructor, double lock,

CodePudding user response:

refer to 7th floor wanghui0380 response:
Quote: refer to 4th floor JHDXHJ response:
writing of aniseed Hui word

Can't, too volume in a garden, so look at the garden articles are too volume, and you can't point out problems, you want to point out the problem, will definitely attack by a crowd,
Whether I said here, or somebody else in the garden himself said, will have to attack by a crowd of
https://www.cnblogs.com/willick/p/14152115.html
Have a look at the comments here, hey hey, want to say as a people, will be beaten,

As for the post, itself, you don't reply, I didn't answer, let them roll die well,
Since the reply, we still say, did not discuss what context you, if you like to see the garden, that garden is looking for people to play, the ans here only focus on context

So, regardless of what a multithreaded problem, said the problem first, static variables and instance you have relations? Static variables and instance has nothing to do, so if you want to static variables, don't need what "singleton", singleton===full name globally unique instance, discussed is the instance -- -- -- -- -- -- -- -- -- -- - a design pattern in three categories: the first kind of creational pattern, to discuss how to create and how new

So -- -- -- -- -- -- -- -- -- -- static variables need new an instance?

The new variable need? What you said...

CodePudding user response:

Also learn Sino-British mix: by the way, have said is Toast, and what the context...

CodePudding user response:

refer to 7th floor wanghui0380 response:
Quote: refer to 4th floor JHDXHJ response:
writing of aniseed Hui word

Can't, too volume in a garden, so look at the garden articles are too volume, and you can't point out problems, you want to point out the problem, will definitely attack by a crowd,
Whether I said here, or somebody else in the garden himself said, will have to attack by a crowd of
https://www.cnblogs.com/willick/p/14152115.html
Have a look at the comments here, hey hey, want to say as a people, will be beaten,

As for the post, itself, you don't reply, I didn't answer, let them roll die well,
Since the reply, we still say, did not discuss what context you, if you like to see the garden, that garden is looking for people to play, the ans here only focus on context

So, regardless of what a multithreaded problem, said the problem first, static variables and instance you have relations? Static variables and instance has nothing to do, so if you want to static variables, don't need what "singleton", singleton===full name globally unique instance, discussed is the instance -- -- -- -- -- -- -- -- -- -- - a design pattern in three categories: the first kind of creational pattern, to discuss how to create and how new

So -- -- -- -- -- -- -- -- -- -- static variables need new an instance?

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related