Let's say there are two classes, one is the user class, which contains the user information; the other one is the payment transaction class. The scenario is simple, if the user's age is > 65, create type A payment transaction; otherwise, create type B payment transaction.
There are several ways to do this:
- Create a method not belongs to user nor transaction, just call CreateTransaction. The logic is stated in this method:
func CreateTransaction(user, transaction) {
if user.GetAge() > 65:
transaction.CreateA()
else:
transaction.CreateB()
}
- The other option is the create a method for the user class:
class User {
...
func CreateTransaction(transaction) {
if user.GetAge() > 65:
transaction.CreateA()
else:
transaction.CreateB()
}
}
Then there is a CreateTransactionController method that calls the function like:
func CreateTransactinController(user, transaction) {
user.CreateTransaction()
}
My question is, is the option 1 considered as procedural programming since the logic is actually not owned by any object? (Or anemic pattern?) Is the difference between 1 and 2 be just different place to put the logic?
Thanks!