The difference and advantage of single case mode and static class

Source: Internet
Author: User
Tags object object static class advantage

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.

Large-Scale Price Reduction
  • 59% Max. and 23% Avg.
  • Price Reduction for Core Products
  • Price Reduction in Multiple Regions
undefined. /
Connect with us on Discord
  • Secure, anonymous group chat without disturbance
  • Stay updated on campaigns, new products, and more
  • Support for all your questions
undefined. /
Free Tier
  • Start free from ECS to Big Data
  • Get Started in 3 Simple Steps
  • Try ECS t5 1C1G
undefined. /

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.