25-Dark Horse programmer------OC Language Learning Notes---protocol

Source: Internet
Author: User

Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Protocols are used to define the specifications that multiple classes should follow, and the protocol does not provide any implementations. The agreement embodies the design philosophy of standardizing and realizing separation. The protocol is used to declare a large stack of methods and cannot declare member variables. As long as a class adheres to this protocol, it is equivalent to having all the method declarations in this agreement. As long as a class adheres to this protocol, it is equivalent to having all the method declarations in this agreement.
1, the format of the Agreement;
@protocol Agreement name < Other protocol name 1, other protocol name 2>
Method declaration List ....
@end
2. The keyword of the method declaration in the agreement
1> @required (default) requires implementation, and if not implemented, a warning is issued
2> @optional does not require implementation,
3, the type of compliance with the agreement format:
@interface Class Name: Parent class name < protocol name 1, protocol name 2>

@end

//protocol1.h, Inherits NSObject Protocol, advises each agreement to abide by the agreement @protocol Protocol1 <NSObject>      @required require implementation, do not implement will be issued a warning, the default is//@optional do not require implementation-(void) eat;    @optional-(void) test3;    @end//protocol2.h @protocol Protocol2 <NSObject>-(void) cry; @end//person.h, as long as a class complies with a certain agreement, it is able to have all the methods in this Agreement declared//: Inherit parent class//<> Compliance Agreement @interface Person:nsobject <protoco  l1,protocol2> @end//person.m @implementation Person-(void) Eat {NSLog (@ ' person eat ');  }-(void) Cry {NSLog (@ "person cry"); } @end 

If you need to use protocols to define variables, there are two methods:
nsobject< Protocol 1, Protocol 2...> *obj;
id< protocol 1, Protocol 2...> *obj; The properties declared in the
@property can also be used as a compliance restriction:
@property (Nonatomic, Strong) class name < protocol 1, Protocol 2...> * attribute name;
@property (nonatomic, Strong) id< protocol 1, Protocol 2...> attribute name;
4     Proxy design mode
1, Agent Design principle: A class does not want to implement some methods themselves, define member variables or properties, the member variable implements these methods, you can call the method through the member variable. The object that this member variable points to is called the proxy object.
2, Design principles:
    (1) to have a proxy object properties
    (2) What are the methods of the agent
    (3) to be able to decouple
3, implement the solution:
    (1) defines a protocol that declares some method of communication with the agent
    (2) has a Proxy property ID delegate
    (3) Let the agent follow protocol

TicketDelegate.h statement Some errands method @protocol ticketdelegate <NSObject>//Return fare-(double) ticketprice;    How many tickets are left-(int) leftticketsnumber;  @end//agent.h, proxy object @interface agent:nsobject <TicketDelegate> @end @implementation Agent//AGENT.M Number of votes left-(int) Leftticketsnumber {//...  Run the cinema in person or call return 1; }//How much does each ticket cost-(double) Ticketprice {//...  Run the cinema in person or call return 1000; } @end//nextagent.h, proxy object @interface agent:nsobject <TicketDelegate> @end @implementation Agent//n  EXTAGENT.M//Number of votes left-(int) Leftticketsnumber {return 500;  }//How much does each ticket cost-(double) Ticketprice {return 10;    } @end//person.h @interface person:nsobject-(void) buyticket;    Have a proxy attribute//ID on behalf of the agent's class name casually//But must comply with the Ticketdelegate protocol @property (nonatomic, retain) id<ticketdelegate> delegate; @end//person.m @implementation person//Buy movie ticket-(void) Buyticket {//Call an agent to help yourselfBuy tickets (ask about fares, ask for the remaining number of tickets) Double price = [_delegate Ticketprice];            int number = [_delegate leftticketsnumber];  NSLog (@ "Through the agent's help, the fare =%f, there are%d tickets left", price, number);      }-(void) Dealloc {[_delegate release];  [Super Dealloc];      } @end int main (int argc, const CHARCHAR * argv[]) {//human person *p = [[Man alloc] init];  Proxy Agent *a = [[Agent alloc] init];    The first agent nextagent *na = [[Nextagent alloc] init];            The second agent//set the agent of the person p.delegate = A;            People are going to see a movie [P Buyticket];            Set the agent of the person p.delegate = na;          People are going to see a movie [P Buyticket];      [A release];      [Na release];        [P release];  return 0;   }

  

25-Dark Horse programmer------OC Language Learning Notes---protocol

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.