A singleton design pattern in Java

Source: Internet
Author: User

Design patterns are reusable solutions that have been proposed by 4 authors, called "GoF", also known as quad groups.

The design pattern is divided into: creation type, structure type, behavior type

The singleton design pattern belongs to the creation type

The three elements of a singleton pattern:

1. Variables of this type that need to have static private in the class body

2. The construction method must be private

3. Provide a public static entry point method

Lazy-public class singleton{    private static singleton st;// Think about why it's static?     private singleton () {        }     public static singleton getinstance () {         if (st==null) {            st = new  singleton ();        }         return st;    }}//a hungry man type public class singleton{     Private static singleton st=new singleton ();//Think about why it's static?     private singleton () {        }     public static singleton getinstance () {         return st;    }} 


This article is from the "Gaogaozi" blog, make sure to keep this source http://hangtiangazi.blog.51cto.com/8584103/1661146

A singleton design pattern in Java

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.