O'Reilly Forums: Design Pattern In Real World - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Design Pattern In Real World

#1 User is offline   dontknow dontknow 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 27-August 14

Posted 27 August 2014 - 08:37 PM

I would like to know How inheritance will be used in real world project.
E.g. Consider an example of Bank Account
Account {
String name;
double balance;
operation1() {
}
}
SavingAccount extends Account {
// some extra fields and methods related to saving account
operation1() {
}

}

CurrentAccount extends Account {
// Some extra fields and methods related to current account.
operation1() {
}
}

When I map these classes with database, using Hibernate using one of the inheritance strategy, e.g. table per sub class, I will end up with three tables. Account, Saving_account and Current_account.
Advantage: I can call operation1() depending on the type of object using polymorphism.
Disadvantage: more tables and classes.


Account {
string name;
double balance;
string type;
operation1() {
}
}

I need only 1 table for this approach called Account. And "type" field will identify the type of the account.

Advantage: Only 1 table and class.
Disadvantage: I will lose Object oriented world and every place I have to put the condition as below.

if (type == saving) {
// do this;
} else if (type == current) {
// do that;
}

As per theory, approach 1 is correct and best. But currently in my project, approach 2 is used. My project is not banking. I took it as an example for the simplicity. Also approach 1 is good for maintaining large complex projects.

Thanks in advance for your inputs.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users