-
December 12th, 2009, 06:50 PM
#1
A theoretical question: partialy overriden method
Hello
I'm writing an SQL mapper framework and I have a doubt. Mybe I've just spent too much coding and my brains just refuses to see the bigger picture, so please don't laugh.
Is there a way to "override" a method so part of it would remain? I know this sounds ridiculous but the idea is to create a pattern method which would take unlimited quantity of expressions (could be another method) as a parameter or embed these in a defined section of its body.
An example:
PHP Code:
//here's a classical jdbc method public static User loadUserSession(String userName, String pwd){ User user = null; try{ con = connector.connect(); sql = "select id_user,username,password,status from user where username = ? and password = ?"; stmt = con.prepareStatement(sql); stmt.setString(1,userName); stmt.setString(2,Util.sh1Enc(pwd)); rs = stmt.executeQuery(); if(rs.next()){ //something with user object } }catch(SQLException e){ LogUtil.log("error", Level.SEVERE, e); }finally{ try {if(con != null){con.close();}} catch (SQLException e) {LogUtil.log("error", Level.SEVERE, e);} try {if(stmt != null){stmt.close();}} catch (SQLException e) {LogUtil.log("error", Level.SEVERE, e);} try {if(rs != null){rs.close();}} catch (SQLException e) {LogUtil.log("error", Level.SEVERE, e);} } return user; }
What I want is to conserve the try catch finally parts while query and all the rs logic would be customized depending on each case, so I have something like this:
PHP Code:
public static User loadUserSession(String userName, String pwd){ User user = null; sql = "select id_user,username,password,status from user where username = ? and password = ?"; stmt = con.prepareStatement(sql); stmt.setString(1,userName); stmt.setString(2,Util.sh1Enc(pwd)); rs = stmt.executeQuery(); if(rs.next()){ //something with user object } return user; }
Last edited by Xeel; December 12th, 2009 at 06:56 PM.
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
December 13th, 2009, 08:32 AM
#2
Re: A theoretical question: partialy overriden method
Yes, you can do this. There are several approaches, depending on your exact requirements.
For example: if you have variations on some process that uses chunks of common code, you can use the Template Method pattern. This involves writing a template method in an abstract process class that contains all the constant (common) code, and replacing the variable code parts with calls to abstract methods. For each variation, you subclass the process class and implement the abstract methods to execute the particular variation you need. In the example you posted, you'd call an abstract method in the 'try' block. This would be implemented in a subclass.
Alternatively, you can define an interface with the method(s) that your process method will call, and have objects that implement the interface. Then you'd pass these objects to the processor method by the interface type, and the processor would call the interface methods on the objects. In its simplest form, the interface would have a single method and all the object code would be inside it (Function Object pattern). This can be done with an anonymous inner class. A more complex form could use multiple methods, separate classes to implement them, and might wrap existing objects.
The first (Template Method) example, is suited to situations where there are a small number of variations known in advance, and they are different forms of processor. The second example is suited to situations where there may be any number of variations, and the process is manipulating unrelated objects (i.e. the objects are basically independent of the processor).
Optimism is an occupational hazard of programming: testing is the treatment...
K. Beck
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 14th, 2009, 01:52 PM
#3
Re: A theoretical question: partialy overriden method
Interesting... Still I don't understand how interface would solve the problem here...
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S (from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"));
USE [code] TAGS! Read this FAQ if you are new here. If this post was helpful, please rate it!
-
December 14th, 2009, 07:50 PM
#4
Re: A theoretical question: partialy overriden method
Originally Posted by Xeel
...I don't understand how interface would solve the problem here...
The way interfaces do - polymorphism. You can call the same methods on all class objects that implement the interface, and each object can do what its particular class does. To the caller they all look the same.
In this case, the caller would be your loadUserSession method with its try..catch code, in which it would call some common interface methods on passed in objects so they can execute their own expressions.
So instead of calling methods overridden by subclasses to do different things in each subclass, it calls interface methods on objects implemented to do different things in each object.
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time...
T. Cargill
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
December 15th, 2009, 09:38 AM
#5
Re: A theoretical question: partialy overriden method
The other way is to make use of Aspect Oriented Programming, leveraging AspectJ.
http://en.wikipedia.org/wiki/Aspect-...ed_programming
It has some very wide spread use in a lot of open source frameworks and you could define a point cut where around certain method signatures, you will always add in some code of execution no matter what, and you are free as the concrete implementor to decorate without worry (or having to see) the code that will be weaved around it at compile time.
------
If you are satisfied with the responses, add to the user's rep!
-
December 15th, 2009, 07:16 PM
#6
Re: A theoretical question: partialy overriden method
Originally Posted by Deliverance
The other way is to make use of Aspect Oriented Programming, leveraging AspectJ.
Fine if you don't mind having hidden or invisible stuff going on behind the scenes in your code. My own preference is that this kind of thing is best left to use in framework and library code, so that user code generally does only what it appears to do...
In theory, there is no difference between theory and practice, but not in practice...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|