123avi (30) [Avatar] Offline
#1
First I want to say that it is a pleasure reading this book. I implemented most of the stuff in my designs. Just wanted to ask regarding decoupling the logic from the ADT as shown in listing 1.5.

since AccountService is handling Account behavior, is it a mistake or misconception to force a mix-in of this trait with Account rather than using a concrete instance ?
e.g:
trait AccountService{
  this:Account =>
 def debit( amount: Amount): Try[Account] = {             
            if (balance.amount < amount)  
               Failure(new Exception("Insufficient balance in account"))    
           else Success(this.copy(balance = Balance(this.balance.amount – amount)))   
   }    
  def credit( amount: Amount): Try[Account] =      
        Success(this.copy(balance = Balance(this.balance.amount + amount))) 
}
Debasish Ghosh (113) [Avatar] Offline
#2
Hi -

Thanks for your comments.

First, I would like to say that in design and modeling there's hardly anything that's considered a wrong option. Stated otherwise all modeling options suck, some just happen to work smilie.

But I follow a principle while modeling - that's using the option that brings in the least of coupling between abstractions. In this case if u mix the Account in the AccountService, it will work no doubt. But it creates a coupling that could be avoided. And self type annotation is basically a round about form of inheritance. And inheriting an AccountService from Account doesn't make much sense intuitively.

Thanks.