. NET design patterns for single-instance mode

Source: Internet
Author: User

Tell me why I studied the singleton pattern:

In the MDI WinForm program, you need each subform can only open once (at the same time only one child form instance), before the Internet to see a good variety of solutions, a variety of attempts to solve the problems I encountered, I believe many people with me, I can now tell you, simple interest mode can be solved perfectly.

Summary of the Singleton pattern:

    1. Target: Only one instance of a class exists
    2. Instances of the class are created by themselves.
    3. Because the class instance can have only one, the constructor for the class must be private
    4. A logical unit must be inside a class to return to an external class instance (object)

The following is the implementation process:

My MDI subroutine subform (User management--usermgrform)

        Private StaticUsermgrform Usrform;//child Form Instance        Private Static ReadOnlyObject Asyncroot =NewObject ();//read-only object objects//constructor Function        PrivateUsermgrform () {InitializeComponent (); }//The subform creates an instance of the method (must be static, because the instance method is not allowed to be invoked with the class instance, which is meaningless)         Public Staticusermgrform getinstance () {if(Usrform = =NULL)            {                Lock(Asyncroot)//This is a double lock used in multi-threaded{//determine if the subform instance is null                    if(Usrform = =NULL) {Usrform=NewUsermgrform (); }                }            }            returnUsrform; }//Modify the code in the subform UserMgrForm.Designer.cs file to       protected Override voidDispose (BOOLdisposing) {            if(Disposing && (Components! =NULL) ) {components.            Dispose (); }//base.  Dispose (disposing); //the original Code            Base.        Hide (); }//How to implement open subforms in the main form://User Management        Private voidMiempmgr_click (Objectsender, EventArgs e) {SysMenu.UserMgrForm.GetInstance (). MdiParent= This; Setui.setformtitle (SysMenu.UserMgrForm.GetInstance (),"User Management");//Form TitleSetui.setmdichildsize (SysMenu.UserMgrForm.GetInstance ());//set the initial size of the subformSetui.setmdiformstate (SysMenu.UserMgrForm.GetInstance ());//Set the subform stateSysMenu.UserMgrForm.GetInstance (). Show ();//Show Subform}

There are several ways to do the singleton pattern, and here's a few.

. NET design patterns for single-instance mode

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.