I would Prefer the Layered Approach, and What This approach simply tell us is:
- You are having your model class Customer
You are having a contract with client through Interface CustomerDAO
public interface CustomerDAO{
public void saveCustomer(Customer customer);
public Customer getCustomer(int id);
}
You are having a concrete Implementation like CustomerDAOImpl
public class CustomerDAOImpl extends CustomerDAO{
public void saveCustomer(Customer customer){
saveCustomer(customer);
}
public Customer getCustomer(int id){
return fetchCustomer(id);
}
}
Then Write a Manager to Manage these or Encapsulating some other DAOs like:
public class ManagerImpl extends Manager{
CustomerDAO customerDAOObj;
// maybe you need to collect
// all the customer related activities here in manger
// because Client must not bother about those things.
UserBillingDAO userBillingDAOObj;
public void saveCustomer(Customer customer){
customerDAOObj.saveCustomer(customer);
}
public Customer getCustomer(int id){
return customerDAOObj.fetchCustomer(id);
}
// Note this extra method which is defined in
//UserBillingDAO which I have not shown, but you are exposing
//this method to your Client or the Presentation layer.
public boolean doBillingOFCustomer(id) {
return userBillingDAOObj.doBilling(id);
}
}
Now the presentation layer or the main Class will contain the code like this:
public static void main(String... ar){
Manager manager = new ManagerImpl();
manager.saveCustomer();
// or manager.doBillingOfCustomer(); // etc
}