Results 1 to 6 of 6
Thread: avoiding if else
- 08-28-2012, 03:24 PM #1
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
avoiding if else
My project generates DDL Script for an ER Model for multiple database vendors. I will be needing create and alter statements for various database objects. I want to avoid using if else and use some good OO Practices to design this. Can anyone help. I hope i am clear with my question.
- 08-28-2012, 04:06 PM #2
Re: avoiding if else
Can you explain why? The if statement is one of the standard ways of testing conditions and controlling execution flow.I want to avoid using if else
There are not many programs that do not use if statements.If you don't understand my response, don't ignore it, ask a question.
- 08-28-2012, 04:26 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: avoiding if else
You'll need one somewhere to differentiate the target database, but after that just create a database specific concrete version of some database interface.
A factory method would be a reasonable approach:
then use that returned DatabaseBuilder reference.Java Code:public DatabaseBuilder getDatabaseBuilder(String dbname) { // or enum or whatever, this is not cut 'n' paste code here if (dbname.equals("MYSQL")) return new MySQLDatabaseBuilder(); if (dbname.equals("Oracle")) return new OracleDatabaseBuilder(); // and so on }
In fact, a singleton set up would make some sense if, on a single execution, this was run against only a single database.Please do not ask for code as refusal often offends.
- 08-29-2012, 06:16 AM #4
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: avoiding if else
Currently we use if else only. But when new databases are added I have to test code for the previously supported databases also. And the use of if else makes that code highly error prone and absolutely unreadable. Its partially also because of some poor programming practices being adopted by our team. When i started reading about actually using interfaces and inheritance I thought this can be an area in our code where we can use inheritance or interfaces. Or any good design pattern.
- 08-29-2012, 06:20 AM #5
Member
- Join Date
- Aug 2012
- Posts
- 3
- Rep Power
- 0
Re: avoiding if else
Yes I was also thinking on the same lines. If I am not wrong this is something similar to the Strategy design pattern (The first DP mentioned in Head First Design Patterns). But I was not able to understand the singleton thing u mentioned. Thanks for the help. :)
- 08-29-2012, 10:02 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: avoiding if else
Depends on how your system works, but if it's run against a single database at any one time then you only need one of those DatabaseBuilders. That implies a singleton.
Except (thinking about it) it could be a bit awkward passing in the parameter for which database to use...Please do not ask for code as refusal often offends.
Similar Threads
-
Avoiding allocations
By Skiller in forum New To JavaReplies: 7Last Post: 02-02-2011, 12:26 PM -
avoiding memory over-consuming
By itaipee in forum New To JavaReplies: 4Last Post: 12-14-2009, 11:59 AM -
Avoiding system.exit()
By swati.jyoti in forum New To JavaReplies: 5Last Post: 07-01-2009, 10:17 AM -
Avoiding refresh
By java_srinivasan in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 06-25-2008, 09:01 AM -
avoiding if statements
By valoyivd in forum New To JavaReplies: 1Last Post: 04-02-2008, 09:08 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks