[url=http:/civis/viewtopic.php?p=31765481#p31765481:1pv627jp said:
jdietz[/url]":1pv627jp]
=snip=
The only one I know is defining a function. I don't program for a living. Is there anyone who can program who can't define a function?
You'd be surprised...
What kind of problem needs object-oriented programming to solve?
Nothing
needs OOP, but some problems become
much easier to work using an OO approach. IME, anything that involves managing a lot of state information (have I been initialized, have I been updated, when was I last updated, is it time to send an alert yet, have I sent an alert, is it time to send an alert again, etc.) really benefits from an OO approach.
It's not a silver bullet, and there are times when it is overkill. If your problem can be cleanly defined in terms of inputs and outputs (a filter like grep, for example), then you really don't need OO.
Being an amateur programmer, I found Python (both versions) to be easy to program in.
I understand how to use built-in object methods and properties, but I don't understand why I'd want to define my own object classes. Inheritance is right out.
Here's an example of iheritence based on some work I'm doing right now.
I'm working on a monitor process that regularly polls a bunch of client instances and reports their status to various systems, including sending email alerts. So I created an abstract StatusListener class (interface) that exposes an update() method. I then realized multiple concrete classes that all implement the StatusListener (one sends emails, one sends updates to xymon, one attempts to restart down instances). At startup, each class registers itself with the poller class. As its running, the poller periodically cycles through the list of registered listeners and calls the update() method for each (IOW, it's an example of the Observer pattern, which gets used a
lot in this particular app).
To extend this to send alerts to new systems (such as an internal support ticket generator, which was a requirement but now isn't), I just need to realize a new implementation of the StatusListener.
Code:
class StatusListener {
public:
virtual ~StatusListener() {}
virtual void update( Properties& stateOfTheWorld ) = 0;
};
class XymonStatusListener : public StatusListener {
public:
XymonStatusListener( ... ); // sets up parameters specific to updating xymon
virtual ~XymonStatusListener( );
/**
* Methods specific to communicating with xymon
*/
...
void update( Properties& stateOfTheWorld );
...
};
class EmailStatusListener : public StatusListener {
public:
EmailStatusListener( ... ); // sets up parameters specific to sending email
virtual ~EmailStatusListener( );
/**
* Methods specific to setting up email
*/
...
void update( Properties& stateOfTheWorld );
...
};
And then, somewhere in the startup code:
Code:
...
StatusListener *xymonListener = new XymonStatusListener( ... );
StatusListener *emailListener = new EmailStatusListener( ... );
...
poller.addListener( xymonListener );
poller.addListener( emailListener );
...
Then, within the poller itself:
Code:
void Poller::addListener( StatusListener *l )
{
m_listeners.push_back( l );
}
void Poller::doUpdates( )
{
for ( std::vector<StatusListener*>::iterator it = m_listeners.begin(); it != m_listeners.end(); ++it )
{
it->update( this->stateOfTheWorld );
}
}
Of course, because I'm a talentless hack, my code is pretty ugly. And the actual implementation isn't quite so nicely partitioned (there's a lot of leakage between classes, because the act of sending an update changes the state of the client). It's basically a mess.