Procedural,
Structured, and Object-Oriented Programming explained
Procedural Programming
Until recently, computer programs were thought of as a
series of procedures that acted on
data. A procedure, also called a function or a method, is a set of specific instructions executed
one after the other. The data was separate from the procedures, and the trick in programming
was to keep track of which functions called which other functions, and what
data was changed. To make sense of this potentially confusing situation, structured programming
was created.
The principal idea behind structured programming is the concept of “divide and conquer.”
A computer program can be thought of as consisting of a set of tasks. Any task
that is too complex to be described simply is broken down into a set of smaller component
tasks until the tasks are sufficiently small and self-contained enough that each is
easily understood.
As an example, computing the average salary of an employee of a company is a rather
complex task. You can, however, break it down into the following subtasks:
data. A procedure, also called a function or a method, is a set of specific instructions executed
one after the other. The data was separate from the procedures, and the trick in programming
was to keep track of which functions called which other functions, and what
data was changed. To make sense of this potentially confusing situation, structured programming
was created.
The principal idea behind structured programming is the concept of “divide and conquer.”
A computer program can be thought of as consisting of a set of tasks. Any task
that is too complex to be described simply is broken down into a set of smaller component
tasks until the tasks are sufficiently small and self-contained enough that each is
easily understood.
As an example, computing the average salary of an employee of a company is a rather
complex task. You can, however, break it down into the following subtasks:
1. Count how many employees you have.
2. Find out what each employee earns.
3. Total all the salaries.
4. Divide the total by the number of employees you have.
Totaling the salaries can be broken down into the following steps:
1. Get each employee’s record.
2. Access the salary.
3. Add the salary to the running total.
4. Get the next employee’s record.
In turn, obtaining each employee’s record can be broken down into the following:
1. Open the file of employees.
2. Go to the correct record.
3. Read the data.
Structured programming remains an enormously successful approach for dealing with
complex problems. By the late 1980s, however, some of the deficiencies of structured
programming had become all too clear.
First, a natural desire is to think of data (employee records, for example) and what you
can do with that data (sort, edit, and so on) as a single idea. Unfortunately, structured
programs separate data structures from the functions that manipulate them, and there is
no natural way to group data with its associated functions within structured programming.
Structured programming is often called procedural programming because of its
focus on procedures (rather than on objects).
Second, programmers often found themselves needing to reuse functions. But functions
that worked with one type of data often could not be used with other types of data, limiting
the benefits gained.
Object-oriented programming responds to these programming requirements, providing
techniques for managing enormous complexity, achieving reuse of software components,
and coupling data with the tasks that manipulate that data. The essence of object-oriented
programming is to model objects (that is, things or concepts) rather than data. The
objects you model might be onscreen widgets, such as buttons and list boxes, or they
might be real-world objects, such as customers, bicycles, airplanes, cats, and water.
Objects have characteristics, also called properties or attributes, such as age, fast, spacious,
black, or wet. They also have capabilities, also called operations or functions, such
as purchase, accelerate, fly, purr, or bubble. It is the job of object-oriented programming
to represent these objects in the programming language.
Object-Oriented Programming
Object-oriented programming responds to these programming
requirements, providing techniques for managing enormous complexity, achieving
reuse of software components, and coupling data with the tasks that manipulate
that data. The essence of object-oriented programming is to model objects (that
is, things or concepts) rather than data. The objects you model might be
onscreen widgets, such as buttons and list boxes, or they might be real-world
objects, such as customers, bicycles, airplanes, cats, and water.
Objects have characteristics, also called properties or
attributes, such as age, fast, spacious, black, or wet. They also have
capabilities, also called operations or functions, such as purchase,
accelerate, fly, purr, or bubble. It is the job of object-oriented programming
to represent these objects in the programming language.
Pillars of Object-Oriented Programming
The three pillars of object-oriented development are:
encapsulation, inheritance, and polymorphism.
Encapsulation
When an engineer needs to add a resistor to the device she
is creating, she doesn’t typically build a new one from scratch. She walks over
to a bin of resistors, examines the colored bands that indicate the properties,
and picks the one she needs. The resistor is a “black box” as far as the
engineer is concerned—she doesn’t much care how it does its work, as long as it
conforms to her specifications. She doesn’t need to look inside the box to use
it in her design.
The property of being a self-contained unit is called
encapsulation. With encapsulation, you can accomplish data hiding. Data hiding
is the highly valued characteristic that an object can be used without the user
knowing or caring how it works internally.
Similarly, when the engineer uses the resistor, she need not
know anything about the internal state of the resistor. All the properties of
the resistor are encapsulated in the resistor object; they are not spread out
through the circuitry. It is not necessary to understand how the resistor works
to use it effectively. Its workings are hidden inside the resistor’s casing.
Inheritance and Reuse
When the engineers at Acme Motors want to build a new car,
they have two choices: They can start from scratch, or they can modify an
existing model called Star. Perhaps their Star model is nearly perfect, but
they want to add a turbocharger and a six-speed transmission. The chief
engineer prefers not to start from the ground up, but rather to say, “Let’s
build another Star, but let’s add these additional capabilities. We’ll call the
new model a Quasar.” A Quasar is a kind of Star, but a specialized one with new
features.
Polymorphism
A new Quasar might respond differently than a Star does when
you press down on the accelerator. The Quasar might engage fuel injection and a
turbocharger, whereas the Star simply lets gasoline into its carburetor. A
user, however, does not have to know about these differences. He can just floor
it, and the right thing happens, depending on which car he’s driving.
No comments:
Post a Comment