u 프로토콜

n         정의

Protocols는 클래스 정의와 관련 없는 mothod선언 목록.

Protocols는 정의되어 있지 않은 객체에 메시지를 전달하고자 할 때 사용 가능

 

n         사용 범위

- 다른 사람들이 구현해야 할 mothod를 선언할 때

- 클래스는 숨긴 상태에서 object interface을 선어할 때

- 계층적으로 관련없는 클래스 간에 유사성을 가지게 할 때

 

n         Declaring Interfaces for Others to Implement

프로토콜로 선언된 메소드를 사용하려면, 프로토콜을 adopt(클래스가 프로토콜의 모든 메소드를 구현한다는 의미)하고, 그 메소드들을 구현.

- (void)mouseDown:(NSEvent *)theEvent;

- (void)mouseDragged:(NSEvent *)theEvent;

- (void)mouseUp:(NSEvent *)theEvent;

 

n         Methods for Others to Implement

- setAssistant:anObject

{

    assistant = anObject;

}

 

n         Declaring Interfaces for Anonymous Objects

id formatter = [receiver formattingService];

 

n         Non-Hierarchical Similarities

- (NSXMLElement *)XMLRepresentation;

- initFromXMLRepresentation:(NSXMLElement *)xmlString;

 

n         Formal Protocols (선언 방법)

@protocol 지시자를 사용

@protocol ProtocolName

method declarations

@end

 

n         XML을 이용한 protocol 표현이 가능

@protocol MyXMLSupport

- initFromXMLRepresentation:(NSXMLElement *)XMLElement;

- (NSXMLElement *)XMLRepresentation;

@end

 

n         주의점

- protocol은 클래스랑 연관지어 쓰는 것으로 단독으로 사용되지 않는다.

  따라서, protocol 이름은 자체의 namespace안에만 있게 되어 global하게 접근할 수 없다.

- protocol을 사용하려면 class가 그 protocol adopt해야 한다.

- protocol adopt할 클래스에서는 protocol이 선언된 헤더파일을 import 해야 한다.

 

n         Informal Protocols(Category 선언 내에 method를 선언한 것)

@interface NSObject ( MyXMLSupport )

- initFromXMLRepresentation:(NSXMLElement *)XMLElement;

- (NSXMLElement *)XMLRepresentation;

@end

              특징

              - informal protocol category와 선언방식은 똑같다.

차이점은 Catogory는 구현을 따로 카테고리로 만들어서 해야 하는 반면,

informal protocol은 구현을 클래스의method로 해 준다

 

n         Protocol Objects

런타임시 클래스는 class object method selector formal protocol protocol class

protocol object @protocol(protocol_name)지시자로 참조할 수 있다.

Protocol *myXMLSupportProtocol = @protocol(MyXMLSupport);

 

n         Adoption a Protocol(adopt는 클래스가 protocol의 모든 mothod를 구현한다는 의미)

@interface ClassName : ItsSuperclass < protocol list >

@interface ClassName ( CategoryName ) < protocol list >

@interface Formatter : NSObject < Formatting, Prettifying >

 

n         Conforming to a Protocol

-클래스가 protocol adopt하고 있는지 알아보기 위해 conformsToProtocol:메시지를 object에 보낸다.

-respondsToSelector:는 한 method이 구현되어 있는지를 알려준다.

-isKindOfClass:는 클래스가 상속 계층구조안에 속하는지 여부를 알려준다.

if ( ! [receiver conformsToProtocol:@protocol(MyXMLSupport)]  ) {

    // Object does not conform to MyXMLSupport protocol

    // If you are expecting receiver to implement methods declared in the

    //  MyXMLSupport protocol, this is probably an error

}

 

n         Type Checking

protocol 타입선언 방법은 “type_name <protocol_name> 변수형태이다.

- (id <Formatting>)formattingService;

id <MyXMLSupport> anObject;

 

Formatter *anObject;

id <Formatting> anObject;
Formatter <Formatting> *anObject;

 

n         Protocols Within Protocols

@protocol ProtocolName < protocol list >

@protocol Paging < Formatting >

id <Paging> someObject;

 

if ( [anotherObject conformsToProtocol:@protocol(Paging)] )

 

n         상호참조 문제 해결

// A 선언

#import "B.h"

 

@protocol A

- foo:(id <B>)anObject;

@end

 

// B 선언

#import "A.h"

 

@protocol B

- bar:(id <A>)anObject;

@end

 

// A, B protocol이 상호 참조

@protocol B;

 

@protocol A

- foo:(id <B>)anObject;

@end

 

 

 


Posted by 김반장78
,