IOS design patterns Part 3: Design Patterns

Source: Internet
Author: User

IOS design patterns Part 3: Design Patterns

Design Mode:

This design pattern provides a separate interface for a complex subsystem. Instead of exposing a group of classes and APIs, you only expose a simple and the same API.

The following picture explains this concept:

API users do not know the complexity of the subsequent systems. This mode is ideal for handling a large number of classes, especially when they are used in a complicated way or difficult to understand.

This design mode separates code using system interfaces and your hidden implementations. It also reduces subsystem operations dependent on external code. This is also useful if the class in the design pattern may change, the external class can keep the same API while changing the things behind the scenes.

For example, if one day you may want to replace your server, you do not need to change the code because your API has not changed.

How to use the design mode

Currently, you have PersistencyManager to save the album data locally and HTTPClient can handle remote communication. Other classes in project A do not need to pay attention to this logic.

To implement this design model, only libraryAPI should have instances of PersistencyManager and HTTPClient. Then, libraryAPI will expose simple APIs to access these services.

Generally, only one instance exists in the lifecycle of an application. You cannot keep too many strong reference pointers to other objects in a single instance, because you know that the application is disabled before it can be released. For example:

LibraryAPI will expose other code, but hide the complexity of HTTPClient and PersistencyManager from the rest of the application.

Open LibraryAPI. h to add the import header file:

Next, add the following method definition:

-(Void) addAlbum :( Album *) album atIndex :( int) index;

-(Void) deleteAlbumAtIndex :( int) index;

-(Void) saveAlbums;

These are all methods that you will expose to other classes.

Import the following two header files in the libraryAPI implementation file:

# Import "PersistencyManager. h"

# Import "HTTPClient. h"

This is the only place where you import these classes. Remember: Your API will be the only entry point into your complex system.

Now add some private instance variables in the implementation file after @ implementation.

@ Implementation LibraryAPI

{

PersistencyManager * persistencyManager;

HTTPClient * httpClient;

BOOL isOnline;

}

@ End

IsOnline decides whether to respond to any changes from the Update Server to the album list, such as adding or deleting an album.

1: Now you need to initialize the instance variable in the init method and add the following code:

-(Id) init

{

Self = [superinit];

If (self ){

PersistencyManager = [[PersistencyManageralloc] init];

HttpClient = [[HTTPClientalloc] init];

IsOnline = NO;

}

Return self;

}

This HTTP client does not actually use a real server, but only shows how to use the design mode. Therefore, isOnline will always be no.

2: Add the following three methods in the implementation file:

-(NSArray *) getAlbums

{

Return [persistencyManagergetAlbums];

}


-(Void) addAlbum :( Album *) album atIndex :( int) index

{

[PersistencyManager addAlbum: album atIndex: index];

If (isOnline)

{

[HttpClient postRequest: @ "/api/addAlbum" body: [album description];

}

}


-(Void) deleteAlbumAtIndex :( int) index

{

[PersistencyManagerdeleteAlbumAtIndex: index];

If (isOnline)

{

[HttpClient postRequest: @ "/api/deleteAlbum" body: [@ (index) description];

}

}

Take a look at addAlbum: atIndex:. This class updates local data for the first time, and if there is no network connection, it updates this remote server. This is the essence of the design pattern. When other types of systems add new albums, you do not need to know the complexity of subsystem implementation.

Note: When designing your subsystem in the design mode, remember nothing to prevent the client from directly accessing these hidden classes. Do not mean to guard the code. Do not think that all customers will use the design pattern to use them.

Compile and run your application. You will see a black screen.

We will need something to present the album data on the screen, which is a good use of the next design pattern. Decorative Design Mode.


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.