Objective-C class design, inheritance & delegation
I'm an experienced professional C & C++ coder, and I'm looking at writing my first Objective-C app to learn Cocoa. Now my design calls for a base "Device" controller class, which has functionality to add & remove Devices, connect & disconnect them and so forth. But I want to actually have different types of Devices, each of which has a different Process function. So for example, I can create a "DistortionDevice" class and connect it to the "FilterDevice" class.
In C++, I would do this by having a class Device, with common methods & data -- like Device::connect(), Device::remove(), Device::isRunning for example, and some more pure virtual methods like Device::getName()=0, Device::process()=0 and maybe even some protected methods like Device::canConnect()=0. I could then create a number of subclasses of Device which then provide concrete implementations of these pure virtual methods. And my host program just deals with Devices that are added to it, etc.
Now, I'm not sure how to do this 'correctly' in Objective-C. I could have a class Device with all the common methods & data as before, so that handles connection, removal, activation of devices and so on. But there doesn't seem to be any way of supplying pure virtual methods like -(NSString*)getName=0; and -(void)process=0; Also, as all methods declared in the interface are public, there is no way to have a protected pure virtual methods like -(BOOL)canConnect=0;
The way that Objective-C seems to work is that you use @protocols to define pure virtual methods. But, if I understand correctly, a protocol cannot have any instance data or methods of it's own. So I would need to reimplement the common methods like -(void)connect; in every single class that conforms to this protocol. And that seems wrong to me as well.
So I'm guessing that cocoa would want something like: A DeviceProcessing protocol that defines the pure virtual functions that I want, like -(NSString*)getName; and -(void)process; and a base Device class that has the common functionality & data like -(void)connect; and so on. My "concrete" devices are then derived from the Device base class AND conform to the DeviceProcessing protocol.
Is that right? What would be the best way of linking all this up? Should the Device base class implement the DeviceProcessing protocol, and implement this by forwarding calls through to it's delegate, or what?
iMac 2.8GHz 24", Mac OS X (10.5.4)