|
|
12 Module 7: Essentials of Object-Oriented Programming
Object-Oriented Programming
Object-oriented programming arose to alleviate these problems. Object-oriented
programming, if understood and used wisely, is really person-oriented
programming because people naturally think and work in terms of the highlevel
behavior of objects.
The first and most important step away from procedural programming and
towards object-oriented programming is to combine the data and the functions
into a single entity.
Module 7: Essentials of Object-Oriented Programming 13
Controlling Access Visibility
n Methods Are Public, Accessible from the Outside
n Data Is Private, Accessible Only from the Inside
Withdraw( )
Deposit( )
balance
BankAccount ?
Withdraw( )
Deposit( )
balance
BankAccount
?
In the graphic on the left, Withdraw, Deposit, and balance have been grouped
together inside a “capsule.” The slide suggests that the name of the capsule is
BankAccount. However, there is something wrong with this model of a bank
account: the balance data is accessible. (Imagine if real bank account balances
were directly accessible like this; you could increase your balance without
making any deposits!) This is not how bank accounts work: the problem and its
model have poor correspondence.
You can solve this problem by using encapsulation. Once data and functions are
combined into a single entity, the entit y itself forms a closed boundary,
naturally creating an inside and an outside. You can use this boundary to
selectively control the accessibility of the entities: some will be accessible only
from the inside; others will be accessible from both the inside and the outside.
Those members that are always accessible are public, and those that are only
accessible from the inside are private. It is not possible to have members that
are only accessible from the outside.
To make the model of a bank account closer to a real bank account, you can
make the Withdraw and Deposit methods public, and the balance private.
Now the only way to increase the account balance from the outside is to deposit
some money into the account. Note that Deposit can access the balance
because Deposit is on the inside.
14 Module 7: Essentials of Object-Oriented Programming
C#, like many other object-oriented programming languages, gives you
complete freedom when choosing whether to make members accessible. You
can, if you want, create public data. However, it is recommended that data
always be marked private. (Some programming languages enforce this
guideline.)
Types whose data representation is completely private are called abstract data
types (ADTs). They are abstract in the sense that you cannot access (and rely on)
the private data representation; you can only use the behavioral methods.
The built- in types such as int are, in their own way, ADTs. When you want to
add two integer variables together, you do not need to know the internal binary
representation of each integer value; you only need to know the name of the
method that performs addition: the addition operator (+).
When you make members accessible (public), you can create different views of
the same entity. The view from the outside is a subset of the view from the
inside. A restricted view relates closely to the idea of abstraction: stripping an
idea down to its essence.
A lot of design is related to the decision of whether to place a feature on the
inside or on the outside. The more features you can place on the inside (and still
retain usability) the better.
Module 7: Essentials of Object-Oriented Programming 15
Why Encapsulate?
n It Allows Control
l Use of the object
is solely through the
public methods
n It Allows Change
l Use of the object
is unaffected if the
private data type
changes
Withdraw( )
Deposit( )
dollars 12
Withdraw( )
Deposit( )
balance 12.56
cents 56
?
Two reasons to encapsulate are:
n To control use.
n To minimize the impact of change.
Encapsulation Allows Control
The first reason to encapsulate is to control use. When you drive a car, you
think only about the act of driving, not about the internals of the car. When you
withdraw money from an account, you do not think about how the account is
represented. You can use encapsulation and behavioral methods to design
software objects so that they can only be used in the way you intend.
Encapsulation Allows Change
The second reason to encapsulate follows from the first. If an object’s
implementation detail is private, it can be changed and the changes will not
directly affect users of the object (who can only access the public methods). In
practice, this can be tremendously useful. The names of the methods typically
stabilize well before the implementation of the methods.
The ability to make internal changes links closely to abstraction. Given two
designs for a class, as a rule of thumb, use the one with fewer public methods.
In other words, if you have a choice about whether to make a method public or
private, make it private. A private method can be freely changed and perhaps
later promoted into a public method. But a public method cannot be demoted
into a private method without destroying client code. |
|