Objective-C 中所谓的“类簇”究竟是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1844158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:35:13  来源:igfitidea点击:

What exactly is a so called "Class Cluster" in Objective-C?

objective-ccocoacocoa-design-patternsclass-cluster

提问by openfrog

I was reading that NSArray is just such a thing. Sounds heavy. I have 7 really fat books here on my desk about Objective-C, Cocoa and C. None of them mention Class Cluster at all, at least I can't find it in the Index at the back of the books. So what's that?

我读到 NSArray 就是这样的东西。听起来很沉重。我的桌子上有 7 本书关于 Objective-C、Cocoa 和 C 的非常丰富的书。他们根本没有提到 Class Cluster,至少我在书后的索引中找不到它。那是什么?

采纳答案by echo

From Apple's docs.... In short it's a design pattern used in the Foundation framework, which is probably why it's not mentioned in ObjC books.

从苹果公司的文档......。简而言之,它是 Foundation 框架中使用的一种设计模式,这可能是 ObjC 书籍中没有提及它的原因。

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.

类簇是一种体系结构,它将许多私有的、具体的子类组合在一个公共的、抽象的超类下。以这种方式对类进行分组为用户提供了一个简化的界面,用户只能看到公开可见的架构。

回答by Frank C.

I don't know what is in the CDP that Steve referenced but basically the Objective-C Class Cluster is a construct that supports implementing the abstract Factorypattern.

我不知道史蒂夫引用的 CDP 中有什么内容,但基本上 Objective-C 类集群是一种支持实现抽象工厂模式的构造。

The ideais simple: You want to provide a Factory (Cluster) interface that, with minimal description, manufactures and returns a specific concrete instance of a Factory Object that satisfies the behavior of the cluster family described by the Factory (Cluster) interface.

想法很简单:你要提供一个工厂(群)接口,用最少的描述,制造并返回工厂对象的特定的具体实例满足由厂(Cluster)的接口描述的集群家人的行为。

A simple concrete example: This example provides a Laugh factory that produces concrete classes of specific laughter types (e.g. Guffaw, Giggle). Pay attention to the Laugh initWithLaughter:method.

一个简单的具体示例:此示例提供了一个 Laugh 工厂,该工厂可生成特定笑声类型(例如 Guffaw、Giggle)的具体类。注意Laugh initWithLaughter:方法。

In Laugh.h:

在 Laugh.h 中:

#define kLaughWithGuffaw  1
#define kLaughWithGiggle  2

@interface Laugh: NSObject {}
- (Laugh *) initWithLaughter:(NSUInteger) laughterType;
- (void) laugh;
@end

In Laugh.m:

在 Laugh.m 中:

@interface Guffaws:Laugh {}
- (void) laugh;
@end

@interface Giggles:Laugh {}
- (void) laugh;
@end

@implementation Laugh
- (Laugh *) initWithLaughter:(NSUInteger) laugherType {
    id instanceReturn=nil;
    ; // Removed for ARC [self release]
    if ( laughterType == kLaughWithGuffaw )
        instanceReturn = [[Guffaws alloc]init];
    else if( laughterType == kLaughWithGiggle )
        instanceReturn = [[Giggles alloc]init];
    else
        ; // deal with this
    return instanceReturn;
}

- (void) laugh {
    NSLog(@"Humbug");
}
@end

@implementation Guffaws
    - (void) laugh {
        NSLog(@"OH HA HA HOWAH HA HA HA");
    }
@end

@implementation Giggles
    - (void) laugh {
        NSLog(@"Tee hee");
    }
@end

回答by t011phr33

From programming in objective c by Stephen Kochan on page 498 in the glossary, cluster:

从 Stephen Kochan 在词汇表第 498 页的目标 c 中编程,集群:

An abstract class that groups a set of private concrete subclasses, providing a simplified interface to the user through the abstract class.

一个抽象类,将一组私有的具体子类分组,通过抽象类为用户提供一个简化的界面。

回答by quellish

Class clusters provide a single public interface to a group of concrete, private subclass implementations. An objective-c programmer uses class clusters often and rarely realizes it - and this is the whole point of a class cluster. A class cluster's job is to hide the complexity of implementation detail behind a public interface.

类集群为一组具体的私有子类实现提供了一个公共接口。Objective-c 程序员经常使用类簇,但很少意识到它——这就是类簇的全部意义。类集群的工作是将实现细节的复杂性隐藏在公共接口后面。

Many of the Foundation classes are class clusters, such as NSString, NSArray, NSDictionary, and NSNumber. When you call [NSString stringWithFormat:]the class cluster is giving you some concrete class that implements the NSStringinterface. It could be an NSConcreteString, NSCFString, NSFooBarString, etc. Which the class cluster gives you is based on the constructor or initializer you are calling and the arguments.

许多 Foundation 类都是类簇,例如 NSString、NSArray、NSDictionary 和 NSNumber。当您调用[NSString stringWithFormat:]类时,集群会为您提供一些实现NSString接口的具体类。它可以是一个NSConcreteStringNSCFStringNSFooBarString,等哪一个类簇为您提供了基于构造函数或初始化程序您调用和参数。

Because of this, class clusters are one of the most empowering concepts in Objective-C programming.

因此,类集群是 Objective-C 编程中最强大的概念之一。

  • Very easy to implement
  • Easy to change the underlying implementation without changing the code that calls it.
  • Easy to provide different concrete implementations at runtime (i.e. test resources or mock objects)
  • Because of the above, easy to test and refactor
  • 非常容易实施
  • 无需更改调用它的代码即可轻松更改底层实现。
  • 易于在运行时提供不同的具体实现(即测试资源或模拟对象)
  • 由于上述原因,易于测试和重构

If you are coming from other languages you may be familiar with the Gang of Four patterns. Class clusters have elements of both the abstract factory and the facade patterns.

如果您来自其他语言,您可能熟悉四人帮模式。类集群具有抽象工厂和外观模式的元素。

Apple's public documentation covers class clusters (and how to implement and extend them) quite extensively. Unfortunately, I have found that for many iOS developers this and other Cocoa-specific patterns are a blind spot.

Apple 的公共文档非常广泛地涵盖了类集群(以及如何实现和扩展它们)。不幸的是,我发现对于许多 iOS 开发人员来说,这个和其他 Cocoa 特定的模式是一个盲点。

Cocoa Core Competencies: Class cluster

可可核心能力:班级集群

Cocoa Fundamentals Guide: Class Clusters

Cocoa 基础指南:类簇

Examples of how to implement your own class clusters are available on GitHub

GitHub 上提供了如何实现自己的类集群的示例

回答by NSResponder

The NSArray class cluster isn't "heavyweight", it's a way for any number of implementations of an array class to be used without your code knowing or caring about the particular implementation. Under the hood, there are concrete subclasses of NSArray that are appropriate to different use cases, such as large, sparse arrays, or arrays containing a specific number of elements that are known at compile time.

NSArray 类簇不是“重量级”,它是一种在您的代码不知道或不关心特定实现的情况下使用数组类的任意数量实现的方法。在幕后,有适用于不同用例的 NSArray 的具体子类,例如大型稀疏数组或包含特定数量的在编译时已知的元素的数组。

NSArray, NSString, and NSNumber are the class clusters you'll most often encounter.

NSArray、NSString 和 NSNumber 是您最常遇到的类簇。