Page 1 of 1

About Question enthuware.ocpjp.v7.2.1107 :

Posted: Wed Mar 13, 2013 6:49 am
by ETS User
I couldn't figure out why only option first is correct ? i believe second option is more correct.

Re: About Question enthuware.ocpjp.v7.2.1107 :

Posted: Wed Mar 13, 2013 9:10 am
by admin
Not entirely true. The main purpose of DAO is to remove database logic from business logic.
You could use the program to an interview principal while implementing DAO as well but that is not fundamental to the DAO as such. In other words, you can choose not to have a DAO interface and still use the DAO class directly.

HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1107 :

Posted: Tue Jan 27, 2015 4:47 pm
by ewebxml
"To reduce code complexity in business objects" is a bit ambiguous.

I believe a better answer for option A) would be

"To reduce code complexity in business objects and hide the data access implementation from its client."

If you say
"To reduce code complexity in business objects"
by itself, the person who reads it thinks

"This could be stand alone JavaBeans on a desktop."

If no reference is made to database access, then why would one select the DAO pattern?

I selected option B) because when you use the DAO pattern,
your class implements an interface DAO.

Code: Select all

public class User {
  private int userId;
  private String name;
  public int getUserId() {
    return userId;
  }
  public void setUserId(int userId) {
    this.userId = userId;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}
public interface UserDAO {
  public void insert(User user);
  public void update(User user);
}
public class UserDAOImpl implements UserDAO {
  public void insert(User user) {
    // insert user into user table
  }
  public void update(User user) {
    // update user information in user table
  }
}

My basic understanding of the DAO pattern is to place the business logic in a tier,
that makes it independent of the RDBMS.

So if you switch from Oracle 11G to DB2 10.1,
only a small amount of Java code will have to be changed.

Please confirm.

Re: About Question enthuware.ocpjp.v7.2.1107 :

Posted: Tue Jan 27, 2015 9:43 pm
by admin
Yes, it is true that it is a bit ambiguous. But it is there because you can expect similar statements in the exam.
Your understanding about DAO is correct but it is not just the change in database. It protects upper layer code even from a change in persistence mechanism i.e. it allows you to change from say Hibernate to iBatis.
HTH,
Paul.

Re: About Question enthuware.ocpjp.v7.2.1107 :

Posted: Thu Jan 05, 2017 8:58 am
by jagoneye
Suppose the first option was not there. Then the option
"Program to an interface and not to an implementation" principle.
would be correct or not?

Re: About Question enthuware.ocpjp.v7.2.1107 :

Posted: Thu Jan 05, 2017 9:43 am
by admin
No, you can have a DAO without using any interface. See this:
http://www.oracle.com/technetwork/java/ ... 38824.html
In the above link it uses:
DAOFactory cloudscapeFactory =
DAOFactory.getDAOFactory(DAOFactory.DAOCLOUDSCAPE);

the call returns an object of class CloudscapeCustomerDAO that implements CustomerDAO.

But you could have a CustomerDAO abstract class as well or even a concrete one if you don't want multiple kinds of CustomerDAOs.

Of course, it may not be as flexible but it would still be a valid DAO.