Interaction between objects

Source: Internet
Author: User

Http://stackoverflow.com/questions/3744345/object-oriented-style-programming-for-interaction-between-objects?rq=1

One thing I ' ve noticed is so people that's new to OOP get caught on trying to map the physical world into the code They is writing. Do you really care that John and Betty is people or is actually wanting to depict a bank account? I think your choice of objects in the example actually make it harder to figure out the solution to the problem.

The important parts of this is 1) Where to put the logic's to move the money. 2) Where to store the data, how much money, each who has.

You need to decide if your want to talk about the problem in the context of a person or a customer's a bank (may be a pers On, company, or something else). I ' m guessing talking about a customer because assuming it's a person would be limiting and misleading. Also, a Bank is a pretty generic term, was it the big brick building with people inside of it or it the online website W ith several different pages that does different things. A bank account object can has a method (possibly static depending on the decide to the store your data and what's a Re going to the use of your object for) this knows how to transfer from the one account to another. The logic of how to transfer does isn't belong to Betty or John or a bank that it belongs to a bankAccount which can has specia L logic based on the type of account if there is fee ' s involved or the like. If you gave this logic to the bank would end up with a giant bank class with methods for everything from Greating a CU Stomer to dealing with MOney in very specific account types. Each account type my has different rules for how it handles transfers. Think of times where are want to show a transfer or deposit as pending.

If you were just solving the problem of transfering money, there was no need to create a bunch of objects. Based on the known requirements and presumed future requirements the below would is a good choice. Checkingaccount.transfer (Johnsaccountno, Bettysaccountno, amount)

Http://codereview.stackexchange.com/questions/63441/object-interaction-in-oop-especially-python

You should try to find a better-decouple them. An idea cold is removing the need of keeping a list of groups in and Person a list of people in Group . You could does it by introducing a third class, let's call it Subscriptions , that would contain a list of subscriptions. An easy-to-do, could be a (person, group) pair. That class would then offer all the methods-to-edit subscriptions (with a and an as well as subscribe(person, group) unsubscribe(person, group) methods to query th EM, like get_groups(person) or get_members(group) .

Interaction between objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.