Design Mode (2)-Singleton)

Source: Internet
Author: User

(3) static and Singleton Modes

Because the storage location of static data members is fixed, actually, using pointers to access different instances of the same static data member accesses the same memory address. That is to say, accessing different instances of the same static data member actually accesses the same instance (single instance ).

Main. cpp the following code example: s1, s2, s3, s4, and s5 are different instances with different memory types. But modify s1-> test, s4-> test, and modify the static test values of s3, s4, and s5 instances. Essentially, s1-> test, s2-> test, s3-> test, s4-> test, and s5-> test are "same" instances.

[Html]
// Test2
QDebug () <s1-> test; // s1-> test = 1
S2-> test ++;
QDebug () <s1-> test; // s1-> test = 2
QDebug () <s2-> test; // s2-> test = 2
Singleton * s4 = new Singleton;
QDebug () <s4-> test; // s4-> test = 1
Singleton * s5 = new Singleton;
QDebug () <s5-> test; // s5-> test = 1
 
S1-> test ++;
S4-> test ++;
QDebug () <s1-> test; // s1-> test = 3
QDebug () <s2-> test; // s2-> test = 3
QDebug () <s3-> test; // s3-> test = 3
QDebug () <s4-> test; // s4-> test = 3
QDebug () <s5-> test; // s5-> test = 3
// Test2
QDebug () <s1-> test; // s1-> test = 1
S2-> test ++;
QDebug () <s1-> test; // s1-> test = 2
QDebug () <s2-> test; // s2-> test = 2
Singleton * s4 = new Singleton;
QDebug () <s4-> test; // s4-> test = 1
Singleton * s5 = new Singleton;
QDebug () <s5-> test; // s5-> test = 1

S1-> test ++;
S4-> test ++;
QDebug () <s1-> test; // s1-> test = 3
QDebug () <s2-> test; // s2-> test = 3
QDebug () <s3-> test; // s3-> test = 3
QDebug () <s4-> test; // s4-> test = 3
QDebug () <s5-> test; // s5-> test = 3
 

[UML diagram]

 

Figure 1 Singleton mode UML

1 Singleton applies the Singleton mode and defines a Private Static _ instance of the Singleton type and a public _ test of the int type, and a _ name object of QString type.

2. Use the static createInstance method to create an instance.

3 defines two public methods setName () and getName ().

 

[Sample Code]

Singleton. h

[Html] view plaincopyprint? # Ifndef SINGLETON_H
# Define SINGLETON_H
 
# Include <QString>
 
Class Singleton
{
Public:
Singleton ();
 
Private:
Static Singleton * _ instance;
QString _ name;
 
Public:
Static int test;
 
Public:
Static Singleton * createInstance ();
Void setName (QString name );
QString getName ();
};
 
# Endif // SINGLETON_H
# Ifndef SINGLETON_H
# Define SINGLETON_H

# Include <QString>

Class Singleton
{
Public:
Singleton ();

Private:
Static Singleton * _ instance;
QString _ name;

Public:
Static int test;

Public:
Static Singleton * createInstance ();
Void setName (QString name );
QString getName ();
};

# Endif // SINGLETON_H
 

Singleton. cpp

[Html] view plaincopyprint? # Include <QDebug>
# Include "singleton. h"
 
Singleton * Singleton: _ instance = NULL;
Int Singleton: test = 0;
 
Singleton: Singleton ()
{
QDebug () <"construct ";
Test = 1;
}
 
Singleton * Singleton: createInstance ()
{
If (_ instance = NULL)
{
_ Instance = new Singleton;
}
 
Return _ instance;
}
 
Void Singleton: setName (QString name)
{
_ Name = name;
}
 
 
QString Singleton: getName ()
{
Return _ name;
}
# Include <QDebug>
# Include "singleton. h"

Singleton * Singleton: _ instance = NULL;
Int Singleton: test = 0;

Singleton: Singleton ()
{
QDebug () <"construct ";
Test = 1;
}

Singleton * Singleton: createInstance ()
{
If (_ instance = NULL)
{
_ Instance = new Singleton;
}

Return _ instance;
}

Void Singleton: setName (QString name)
{
_ Name = name;
}


QString Singleton: getName ()
{
Return _ name;
}
 

Main. cpp

[Html] view plaincopyprint? # Include <QDebug>
# Include "singleton. h"
 
Int main (void)
{
Singleton * s1 = Singleton: createInstance ();
Singleton * s2 = Singleton: createInstance ();
 
If (s1 = s2)
{
QDebug () <"s1, s2 are the same instance ";
}
 
 
S1-> setName ("zhangsan ");
QDebug () <s1-> getName ();
QDebug () <s2-> getName ();
S2-> setName ("lisi ");
QDebug () <s1-> getName ();
QDebug () <s2-> getName ();
 
// Test1
Int I;
I = s1-> test;
I = Singleton: test;
S1-> createInstance ();
 
Singleton * s3 = new Singleton;
I = s3-> test;
 
// Test2
QDebug () <s1-> test; // s1-> test = 1
S2-> test ++;
QDebug () <s1-> test; // s1-> test = 2
QDebug () <s2-> test; // s2-> test = 2
Singleton * s4 = new Singleton;
QDebug () <s4-> test; // s4-> test = 1
Singleton * s5 = new Singleton;
QDebug () <s5-> test; // s5-> test = 1
 
S1-> test ++;
S4-> test ++;
QDebug () <s1-> test; // s1-> test = 3
QDebug () <s2-> test; // s2-> test = 3
QDebug () <s3-> test; // s3-> test = 3
QDebug () <s4-> test; // s4-> test = 3
QDebug () <s5-> test; // s5-> test = 3

Return 0;
}
# Include <QDebug>
# Include "singleton. h"

