Object-oriented programming

Object-oriented programming 37C00200 Information Systems Development Antti Salovaara 8 March 2016 1 1 Learning objectives •  Separation of concern...
Author: Darlene Paul
2 downloads 2 Views 2MB Size
Object-oriented programming 37C00200 Information Systems Development Antti Salovaara 8 March 2016

1

1

Learning objectives •  Separation of concerns as a principle of good software design •  What is object-oriented analysis and programming (OOA / OOP) –  Objects and classes –  Why OOA supports good software design

•  Two OOP modeling methods –  Class diagrams –  Use cases

2

Software complexity 1.  Repeat: 2.  PersonNum = 1 3.  Repeat: 4.  CardNum = 1 5.  Take card 6.  Give card to person (personNum) 7.  if (CardNum = N): 8.  Stop repeat 9.  Else: 10.  CardNum = CardNum +1 11.  if (personNum = M): 12.  Stop 13.  Else: 14.  PersonNum = PersonNum + 1 15. End program (our example from previous lecture; 15 lines)

QuikSort Average iPhone app World of Warcraft (server side)

15,000 5,000,000

Google Chrome browser

8,000,000

Boeing 787 Microsoft Office 2013

14,000,000 44,000,000

Large Hadron Collider @ CERN Facebook

50,000,000 62,000,000

All Google internet services

http://www.wired.com/2015/09/google-2-billion-lines-codeand-one-place/ http://www.informationisbeautiful.net/visualizations/million-lines-of-code/

3

Lines of code: 20

2,000,000,000

http://www.informationisbeautiful.net/ visualizations/million-lines-of-code/

4

What happens when software grows: “Spaghetti code” •  Parts of software use each other •  In already quite small projects this starts to lead to “spaghetti code”

http://www.actionscript.org/resources/articles/829/2/ Centralized-Event-Management-in-ActionScript-3/Page2.html

5

What is Spaghetti Code - video

https://www.youtube.com/watch?v=YrOiuIjLp9k (watch until 1:40) 6

An example of spaghetti Showing CO2 emissions: 1.  Find out what travel method user has selected 2.  If travel method is “car”: 3. 

Calculate one way

4.  Else if travel method is “ship”: 5. 

Calculate another way

6.  Else if travel method is “bicycle”: 7. 

Calculate third way

8.  Show the calculated value to user

You are developing a travel-related web service. Customers can compare travel methods (car, ship, bicycle) based on their CO2 emissions. çYou calculate the CO2 emissions in this way

But this causes problems: The same calculations are needed in many places in your software. If you need to change the calculation formula, you have to remember to change it everywhere Adding a new travel method (e.g., bus) requires also similar changes è lots of errors

7

An example of spaghetti You are developing a travel-related web service.

Your calculation code snippets

Lots of identical calculations in different places L 8

Customers can compare travel methods (car, ship, bicycle) based on their CO2 emissions. çYou calculate the CO2 emissions in this way

But this causes problems: The same calculations are needed in many places in your software. If you need to change the calculation formula, you have to remember to change it everywhere Adding a new travel method (e.g., bus) requires also similar changes è lots of errors

Summary of challenges Process Uncertain requirements (Lectures 2 & 3)

Requirements engineering Early user/customer feedback Agile development

Error-proneness (Lecture 3)

Iteration Test-driven development

Complexity Spaghetti code

Separation of concerns Modular design with functions Object-oriented programming

Architecture 9

Good architecture design

Separation of concerns / modularization / encapsulation / Two ways in which programming languages support it: 1.  Functions 2.  Object-oriented programming

10

Modularity = Encapsulation = Separation of concerns = “Divide your software into modules that can be used without knowing how they work”

11

1. Solving complexity using functions

12

Example of travel service continues… Instead of this:

Using functions this will be much simpler:

Showing CO2 emissions:

Showing CO2 emissions:

