[Top] Single example mode of understanding and application _ design pattern

Source: Internet
Author: User
Tags static class
Single case mode

The following is an example of using a single example pattern in Java

public class businessbll{  
    private static BUSINESSBLL BUSSINESSBLL = null;  
      
    private context;  
    Public Businessbll {  
        This.context = context;  
    }  
      
    public static BUSINESSBLL GETBUSINESSBLL {  
        if (BUSINESSBLL = = null) {  
            BUSINESSBLL = new BUSINESSBLL (context);  
        }  
        Return BUSINESSBLL  
    }  
}  

The following constructor is the key to a single example

public static BUSINESSBLL GETBUSINESSBLL {  
        if (BUSINESSBLL = = null) {  
            BUSINESSBLL = new BUSINESSBLL (context);  
        }  
        return BUSINESSBLL;  
    }  

A single case pattern is implemented in C + + as follows

Simply put, the other part uses the business logic layer only by copying the original object

How to copy.

The cdalfile here is class, Pobjfile is the object of the class, as a parameter to the function init, in the INIT function to modify the object in the single case mode

This is a single example in C + +, which is a design pattern,

As far as the business logic layer is concerned, only the business logic Layer object can be instantiated once, simply by being a new one,

In single case mode

Single case mode code public class DD {public static int icount = 0;           
              Static class ABC {public static ABC m_a; The public static ABC Initabc () {//2. Custom function Initabc encapsulates the constructor ABC () so that the constructor can only be in the//init                                           
                 The ABC () is invoked, and through the IF statement, the constructor ABC () can only be invoked once if (m_a = = null) {m_a = new ABC ();
              return m_a; Private ABC ()//1. A null constructor with no parameters and private type, guaranteed not to call {}/* Test side arbitrarily
                     Method: Print M_a Value public boolean getm_a () {System.out.println (m_a);
             return true; 
        } */}/** * @param args/public static void main (string[] args) {Abc.m_a = null;//constructor is private or protected, you cannot construct an object with the new method.Can be abc.initabc when public ();
              ABC test1 = ABC.INITABC ()//When the Initabc method is invoked, the ABC () is skipped, and the M_a value is the value//test1.getm_a () at the time of the first construction.
              ABC Test2;                                    
              Test2 = ABC.INITABC ()//When the Initabc method is invoked, the ABC () is skipped, and the M_a value is the value//test2.getm_a () at the time of the first construction.
       System.out.println ("Nihao"); }
}


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.