Consider the following source snippets:
Snippet #1
StoredProcedure sp = new StoredProcedure( "PROC_NAME", getConnection() );
sp.putParameter( "ID", getId() );
sp.execute();
Snippet #2
StoredProcedure sp = new StoredProcedure( "PROC_NAME" );
sp.setConnection( getConnection() );
sp.putParameter( "ID", getId() );
sp.execute();
Snippet #3
StoredProcedure sp = new StoredProcedure( "PROC_NAME" );
sp.putParameter( "ID", getId() );
sp.execute( getConnection() );
Q: Which snippet is the most object-oriented, and why?
Q: What are the pros and cons of each snippet?
My opinion: None and all of them at the same time.
All snippets show a method which is namedaction. Part of OO design in general is that each method does only one thing and the method name reflects that;actionas a method name isn’t reflective and can be used as a blanket title for anything. By looking at what the thing actually does, this method should apparently be called something likeexecuteProcName.OO is also a lot about Law of Demeter, also known as Principle of Least Knowledge. This means that using getters is a good thing and since all snippets already do that, they really all are OO and equivalent in that case, just like jball says in his answer. If I had to pick the one I’d prefer, it’d be #3 for obtaining the needed external classes/values at the last possible moment (in this case they do affect performance) or #2 because it’s the easiest to read.
That’s pretty much what I think can be said about this without going deeper into academic semantics.