Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Sep 20, 2006, 05:15 AM   #1
w00tmaster
macrumors regular
 
Join Date: Jun 2004
How would you have answered this interview question

This is the question I got in an interview and I froze, so I'm 99.9% sure I didn't get the job, but curiosity has gotten the best of me.

I have copied down(roughly, it was spoken to me, not written) a class diagram for drivers. Some of them are USB, some not, so how would you create a USB class such that you didn't have to re-write the USB code? Keep in mind this would have to be in Java without using multiple inheritance.

drivers
/ \ \
printers cdroms digital cameras
/ \ \ / \ \ / \ \
hp can ep. sony acer pioneer sony nikon fujifilm
\ \ / \
usb usb usb usb

The answer hit me after I hung up(well, what I think is the answer anyway) would be to add a USB to the 2nd level of the hiearchy, and include an instance of that class in the USBs, and then use that to do your USB specific calls. Though there probably is a more elegant solution.
w00tmaster is offline   0 Reply With Quote
Old Sep 23, 2006, 08:33 AM   #2
superbovine
macrumors 68030
 
superbovine's Avatar
 
Join Date: Nov 2003
I wouldn't
http://jusb.sourceforge.net/
superbovine is offline   0 Reply With Quote
Old Sep 23, 2006, 01:15 PM   #3
mrichmon
macrumors 6502a
 
Join Date: Jun 2003
Quote:
Originally Posted by w00tmaster
I have copied down(roughly, it was spoken to me, not written) a class diagram for drivers. Some of them are USB, some not, so how would you create a USB class such that you didn't have to re-write the USB code? Keep in mind this would have to be in Java without using multiple inheritance.

drivers
/ \ \
printers cdroms digital cameras
/ \ \ / \ \ / \ \
hp can ep. sony acer pioneer sony nikon fujifilm
\ \ / \
usb usb usb usb

The answer hit me after I hung up(well, what I think is the answer anyway) would be to add a USB to the 2nd level of the hiearchy, and include an instance of that class in the USBs, and then use that to do your USB specific calls. Though there probably is a more elegant solution.
Here are a couple of suggestions:
  • if the USB code is specific to each class of drivers then introduce a "legacy" and a "usb" class under cdroms and under digital cameras. This makes sense if there are usb operations that are specific to cdroms only and other operations that are specific to digital cameras.
  • if the intention of the question was that the usb operations are truely generic then you need to introduce a "legacy" (or parallel port, etc) class that inherits from driver. You then also create a "usb" class under driver and reshuffle the rest of the classes. Ideally the reshuffle would introduce cdrom, printer and digital camera interfaces.

Code:
                                  drivers
                         /                       \
                    legacy                        usb
                  /        \                  /         \
    legacyPrinter     legacyCdrom       usbPrinter        usbCdrom
       /        \                \           /                  \
     HP      Sony                IBM       HP                  Sony
With legacyPrinter and usbPrinter both implementing the Printer interface. Similarly, legacyCdrom and usbCdrom should implement a Cdrom interface. I assume that "HP", "Sony", etc are refering to specific models of devices so "HP" is shorthand for "HP Laserjet 1234j" or whatever.
mrichmon is offline   0 Reply With Quote
Old Sep 23, 2006, 04:11 PM   #4
notjustjay
macrumors 603
 
notjustjay's Avatar
 
Join Date: Sep 2003
Location: Canada, eh?
I'm not really certain I understand all that's being asked, particularly since your class diagrams don't show up too well with the variable-spaced fonts, but I get the gist.

Couple of ways to keep USB code encapsulated and reusable.

One would be multiple inheritance, but as you say, Java doesn't support that.

As the guy in the response above me said, you could subclass Driver and create a UsbDriver class. Subclass that for particular devices as you see fit. However, this completely changes any existing class hierarchy, which may not always be a good thing.

Alternately, you could create something like a USBCommunicator object, which classes could instantiate, though this method is not nearly as elegant. However, it would allow you to keep all your USB comms code in one place, and add USB functionality to whatever other classes you want, without changing their existing hierarchy.
__________________
.
notjustjay is offline   0 Reply With Quote
Old Sep 28, 2006, 05:35 AM   #5
wiseguy27
macrumors 6502
 
Join Date: Apr 2005
Location: USA
Why not create a USB class that's outside this class hierarchy and use composition instead of inheritance??? Instantiate the USB class in each of the drivers and use its services as required. You could also look at the decorator pattern if you'd like to extend the functionality of the USB class without modifying it.

Composition provides more flexibility compared to inheritance when you want to change the behavior of objects at runtime instead of deciding at compile time.

"Head First Design Patterns" (http://www.amazon.com/Head-First-Des...dp/0596007124/) is a good book!

wiseguy27 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
question answered:What part is this? stupid question from a stupid... mikentosh2003 MacBook Pro 3 Sep 9, 2013 09:25 AM
Really want this answered... BrokeTechLover Mac Applications and Mac App Store 14 Oct 9, 2012 01:33 AM
Interview question kazpod Community Discussion 6 Sep 19, 2012 12:31 PM
ok sorry but i have not seen this answered anywhere.... weeble8604 iPhone 10 Sep 13, 2012 09:51 AM
How soft is the aluminum? [My question got answered, thanks you all] Nevzorus iPad 17 Aug 1, 2012 01:53 PM

Forum Jump

All times are GMT -5. The time now is 12:55 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC