开发者

OOP Design for an Economy

开发者 https://www.devze.com 2022-12-26 23:52 出处:网络
Not sure where to start, so I\'m just going to plow in. Let\'s say I\'m trying to represent an economy in OOP. A basic design I\'ve come up with is:

Not sure where to start, so I'm just going to plow in. Let's say I'm trying to represent an economy in OOP. A basic design I've come up with is:

class Person{
  int $money; // Money someone has in wallet/purse
  int $bank_account_id;

  function getAmountOfMoney()
  function addMoney($amountToAdd)
  function subtractMoney($amountToSubtract)
}

class BankAccount{
  int $money; // Money in Bank Account
  int $interest_per_year;

  function giveInterest()
  function depositMoney() // Calls $person->subtractMoney()
  function withdrawMoney(开发者_StackOverflow社区) // Calls $person->addMoney()

}

Are there any design flaws here?


Looking at what you've started with I would suggest that you should try to keep your classes to having a single responsibility. Some food for thought is it the BankAccount that is responsible for calculating interest? Probably not.

If you want to learn good principles on OO development I'd suggest looking at S.O.L.I.D: http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod (+100's of other links)


One thing I notice right off, is that a person and a bank account aren't a 1 to 1 relationship. Savings and Chequeing account, right off, for example. Or some people would have multiple banks, or I know like my parents, several accounts with the same bank. Storing the $bank_account_id in Person is what I'm referring to.

It would be better to have an array containing every account the person has.

Also, if you have the ID in Person, you should probably have it in BankAccount as well.

Similar thing with depositMoney() and withdrawMoney(), you have them working on Person, when they should be working on themselves, with Person accessing BankAccount not the other way around.

Basically, you have the relationship between the two classes somewhat backwards from how it would be to represent a real-life (and practical) usage.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号