|
Programming objects are often used to model the real-world objects (object that you find in everyday life). The real-world objects share two features: They have a state and certain behavior. For instance, dogs in the real world have state (name, color, hungry) and behavior (barking, fetching, or wagging its tail). Programming objects are often modeled after real-world objects and hence they too have state and behavior. A programming object maintains its state in one or more variables and implements its behavior using methods.
|
|
For programming objects, state is usually the data and behavior is the code to perform operations on that data. There are multiple terms such as data, information, member variables, fields which all mean the state of an object. There are also multiple terms such as code, operations, methods, procedures, responsibilities, functions which implies the behavior of an object. |
In real world banking, you will find objects such as a bank account, a teller, a customer and so on. You can model equivalent programming objects. Say for instance, a bank account in real world can be modeled into a programming object. Let us consider a bank account for a fictitious person “John Q. Doe”. It can be represented as an Account object with a state (account number, balance) and behavior (get the balance, deposit money, withdraw money). Figure 1.1 uses the widely used notion of UML diagrams to represent the programming object. It shows the bank account object for “John Q. Doe” as a donut. The inner circle illustrates the account object’s state and outer one shows the expected behavior from that account object.
![]() |
Figure 2.1 Object representation of John Q. Doe’s bank account
One of the easy ways to find out an object’s behavior is by imagining it as a person. When you personify an object, you can find out what this person should do as a part of her responsibility. This approach is particularly helpful when you are trying to figure out the behavior of a complex object. You may ask a question like what this object (as a person) should do and the answers you get are probably the responsibilities of that object. For example, you can find out account’s behavior by asking questions such as “what an account is responsible for?” It is easier to think that getting balance or depositing money is account’s responsibility rather than its behavior. Object’s behavior is often found by finding its responsibilities.