What that article is saying is that OO means encapsulation of data and providing behaviour. By providing properties you're not encapsulating data. For example, you could implement a Account
class like this:
public class Account
{
public decimal Balance { get;set; }
}
But, that's no different, conceptually, than:
public class Account
{
public decimal Balance;
}
In either case the class has no behaviour. All the behaviour that operates on Account
needs to be external to Account
. What the article is saying is that behaviour and state should live together. So, you might have something like this instead:
public class Account
{
private decimal balance;
//...
public void DepositFunds(Money money)
{
balance += ValidateAndConvert(money);
}
public void WithdrawFunds(Money money)
{
balance -= ValidateAndConvert(money);
}
public void AdjustBalance(Money money)
{
balance -= ValidateAndConvert(money);
}
private decimal ValidateAndConvert(Money money)
{
// TODO: validate, convert
}
}
With an account that has a balance as a property or a field, external logic can modify it as it sees fit. this usually scatters business logic across the code base. If logic to withdraw funds from an account was required to check the balance and verify that the account cannot be overdrawn, or only overdrawn by a certain amount, many places in the code would have to provide that logic. If that logic needed to change many places would have to be found and changed (the risk being one gets missed and inconsistent withdrawl occurs). When the data is encapsulated within the object and only behaviour is provided, there's no way for that logic to be scattered about the code base. This also allows more explicit code. What may have been account.Balance -= someValue;
could have been a withdrawl, an adjustment, etc. Now it can be explicit: account.Widthdraw(someValue);
or account.AdjustBalance(someOtherValue);`--it's explicit what is happening to the account.