Common design Patterns in iOS

Source: Internet
Author: User

One, single case mode

1. What is a singleton mode?

In the life cycle of an iOS app, there is only one instance of a class.

2. What problem does the singleton mode solve?

Imagine, if we want to read the file configuration information, then every time we want to read, we will create a file instance, and then we can get the relevant configuration information inside, so if we want to read the configuration information of this file more than once, it is necessary to create multiple instances, which seriously wasted memory resources. In practice, when the class that we want to use may be used repeatedly, you can generally consider using a singleton pattern. This can greatly reduce the memory waste associated with creating a new instance.

3. How to implement the singleton mode

Typically encapsulates a static property and provides a way to create a static instance (a method that blindly uses GCD technology to ensure that the entire program life cycle runs only once: Dispath_once ()).

4. Application examples

    • UIApplication: Provides centralized control points for the application to maintain the state of the application.
    • Nsuserdefaults: Read the app settings item.
    • Nsnotificationcenter: Provides information broadcast notifications.
    • Nsfilemanager: Accesses the common operation of the file system.
    • NSBundle: Dynamically load or unload executable code, locate resource files, localize resources, and Access file systems.

[[UIApplication sharedapplication] Openurl:[nsurl urlwithstring:@ "www.baidu.com"  *filemanager = [Nsfilemanager Defaultmanager];

Second, the Commission model

1. What is a delegate mode?

    • Basic Framework class + protocol + delegate Object
    • The seemingly powerful and difficult to maintain classes, according to the function of functions to extract it into a protocol, entrusted to other objects to help themselves implement the method of the Protocol

2. What problem does the delegate model solve?

In order to reduce the complexity and coupling degree of an object, the main framework class can be generalized, and the other methods of the next branch stub are left to the entrusted object to realize.

3. Implementation principle of the entrustment mode

    • The Framework class keeps a reference to the delegate object through the delegate property and sends a message to the delegate object at a specific time to notify it to do something.
    • The delegate object must meet two conditions: 1. Follow Agreement 2. Set as a delegate for a framework class

4. Application examples

Uitextfielddelegate

  #import   "  ViewController.h   "  @interface  Viewcontroller () <uitextfielddelegate> @property (strong, nonatomic) Uitextfield  *textfield;   @end   @implementation   Viewcontroller -(void  ) viewdidload {[Super viewdidload];  //  do no additional setup after loading the View, typically from a nib.   Self.textfield.  delegate  = self;}   @end  

Third, the Observer pattern

1. What is Observer mode?

The observer pattern is also called the Publish/subscribe mode. For example, subscribe to the weather forecast, which has the following three characters:

    • Meteorological bureau
    • China Mobile SMS Center
    • Mobile users

The first step: mobile phone users to subscribe to the China Mobile SMS Center weather forecast business.

Second step: When it rains, the Meteorological Bureau issued a message to China Mobile Message Center: "Rain"

The third step: mobile phone users will receive China Mobile Message Center information: "There is rain", and then users will know what should be taken action: "Go out with Umbrella"

The communication between the Meteorological Bureau and the user is anonymous, the user only knows the short interest of China Mobile, and does not know the existence of the Meteorological Bureau.

2. What problem does the Observer pattern solve?

Eliminate the coupling between objects with different behaviors, and through this pattern, different objects can work together, and they can also be reused elsewhere

3. How the Observer pattern is implemented

Common design Patterns in iOS

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.