Int main (void)
{
Singleton * s1 = Singleton: createInstance ();
Singleton * s2 = Singleton: createInstance ();

If (s1 = s2)
{
QDebug () <"s1, s2 are the same instance ";
}


S1-> setName ("zhangsan ");
QDebug () <s1-> getName ();
QDebug () <s2-> getName ();
S2-> setName ("lisi ");
QDebug () <s1-> getName ();
QDebug () <s2-> getName ();

// Test1
Int I;
I = s1-> test;
I = Singleton: test;
S1-> createInstance ();

Singleton * s3 = new Singleton;
I = s3-> test;

// Test2
QDebug () <s1-> test; // s1-> test = 1
S2-> test ++;
QDebug () <s1-> test; // s1-> test = 2
QDebug () <s2-> test; // s2-> test = 2
Singleton * s4 = new Singleton;
QDebug () <s4-> test; // s4-> test = 1
Singleton * s5 = new Singleton;
QDebug () <s5-> test; // s5-> test = 1

S1-> test ++;
S4-> test ++;
QDebug () <s1-> test; // s1-> test = 3
QDebug () <s2-> test; // s2-> test = 3
QDebug () <s3-> test; // s3-> test = 3
QDebug () <s4-> test; // s4-> test = 3
QDebug () <s5-> test; // s5-> test = 3

Return 0;
}
 
 

[Running result]

[Html] view plaincopyprint? Construct
S1, s2 are the same instance
"Zhangsan"
"Zhangsan"
"Lisi"
"Lisi"
Construct
1
2
2
Construct
1
Construct
1
3
3
3
3
3
Construct
S1, s2 are the same instance
"Zhangsan"
"Zhangsan"
"Lisi"
"Lisi"
Construct
1
2
2
Construct
1
Construct
1
3
3
3
3
3
 
S1 and s2 are the same instance. When s2-> setName is called to modify s2, S1.

 

[Instance profiling]

Instance 1

Perform related operations on SQL in QT in the singleton mode. Description in sqlite3 database.

1. Call the following code when establishing a connection:

[Html] view plaincopyprint? Bool initSql: createConnection ()
{
QSqlDatabase db = QSqlDatabase: addDatabase ("QSQLITE ");
Db. setDatabaseName (dbName );
If (! Db. open ())
{
QMessageBox: warning (0, QObject: tr ("Database Error"), db. lastError (). text ());
Return false;
}
Return true;
}
Bool initSql: createConnection ()
{
QSqlDatabase db = QSqlDatabase: addDatabase ("QSQLITE ");
Db. setDatabaseName (dbName );
If (! Db. open ())
{
QMessageBox: warning (0, QObject: tr ("Database Error"), db. lastError (). text ());
Return false;
}
Return true;
}
 

2. call:

[Html] view plaincopyprint? Void initSql: closeConnection ()
{
QString name;
{
Name = QSqlDatabase: database (). connectionName ();
}
 
QSqlDatabase: database (). close ();
QSqlDatabase: removeDatabase (name );
}
Void initSql: closeConnection ()
{
QString name;
{
Name = QSqlDatabase: database (). connectionName ();
}

QSqlDatabase: database (). close ();
QSqlDatabase: removeDatabase (name );
}
 
 

In this way, the advantage is that you only need to connect to the database once to get a static instance, you can operate the database anywhere in the project through this instance, instead of connecting the second time. The defect is that you cannot create more than one instance at the same time.

 

In fact, the advantages and disadvantages of the singleton mode are caused by the creation of only one instance.

Advantages are easy to understand and disadvantages are described.

Using sqlite3 to create a database, we now program to access the database. The problem is that, at the same time, the cgi program on the web page and the program written by QT must simultaneously access the database. Cgi does not use the singleton mode, while Qt uses the singleton mode. For more information about CGI operations on sqlite3, see how to use C to control sqlite3? Article.

Assume that the QT program has established a connection with the database. At this time, the instance is static. After the connection is closed, the static object is not revoked, but remains in the memory until the program ends. Www.2cto.com

QT always occupies the database when it is disconnected. The consequence is that the cgi program cannot establish a connection with the database. The solution is to first disconnect the database from Qt. Let the cgi program establish a connection. after use, the connection is closed. Then establish the connection between Qt and the database. The ideal way is to execute a connection-> operation-> disconnection process like a cgi program every time you execute an operation. The ideal is always beautiful, but not the reality.

Because the connection between Qt and data is single-instance, only one instance can be created. After the connection is disconnected, the instance is not revoked. In fact, no connection can be established with the database before exiting the program. The connection is closed. You cannot use the connection to perform any operations on the database and cannot connect to the database again until the program ends.

 

* This explanation sets an alarm for Qt database programming. When the singleton mode is applied, do not close the database connection in the view and "Give way" to other programs, then connect again.

 

Instance 2

In the article JavaMe programming serialization (9)-reconstruction of permanent data storage, this article describes how to simulate a common database. At the end of this article, we analyze the impact of the singleton mode. If you want to create multiple instances in the program, do not use the singleton mode. The encoding instance is static.

 

Author: tandesir
 

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.