3.3 Objects and Modeling


3.3 Objects and Modeling

Objects are the central focus of the object-oriented approach to problem solving. Objects are given the responsibility of carrying out specific tasks of the solution. Objects are models of the real-world entities identified in the real-world environment of the problem. Objects with similar characteristics are grouped into collections, also known as classes.

3.3.1 Models

Note

The concept of abstraction is applied in describing the objects of a problem. This involves the elimination of unessential characteristics. A model includes only the relevant aspects of the real world pertaining to the problem to solve.

As mentioned previously, modeling is the task of designing and building a model. The result of modeling is that only the relevant objects and only the essential characteristics of these objects are included in the model.

There are several levels of abstraction and these correspond to the different levels of detail needed to completely define objects and the collections of objects in a model. An important and early task of the modeling process is to identify real-world objects and collections of similar objects within the boundaries of the problem.

Three basic issues in modeling are:

  1. Identifying the objects to include in the model

  2. Describing these objects

  3. Grouping similar objects into collections

3.3.2 Describing Objects

Real-world entities or objects are the fundamental components of a real world system. In modeling, objects are abstract representations of real-world entities; objects can be:

  • Physical objects, which are tangible objects, such as persons, animals, cars, balls, traffic lights, and so on

  • Nontangible objects, which are not directly visible, such as contracts, accounts, and so on

  • Conceptual objects, which do not clearly exist but are used to represent part of the components or part of the behavior of the problem. For example, the environment that surrounds the problem and that affects the entities of the problem

An object is a dynamic concept because objects exhibit independent behavior and interact with one another. They communicate by sending messages to each other; this way all objects collaborate for a common goal. Every object has:

  • State, represented by the set of properties (or attributes) and their associated values

  • Behavior, represented by the operations, also known as methods, of the object

  • Identity, which is a property that can help identify an object

A simple example of an object is an object of class Ball. Its identity is an object of class Ball. The attributes of this object are color, size, and move_status. Figure 3.3 shows the UML diagram for two Ball objects and illustrates their structure. The diagram is basically a rectangle divided into three sections. The top section indicates the class of the object, the middle section includes the list of the attributes and their current values, and the bottom section includes the list of operations in the object.

click to expand
Figure 3.3: Two objects of class Ball.

The values of the attributes of an object of class Ball are of varied types. The value of attribute color is a text string, which is enclosed in quotes. The value of attribute size is a numeric value. The value of attribute move_status is a single text character, which is enclosed in apostrophes. The two objects of class Ball are in different states because their attributes have different values.

An object of class Person is shown in Figure 3.4. This object has only two attributes, name and age, and two operations, play and stop. The values of these attributes are also shown. The behavior of this object is simple; the object can only start playing (with operation play) or stop playing (with operation stop).


Figure 3.4: An object of class Person.

Another simple example of an object is one of class Automobile. The properties are value, color, weight, size, year, model, make, number of miles (or kilometers) in the speedometer, fuel capacity (in gallons or liters), and the fuel consumption per mile (or per kilometer).

The behavior of the automobile object is defined by several operations, such as fill_fuel_tank, update_value, move, show_fuel_level, and show_odometer. The state of the automobile object changes when at least one of its attributes changes value.

3.3.3 Object Interactions

Suppose there is a scenario in which a child called Mike plays with two balls of the same size, one red and the other blue. The child is represented by an object of class Person. The balls are represented by two objects of class Ball. When the object of class Person needs to interact with the two objects of class Ball, the object of class Person invokes the move operation of each object of class Ball. Another possible interaction is the object of class Person invoking the show_color operation of an object of class Ball.

Note

Objects interact by sending messages to each other. The object that sends the message is the requestor of a service that can be provided by the receiver object.

A message represents a request for service, which is provided by the object receiving the message. The sender object is known as the client of a service, and the receiver object is known as the supplier of the service.

The purpose of sending a message is the request for some operation of the receiver object to be carried out; in other words, the request is a call to one of the operations of the supplier object. Objects carry out operations in response to messages.

These operations are object-specific; a message is always sent to a specific object. This is also known as method invocation. A message contains three parts:

  • The operation to be invoked or started, which is the service requested and must be an accessible operation

  • The input data required by the operation to perform, which is known as the arguments

  • The output data, which is the reply to the message and is the actual result of the request

To describe the general interaction between two or more objects (the sending of messages between objects), a UML diagram known as a collaboration diagram is used to describe the interaction. For example, to describe the interaction among an object of class Person with the two objects of class Ball, a simple collaboration diagram is drawn. Figure 3.5 shows a collaboration diagram with these three objects. In this example, the Person object invokes the move operation of the Ball object by sending a message to the first Ball object, and as a result of this message, that object (of class Ball) performs its move operation.

click to expand
Figure 3.5: Collaboration diagram with three objects.




Object-Oriented Programming(c) From Problem Solving to Java
Object-Oriented Programming (From Problem Solving to JAVA) (Charles River Media Programming)
ISBN: 1584502878
EAN: 2147483647
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net