OOP by example

From HashVB
Jump to: navigation, search

This message was in reply to a post in comp.os.ms-windows.programmer.win32 about what advantages OOP gives them:

On 23/09/2011 15:17, Mark wrote:
> I don´t want to argue about this, I just want to understand why all
> those guys prefer OOP, I´d love to have something easier, but HOW does
> it become easier?

Further to all the other posts, and rigidly sticking to the "it depends on
your problem domain", here is one situation where I found OOP very useful.

My core app is a video surveillance product, and as such, it needs to
communicate with various different types of cameras.

The first variant only worked with one type so it was a single module with
functions to enumerate devices and then to connect to one and access the
video data.

We then added support for a different type of camera that connected using
a completely different method.
The enumeration was modified to return an extra "new connection method"
entry which was special cased in most places to use different blocks of
code.

This new method then needed to talk to different styles of camera so grew
a fairly length set of select case blocks which were processed every time
something needed doing.

Unfortunately, as time progressed, this became very unwieldy, and was
effecting performance, and I was tasked with a rewrite.

I'd split it up into a standard interface (Connect, GetData, Disconnect,
etc) and then used a class for each different type of connection method
which contained only the code and data needed for that particular
connection method.
I then had one single (global) factory function that looked at the data it
needed to connect to and created the appropriate object (there are
currently 5) and initialised it as needed.

This had the advantage of creating several much smaller self contained
modules, as well as the performance improvement of being specialised to
one specific connection method.

I also had to do similar for the playback objects as we have several
different recording type available, all with different capabilities but
the same core functionality.

I hope this helps you understand it a bit more, but you probably need to
get the to unmaintainable code point before you realise its usefulness.

Note that this isn't specific to any particular language as 3 different
ones were used throughout this period at all stages.

-- 
Deanna Earley