1.2. Programming Paradigms - Advanced
1.2. Programming Paradigms - Advanced
https://bit.ly/pmt-edu
Specification:
https://bit.ly/pmt-edu
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.
https://bit.ly/pmt-edu
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 startGame calls chooseLevel
which in turn calls both playLevelOne and playLevelTwo and so on.
https://bit.ly/pmt-edu
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 car which 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 EngineSize and
Colour and the methods StartEngine, ApplyHandbrake and 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.
https://bit.ly/pmt-edu
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.
For example, suppose MyCar is an object of the class Car. The code
MyCar.StartEngine() can be written by a developer working on another part of the
program, and the developer doesn’t need to know exactly how the StartEngine
procedure works, just that it starts the car’s engine.
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.
The example above shows the class description for DeLorean. The brackets after class
(shown in red) indicate that the class inherits the Car class and therefore has all of the
properties and methods that the Car class has as well as its own properties and methods
that do not exist in the Car class such as ReactorOutput.
https://bit.ly/pmt-edu
Polymorphism
The word polymorphism comes from the Greek for “many forms”. Polymorphism occurs
when objects are processed differently depending on their class.
For example, if two objects with classes Car and Lorry both had a
ConsumeFuelForOneMile method, it might decrease the Car’s FuelLevel property
less than it would the Lorry’s. The method has taken the object’s class into account and
treated them differently based 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 SetDetails method in the class description
above indicates that the implementation of the SetDetails method in the DeLorean
class differs from the implementation of the method with the same name in the Car class.
Association
If two objects are associated, they can be described as having a “has a” relationship. For
example, objects of the classes Car and Driver could 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.
https://bit.ly/pmt-edu
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.
https://bit.ly/pmt-edu
Writing object-oriented programs
The AQA specification requires you to have practical experience of coding user-defined
classes, involving:
• abstract, virtual and static methods
• inheritance
• aggregation
• polymorphism
• public, private and protected specifiers.
We recommend that you use online documentation to find out what the syntax for this
looks like in the programming language that you will use when you sit the exam.
Virtual methods are methods in a base class that can be overridden by derived classes.
This allows subclasses to provide their specific implementation while maintaining a
consistent method interface. Virtual methods facilitate polymorphism, enabling method
behaviour to vary based on the object that invokes the method.
Virtual methods belong to the class itself rather than any instance of the class. They can
be called on the class directly and do not require an instance to be invoked.
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.
https://bit.ly/pmt-edu
Inheritance arrows should always point upwards.
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.
Student
- Name: String
- Age: Integer
+ GetName
+ GetAge
+ SetDetails
https://bit.ly/pmt-edu
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.
https://bit.ly/pmt-edu