类簇Class cluster

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.


class cluster 是这样一种结构,即在一个公共的、抽象的父类下聚集了一系列私有的、具体的子类。这些聚集的类以这种方式给使用者提供了简单的接口,使用者只只看到公有的可视化的结构。当然,表面之下,抽象类调用最适合的私有子类来处理特殊的任务。例如,一些常见的Cocoa类被设计成 Class cluster, 包括NSArray ,NSString ,和NSDictionary.


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.)

你创建和使用这些Class cluster和其他类一样。表面上,当你创建了一个公共类的实例,其实返回的对象取决于你的调用的创建方法。(你不需要也不能选择实例的实际类)

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.

class cluster的主要益处是提升效率。

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

class cluster 的结构要权衡简便性和可扩展性。

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.

没必要创建类簇的子类。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值