Learn the design mode of terrylee: single-State mode (with full code) _ ax

Source: Internet
Author: User
I am very interested in the Design Pattern recently and began to learn terrylee's. Net Design Pattern series. Article : The link below
Http://terrylee.cnblogs.com/archive/2006/06/01/334911.html

[Single-State mode] creates a class. We can only use the same instance of this class.

1 Using System;
2
3 Namespace Classlibrary_ax
4 {
5 Public   Sealed   Class Singleton
6 {
7 // Create a static Singleton instance, which is not accessible externally (different projects in the same solution)
8 Static   Readonly Singleton onlyone = New Singleton ();
9 // Private constructor cannot create instances externally
10 Private Singleton ()
11 {
12}
13 // Returns an onlyone instance through the onlyone attribute of Singleton.
14 Public   Static Singleton onlyone
15 {
16 Get
17 {
18ReturnOnlyone;
19}
20 }
21 }
22 }

[Append]
You can set:Public sealed class Singleton
Changed:Public class Singleton
Because: Constructor is marked as private, which cannot be instantiated.
Strictly speaking, it cannot be instantiated outside the class. It can be instantiated inside the class (this method can be used to implement the single-piece design mode ). Note that such classes cannot be inherited as base classes.

Reference: Interface part of epjnpe
Http://epjnpe.cnblogs.com/archive/2006/06/11/423311.html

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.