Tim Ross

Software Engineer - iOS, Ruby/Rails, .NET

Managers vs Controllers

A conversation on Twitter last week got me thinking about how I name my classes, particularly the difference between a Manager and a Controller class.

For me, a Manager is the go-to-guy for a particular aspect of functionality or data. For example, a FileManager handles anything related to accessing the file system. A Manager class contains only class methods, or is implemented as a Singleton, as no separate instances are usually required.

1
[MyFileManager fileExistsAtPath:myPath]; // Class method

or

1
[[MyFileManager sharedManager] fileExistsAtPath:myPath]; // Singleton

A Controller class co-ordinates a process. For example, a PurchaseController controls the series of actions in purchasing a product. A ViewController co-ordinates a series of actions for interacting with a View.

1
2
3
4
MyPurchaseController *purchaseController = [[MyPurchaseController alloc] initWithDelegate:self];
[purchaseController purchaseProduct:productId]; // Begins purchase process
...
[purchaseController cancelPurchase]; // Cancels process

Everyone has their own naming preferences, and yours might be different. It’s not really the names themselves that’s important, but that the naming is consistent throughout the application. Remember, it’s all about communicating intent to yourself and others who use your code.

Comments