1.  Find out what travel method user has selected 2.  If travel method is “car”: 3.  Calculate one way 4.  Else if travel method is “ship”: 5.  Calculate another way 6.  Else if travel method is “bicycle”: 7.  Calculate third way 8.  Show the calculated value to user

1.  Find out what travel method user has selected 2.  Use separate function to calculate CO2 3.  Show the calculated value to user

Lots of identical calculations in different places L 13

Separate function: 1.  If travel method is “car”: 2.  Calculate one way 3.  Else if travel method is “ship”: 4.  Calculate another way 5.  Else if travel method is “bicycle”: 6.  Calculate third way 7.  Return answer to code that called this function

All calculations in one place J

Example of travel service continues… Instead of this:

Using functions this will be much simpler:

Function

Lots of identical calculations in different places L 14

All calculations in one place J è Separation of concerns

Functions – interactive illustration

•  Illustrates: –  Iterative work process –  Writing only small pieces at a time –  Use of functions –  Separation of concerns

Drawing this diagram using Scratch language: https://studio.code.org/s/20-hour/ stage/15/puzzle/10 15

Another example of separation of concerns •  Web pages have a clear separation of concerns: –  HTML: the content –  CSS: the visual layout –  JavaScript: the interaction

•  Demo: http://codepen.io/asalovaa/pen/yYbZpO

16

2. Solving complexity using object-oriented programming

17

Object-oriented programming •  Is a more sophisticated solution for developing modular architecture •  Is a programming approach that models the world as objects and their interactions •  All major programming languages are object-oriented –  Java, JavaScript, Python, PHP, C++, C#, Scala, …

18

Objects and classes Vehicle

Classes form hierarchies

Car

Objects are unique instances of classes

Image credits: see end of presentation

19

Ship

Bicycle

Objects know and can do things Vehicle

Is a

Car Seat count Tyre size How much fuel left Put air conditioning on

20

Weight Power source Max speed

Data (know)

Calculate CO2 emissions

Function (do)

Is a

Is a Bicycle

Ship Crew size Cargo contents

Tyre size Handle bar type

Example of travel service continues… Using functions it was simple:

Even more simple with objects:

Showing CO2 emissions:

Showing CO2 emissions:

1.  Find out what travel method user has selected 2.  Use separate function to calculate CO2 3.  Show the calculated value to user

1.  Get a Vehicle object that tells all the info about the chosen travel method 2.  Ask the object to calculate the CO2 3.  Show the calculated value to user

Separate function: 1.  If travel method is “car”: 2.  Calculate one way 3.  Else if travel method is “ship”: 4.  Calculate another way 5.  Else if travel method is “bicycle”: 6.  Calculate third way 7.  Return answer to code that called this function 21

Vehicle

calculateCO2()

Car

Ship

Bicycle

calculate CO2()

calculate CO2()

calculate CO2()

Example of travel service continues… Even more simple with objects: Classes provide very good separation of concerns Each class knows its own way to calculate CO2 emissions

Showing CO2 emissions: 1.  Get a Vehicle object that tells all the info about the chosen travel method 2.  Ask the object to calculate the CO2 3.  Show the calculated value to user

Other parts of software only need to ask the value The programming language, not the programmer, takes care choosing the correct version of calculateCO2() = programmer does not need to write the if… code as in the version that used a separate function 22

Vehicle

calculateCO2()

Car

Ship

Bicycle

calculate CO2()

calculate CO2()

calculate CO2()

Separation of concerns using classes Vehicle Weight Max speed User’s choice

Code that shows CO2 emissions: This code does not need to know what travel method user has chosen. It just asks the travel method object to do its calculation and return some value. Code shows the value to user.

23

•  Each class is only responsible for data and operations that naturally belong to its class •  How a class stores its data and does its operations can be changed independently of the rest of the software •  Object-oriented software is written so that classes never mess with other classes’ internal issues

Two modeling methods

Class diagrams Use cases

24

Class diagrams All the previous drawings have been examples of class diagrams

