5

I have many sublcasses of one class. I choose concrete class based on type (simple enum). I like idea of class cluster. Should i use it or just create factory class (but each class in objective-c is actually a factory)?

What is the difference between factory pattern and class cluster pattern. When to use it in your code and why?

6

There isn't really any difference; a class cluster is a specific implementation of the Factory pattern. In the class cluster, the initialiser is used as the factory method to decide what instance to return.

Stylistically, the class cluster will be familiar to users of your API because it's common in the Apple frameworks: the collection classes, number classes and others make use of the pattern. It also satisfies the higher-level pattern of Smalltalk and Objective-C where the class object is itself a factory for its instances.

In addition the class cluster means fewer things to remember: rather than thinking "I have to make a ThingFactory then call [factory thingForCase: foo]" they just look at the initialiser for Thing, which is what you'd probably reach for by default.

There is a situation in which you would reach for a more "classic" implementation of Factory, which is (as discussed in the comment thread) where the objects you might return won't necessarily be subclasses of a common parent. In the class cluster pattern, there's a common superclass and client code treats whatever object is returned as an instance of that superclass. If this restriction in the type hierarchy is not suited to your case then a class cluster is not going to work.

  • Just bought your APPropriate Behaviour, btw :) – Denis Mikhaylov Apr 2 '13 at 9:44
  • One more question. Factory (in classic implementation) is used to create instances implementing some interface (protocol), class cluster is only suitable for creation of subclasses of abstract class. Am I right? – Denis Mikhaylov Apr 2 '13 at 9:53
  • 1
    @DenisMikhaylov that's right, class clusters are tied into a particular part of the class hierarchy as all of the cluster types are subclasses of the "factory" class. If you wanted to return general protocol-conforming objects you would use a factory. – user4051 Apr 2 '13 at 10:12
  • Here is some of the relevant documentation from Apple that may be helpful: Cocoa Core Competencies: Class cluster Cocoa Fundamentals Guide: Class Clusters – quellish Apr 15 '14 at 20:48
  • Examples for those interested in implementing class clusters: github.com/quellish/ClassClustersExamples – quellish Mar 23 '16 at 23:16
-2

The class cluster means fewer things to remember: rather than thinking "I have to make a ThingFactory then call [factory thingForCase: foo]" they just look at the initialiser for Thing, which is what you'd probably reach for by default.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.