iOS common design Patterns: MVC, Singleton, Agent, observer.

Source: Internet
Author: User

Mvc

Model-View-controller (MVC) design pattern

MVC divides classes by role, involving three roles:

Model: Models Save the application's data.

View: Views are visual representations of the model and controls for user interaction.

Controller: Controllers are intermediaries who coordinate all work. It accesses the data in the model and displays them in the view, while they also listen for events and manipulate data.

Single Case design mode

The singleton design pattern ensures that there is only one instance (object) for a class, and that the object has a global access point. Lazy loading is usually used to create the object the first time it is used.

How to create a single case:

Such as: Declare the class method in SortDetailsModelDown.h + (Sortdetailsmodeldown *) Sharesortdetailsmodeldown;

Implement this method in SORTDETAILSMODELDOWN.M

Static Sortdetailsmodeldown * single = nil;

+ (Sortdetailsmodeldown *) sharesortdetailsmodeldown{

@synchronized (self) {

if (!single) {

single = [[Sortdetailsmodeldown alloc]init];

}

}

return single;

}

Agent

Custom protocols:

Agents: Objects that follow the agreement

First step: Make Agreement: (Protocol Name: class name +delegate)

@class MyView; @protocol myviewdelegate <NSObject> @required//methods that must be implemented-(void) Changeviewbackgroudcolor: (MyView * ) view, @optional//optional Method-(void) test; @end

Step two: Make a proxy

@interface Myview:uiview Step Two: Specify the proxy @property (nonatomic,assign) ID Delegate ; @end

The third step: the agent follows the agreement.

The fourth step: proxy implementation of the Protocol must be implemented in the method and other optional methods.

Fifth step: The client notifies the agent to start the execution method.

Observers

KVO Key-value observing. It is a mechanism that KVO automatically notifies the appropriate observer when the properties of the specified object have been modified.

  1. Registered Observer[message addObserver:self forKeyPath:kKVOPathKey options:NSKeyValueObservingOptionNew context:Nil];
  2. Change the value of the Theme object property, which is the notification that triggered the change sent_message.key = @"asdfasd";
  3. in the developed callback function, process the received change notification
    12345 6 -( void void *) context { &NBSP; if ([ KeyPath Isequal:kkvopathkey] &amp;&amp; Object = = _message) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; NSLog (@ "Get%@" ,change); &NBSP; } }
  4. Logout Observer[_message removeObserver:self forKeyPath:kKVOPathKey];

iOS common design Patterns: MVC, Singleton, Agent, observer.

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.