Java singleton patterns and benefits of getinstance

Source: Internet
Author: User

1. What is a singleton mode

Simply understood, there is a class that can have only one instantiated object, which is the singleton pattern.

2. Benefits of GetInstance

First look at how to use getinstance to implement singleton mode

 Public classConnectionPool {Private StaticConnectionPool Pool; //other member variables//...    PrivateConnectionPool () {DS=NewCombopooleddatasource (); }     Public Static FinalConnectionPool getinstance () {Try{Pool=NewConnectionPool (); } Catch(Exception e) {e.printstacktrace (); }        returnPool; }    //Other methods//...}

The advantage of doing this is to save memory and prevent memory wastage. The other is to achieve the same provisioning of resources to prevent data conflicts:

In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent a communication port from being called simultaneously by two requests. In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political.

In addition, abstract classes can not be instantiated, so use getinstance

Java singleton patterns and benefits of getinstance

Related Article

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.