Results 1 to 6 of 6
  1. #1
    sumit919 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Post 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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: avoiding if else

    I want to avoid using if else
    Can you explain why? The if statement is one of the standard ways of testing conditions and controlling execution flow.
    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.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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:
    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
    }
    then use that returned DatabaseBuilder reference.
    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.

    ** This space for rent **

  4. #4
    sumit919 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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.

  5. #5
    sumit919 is offline Member
    Join Date
    Aug 2012
    Posts
    3
    Rep Power
    0

    Default 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. :)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

Similar Threads

  1. Avoiding allocations
    By Skiller in forum New To Java
    Replies: 7
    Last Post: 02-02-2011, 12:26 PM
  2. avoiding memory over-consuming
    By itaipee in forum New To Java
    Replies: 4
    Last Post: 12-14-2009, 11:59 AM
  3. Avoiding system.exit()
    By swati.jyoti in forum New To Java
    Replies: 5
    Last Post: 07-01-2009, 10:17 AM
  4. Avoiding refresh
    By java_srinivasan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-25-2008, 09:01 AM
  5. avoiding if statements
    By valoyivd in forum New To Java
    Replies: 1
    Last Post: 04-02-2008, 09:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •