Jump to content


Photo

Design Pattern In Real World


  • Please log in to reply
No replies to this topic

#1 dontknow dontknow

dontknow dontknow

    New Member

  • Members
  • Pip
  • 1 posts

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 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users