@protocol与类集群 [英] @protocol vs Class Cluster

查看:137
本文介绍了@protocol与类集群的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@protocol pro 和 contra 是什么developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW34rel =nofollow>类群集 Objective-C中的概念?

What are those major pro and contra for @protocol and Class Clusters concepts in Objective-C ?

两者都介绍了松耦合的程序架构。

Both of them introduce Loose Coupling in program architecture. Are they conceptually almost equal, or is there something else worth to know ?

推荐答案

注意:不是可可专家,不相信他们是平等的。

Caveat: Not a cocoa pro, but I don't believe they are equal at all.

使用类集群


类集群是Foundation框架广泛使用的设计模式。类集群组一些公共抽象超类下的私有具体子类。以这种方式对类进行分组简化了面向对象框架的公开可见的体系结构,而不会降低其功能丰富性。类集群基于Cocoa Design Patterns中讨论的抽象工厂设计模式。

Class clusters are a design pattern that the Foundation framework makes extensive use of. Class clusters group a number of private concrete subclasses under a public abstract superclass. The grouping of classes in this way simplifies the publicly visible architecture of an object-oriented framework without reducing its functional richness. Class clusters are based on the Abstract Factory design pattern discussed in "Cocoa Design Patterns."

@protocols 另一方面,更像是Java接口。

@protocols on the other hand, are more like Java interfaces.


Objective-C扩展叫做协议非常像Java中的接口。两者都是一个方法声明的列表,发布一个任何类可以选择实现的接口。

The Objective-C extension called a protocol is very much like an interface in Java. Both are simply a list of method declarations publishing an interface that any class can choose to implement. The methods in the protocol are invoked by messages sent by an instance of some other class.

简单来说,类集群是子类/超类其中子类符合超类的整个身份,使得可以向用户隐藏该实现。这在 NSArray 的情况下是显而易见的,其中编译器使用上下文来选择要使用的最佳类型的数据结构。你不像在Java中一样调用 NSTree NSLinkedList 。您可以看到 NSNumber 如何在这里实现,特别是它所说的部分:

In short, Class Clusters are subclass/superclass where the subclass conforms to the entire identity of the superclass so that the implementation can be hidden from the user. This is apparent in the case of NSArray where the compiler uses context to choose the best type of data structure to use. You don't call NSTree or NSLinkedList like you might in Java. You can see how NSNumber is implemented here, especially the part where it says:


// NSNumber实例方法 - 调用...

// NSNumber instance methods -- which will never be called...

@protocols就像客户端/服务器关系,其中客户端类采用服务器类的协议,可以调用客户端上的功能。 < NSAppDelegate> < UIAlertViewDelegate> 是使用协议的绝佳示例。

@protocols are like client/server relationship where the client class adopts a protocol of the server class, so the server can call functionality on the client. <NSAppDelegate> and <UIAlertViewDelegate> are great examples of the use of protocols.

这篇关于@protocol与类集群的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