Application of the appearance pattern in the example analytic design pattern in the development of IOS app _ios

Source: Internet
Author: User

Appearance mode (façade) provides a consistent interface for a set of interfaces in a subsystem that defines a high-level interface that makes this subsystem easier to use.

Here to show you the structure of the class diagram, we must see that we understand:

In fact, this pattern, there is no class and the inheritance between the relationship, but only a simple class reference, unified the external interface. Does it look very simple? Nonsense not much said, the following simple to show you the code!

Note: All of the code in this article is compiled in an arc environment.

Subsystemone class Interface

Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface Subsystemone:nsobject
-(void) MethodOne;
@end


Subsystemone class implementation
Copy Code code as follows:

#import "SubSystemOne.h"

@implementation Subsystemone
-(void) methodone{
NSLog (@ "subsystem method one");
}
@end


Subsystemtwo class Interface
Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface Subsystemtwo:nsobject
-(void) methodtwo;
@end


Subsystemtwo class implementation
Copy Code code as follows:

#import "SubSystemTwo.h"

@implementation Subsystemtwo
-(void) methodtwo{
NSLog (@ "Subsystem Method II");
}
@end


Subsystemthree class Interface
Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface Subsystemthree:nsobject
-(void) methodthree;
@end


Subsystemthree class implementation
Copy Code code as follows:

#import "SubSystemThree.h"

@implementation Subsystemthree
-(void) methodthree{
NSLog (@ "Subsystem method III");
}
@end


Subsystemfour class Interface
Copy Code code as follows:

#import <Foundation/Foundation.h>

@interface Subsystemfour:nsobject
-(void) methodfour;
@end


Subsystemfour class implementation
Copy Code code as follows:

#import "SubSystemFour.h"

@implementation Subsystemfour
-(void) methodfour{
NSLog (@ "subsystem method four");
}
@end


Facade class Interface
Copy Code code as follows:

#import <Foundation/Foundation.h>

@class subsystemone;//Here the function of the @class keyword is to declare (not define OH) the referenced class
@class Subsystemtwo;
@class Subsystemthree;
@class Subsystemfour;
@interface facade:nsobject{
@private Subsystemone *one;
@private Subsystemtwo *two;
@private Subsystemthree *three;
@private Subsystemfour *four;
}
-(facade*) Myinit;
-(void) MethodA;
-(void) MethodB;
@end


Facade class implementation
Copy code code as follows:

#import "Facade.h"
#import "SubSystemOne.h"
#import "SubSystemTwo.h"
#import "SubSystemThree.h"
#import "SubSystemFour.h"

@implementation Façade
-(facade*) myinit{
one= [[Subsystemone alloc]init];
two= [[Subsystemtwo alloc]init];
three= [[Subsystemthree alloc]init];
four= [[Subsystemfour alloc]init];
return self;
}
-(void) methoda{
NSLog (@ "\ n Method Group A ()----");
[One methodone];
[Two methodtwo];
[Three methodthree];
[Four Methodfour];
}
-(void) methodb{
NSLog (@ "\ n Method Group B ()----");
[Two methodtwo];
[Three methodthree];
}
@end


The Main () method calls the
Copy Code code as follows:

#import <Foundation/Foundation.h>
#import "Facade.h"

int main (int argc,const char * argv[])
{
@autoreleasepool {
Façade *facade = [[Façade alloc]myinit];
[Façade MethodA];
[Façade methodb];
}
return 0;
}


In the development of software, consider the use of the appearance of the pattern is generally divided into three kinds of situations. In the first case, in the initial stage of design, it is necessary to have a conscious separation of two separate layers to create a façade between layer and layer, which can provide a simple interface for complex subsystems, thus greatly reducing the coupling. In the second case, subsystems tend to become more complex in the development phase because of continuous refactoring, and an increase in façade facades can provide a simple interface to reduce dependencies between them. In the third case, when maintaining a large legacy system, it is possible that the system is already very difficult to maintain and expand, and if there are new requirements, a façade class for the new system can be developed to provide a relatively clear and simple interface for the design of rough or highly complex legacy code. Let the new system interact with the façade object, and the façade interacts with the legacy code for all the complex work so that it can maintain a low degree of coupling.

Instance Advanced
you currently have persistencymanager to store album data locally, httpclient to handle remote communications. The other classes in the project are not related to these logic.

Execute this pattern, only libraryapi to save instances of Persistencymanager and httpclient. After that, LIBRARYAPI will expose a simple API to access these services.

Libraryapi will be exposed to other code, but it hides the complex parts of HttpClient and Persistencymanager in the APP.

Open LibraryAPI.h and introduce the face file at the top:

#import "Album.h"
Next, add the following methods under LibraryAPI.h:

Copy Code code as follows:

-(nsarray*) getalbums;
-(void) Addalbum: (album*) Album atindex: (int) index;
-(void) Deletealbumatindex: (int) index;

Now, these methods are exposed to other classes.

The following two files were introduced in the LIBRARYAPI.M file:

#import "PersistencyManager.h"
#import "HTTPClient.h"
Only in this place will you need to introduce these classes. Remember: Your API will be the only access point for your "complex" system.

Now add some private properties in the extension of your class (above @implementation)

Copy Code code as follows:

@interface Libraryapi () {
Persistencymanager *persistencymanager;
HttpClient *httpclient;
BOOL Isonline;
}
@end

Isonline is used to determine if album list data changes can be updated to the server, such as adding or deleting albums.

You now need to initialize these variables in the Init method and add the following code to the LIBRARYAPI.M:

Copy Code code as follows:

-(ID) init
{
self = [super init];
if (self) {
Persistencymanager = [[Persistencymanager alloc] init];
HttpClient = [[HttpClient alloc] init];
Isonline = NO;
}
return self;
}

This HTTP client is not really working here, it is just a demonstration of the use of the design inside the role, so isonline is always NO.

Next, add the following three methods to the LIBRARYAPI.M:

Copy Code code as follows:

-(nsarray*) getalbums
{
return [Persistencymanager getalbums];
}

-(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
{
[Persistencymanager Deletealbumatindex:index];
if (isonline)
{
[HttpClient postrequest:@ "/api/deletealbum" body:[@ (index) description]];
}
}


Take a look at Addalbum:atindex:. This class first updates the local data, and if it is networked, it updates the remote server. This is the strength of the design; When a class outside the system adds a new album, it doesn't know-nor does it need to know-complex internal systems.

Tip: When designing a visual class in your subsystem, remember that nothing can prevent customers from accessing these "hidden" classes. To write more defensive code, don't assume that all customers will use your appearance class in the same way.
To run your program, you will look at a screen of blank content on a black background, as follows:

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.