An explanation of design patterns such as MVC in iOS

Source: Internet
Author: User
Tags notification center uikit

An
explanation of design patterns such as MVC in iOS

  • In iOS programming, using design patterns can greatly improve your development efficiency, although you will need to spend more time wrapping up the various business logic at the beginning of writing code. (It turns out to be worth it!)

    Model-View-controller (MVC) Design patterns are widely known and used by people, in fact, in mobile development, especially in the development of iOS, this model is played to the fullest

  • The MVC design pattern consists of three parts: models, views, and controllers.
  • Model An iOS app that contains data, information, logic, or an object that is considered part of the business layer.
  • View a component that contains all the user information, such as a text area, a button, a slider, is considered an iOS app for the presentation layer.
  • Controller is the contact, or communication layer, of an iOS app. A model will never communicate directly to a view, and vice versa. The Controller connection Model view handles all traffic.
  • User behavior occurs in a view, such as a button click. The view sends the message to the controller. If necessary, the controller and the model. The controller returns to the view through information. Using an imaginary Calculator application as an example, the MVC design pattern will be applied as follows:

    • model -COMPUTE function (add, subtract, multiply, divide).
    • View -user interface components (operand and operator buttons, calculated text).
    • controller --objects, listening and communicating information models and views. When the Equal button is selected, for example, the controller makes the request model perform mathematics. The model returns the answer to the controller, which communicates the resulting view in a timely manner. The view is responsible for displaying the answer.
    • MVC is a good design for any iOS application or the core of a Mac application. The benefits of adopting this model are manifold. Many of the objects in these applications are often reusable and often get better defined and their interfaces. MVC-designed applications are also easier to extend than other applications. In addition, many of the technologies and architectures your application can use are based on MVC and require your custom objects to play an MVC role.
    • You may not be aware of this, but you have created an application that is based on MVC: HelloWorld in your iOS app. The model object is the 用户名 property ( NSString的 object) Declaration and HelloWorldViewController the management class. The HelloWorldViewController and class instances of the application's HelloWorldAppDelegate controller object are the text fields of the application's view object, the label, the button, and the background image.

      Model-View-controller's complete information "model-View-controller" concept in OBJECTIVE-C programming.

      Model objects

      A model object encapsulates the logic and computation of an application-defined data operation and processing data. For example, a model object may represent a character in a game or a contact in the Address Book. Sometimes, the model layer of an application is actually one or more graphics-related objects. Most of the data, the persistence state of some applications (whether the persisted state is stored in a file or database) should reside after the data is loaded into the application's model object. Because model objects represent knowledge and expertise related to a specific problem domain, they can be reused in similar problem domains. There should be a "purely" model object for the View object, current its data, and allow the user to edit the data, it should not be concerned about the user interface and demonstrate the problem without a clear connection.

      User actions that create or modify data at the view level are communicated by creating or updating model objects through a controller object and results. When a model object changes (for example, a new receive data is connected over a network), it notifies the controller object that the corresponding view object is updated.

      View objects

      A View object is an object that the user can see in an application. A View object knows how to draw its own actions that might respond to the user. One of the view objects?? The primary purpose is to display data from the application's model object, which can be made to edit the data. Nonetheless, the View object Model objects in an MVC application are usually decoupled.

      Because you typically reuse a view object and reconfigure it, the view object provides consistency between the applications. For iOS, the Uikit framework provides a collection of view classes, and a similar collection for the framework of OS X AppKit. Uikit the View object 的UIView class, eventually inheriting the AppKit, which eventually inherits from the NSView的 class.

      The View object communicates through the application's controller object model data changes, and understands user-initiated changes, such as text in a text field that is entered through the Controller objects application model object.

      Controller Object

      The Controller object acts as a mediator between one or more application view objects and one or more model objects. Therefore, the pipe of the Controller object View object understands the changed model object and vice versa. The Controller object can also perform an application's installation and coordination of tasks and other object lifecycle management.

      The Controller object interprets the actions in the user view object and the new or changed data Model layer. When the model objects change, the controller objects communicate, and the new model data View objects so that they can display it.

      Design patterns to solve problems

      An object-oriented system, as an application, is dynamic. An object can be run at runtime and is not limited to the behavior set at compile time. An object can send messages to other objects, and can be different than the same message in the run-situation target. An object can also be associated with one of the other objects?? Variable group at run time, using a variety of techniques, can effectively complete the work of the application. For a network of objects, or objects, to do this, it must take advantage of the design patterns of many technology and framework architectures to be adaptable.

      The following sections describe many of these technologies and architectures. Consider part of their OBJECTIVE-C programming toolkit.

      Delegation: Agent on behalf of another object

      Delegation, an object invocation represents the delegate behavior, and requires another object. Other, delegated, an object is usually a frame object. At some point during execution, it sends a message to its delegate message to tell some events about the upcoming delegate, and requires some response. A delegate (typically an instance of a custom class) implements a message that invokes the method and returns an appropriate value. Typically, the value is a Boolean value that tells the delegate whether the object continues the action.

    • Therefore, a delegation is a means of injecting application-specific behavior into the workings of a framework that does not require subclass class classes. It is a common and powerful design to extend and influence the behavior of the framework.

      Recall the HelloWorldAppDelegate object of the HelloWorld application when you create a job through your first iOS app. The automatic assignment of Xcode, which is a delegate to the Application object, is a framework object. The application delegate can process 应用:的didFinishLaunchingWithOptions: and send messages from other delegations to its Application object.

      There are two programmatic components of the delegation. The delegate class must define a property (by convention, named 委托 ) that the delegate holds the reference to. It must also declare a protocol that must take the delegate class (see the partial protocol below). Many classes of cocoa touch and cocoa frameworks provide the delegation as a way to application something with a particular application that can be taken to increase the framework's behavior.

      However, the delegation was not limited to framework classes. You can implement a delegation between two custom objects in an application. In a cocoa touch application use a common design delegation that allows a child view controller to communicate some of the values of its parent view controller (typically a value entered by the user) as a means.

      Protocol: Enable inheritance relationships between communication objects

      A protocol is a method of declaring a program interface that can be implemented by any class. An instance of the class associated with the Protocol calls the Protocol of the method and obtains a return value class that formally passes and implements the protocol. This specific goal, such as parsing XML code or copying objects, further strengthens the communication between objects. Objects on either side of the Protocol interface can be remotely interconnected, through inheritance. The agreement is therefore that the delegation, a subclass of substitution, is often part of a framework for the implementation of the delegation.

    • The framework that Apple provides to declare the dozens of agreement. In addition, your application can declare that your class can take a custom protocol. The protocol is part of your programming toolkit. Programming with the Objective-c language, gives a comprehensive description of the protocol.

       

       

      Notification hubs: Notifies an observer about an event

      The notification hub of the foundation framework is a subsystem that broadcasts messages to the observers that notify all objects in an application to register events. (Programmatically, this is an instance of the Nsnotificationcenter class.) The event can occur in any application where the application enters the background state, for example, or the user starts typing in a text field. An opportunity to notify an observer that the event has occurred or is about to occur, thus allowing the observer to respond in an appropriate manner. Notification hubs broadcast notifications are a way to increase collaboration and cohesion between an Application object.

    • For example, the view controller in the iOS application can observe the notifications to UIKeyboardWillShowNotification adjust the geometry of its view to fit the virtual keyboard. This example shows that a notification is an object that has a name that indicates whether a particular event has occurred or is about to occur. It also comes with a reference object to the post (or send) notification, Notification Center, which can contain a dictionary of supplemental information.

      Any object can observe a notification, but to do so, it must be registered to receive it. At registration, you must specify a selection that determines the delivery notification of the method to invoke, and the signature of the method must have one parameter: the notification object. When registering, the observer can also specify the publication object.

      The Notification Center notifies the delegation that a message was sent to an arbitrary object when a specific event occurred. However, the method that handles the notification, the delegate method differs, cannot return a value. Notifications through notification hubs are synchronized, just like the delegation.

      The custom objects in your application can define and publish their own notifications, as well as other custom objects in your application that can be observed by notifications.

      Target action: Message to be sent when an event occurs encapsulation

      The concept of targer-action design is simple. An object stores elements that allow the message to be expressed, when an event occurs, to group the elements together, and to send a message. The element is the throughput message (action), a selector for the object that receives the message (target). The target class method implements the corresponding action and target when it receives the message at run time, and it responds to the execution method of this event.

      Target-action is primarily a feature of the control in the cocoa touch and cocoa framework. A control is a user interface object, such as a button, a slider, or a toggle user action (by clicking, dragging, and so on) to indicate that they intend to go to an application. Cocoa Touch control stores actions and targets with one or more cell object storage targets and actions, most Cocoa touch.

    • Some frameworks use objects other than the Target-action control. For example, the Uikit framework uses the target action to identify gestures in its design. When a gesture recognizes the object recognition gesture, the action message is sent to the target object.
    • Key – Value observation: Notifies an observer when a value has changed

      Key – Value observation allows one object to be observed in one of the other objects?? Property. When you observe an object, you are notified of the change in the value of the property. Understanding the new value, as well as the old one, if the observed attribute is a pair of many relationships (for example, an array), it also learns about the changes involved in the objects contained therein. Volunteering helps the application become more cohesive, keeping the object model, controller, and view layer changes synchronized.

      Similar to the NSNotificationCenter notice, multiple volunteer observers can observe a single property. In addition, KVO is dynamic because it allows to observe any properties of the object without requiring any new APIs, such as notification names. The International Volunteer organization is a lightweight point-to-point communication mechanism that does not allow the observation of all instances of a particular property.

      Frame design based on design pattern

      The Cocoakit and cocoa frameworks also include other designs based on design patterns, including the following:

        • view hierarchies. the comments are arranged in an application that presents a visual-based hierarchy of organizational containment. This model allows the application to treat individual views and opinions of the ingredients evenly. At the root of the hierarchy is a Window object, in each view, the root directory has a parent view and 0 or more child views. Parents ' comments are attached to the child's opinion. The structure component in the view hierarchy, drawing and event handling for both.
        • response chain. The response chain is a series of objects that are mostly commented on, there are also Windows, view controllers, itself as well as the objects in which the object can be handled by an event or action message until an object in the chain handles the event. Therefore, it is a cooperative event-handling mechanism. The response chain is closely related to the view hierarchy.
        • View the controller. Although both of the Uikit and AppKit frame View controller classes, they are particularly important in iOS. A view controller is a special controller object that presents comments and manages a group. View Controller objects provide the infrastructure management content related to the views and coordinate their display and concealment. The view controller manages the application view of the child hierarchy.
        • Categories give you a way to extend a class to add methods. Like delegate, it enables you to customize the behavior without subclasses. The taxonomy is a objective-c function description that writes the OBJECTIVE-C code.

An explanation of design patterns such as MVC 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.