Sometimes the difference between a single case pattern and a static class is not so obvious, are data-independent operations, where the whole process cannot rely too much on data--or, more accurately, object dependencies, preferably the object that handles a certain type (incoming can be an interface), and look at the following code:
Interface Ipeople {
string Name {set;}
int age {set;}
String Tostringpeople ();
}
Class Peopleserver {public
peopleserver () {} public
void Tostringpeople (Ipeople p)
{
Console.WriteLine (P.tostringpeople ());
}
So all the objects that implement the Ipeople interface can be processed by peopleserver to reduce the coupling relationship between the modules. So what is the specific difference between a static class and a single case pattern?
Again, the advantage of the single example pattern is that DAO initialization compares the utilization of system resources, and if you continue to initialize and release resources through static methods, it is better to use the single example mode if you do not involve complex transaction management. and its easy to use, initialization flexibility, personal feeling it better than static class.
In the example of a code:
<summary>///site pseudo URL information class///</summary> public class Siteurls {#region internal properties and methods private static obj
ECT Lockhelper = new Object ();
Private static volatile siteurls instance = null;
String siteurlsfile = Utils.getxmlmappath (dtkeys.file_site_xml_confing);
Private ArrayList _urls;
Public ArrayList URL {get {return _urls}
set {_urls = value;}
Private NameValueCollection _paths;
Public NameValueCollection Paths {get {return _paths;}
set {_paths = value;}
Private Siteurls () {url = new ArrayList ();
Paths = new NameValueCollection ();
Bll.url_rewrite BLL = new Bll.url_rewrite (); list<model.url_rewrite> ls = BLL.
GetList ("");
foreach (Model.url_rewrite Model in ls) {paths.add (model.name, Model.path);
Model.page = Model.page.Replace ("^", "&"); model.querystring = Model.querystring.ReplaCE ("^", "&");
Urls.add (model); #endregion public static Siteurls Getsiteurls () {Siteurls _cache = Cachehelper.get<siteurl
S> (Dtkeys.cache_site_http_module); Lock (Lockhelper) {if (_cache = = null) {Cachehelper.insert (dtkeys.cache_
Site_http_module, New Siteurls (), Utils.getxmlmappath (dtkeys.file_url_xml_confing));
Instance = cachehelper.get<siteurls> (Dtkeys.cache_site_http_module);
} return instance; }
}
Note that this time the private ArrayList _urls can be dynamically increased in the system runtime is variable, when we update the value of Siteurls then the cache object object has been removed, when we call it again then return Instance may have changed, because the data was updated from the database and the new data was put into the cache.