Vehicle Weight Power source Max speed Calculate CO2

Is a Car Seat count Tyre size How much fuel left Put air conditioning on 25

Inheritance relationship: Cars “inherit” the properties and functions from Vehicle: All cars also have a weight, max speed etc. If Vehicle calculates CO2, then Car must also do that.

Membership relationship: Used to show when data within a class consists of instances of other classes Example: Customer register contains many (*) Customers. Each Customer is listed in only one (1) Customer Register.

CustomerRegister List of all customers

1 * Customer Name Address List of previous orders Is unreliable (yes/no)

Example of a membership diagram Can you spot any violations in the separation of concerns? A class should be responsible only for its own issues. OrderProcessor

CustomerRegister List of unreliable customers List of all customers

1

Orders in process Finished orders Customer register

1

Ship a product to customer

Ship a product to customer

1

1

*

*

Customer Name Address List of previous orders Is unreliable (yes/no)

26

Orders

*

1

Product name How many

Class diagrams as architecture descriptions

“OD&D combat simulator UML class diagram” by elisha_abuyah, licensed under Creative Commons Attribution 2.0 Generic (CC BY 2.0). https://flic.kr/p/dNwzT4 27

Use cases •  Are not only useful in requirements gathering •  Are useful as starting points for object-oriented modeling

Ship product

Customer register Workers

Customer Warehouse worker

Customer

Order Processor

Order processor

Change address

Address

*

Ship product

Change address

1 1

Customer Register

Customer Register

1 Class 28

Interactions between classes

Classes

List of customers

Use cases…

Person

Worker

Address

Is a

Change address

Ship product

Order processor

Order Processor Customer register Workers

Customer Warehouse worker

Customer

Is a

Change address

Address

*

Ship product

Change address

1 1

Customer Register

Customer Register

1

29

List of customers

Which parts need more explanation? •  Spaghetti code •  Separation of concerns •  Functions •  Classes •  Objects •  Inheritance in class diagram •  Membership in class diagram •  Use cases

30

Use case diagrams Apply customer discount

Customer

Buy Product

Set warehouse status 31

Sales dept

Warehouse

Example

Apply customer discount

Customer

Buy Product

Set warehouse status 32

Sales dept

Warehouse

Apply customer discount

Example





Buy massive amounts

Customer

Buy as a gift



Sales dept

Buy Product

Set warehouse status 33

Campaigns

Warehouse

Learning objectives •  Separation of concerns as a principle of good software design •  What is object-oriented analysis and programming (OOA / OOP) –  Objects and classes –  Why OOA supports good software design

•  Two OOP modeling methods –  Class diagrams –  Use cases

34

Guest lecture on agile •  10 March Thursday: •  Petri Heiramo, Futurice, Certified scrum trainer

35

Image credits “car” by Tom Hodgkinson, Licensed under Creative Commons Attribution-ShareAlike 2.0 Generic (CC BY-SA 2.0). https://flic.kr/p/ 51sSY “Car” by llee_wu, Licensed under Creative Commons AttributionNoDerivs 2.0 Generic (CC BY-ND 2.0). https://flic.kr/p/pacW5Z “New car!” by Joe Brockmeier, Licensed under Creative Commons Attribution-ShareAlike 2.0 Generic (CC BY-SA 2.0). https://flic.kr/p/M2QkD “COKALIONG SHIPPING” by Tonee Despojo Licensed under Attribution-NonCommercial 2.0 Generic (CC BYNC 2.0). https://flic.kr/p/6DgoBV “Cruise ship” by Remon Rijper, Licensed under Creative Commons Attribution-NonCommercial-NoDerivs 2.0 Generic (CC BY-NC-ND 2.0). https://flic.kr/p/67dnrS “bike” by hellodeer, Licensed under Attribution-NonCommercialNoDerivs 2.0 Generic (CC BY-NC-ND 2.0). https://flic.kr/p/HMcuF 36