A singleton pattern of design patterns

Source: Internet
Author: User

Namespace ConsoleApplication1
{

Single-Case mode
Control a class can instantiate only one object
Access only one object per visit
Do a data access class
Class Dbda
{
public string host;
public string database;
A static member is used to store the object of the class, the object is stored in the DB, and the next control cannot recreate the object.
Directly with the object in db, so that there is only one object
public static DBDA db = null;

So that the class cannot be set to private by the instantiation constructor, it cannot recreate the object.
To create an object internally, provide a method public must be static

Private Dbda ()
{

}
Why is it static? If it's a normal object that can't be made,
Control can only create one object
public static Dbda Duixiang ()
{
if (db = = null)
{
db = new Dbda ();
}
return DB;
}
}

Class Program
{
static void Main (string[] args)
{
DBDA db = Dbda.duixiang ();
Db.host = "HIHK";
Dbda db1 = Dbda.duixiang ();
Console.WriteLine (Db1.host);
Console.ReadLine ();//output results with DB is a result,

}
}
}

A singleton pattern of design patterns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.