C # object-oriented design mode Lecture 2: Singleton pattern single-piece mode (Creation Mode)

Source: Internet
Author: User

(According to the msdn webcast courses)

Mode classification:

Purpose:

Creational: Responsible for object Creation

Structural: A combination of processing classes and objects.

Behavioral: Responsibility Assignment in class-object interaction

In terms of scope:

Class Mode: process the static relationship between classes and subclasses.

Object Mode: process the dynamic relationship between objects.

Classes in single-piece mode ensure that the class has only one instance in the system to ensure the correctness and efficiency of the logic.

Common method: Use a private constructor and use the read-only attribute or get method to generate a unique static instance. (Perform a double check before the Lock Object to apply to the multi-thread mode)

Method for generating new objects:

1. constructor;

2. serialization

In the. NET environment, the implementation of a more streamlined single-piece mode (compatible with multi-threaded environments ):

The disadvantage of this method is that it does not support parametric object construction. However, you can add an initiation method to complete object initialization.

Horizontal extension:

Single-piece mode can be extended to n-piece Mode

Vertical extension:

In single-piece mode, the object construction can still be carried out in new, and the logic for controlling the object generation can be placed in the constructor. Pay attention to the processing in the multi-threaded environment.

<End>

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.