1.2. Programming Paradigms
1.2. Programming Paradigms
www.pmt.education
Specification:
www.pmt.education
The procedural programming paradigm
Data is stored in procedural programs by constants and variables. A data structure is said
to have a global scope if it can be accessed from all parts of the program and a local
scope if it is only accessible from the structure within which it is declared.
Most of the programs that you may have written are likely to have been procedural.
Designing a program from the top down makes maintaining the program easier as
navigation of different elements of the overall solution is improved. If all of a program’s
code is contained within the same module, finding the specific line of code that needs
fixing can be incredibly difficult - especially in large projects.
When a program is split into modules, testing can be carried out on the individual modules
before they are combined to form the overall solution. Furthermore, development can be
split over a team of developers each of which is assigned a different module to work on.
Hierarchy charts
A hierarchy chart graphically represents the structure of a structured program. Each
procedure is displayed as a rectangle which is connected to any other procedures that are
used within it.
www.pmt.education
Example
SUBROUTINE Main()
name ← INPUT
yearBorn ← INPUT
FUNCTION calculateAge(year)
age ← calculateAge(yearBorn)
yearNow ← getYear() FUNCTION getYear()
IF age > 17 THEN
age ← yearNow - year RETURN system.year
OUTPUT name + “can drive”
RETURN age END FUNCTION
ELSE
END FUNCTION
OUTPUT name + “can’t drive”
END IF
END SUBROUTINE
The hierarchy chart above shows that the procedure startGamecalls chooseLevel
which in turn calls both playLevelOneand playLevelTwoand so on.
www.pmt.education
The object-oriented programming paradigm
Objects
Rather than storing data with constants and variables like procedural programs,
object-oriented programs use objects as containers of both data and instructions.
New objects are created from classes. The process of creating an object is called
instantiation. It is possible for one program to have many different objects of the same
class which will all have identical methods and procedures, however the actual values of
their properties are unique to the object. For example, a program could have three
different objects of the class carwhich all have a property called manufacturer . All
three cars could have different manufacturers .
For example, a class for creating car objects might have the properties EngineSizeand
Colourand the methods StartEngine , ApplyHandbrakeand TurnOnLights .
A class can be expressed on paper as a class definition which lists a class’ name,
properties and methods. Exam questions often ask you to write or interpret class
definitions.
The class definition for the car class could look like this:
Car = Class {
Private:
Manufacturer: String
Model: String
EngineCapacity: Float
IsTaxed: Boolean
Public:
Function GetManufacturer
Function GetModel
Function GetEngineCapacity
Function GetIsTaxed
Procedure SetDetails
}
A method or property that is listed as private can only be accessed from within an object.
Public methods allow an interface for accessing and modifying a class’ private properties.
www.pmt.education
Encapsulation
Encapsulation is the name given to the process of combining methods and procedures to
form an object in object-oriented programming. An object is said to encapsulate its
contents, forming a single entity which encompasses all of the object’s properties and
methods.
Inheritance
A class can inherit another class. Inheritance allows one class to share the properties and
methods of another class, while having its own properties and methods too.
DeLorean = Class (Car){
Private:
ReactorOutput: Integer
FluxCapacitorInput: Integer
Public:
Function GetReactorOutput
Function GetFluxCapacitorInput
Procedure SetDetails (Override)
}
The example above shows the class description for DeLorean . The brackets after class
(shown in red) indicate that the class inherits the Carclass and therefore has all of the
properties and methods that the Carclass has as well as its own properties and methods
that do not exist in the Carclass such as ReactorOutput .
www.pmt.education
Polymorphism
The word polymorphism comes from the Greek for “many forms”. Polymorphism occurs
when objects are processed differently depending on their class.
Overriding
An overridden method has the same name as a method in an inherited class but different
implementation. The word override after the SetDetailsmethod in the class description
above indicates that the implementation of the SetDetailsmethod in the DeLorean
class differs from the implementation of the method with the same name in the Carclass.
Association
If two objects are associated, they can be described as having a “has a” relationship. For
example, objects of the classes Carand Drivercould be associated as a car has a
driver. An associated object forms part of its container object as a property. Just like a
property of an object can be a string, it can be an object of another class.
There are two specific types of association that you need to know about: aggregation and
composition.
Aggregation is the weaker of the two kinds of association. When an object is associated
with another by aggregation, it will still exist if its containing object is destroyed.
For example: a car’s passengers are associated with the car by aggregation. If the
car is scrapped, the passengers still exist independently.
For example: a car’s wheels are associated with the car by composition. The
wheels form an integral part of the car and scrapping the car destroys the wheels.
www.pmt.education
Why is object-oriented programming used?
Object-oriented programming provides programs with a clear structure that makes
developing and testing programs easier for developers. Using the paradigm also allows for
large projects to be divided among a team of developers.
The use of classes allows code to be reused throughout the same program and even in
other programs. This improves the space efficiency of code.
There are three design principles used in object-oriented programming that you need to be
aware of. These are: encapsulate what varies, favour composition over inheritance and
program to interfaces, not implementation.
When a new object is created, it can implement an interface which provides it with the
correct properties and methods.
www.pmt.education
Class diagrams
Class diagrams can be used to visually represent the relationships that exist between
classes. Classes are represented by boxes with different connectors representing different
kinds of relationship.
Inheritance diagrams
A special type of class diagram, the inheritance diagram, shows the different inheritance
relationships that exist between classes in an object-oriented program.
Association
Association is shown in class diagrams with diamond headed arrows.
Aggregation Composition
Aggregation (the weaker of the two types Composition (the stronger relationship) is
of association) is shown with an unfilled shown with a filled diamond headed arrow.
diamond headed arrow.
www.pmt.education
Class diagrams with properties and methods
A class diagram can contain more information about classes than just their name. When
this is done, a class is represented by three boxes. The uppermost box contains the class
name, the middle box contains the class’ properties and the bottom box contains the class’
methods.
Student
- Name: String
- Age: Integer
+ GetName
+ GetAge
+ SetDetails
A plus sign indicates that a property or method is public and a minus indicates that the
property or method is private. A pound symbol (#, not £) indicates that a property or
method is protected.
Protected properties and methods are accessible from within the object that declares them
(just like if they were private) and also from any inherited objects.
www.pmt.education