Fundamentals of Programming Paradigms Computer Science Programming Paradigms

  • Slides: 47
Download presentation
Fundamentals of Programming Paradigms Computer Science

Fundamentals of Programming Paradigms Computer Science

Programming Paradigms • We’ve seen this throughout the course so far, but almost every

Programming Paradigms • We’ve seen this throughout the course so far, but almost every problem has multiple ways of solving it – This includes different ways of thinking about those solutions • While this definitely applies to thinking up algorithms, it also applies to how programming languages are used • For example, we’ve seen that Haskell functions very differently to other programming languages 2 Fundamentals of Programming: Programming Paradigms

Programming Paradigms • There is an interesting term that applies to many walks of

Programming Paradigms • There is an interesting term that applies to many walks of life: paradigm Paradigm: a pattern or model of something • It’s almost like a state of mind – Paradigms help us think with set methods when solving (or even thinking about) problems 3 Fundamentals of Programming: Programming Paradigms

Programming Paradigms • The reason we’re mentioning this is because there are multiple ways

Programming Paradigms • The reason we’re mentioning this is because there are multiple ways of thinking about solutions – In terms of programming • More technically, we can model a solution in different ways – Often aided by different programming languages • These different models we can come up with are created through programming paradigms 4 Fundamentals of Programming: Programming Paradigms

Programming Paradigms • While there are multiple programming paradigms out there, this course is

Programming Paradigms • While there are multiple programming paradigms out there, this course is particularly interested in two – Procedural Programming – Object-Orientated Programming • These two paradigms can be used to create an implementation of the same solution • However, how the programmer designs and implements this solution is drastically different 5 Fundamentals of Programming: Programming Paradigms

Programming Paradigms • Before we cover these paradigms in depth, here is a cursory

Programming Paradigms • Before we cover these paradigms in depth, here is a cursory comparison of them Aspect Procedural Design Bases all programs off of local/global variables and subroutines Creates programs to act as simulations (using objects) Top-down Bottom-up Quite insecure (as global variables can be accessed freely from any function) Secure (as data can be hidden inside objects) Very code complex (unique subroutines need to be created for every part of a program) Very design complex (as a thorough set of entities and relationships needs to be thought through) Approach Security Complexity 6 Object-Orientated Fundamentals of Programming: Programming Paradigms

Structured Programming (1) • The Procedural paradigm exists through the idea of making a

Structured Programming (1) • The Procedural paradigm exists through the idea of making a solution for a problem – In simpler terms, we’re thinking of the code we’re creating purely as a program • Procedural programming is actually based off another paradigm, but with its own additions to the methodology – That other paradigm: structured programming 7 Fundamentals of Programming: Programming Paradigms

Structured Programming (1) • Let’s understand structured programming before we look at what procedural

Structured Programming (1) • Let’s understand structured programming before we look at what procedural programming added to the mix • This paradigm thinks of programs as a series of statements that directly change computed state – I. e. change the values in registers or memory addresses • Structured programming introduced the idea of control structures – What we know as sequences, selection, iteration, and recursion 8 Fundamentals of Programming: Programming Paradigms

Structured Programming (1) • It was theorised that, from these three control structures, any

Structured Programming (1) • It was theorised that, from these three control structures, any type of program could be described • It was also structured programming that introduced the idea of modules – Small aspects of a solution – Can be dependent/independent of other modules • We can think of the implementation of modules as subroutines 9 Fundamentals of Programming: Programming Paradigms

Structured Programming (1) • Structured programming makes use of a structured approach to design

Structured Programming (1) • Structured programming makes use of a structured approach to design and construct programs • The structure approach follows these steps: Structured design can also be referred to as stepwise refinement (or, top-down design). 1. Take the current problem 2. If it is too large (or contains multiple problems), split it up into subproblems 3. Repeat step 1 for each of the sub-problems 4. Create separate modules/subroutines for each sub-problem 10 Fundamentals of Programming: Programming Paradigms

Structured Programming (1) • For example, let’s say we want to create a program

Structured Programming (1) • For example, let’s say we want to create a program that converts Fahrenheit to Celsius • Like the decomposition we’ve looked at before, we can split this whole problem up • We can see from this that our problem really has three parts 11 1. Convert Fahrenheit to Celsius a. Get Fahrenheit from user b. Calculate Celsius value c. Display Celsius value Fundamentals of Programming: Programming Paradigms

Hierarchy Charts (1) • What if we want a more thorough description of these

