The adapter pattern: abstracting camera hardware

A while back I wrote an application that interacted with a camera and a signature pad device. Part of the specification was that it be as easy as possible to change the camera hardware in the future to allow for upgrades. The adapter pattern was the perfect solution to this. We used a Canon camera with a COM wrapper .NET SDK which we didn't want the application coupled to.

One interface to adapt them all
I created an ICamera interface that described the way we wanted to interact with a camera. It had basic methods on it like connect, take photo, disconnect etc.

The canon camera adapter
I then created a class that implemented the ICamera interface and contained a Canon camera sdk object. This class was the adapter between my simple interfce and the canon sdk.

Adding another camera

Any camera device that can support the basic functionality prescribed in the ICamera interface can easily be added by creating an adapter class. None of the previous code which makes use of the camera needs to be changed to work with a different device.

The adapter pattern defined

The adapter pattern converts the interface of a class into another interface the clients expect. Adapter let's classes work together that couldn't otherwise because of incompatible interfaces (Freeman et al, 2004). Our application code (the client) and the camera code are decoupled by the adapter and unaware of each other thanks to the ICamera interface.



Testability/Mockabillity

As a side note this approach also meant that all of our client code was testable and that with a flick of the dependency injection configuration switch it could be run without an actual camera device at all (which was useful for testing other parts of the application).

Tags: , , , ,

Comments

trackback
DotNetKicks.com
6/25/2010 6:58:41 PM Permalink

The adapter pattern: abstracting camera hardware

You've been kicked (a good thing) - Trackback from DotNetKicks.com

pingback
topsy.com
6/26/2010 8:30:54 AM Permalink

Pingback from topsy.com

Twitter Trackbacks for
        
        instantiate | The adapter pattern: abstracting camera hardware
        [instantiate.co.nz]
        on Topsy.com

trackback
DotNetShoutout
6/30/2010 7:43:38 PM Permalink

instantiate | The adapter pattern: abstracting camera hardware

Thank you for submitting this cool story - Trackback from DotNetShoutout

Pingback from findaudioandvideoaccessories.info

Rca To Mini Adapter | FIND AUDIO AND VIDEO ACCESSORIES

Pingback from best-cheap-camera.madmikessite.com

Apple® iPadâ„¢ Dock – White (MC360ZM/A)

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading