Design pattern for hardware device controller
I hope this is appropriate for SO.
Given two classes: ClassA is the main controller object for an application. ClassB controls a hardware device (over network, USB, serial, something). ClassA creates a ClassB object, and uses it to send/receive data from the hardware device.
When the user wants to shut down the application, ClassA sends a "shutdown" message to ClassB. In some circumstances, ClassB needs an extended period of time to complete the shutdown. Maybe it has to reset the hardware to a known state, or wait for an operation in progress to complete.
What's the best design pattern for communication between ClassA and ClassB to minimize coupling? Callback (method or block) in the shutdown message? Delegate protocol with a defined "shutdownComp开发者_开发技巧lete" method? Something else?
Also, ClassB is really an "abstract" superclass, because there are multiple hardware devices, so there's a subclassB for each of them. I don't think that will effect the design.
Thanks!
I’d use a block callback:
- (void) initiateShutdownWithCompletionHandler: (dispatch_block_t) callback;
精彩评论