A single example design pattern is gradually explained

Source: Internet
Author: User

Tag: Blank target border style title

Single Case design mode:
Instance instances
Singleton Patten single case model

1. Class diagram is divided into three parts, followed by the class name, properties, methods

2. Start with << and end with >> for annotated information

3. Modifier + represents public,-on behalf of private, #代表protected, nothing is visible on behalf of the package.

4. The underlined property or method representation is static.

5. Friends who are unfamiliar with the objects in the class diagram can refer to the article: the relationship of a class in design mode.


Singleton mode should be the simplest of the 23 design patterns. It has the following elements:
? Private method of construction
A private static reference to your own instance
? A static public method that takes its own instance as the return value


Advantages of Singleton mode:
? There is only one object in memory that saves memory space.
Avoid frequent creation of destroyed objects, which can improve performance.
? Avoid multiple uses of shared resources.
? can be accessed globally.


Application scenario: Because of the advantages of single-case mode, it is a more design pattern used in programming. I've summed up what I know about scenarios that are suitable for using singleton mode:
? objects that require frequent instantiation and destruction.
? objects that are too expensive to create objects or that consume too much resources, but are often used.
? A stateful tool class object.
An object that frequently accesses a database or file.
? And all the other scenes I didn't use to ask for only one object.

Single-Case Mode considerations:
? You can only use the method provided by the Singleton class to get the Singleton object, do not use reflection, or you will instantiate a new object.
Do not break a dangerous operation that disconnects a singleton class object from a static reference in a class.
? Multithreading uses a single example when using shared resources, pay attention to thread safety issues.

Implementation of Singleton mode

In Objective-c, implementing the Singleton mode requires only the following four steps:

1. Implement a static instance for Singleton Object, initialize it, and set it to nil;

2. Implement an instance construction method (usually named Sharedinstance or Sharedmanager) to check if the static instance declared above is nil, and if so, create a new instance of this class;

3. Rewrite the Allocwithzone: method to ensure that when other people use alloc and Init to try to obtain a new instance, no new instance is generated.

4. Appropriate implementation of Allocwithzone, Copywithzone, Release and Autorelease.

The role of Dispatch_once is to execute only one block of code (^{...}) throughout the application life cycle, and it means thread synchronization. Using Dispatch_once, you do not have to use a @synchronized-like statement.

The Dispatch_once function can record whether the block of code has been called.

A single example design pattern is gradually explained

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.