I'm working on creating a Cocoa library for connecting to the embedded devices my company makes. I have a superclass, which we'll call Device
, and a number of subclasses for the different device models, which we'll call Device1
, Device2
, etc.
I need to support the automatic detection of the device model, which can be determined after connecting and logging into the device. Since the login code is common to all devices, it can be handled in the superclass. After logging in, the device will need to be represented by the appropriate subclass for its model.
I envisaged instantiating an object of the Device
superclass, logging into the device to read the model and then replacing the object by an instance of the appropriate subclass, say Device1
. I know that it's possible to return a different object in an -init
method but my problem is that the comms to the device can be lengthy so should probably be implemented with callbacks/delegates.
Is it possible to change the subclass of the instantiated object after the -init
method? Or is there a simpler/better way to achieve what I'm trying to do?