Sie sind auf Seite 1von 41

Classes and Objects

When we use object-oriented methods to analyze or


design a complex software system, our basic building
blocks are classes and objects.

An object is an abstraction of something in a problem


domain, reflecting the capabilities of a system to keep
information about it, interact with it, or both
Objects
• Objects have an internal state that is recorded in a set of
attributes.
• Objects have a behavior that is expressed in terms of
operations.
• The execution of operations changes the state of the
object and/or stimulates the execution of operations in
other objects.
• Objects (at least in the analysis phase) have an origin in a
real world entity.
Classes
• Classes represent groups of objects which have the same
behavior and information structures.

• Every object is an instance of a single class



Class is a kind of type, an ADT (but with data), or an 'entity'
(but with methods)
• Classes are the same in both analysis and design
• A class defines the possible behaviors and the information
structure of all its object instances.
The nature of an object

• An object is any of the following:


•A tangible and/or visible thing.
•Something that may be apprehended
intellectually.
•Something toward which thought or action is
directed.

• Informally, object is defined as a tangible entity that


exhibits some well defined behavior.
An object has state, exhibits some well-defined behavior, and has a unique
identity.
• An object represents an individual, identifiable item, unit,
or entity, either real or abstract, with a well-defined role in
the problem domain”.

• Example: bicycle frames and airplane wings

• Some objects may have crisp conceptual boundaries yet


represent intangible events or processes.
– For example, a chemical process in a manufacturing
plant

• Some objects may be tangible yet have fuzzy physical


boundaries. Example: rivers, fog, and crowds of people
Objects
• Time, beauty or colors are not objects but they are
properties of other objects.

• An object is an entity that has state, behavior, and


identity.

• The structure and behavior of similar objects are


defined in their common class.

• The terms instance and object are interchangeable.


State

• The state of an object encompasses all of the


(usually static) properties of the object plus the
current (usually dynamic) values of each of these
properties.
Consider a vending machine that dispenses soft drinks. The
usual behavior of such objects is that when someone puts
money in a slot and pushes a button to make a selection, a
drink emerges from the machine.

• What happens if a user first makes a selection and then puts


money in the slot?

– Most vending machines just sit and do nothing because the user
has violated the basic assumptions of their operation.
– because the vending machine was in a state (of waiting for
money) that the user ignored (by making a selection first).

• Similarly, suppose that the user ignores the warning light


that says, “Correct change only,” and puts in extra money.
– Most machines will swallow the excess money.
• A property is an inherent or distinctive
characteristic, trait, quality, or feature that
contributes to making an object uniquely that
object.

– For example, one essential property of an elevator is


that it is constrained to travel up and down and not
horizontally.

– Properties have some value. The value may be a simple


quantity or it might denote another object.

• The fact that every object has state implies that


every object takes up some amount of space, be it
in the physical world or in computer memory
• e.g. consider the structure of a personnel record in
C++ as follows

struct personnelRecord
{ char name[100];
int socialsecurityNumber;
char department[10];
float salary; };

• This denotes a class.


• Objects are as:
– personnelRecord Tom, Kaitlyn
– 2 distinct objects each of which takes space in memory.
Consider an abstraction of an employee record. Figure 3–1 depicts this
abstraction using the Unified Modeling Language notation for a class.

Figure 3–1 Employee Class with Attributes


•None of these objects shares its space with any other object,
• Although each of them has the same properties;
• Thus, their states have a common representation.

Figure 3–2 Employee Objects Tom and Kaitlyn

Figure 3–3 Employee Class with Protected Attributes and Public Operations
Behavior
• Behavior is how an object acts and reacts, in terms of its state
changes and message passing.
• An operation is some action that one object performs on
another in order to elicit a reaction.

– For example, a client might invoke the operations append


and pop to grow and shrink a queue object, respectively.

– A client might also invoke the operation length, which


returns a value denoting the size of the queue object but
does not alter the state of the queue itself.
• The behavior of an object is a function of its state
and the operation performed on it, with certain
operations having the side effect of altering the
object’s state.
Operations
• An operation denotes a service that a class offers to
its clients.

• A client performs 5 kinds of operations upon an


object.

• Modifier: An operation that alters the state of an


object.
• Selector: An operation that accesses the state of
an object but does not alter the state.
Operations
• Iterator: An operation that permits all parts of an
object to be accessed in some well defined order.
• Constructor: An operation that creates an object
and/or initializes its state.
• Destructor: An operation that frees the state of an
object and/or destroys the object itself.
Roles and Responsibilities
• All of the methods associated with a particular object
comprise its protocol.

• The protocol of an object defines the envelope of an object’s