Hierarchy Charts (1) • What if we want a more thorough description of these sub-problems (perhaps something that is closer to what we would program)? • Rather than showing the sub-problems like we have done before, we can use something called a hierarchy chart • These charts show the problem (split up like we saw in the list structure earlier) – However, they also show data flow – This includes data going into a sub-problem, and data coming out 12 Fundamentals of Programming: Programming Paradigms

Hierarchy Charts (1) • Here is an example of a hierarchy chart – Each

Hierarchy Charts (1) • Here is an example of a hierarchy chart – Each sub-problem spans off the problem (like children) 13 Fundamentals of Programming: Programming Paradigms

Hierarchy Charts (1) • We can even add the input and output for each

Hierarchy Charts (1) • We can even add the input and output for each sub-problem – Arrows going down are input – Arrows coming up are output 14 Fundamentals of Programming: Programming Paradigms

Hierarchy Charts (1) • Based off of this hierarchy chart, we can then create

Hierarchy Charts (1) • Based off of this hierarchy chart, we can then create subroutines for these sub-problems/modules • One key to good structural design is to try and keep modules independent – The fewer modules one module depends on, the better – That’s because it could then easily be used for a different purpose – Always try and make these modules as general as possible – They call this reliance on other modules coupling, and the lower the coupling the better 15 Fundamentals of Programming: Programming Paradigms

 • Create a hierarchy chart for the following problem – Feel free to

• Create a hierarchy chart for the following problem – Feel free to decompose this problem in whatever way you see fit A manager needs to calculate the pay for a single employee. This employee has two types of hours to fill in a value for – regular, and overtime. The pay needs to be calculated by multiplying each hour group by the correct value. Once calculated, the manager needs to ‘output’ this pay to the employee, 16 Fundamentals of Programming: Programming Paradigms

Hierarchy Charts (2) • We can also use hierarchy charts to represent control flow

Hierarchy Charts (2) • We can also use hierarchy charts to represent control flow • We usually write modules in the order they are executed in (from left -to-right) • We can also include notation for when a module needs to be repeated 17 Fundamentals of Programming: Programming Paradigms

Hierarchy Charts (2) • Here’s an example that repeats modules until a condition is

Hierarchy Charts (2) • Here’s an example that repeats modules until a condition is met – In this case, we have found enough primes to know we’ve found the nth one 18 Fundamentals of Programming: Programming Paradigms

Structured Programming (2) • There are quite a few advantages to structured design 19

Structured Programming (2) • There are quite a few advantages to structured design 19 Advantage Explanation Functional Cohesion Well mode functions perform only one logically-related task, which enable them to be replaced by another function serving the same task Individualised Tasks As these modules are independent (and complete one task), programmers can focus on one module at a time (rather than worrying about completing everything at once) Psychological Limitations Most humans manage information in chunks (roughly five separate pieces of information at once), which fits well when creating smaller modules Easy to Debug Due to the small nature (and independence) of modules, programmers can easily test/debug a module with test data (without having to complete the whole program) Fundamentals of Programming: Programming Paradigms

Procedural Programming • We’ve talked about structured programming this whole time • What about

Procedural Programming • We’ve talked about structured programming this whole time • What about procedural programming? • In reality, there is no difference between them! – They both focus on splitting problems into sub-problems – They both make use of subroutines (procedures/functions) to solve problems – They both make use of local/global data • However, while procedural programming is limited to this, structured programming has been modified into other paradigms as well – Like Abstract State Machine programming 20 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • While procedural programming is still highly popular, and very useful, a

