A single example of the Swift design pattern (SINGLETON)

Source: Internet
Author: User

One, intent



II, usage scenario
Span style= "font-size:medium;" >1, use scene

When this unique instance should be extensible by subclasses, and the customer should be able to use an extended instance without changing the code.


2, important three steps to implement
privatization construction method (Swift not supported)


Iii. implementation of the Swift language
swift language does not support the permissions of variables and methods, there is no way to hide variables and methods, you can create an instance directly. The creation of a singleton has many formulations, Swift supports only structs that support static variables, and class does not support static variables, so it is easy to think that using a struct inside a class solves the problem of saving the reference, and the code is as follows:

    1. Class Swiftsingleton {
    2. class Var Shared:swiftsingleton {
    3. Dispatch_once (&inner.token) {
    4. Inner.instance = Swiftsingleton ()
    5. }
    6. Return inner.instance!
    7. }
    8. struct Inner {
    9. static Var Instance:swiftsingleton?
    10. static var token:dispatch_once_t = 0
    11. }
    12. }
Copy Code



Run the following test code for a simple test:

    1. Class Swiftsingletontest:xctestcase {
    2. Func Testsingleton () {
    3. Let Singleton1 = swiftsingleton.shared
    4. Let Singleton2 = swiftsingleton.shared
    5. ASSERT (Singleton1 = = = Singleton2, "pass")
    6. }
    7. }
Copy Code



Run the result, the green checkmark on the left represents the execution through:


where = = = "equivalent" in swift, comparing two variables or constant reference addresses, can only be used for class comparison


In Swift, the static type variable is automatically implemented as a lazy load mode, or it can be implemented as follows:

    1. Class Swiftsingleton {
    2. class Var Shared:swiftsingleton {
    3. Return inner.instance
    4. }
    5. struct Inner {
    6. static Let instance = Swiftsingleton ()
    7. }
    8. }
Copy Code








Iv. implementations that could cause errors
class is a very important difference from the struct:
class: The reference
struct: Value

    1. struct Swiftsingleton {
    2. var name:string = "1"
    3. static Let shared = Swiftsingleton ()
    4. }
    5. var single1 = swiftsingleton.shared
    6. var single2 = swiftsingleton.shared
    7. Single2.name = "2"
    8. println ("------->\ (single1.name)")
    9. println ("------->\ (single2.name)")
Copy Code





The printing results are as follows:

    1. ------->1
    2. ------->2
    3. Program ended with exit code:0
Copy Code




As can be seen from the above, through the implementation of the struct, we can not guarantee that there is only one instance, this implementation is problematic

A single example of the Swift design pattern (SINGLETON)

Related Article

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.