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