Object-Orientated Programming • While procedural programming is still highly popular, and very useful, a lot of programs are being made with the Object-Orientated Programming paradigm • There a lot of features to look into with this paradigm • We’ll start with some terms we’ll need to learn – Then move on to specific things we can implement in a program 21 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming Term Definition Class A definition for a self-contained group of properties (instance

Object-Orientated Programming Term Definition Class A definition for a self-contained group of properties (instance variables) and actions (instance functions) that can be used in modelling complex simulations. They typically capture the common behaviours and characteristics of something in the world (like a Person or a Book). Object A specific version of a class (for example, a Person could have objects for Jane and John). Each object will have its own values for those properties (instance variables). Instantiation Creating an object of a class – more technically, makes the computer set memory aside in RAM for the object’s properties. The memory address used for the object is returned during instantiation (and can often be saved in a variable). Constructor A specific action within a class that is run whenever an object is instantiated. This action is run automatically. Specific data can be asked for in the constructor, making this data a requirement when instantiating an object of that class. 22 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • The main methodology of an Object-Orientated programmer is to thoroughly define

Object-Orientated Programming • The main methodology of an Object-Orientated programmer is to thoroughly define the entities in a system/simulation – This is done before thinking about using these entities in any program • Object-Orientation is about trying to create systems the skew as close to real life as possible • This doesn’t mean making everything that could exist – We still focus on only the information needed – But we make the entities general enough that they could be used in any system/simulation that would use that entity 23 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • Object-Orientated programmers will spend a great deal of time designing these

Object-Orientated Programming • Object-Orientated programmers will spend a great deal of time designing these entities – classes – before thinking about implementing a program • For example, let’s define a Book – Classes are often singular • Which has the following – A title – An author 24 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • This Book has two properties (the author and title), which will

Object-Orientated Programming • This Book has two properties (the author and title), which will have the ability to be unique for every object • However, this Book doesn’t have a way of an object being given a value for either of these – As at the moment they are private – Which means they cannot be directly accessed 25 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • When we instantiate an object of this class, we need to

Object-Orientated Programming • When we instantiate an object of this class, we need to give values to these variables • We can do this using a constructor – Which will require this data when we instantiate an object of this class • This means the variables will now have a way of getting values 26 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • If we want to use this class in a practical setting

Object-Orientated Programming • If we want to use this class in a practical setting (like using it to solve a problem), we need to instantiate an object • As said before, instantiating an object returns a memory address • We need to store this address in a variable – This will make the object usable through this variable 27 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming Term Definition Encapsulation The method in which information in an object (like

Object-Orientated Programming Term Definition Encapsulation The method in which information in an object (like its properties or actions) can be hidden from other parts of a simulation. For example, an object could have a ID property that can not be accessed without going through an action (i. e. it cannot be directly used in an expression). Inheritance The method in which information is passed down from one class to another. A lot of programming languages will have their own implementation of inheritance, but they all follow the same steps: create the base class (that contains information all the children classes need), then make any children classes inherit from the base. Can be used to make more efficient simulations (by sharing code). Aggregation When one class uses another (i. e. an object is saved as a property). For example, a Company is an aggregation of People. Even if the Company were to be ‘deleted’, the People would still exist. Composition When one class owns another (i. e. a class defined inside another class, though this doesn’t have to be the case). For example, a Company can have Accounts. If the Company were to be ‘deleted’, the Accounts would be deleted too. 28 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • Notice how two words have been added before the instance variables?

Object-Orientated Programming • Notice how two words have been added before the instance variables? • That is an access modifier • They come in three varieties – Public: can be accessed directly through the object – Private: can only be directly access inside the class – Protected: same as private, but can also be directly accessed by children classes • This is Encapsulation 29 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • We can extend the Book to make it into a writable

Object-Orientated Programming • We can extend the Book to make it into a writable book – Like a journal or diary • Different languages extend classes in different ways – Typically done in the class’s signature 30 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • Notice the use of super in the constructor? • Whenever a

Object-Orientated Programming • Notice the use of super in the constructor? • Whenever a class inherits from a class that has a constructor, we need to run that constructor here • The super keyword essentially points to the parent (Book in this case) – The keyword on its own runs the constructor – We can add properties/actions after the keyword to run the parent’s versions of these 31 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming Term Polymorphism Definition The method in which inherited actions can be adjusted

Object-Orientated Programming Term Polymorphism Definition The method in which inherited actions can be adjusted to suit the more specific class. For example, an Animal could have the action speak. A Dog (a more specific Animal) might change how speak works so that the Dog barks. A Cat (another specific Animal) would change speak so that the Cat meows. This also applies to changing actions within a class (to let the class decide which action to use based on the input data). In simpler terms, Polymorphism is all about changing/mutating information and actions based off of the current class, or input data. Overriding The specific technique for changing an inherited action (see above). Overloading The specific technique for changing an action within a class based off input data (see above). 32 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • A lot of programming languages have built-in functions for converting data

Object-Orientated Programming • A lot of programming languages have built-in functions for converting data into Strings (and other data-types) • Typically this outputs the memory address of the object • We can override this function to change how it works 33 Fundamentals of Programming: Programming Paradigms

Object-Orientated Programming • We can also overload a function – If we want to

Object-Orientated Programming • We can also overload a function – If we want to change its behaviour based on input data • Most languages support overloading • The important thing is that the datatypes and number of parameters in the function must be different 34 Fundamentals of Programming: Programming Paradigms

 • Have some practice creating and overriding classes! 1. Create an Animal class

• Have some practice creating and overriding classes! 1. Create an Animal class that has a speak action. 2. Then create three specific children classes 3. For each of these children classes, change how speak works (based on the animal). 4. Then, create a list of Animals, and give it at least one instance of each of the children classes 5. Loop through this list and run its speak function 35 Dog, Cat, and then whatever other animal you would like! Does the output make sense for each Animal? Fundamentals of Programming: Programming Paradigms

UML Class Diagrams • There are many techniques used to help design pieces of

UML Class Diagrams • There are many techniques used to help design pieces of software • One is especially useful – For object-orientated programs • Known as Unified Modelling Languages – Shortened to UML 36 Fundamentals of Programming: Programming Paradigms

UML Class Diagrams • It’s actually a pictorial language – Meaning the components of

UML Class Diagrams • It’s actually a pictorial language – Meaning the components of the language are illustrative • UML can be used for all kinds of things – Though the one we’re interested in OOP software design is in making Class Diagrams • Class Diagrams are used to illustrate relationships between classes – As well as what those classes have/can do 37 Fundamentals of Programming: Programming Paradigms

UML Class Diagrams • In Class Diagrams, we use simple diagrams to represent individual

UML Class Diagrams • In Class Diagrams, we use simple diagrams to represent individual classes – Rectangles that contain three sections The name of the class The attributes/properties of the class The procedures/functions of the class 38 Fundamentals of Programming: Programming Paradigms

UML Class Diagrams The Class Name • Simply the name of the class being

UML Class Diagrams The Class Name • Simply the name of the class being created! The Attributes/Properties • These are things that every object of the class will have • Examples: names, ages, heights, weights The Procedures/Functions • These are things that every object of the class can do • Examples: login(), eat(), output. Info(), collide. With() 39 Fundamentals of Programming: Programming Paradigms

UML Class Diagrams • You may have also noticed symbols before each attribute/procedure +

UML Class Diagrams • You may have also noticed symbols before each attribute/procedure + - * • These are access modifiers – The + : public; can be accessed from any class – The - : private; can only be accessed in that class it belongs to – The * : protected; can only be accessed by family member classes • That includes the current class and any children 40 Fundamentals of Programming: Programming Paradigms

 • Create a class for the following class diagram • You can make

• Create a class for the following class diagram • You can make these procedures/functions do whatever you want 41 Fundamentals of Programming: Programming Paradigms

UML Class Diagrams • Class Diagrams can also be used to show relationships between

UML Class Diagrams • Class Diagrams can also be used to show relationships between classes • The simplest of which is inheritance – After making both classes – We link the child to the parent – So the arrow points to the parent class 42 Fundamentals of Programming: Programming Paradigms

UML Class Diagrams • There are three more relationships to think about • Association:

UML Class Diagrams • There are three more relationships to think about • Association: where two classes use each other – As stored attributes/properties 0. . 1: At most one object 1. . *: At least one object For any N. . M: N – the lowest allowed number M – the most allowed number 43 Fundamentals of Programming: Programming Paradigms

UML Class Diagrams • Aggregation: when one class uses another – But the used

UML Class Diagrams • Aggregation: when one class uses another – But the used class is still independent – It can still exist even without the using class • In this example, a Lesson has a Teacher and multiple Students • However, if the Lesson were to be closed, the Teacher and Students would still be ‘alive’ 44 Fundamentals of Programming: Programming Paradigms These are simplified classes We don’t bother with attributes or functions

UML Class Diagrams • Composition: when one class owns another – The owned class

UML Class Diagrams • Composition: when one class owns another – The owned class is dependent on the owner – If the owner is destroyed, the owned class is too • In this example, the School owns the Departments • If the School is destroyed, the Departments are destroyed as well 45 Fundamentals of Programming: Programming Paradigms

UML Class Diagrams • Here’s a summary of these relationships 46 Relationship Arrow Description

UML Class Diagrams • Here’s a summary of these relationships 46 Relationship Arrow Description Inheritance White/clear arrow from child to parent When one class extends another (to reuse a code base) Association Line between the classes with numbers for the number of objects in the relation When two classes use each other (like a list of classes having a reference to their user) Aggregation White/clear diamond (with diamond closest to using class) When independent classes are used by another (such as an engine in a car) Composition Black/filled diamond (with diamond closest to owning class) When dependent classes are owned by another (such as rooms being built in a house) Fundamentals of Programming: Programming Paradigms