. NET C # Monomer Mode (Singleton) _ Practical Tips

Source: Internet
Author: User
Monomer mode (Singleton) is an object that is often generated to ensure that an application operates on a global object, such as a lock on read and write to a file, a transaction rollback at the time of a database operation, and a task manager operation, which is read in a single mode.
To create a singleton pattern class, you must meet three criteria:
1: Private constructor (prevents other objects from creating instances);
2: A private variable of a monomer type;
3: Static Global Access interface

Below I write a class, in order to see is not a monomer, add a counter, if the same class, then the count of this class each call should automatically add one, instead of resetting the new object to zero:

. NET C # Monomer mode

Using System;
Using System.Threading;
public class singleton{
private int objcount=0;
Private Singleton () {
Console.WriteLine ("Create object");
}
private static Singleton objinstance = null;
public static Singleton getinstance () {
if (objinstance==null) objinstance=new Singleton ();

return objinstance;
}
public void Showcount () {
objcount++;
Console.WriteLine ("Single object was called {0} times", Objcount);
}
};


And then we'll test it:
public class consoletest{
public static void Main (string[] args) {
Console.WriteLine ("Start executing monomer mode");
for (int i=0;i<5;i++) {
Singleton.getinstance (). Showcount ();
}

Console.ReadLine ();
}
};


I performed 5 times in this main to see the results of the output:
Start to execute the monomer mode
Creating objects
A single object was called 1 times
A single object was called 2 times
A single object was called 3 times
A single object was called 4 times
A single object was called 5 times
It can be seen here that each time the same object is used, the monomer is implemented.
To test whether the monomer is below multithreading, I write a multithreaded test:
Class Apartmenttest
{
public static void Runmorethread ()
{
Thread newthread = new Thread (new ThreadStart (Threadsinglemethod));
Newthread.setapartmentstate (APARTMENTSTATE.MTA);
Console.WriteLine ("ThreadState: {0}, ApartmentState: {1},managedthreadid:{2}", Newthread.threadstate, Newthread.getapartmentstate (), newthread.managedthreadid);
Newthread.start ();
}

public static void Threadsinglemethod () {
Singleton.getinstance (). Showcount ();
}
};


Then execute Apartmenttest.runmorethread () for the For loop every time;
Then look at the output: start the monomer mode
threadstate:unstarted, Apartmentstate:mta,managedthreadid:3
Creating objects
A single object was called 1 times
threadstate:unstarted, Apartmentstate:mta,managedthreadid:4
A single object was called 2 times
threadstate:unstarted, Apartmentstate:mta,managedthreadid:5
A single object was called 3 times
threadstate:unstarted, Apartmentstate:mta,managedthreadid:6
A single object was called 4 times
threadstate:unstarted, Apartmentstate:mta,managedthreadid:7
A single object was called 5 times

According to Managedthreadid, it can be seen that different lines of access have reached the monomer, ok!

Introduction of C # 's monomer mode is finished;

Here is the code for the PHP monomer pattern:
<?php

Header ("content-type:text/html; Charset=utf-8 ");

Class singleton{
Private $ObjCount = 0;
Private Function __construct () {
Echo ("Create object }

public static function getinstance () {
static $objInstance = null;
if ($objInstance = = null)
$objInstance = new Singleton ();
return $objInstance;

}
Public Function Showcount () {
$this->objcount++;
Echo ("single object invoked". $this->objcount. ' Times }


};


For ($i =0 $i <5; $i + +)
Singleton::getinstance ()->showcount ();
?>

Please note that the above PHP code's private static variable range, do not put in the class body.
Execution results:
Creating objects
--------------------------------------------------------------------------------
A single object was called 1 times
--------------------------------------------------------------------------------
A single object was called 2 times
--------------------------------------------------------------------------------
A single object was called 3 times
--------------------------------------------------------------------------------
A single object was called 4 times
--------------------------------------------------------------------------------
A single object was called 5 times

Now let's look at the Java version:

Single Mode Java
Class singleton{
private int objcount=0;
Private Singleton () {
System.out.println ("Create object");
}
private static Singleton objinstance = null;
public static Singleton getinstance () {
if (objinstance==null) objinstance=new Singleton ();

return objinstance;
}
public void Showcount () {
objcount++;
System.out.println ("single object called" + Objcount + "Times");
}
};
Class consoletestsingleton{
public static void Main (String args[]) {
System.out.println ("Start executing monomer mode");
for (int i=0;i<5;i++) {
Singleton.getinstance (). Showcount ();
}
}
};
The base code is consistent with C # to perform the result:
Start to execute the monomer mode
Creating objects
A single object was called 1 times
A single object was called 2 times
A single object was called 3 times
A single object was called 4 times
A single object was called 5 times

Can be seen, is also a complete monomer, as long as you remember the first mentioned three points, you can grasp the model.
----------
1: Today L Travel
2: The party yesterday, compare happy
3:SL and Z are all mobile Q-nets.
4: The winter of Nothing

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.