A single example design pattern is gradually explained

Source: Internet
Author: User

A Singleton class Returns the same instance no matter how many times an application requests it. The singleton design returns only the same instance at a time; A typical class permits callers to create as many instances of the class as they want, requesters can create many instances as long as they want. ; Whereas with a singleton class, there can is only one instance of the class per process. A singleton object provides a global point of access to the resources of its class. Singletons was used in situations where this single point of control was desirable, such as with classes the offer some GE Neral Service or resource.

You obtain the global instance from a singleton class through a factory method. The class lazily creates its sole instance the first time it's requested and thereafter ensures that's no other instance CA n be created. A Singleton class also prevents callers from copying, retaining, or releasing the instance. The Singleton class does not allow the user to make modifications to the copy and so on; You might create your own singleton classes if you find the need for them. You can do it yourself by creating a singleton class that you think is necessary; for example If you had a class that provides sounds to other objects in an application, you might make it a singleton.

Several Cocoa Framework classes is singletons. They include Nsfilemanager, Nsworkspace, and, in UIKit, UIApplication and Uiaccelerometer. The name of the factory method returning the singleton instance have, by convention, the form Sharedclasstype. Examples from the Cocoa frameworks is Sharedfilemanager, Sharedcolorpanel, and SharedWorkspace.

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.