<<

Programming Methodologies

• Unstructured Programming G6DICP - Lecture 22 •

The Theory of • Modular Programming Object Oriented Programming • Object Oriented Programming

2

Unstructured Programming Procedural Programming

The main program consists of Sequences of statements are combined statements that directly operates on into one place called a procedure. This global data (ie data that is always can then be invoked as often as needed by available). the main program. Program Program main program Procedure data main program

3 Procedure 4

Properties of Modular Modular Programming Programming modules • Programs consist of several (sometimes many) Procedures are grouped into or interacting parts. libraries. • Libraries can be re-used. Program • Each module can have its own data, and can manage its own internal state. main program • Each module exists once in a program, and has a single state.

Module1 Module1 • Management of the state of a module can become complex. Procedure Procedure Procedure 1 2 3 5 6 Object Oriented Programming Object Oriented Languages

• OO programs consist of a web of interacting • Pure OO languages objects - each with their own state. – Smalltalk • Each object is responsible for initialising and – Java destroying itself correctly. • Impure OO languages • This addresses the complexities of modular – ++ programming. – – Pascal/ • Objects have other attributes that greatly add to • Mixed paradigm languages the power of manipulating them. – Visual Basic – JavaScript 7 8

Objects Properties of OO Programming

• The world is full of objects. • Encapsulation – Combining data with the code that acts upon that data • OO programming was originally developed to form a new data-type - an object. for the creation of simulations. • Inheritance • Because programs ultimately interact with – Arranging objects into a hierarchy of descendant the real world, many (the vast majority) of objects, with each descendant inheriting access to all of programming problems can be solved by its ancestors code and data. mapping program code to “objects” that • Polymorphism – A single action may be used in different ways in mirror real world objects. different contexts - the implementation of that action being appropriate to the current usage. 9 10 • Dynamic method binding

Encapsulation Classes

• Objects model the real world - they are the ultimate • In most OO languages encapsulation is form of data abstraction. implemented by the class. • Encapsulation means keeping all of the constituents • Java, C++, , and many other of an object in the same place. programming languages implement OO in this • Consider an orange: way. – Mathematical view - abstracted into separate components • Classes are user-defined data types that (area of skin, weight, fluid volume, number of seeds etc). encapsulate code (methods) together with data – Painters view - encapsulated on canvas an abstract whole. (variables). • Encapsulation ensures that the relationships between • Each object is a separate instance of a class, and the components of an object are preserved. therefore has its own state.

11 12 The Taxonomy of Insects Inheritance (simplified) • Much of human thought is hierarchical Insects • Hierarchies are trees, with a single overall category at the top, and increasing diversity Winged Insects Wingless Insects further down. • Characteristics are inherited down the hierarchy (ie any daughter category will Flies Social Insects Butterflies Beetles inherit the properties of its parents). • An example is biological taxonomy. Bees Wasps Ants

13 14

Objects: Hierarchical Taxonomy Data structures that inherit (1) • Consider a program that handles graphics. • Consider • We might define a series of classes to draw shapes on the – How similar is an item to the others of its general screen. class? • The top level class is Location – In what ways does it differ from them? • This represents a position on screen • Each category has a set of behaviours and characteristics that define it. Location • The highest levels are the most general (ie the X co-ordinate (integer) most simple)- lower levels become more Y co-ordinate (integer) specific. • Once a characteristic is defined all categories below that in the hierarchy inherit that 15 16 characteristic

Objects: Objects: Data structures that inherit (2) Data structures that inherit (3) • If we want to display a pixel we can use a • Instances of class point (objects) subclass Point. firstPoint

Point (subclass of Location) secondPoint ( inherited - X co-ordinate ) ( inherited - Y co-ordinate )

visible (boolean) thirdPoint

17 18 Objects: Objects: Data structures that inherit (4) Data structures that inherit (5) • Classes contain data (X co-ordinate, Y co-ordinate • Methods and variables may be public (ie invoked and visible), encapsulated with code that operates from anywhere), or private (ie only invoked from on that data. other methods in the class) • A method called drawPoint • A class may have a constructor (a method that is automatically invoked when an instance of the class is created). Point (subclass of Location) • A class may have a destructor (a method that is

( inherited - X co-ordinate ) automatically invoked when an object is destroyed).

( inherited - Y co-ordinate ) NB Java does not use destructors! visible (boolean) drawPoint (method) 19 20

Objects: Objects: Data structures that inherit (6) Data structures that inherit (7) Point (subclass of Location) • Point may be subclassed as Circle or Square public ( inherited - X co-ordinate ) public ( inherited - Y co-ordinate ) Circle (subclass of Point) public visible (boolean) ( inherited - X co-ordinate ) private drawPoint (method) ( inherited - Y co-ordinate ) private deletePoint (method) ( inherited - visible ) public Point (constructor) - calls drawPoint radius - integer public togglePoint - calls drawPoint or deletePoint Square (subclass of Point) ( inherited - X co-ordinate )

21 ( inherited - Y co-ordinate ) 22 ( inherited - visible )

Objects: Data structures that inherit (8) Circle (subclass of Point) ( inherited - X co-ordinate ) • NB This is not implemented in Java! ( inherited - Y co-ordinate ) • It is implemented in C++ ( inherited - visible ) • A class may have more than one parent. radius - integer Circle (constructor) Point String togglePoint (inherited but overridden) Square (subclass of Point) ( inherited - X co-ordinate ) ( inherited - Y co-ordinate ) ( inherited - visible ) Drawable String length of side - integer Square (constructor) togglePoint (inherited but overridden) 23 24 Polymorphism Dynamic Method Binding

• Although methods are inherited, their behaviour • Where several possible methods are available sometimes needs to be modified at different (eg polymorphic methods) the appropriate points in the hierarchy. method does not need to be indicated to the . • The behaviour must be appropriate for the context of use. • The decision as to which method to use is made at runtime. • For example - the X,Y coordinates of location could be absolute pixel values or percentages of • In Java, this means that the VM selects the the screen. A polymorphic method would correct method to use at runtime. implement the appropriate functionality. 25 26