IOS class cluster
来源:互联网 发布:绿标域名认证生成器 编辑:程序博客网 时间:2024/06/03 19:26
A class cluster is an architecture that groups a number of private, concrete subclasses under a public, abstract superclass. The grouping of classes in this way provides a simplified interface to the user, who sees only the publicly visible architecture. Behind the scenes, though, the abstract class is calling up the private subclass most suited for performing a particular task. For example, several of the common Cocoa classes are implemented as class clusters, including NSArray
, NSString
, and NSDictionary
. There are many ways in which they can represent their internal data storage. For any particular instance, the abstract class chooses the most efficient class to use based on the data that instance is being initialized with.
You create and interact with instances of the cluster just as you would any other class. Behind the scenes, though, when you create an instance of the public class, the class returns an object of the appropriate subclass based on the creation method that you invoke. (You don’t, and can’t, choose the actual class of the instance.)
Taking the Foundation framework’s NSString
class as an example, you could create three different string objects:
NSString *string1 = @"UTF32.txt";
NSString *string2 = [NSHomeDirectory() stringByAppendingPathComponent:string1];
NSTextStorage *storage = [[NSTextStorage alloc] initWithString:string2];
NSString *string3 = [storage string];
Each string may be an instance of a different private subclass (and in fact, on OS X v10.5, each is). Although each of the objects is of a private subclass of NSString
, it’s convenient to consider each of the objects to be instances of the NSString
class. You use the instance methods declared by NSString
just as you would if they were instances of NSString
itself.
Benefits
The benefit of a class cluster is primarily efficiency. The internal representation of the data that an instance manages can be tailored to the way it’s created or being used. Moreover, the code you write will continue to work even if the underlying implementation changes.
Considerations
The class cluster architecture involves a trade-off between simplicity and extensibility: Having a few public classes stand in for a multitude of private ones makes it easier to learn and use the classes in a framework but somewhat harder to create subclasses within any of the clusters.
A new class that you create within a class cluster must:
Be a subclass of the cluster’s abstract superclass
Declare its own storage
Override the superclass’s primitive methods
If it’s rarely necessary to create a subclass—as is the case with class clusters in the Foundation framework—then the cluster architecture is clearly beneficial. You might also be able to avoid subclassing by using composition; by embedding a private cluster object in an object of your own design, you create a composite object. This composite object can rely on the cluster object for its basic functionality, only intercepting messages that it wants to handle in some particular way. Using this approach reduces the amount of code you must write and lets you take advantage of the tested code provided by the Foundation Framework.
Prerequisite Articles
(None)
Related Articles
(None)
Definitive Discussion
- Class Clusters
- IOS class cluster
- 【iOS】类簇(class cluster)
- 【iOS】类簇(class cluster)
- iOS 类簇(Class Cluster)使用心得
- iOS抽象工厂模式-class cluster
- iOS类簇(Class Cluster)使用心得
- Class Cluster
- Class Cluster
- 类簇Class cluster
- objc’s category and class cluster 详解
- objc’s category and class cluster 详解
- objc’s category and class cluster(类簇)
- IOS-@class
- iOS @class
- cluster
- Cluster
- cluster
- Cluster
- Java JVM内存介绍与配置
- 微信红包体系设计分析
- 使用dbca建库只输出:Exception in thread ”main"
- HttpUrlConnection与HttpClient的post、get的请求网络数据的方法
- 和日期有关的sql
- IOS class cluster
- 计算机技术领域当前的主流技术及其社会需求调查报告
- [Golang]优秀开源库剖析
- webview
- PHP读取大文件
- 播放视频
- 论文修改内容,第一篇
- dubbo,zookeeper,spring 案列
- 地表最强! Unity 5.x 插件合集专用贴 - 更新日期 2015.12.06