Design Patterns Learning Notes (14: Single-piece mode)

Source: Internet
Author: User

1.1 Overview

ensure that a class has only one instance and provides a global access point to access it. This is the definition of a single-piece pattern.

In some cases, we may need a class to create only one object, that is, to not let the user instantiate more than two instances with that class.

For example, in an official document management system, an example of document class "Official document", need to be the official seal class as a member of their own, to show that they are an effective document file, then the system designers need to ensure that the official seal class only one instance, You cannot allow users to create the first 2 instances using the construction method of the official seal class .

A single-piece mode is a mature pattern of how to design a class that has only one instance of the class, the key of which is to set the class's construction method to private permission and provide a class method that returns a unique instance of it (static ).

Structure of the 1.2 pattern

The structure of the single-piece mode is very simple and has only one role:

(1) single-piece class (Singleton): A single-piece class can only create one instance.

The single-piece pattern structure class diagram looks like this:

The design of the single-piece class has the following characteristics:

(1) A single-piece class contains a class variable with its own declaration, which is a unique instance of a single-piece class.

(2) The construction method of a single-piece class access is private. To ensure that class variables declared in a single class are unique instances of a single class, a single class must set the access permission of the construction method to private. This makes it impossible for other classes to use objects created by a single-item class.

(3) A single-piece class is responsible for creating its own unique instance and providing a class method to access the instance. Because the construction method of a single-item class is set to private, a single-piece class must itself be responsible for creating an instance of its own declaration. In order for the user to use the unique instance of the single-piece class, a single-piece class must provide a class method so that other users can call this class method using the class name of the single class to access the unique instance of the single-item class.

1.3 single Piece advantages of the model

(1) The only instance of a single-piece class is controlled by the single-piece class itself, so it gives you a good control over when the user accesses it.

1.4 Fit using a single piece pattern of the scene

(1) When the system requires a class to have only one instance

Design Patterns Learning Notes (14: Single-piece 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.