I have the finance application where we can have different types of transactions, like balance enquiry, funds transfer, and pin change etc. Now the Transaction is a base class, and there are specific child classes implementing it as per above use cases.
Class Transaction
{
int txnId;
String sourceAccountNumber;
}
Class PinChange extends Transaction
{
int pin;
}
Class FundsTransfer extends Transaction
{
int amount;
String destAccountNumber;
}
Now I have a BankService object which takes transaction and executes it.
Class BankService
{
executeTransaction(Transaction transaction)
}
I want to know:
How will
BankServiceknows what is the type of transaction? Do I need to wrap it up in a new Enum likeTransactionTypeand put it in transaction base class?When the
BankServicecomes to know type of transaction, how will it access specific class details? I think it needs to type cast transaction to specific child class, which seems bad to me. Thoughts?
executeTransactionhas a single responsibility?