"Categories" are, along with protocols (see my writeup under Protocol), how Objective C handles mix-ins and how it gets around the fact it does not have multiple inheritance. Protocols and categories encompass one of the more visible bits of Objective C's Smalltalk heritage, and are taken together really rather similar to a more nimble (in my opinion, anyway) equivalent of the interface )("implements") capability in java. Categories begin to feel a bit clumsy when used extensively, but some would claim you are far better off using them than you are using multiple inheritance.

Categories are specified separately from any existing class, usually in independent .h and .m files. If we were going to make a category named DemoCategory that extended a class named HypotheticalClass by defining two methods named thing1 and thing2, the syntax would look like this:

In DemoCategory.h:

@interface HypotheticalClass (DemoCategory)
    - thing1;
    - thing2;
@end;
In DemoCategory.m:
@implementation HypotheticalClass (DemoCategory) {
    - thing1 {
        puts("I AM UNUSABLE SAMPLE CODE.");
    }
    - thing2 {
        puts("THERE ARE SPIDERS BITING ME");
    }
}
When the program is run, no distinction whatosever is made between regular methods and Category-inserted methods. Category methods can access object variables natively, and are inherited the same by subclasses. So, categories can be used to let you organize and split up individual objects across multiple files, or to define a group of methods that may be inserted into more than one class in the same way, or if you for some reason want to write several different variations on how a certain group of methods is implemented and easily switch between the variations. Also, because categories are compiled separately from their home classes, you can use a category to modify the workings of a class that you do not have access to the source code of.

The really interesting thing (although this is not often done) is that Categories, once compiled, can be arbitrarily applied to and removed from a given class while the program is running. See Apple's documentation on NSBundle.


Alternately, see Category Theory.

Categories are one kind of generalized mathematical object. Many branches of mathematics can be interpreted as taking place within certain categories. Category theory then overlaps with these fields, and provides one point of unity between the various sub-fields of mathematics. From one standpoint, this represents an paradigm shift away from the Nicolas Bourbaki school of mathematics, which used set theory as the point of unification. Favoring this interpretation for a moment, we can view the shift by analogy with economics. In barter systems, it is the actual transfer of goods that forms the backbone of the economy. This is like how set theory prefers working with the data of a mathematical structure encoded into the form of a set. By this analogy, category theory is like a paper economy in that it is the flow of the thing that matters.

A category is just a bunch of 'objects' together with a bunch of 'morphisms', or ways that objects turn into each other. There are certain axioms that categories have to satisfy to prevent some pathological examples from being considered as categories. First, each object has to be able to transform into itself, or in other words, it has to have the 'identity morphism'. The second axiom is easier to explain in the particular category of computer programs, whose objects are data and whose morphism are programs. If one program can convert a company's account data into a list of profits, and another program can convert a list of profits into a record of taxes owed, then there is a program that can convert the raw account data directly into tax records by doing the first program, then the second. In category theory terms, this axiom states that any two morphisms that line up properly can be composed into a single morphism going from the source of the first to the target of the second. There are also two axioms that ensure that composition and identity morphisms behave as one would expect them. Identity morphisms can always be cancelled when they are composed with anything, and composition is always associative.*

Since a lot of mathematics deals with transformations, processes, programs, and functions that can be composed in this way, category theory appeals to such broad areas as topology, logic, computer science, and control theory. Historically, category theory arose out of a particularly abstract way of doing homology developed by Samuel Eilenberg and Saunders MacLane. MacLane particularly contributed to the popularization of category theory with his book with Garret Birkhoff on (modern) Algebra and a little Springer-Verlag tome called Categories for the Working Mathematician. This latter book formed something of a meme in mathematics: "X for the Working Mathematician" articles infrequently show up in journals.

Lest I lead you astray, MacLane and Eilenberg were quick to point out that they did not create category theory to talk about categories and morphisms, but rather to talk about functors (which are like morphisms between morphisms) and natural transformations (which are like morphisms between functors). If you'd like to learn more about categories, a word of caution: most introductory texts assume that the reader has a passing familiarity with several different kinds of mathematical structures to use as examples of categories. While one could theoretically learn everything category theory had to offer and then discover the particular sub-fields, it seems to be quite difficult. The level of abstraction is so high that without particular examples to keep in mind, it becomes difficult to understand why the category way is the way it is.

That being said, there is a wealth of online information on category theory. The epic online text is Wells' and Barr's Toposes, Triples, and Theories, which is extremely thorough at the cost of being a bit less than user-friendly. There are also several blogs devoted to the topic, and a YouTube group, the Catsters, that regularly post minilectures in category theory.

I will leave you then with a common example of categories: the category of groups, often notated as Grp. (Categories are frequently given multi-character names written in bold, roman font.) The objects of Grp are individual groups, and the morphisms are group homomorphisms. Every group has an identity homomorphism, and every pair of composable homomorphisms can be composed, so the whole thing can be considered as a category. One interesting thing about this category is that there is an object which has exactly one morphism to every other object (a so-called initial object): the trival group whose only element (written additively) is 0.

Finally, do not be swayed by crazy talk that it is all just abstract nonsense.


*A somewhat technical footnote: These requirements do make a few mathematical objects incapable of being brought into category theory. For example, any given non-associative group cannot be expressed as a category, since the group's operation is not associative, as the name suggests. However, the whole bunch of non-associative groups of a certain type do make a category under homomorphisms that preserve whatever weakened form of associativity is in play.

Cat"e*go*ry (?), n.; pl. Categories (#) [L. categoria, Gr. , fr. to accuse, affirm, predicate; down, against + to harrangue, assert, fr. assembly.]

1. Logic. One of the highest classes to which the objects of knowledge or thought can be reduced, and by which they can be arranged in a system; an ultimate or undecomposable conception; a predicament.

The categories or predicaments -- the former a Greek word, the latter its literal translation in the Latin language -- were intended by Aristotle and his followers as an enumeration of all things capable of being named; an enumeration by the summa genera i.e., the most extensive classes into which things could be distributed. J. S. Mill.

2. Class; also, state, condition, or predicament; as, we are both in the same category.

There is in modern literature a whole class of writers standing within the same category. De Quincey.

 

© Webster 1913.

Y'know, if you log in, you can write something here, or contact authors directly on the site. Create a New User if you don't already have an account.