The IOS app uses the template method pattern in design mode to develop sample _ios

Source: Internet
Author: User
Tags abstract

The core idea of the template method pattern is to move the invariant behavior to the superclass and remove the duplicated code in the subclass to reflect its advantages. In fact, the template method pattern is to provide a good code reuse platform.

First, let's take a quick look at the definition:

Template method pattern, which defines the skeleton of an algorithm in an operation, and delays some steps into subclasses. Template methods enable subclasses to redefine certain steps of the algorithm without altering the structure of an algorithm.

Below and show you the class structure chart:

In fact, the structure is very simple, only two layers of relationship, the core idea is to refer to the public method up the parent class. The code is not complex to implement. The following is the same as the old, to give you a simple demonstration of how the code to achieve.

The first is a protocol, equivalent to the interface in Java:

Copy Code code as follows:

Initializing the Scripting protocol
@protocol Ylsinitialscript <NSObject>

-(void) Doinit: (ylsclientinfo*) Clientinfo;

@end

An abstract class is then defined to implement the overall framework algorithm for this interface, but the specific implementation is declared as an abstract method:
Copy Code code as follows:

@interface ylsinitialscripttemplate:nsobject<ylsinitialscript>

-(ID) Initorigin: (Ylsinitialoperator *) operator;

Abstract methods, implemented by subclasses
-(void) createeverythingforfirsttime;
-(void) update;
-(nsstring*) stepmsg;

@end

Copy Code code as follows:

@implementation Ylsinitialscripttemplate

Ylsinitialoperator *origin;

-(ID) Initorigin: (Ylsinitialoperator *) operator
{
origin = operator;
return self;
}

-(void) Doinit: (ylsclientinfo*) Clientinfo
{
if ([Clientinfo Shouldinit]) {
[Self createeverythingforfirsttime];//no table, initializing
else if ([Clientinfo shouldupdate]) {
[Self update];//upgrade
}
[Origin notifystepdone:[self stepmsg]];//notify Bootstrap View Controller Refresh progress bar
}

The following 3 are abstract methods that are deferred to the subclass implementation
-(void) createeverythingforfirsttime
{
[Self doesnotrecognizeselector:_cmd];
}

-(void) update
{
[Self doesnotrecognizeselector:_cmd];
}

-(nsstring*) stepmsg
{
[Self doesnotrecognizeselector:_cmd];
return nil;
}

@end

Finally, the specific subclasses do not need to implement the Doinit () method specified in the protocol, as long as the 3 abstract methods in the abstract class are implemented:
Copy Code code as follows:

@interface Ylsservicedatainitscript:ylsinitialscripttemplate

@end

Copy Code code as follows:

@implementation Ylsservicedatainitscript

-(void) createeverythingforfirsttime
{
Concrete Logic
}

-(void) update
{
}

-(nsstring*) stepmsg
{
Concrete Logic
}

@end

Syntax is not so clear in Java, the key is to use in the abstract class
Copy Code code as follows:

[Self doesnotrecognizeselector:_cmd];

This line of code implements a Java-like abstract keyword effect
Finally, the client code that implements the call:
Copy Code code as follows:

Scripts = [Nsmutablearray new];

The scripts that need to be executed are added below
[Scripts Addobject:[[ylsshowdatainitscript new] initorigin:self];
[Scripts Addobject:[[ylsservicedatainitscript new] initorigin:self];
[Scripts Addobject:[[ylsmemberdatainitscript new] initorigin:self];
[Scripts Addobject:[[ylsbilldatainitscript new] initorigin:self];
[Scripts Addobject:[[ylsemployeedatainitscript new] initorigin:self];
[Scripts Addobject:[[ylsbackupdatainitscript new] initorigin:self];

for (int i = 0; i < [scripts count]; i++) {
[[Scripts Objectatindex:i] doinit:clientinfo];
}

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.