allowable behavior and comprises the entire static and
dynamic view of the object.

• It is useful to divide this larger protocol into logical groupings


of behavior. These collections, which thus partition the
behavior space of an object, denote the roles that an object
can play.

• The responsibilities of an object are all the services it provides


for all of the contracts it supports
In the course of one day, the same person may play the role
of mother, doctor, gardener, and movie critic.

Objects can play many different roles.


Active and Passive objects
• An active object is one that encompasses its own thread of control,
whereas a passive object does not.

• Active objects are generally autonomous, meaning that they can


exhibit some behavior without being operated on by another object.
Passive objects, on the other hand, can undergo a state change only
when explicitly acted on.
Detailed behavior of a philosopher
Identity
Identity is that property of an object which
distinguishes it from all other objects.
Class and Object
• An object is a concrete entity that exists in time and
space, a class represents only an abstraction.

• A class is a set of objects that share a common


structure, common behavior, and common
semantics.

• A single object is simply an instance of a class.


A class represents a set of objects that share a common structure
and a common behavior.
Relationships among Objects

• Objects contribute to the behavior of a system by


collaborating with one another

• Example: In an airplane, only the collaborative efforts


of all the component objects of an airplane enable it to
fly.

• Object relationships :
– 1. Links
– 2. Aggregation
Links
• Physical or conceptual connection between objects.

• An object collaborates with other objects through


its links to these objects

Figure 3–5 Links


• Figure 3–5 illustrates several different links.

• Line between two objects represents the existence of a link


between the two and means that messages may pass along this
path.

• Messages are shown as small directed lines representing the


direction of the message, with a label naming the message
itself.

• the FlowController object has a link to a Valve object. The Valve


object has a link to the DisplayPanel object.

• Message passing between two objects is typically unidirectional,


although it may occasionally be bidirectional.
• In the example, the FlowController object invokes operations
on the Valve object (to change its setting) and the
DisplayPanel (to change what it displays), but these objects
do not themselves operate on the FlowController object.

• Although message passing is initiated by the client (such as


FlowController) and is directed toward the supplier (such as
the Valve object), data may flow in either direction across a
link.

• For example, when FlowController invokes the operation


adjust on the Valve object, data (i.e., the setting to change to)
flows from the client to the supplier.

• If FlowController invokes a different operation, isClosed, on


the Valve object, the result (i.e., whether the valve is in the
fully closed position) passes from the supplier to the client.
Relationships among Classes
• Three basic kinds of class relationships

1. Generalization/Specialization-- denoting an “is a”:


– For instance, a rose is a kind of flower, meaning that a rose is
a specialized subclass of the more general class, flower

2.Whole/Part-- which denotes a “part of” relationship:


– a petal is a part of a flower.

3.Association-- denotes some semantic dependency


among otherwise unrelated classes, such as between
ladybugs and flowers.
Association

Figure 3–7 Association

One-to-Many Association

A one-to-many association: Each instance of Wheel relates to one


Vehicle, and each instance of Vehicle may have many Wheels (noted by
the *).
Multiplicity
• Three kinds of multiplicity across an association

• One-to-one:
– Example class Sale and the class CreditCardTransaction
• One-to-many:
• Many-to-many: class Customer and class
SalesPerson
Inheritance
• To express generalization/specialization relationships

Single Inheritance

• Inheritance is a relationship among classes wherein one class


shares the structure and/or behavior defined in one (single
inheritance) or more (multiple inheritance) classes.
• We call the class from which another class inherits its
superclass.
• a class that inherits from one or more classes a subclass.
• defines an “is a” hierarchy among classes.
Inheritance

Figure 3–8 ElectricalData Inherits from the Superclass TelemetryData


Single Inheritance

Figure 3–10 DisplayItem Class Diagram


Figure 3–11 Multiple Inheritance
Polymorphism
With polymorphism, an operation can be
implemented differently by the classes in the
hierarchy.

• A subclass can extend the capabilities of its


superclass or override the parent’s operation,
Aggregation
• The whole/part relationships
• Aggregation asserts a direction to the whole/part
relationship.
• For example, the Heater object is a part of the
TemperatureController object, and not vice versa.

Figure 3–12 Aggregation


Dependency
• A dependency indicates that an element on one
end of the relationship, in some manner, depends
on the element on the other end of the
relationship.

• If one of these elements changes, there could be an


impact to the other
Abstract Classes
• Classes with no instances are called abstract
classes.
• An abstract class is written with the expectation
that its subclasses will add to its structure and
behavior, usually by completing the implementation
of its (typically) incomplete methods.

Das könnte Ihnen auch gefallen