Whats the point of Object Orientation? Iwan Vosloo EuroPython 2016 - - PowerPoint PPT Presentation

what s the point of object orientation
SMART_READER_LITE
LIVE PREVIEW

Whats the point of Object Orientation? Iwan Vosloo EuroPython 2016 - - PowerPoint PPT Presentation

Whats the point of Object Orientation? Iwan Vosloo EuroPython 2016 Introduction About ValidationConstraint Layout Button Action post.save() https://goo.gl/3DMHFH EuroPython 2016 Introduction About data_table = DataTable(view,


slide-1
SLIDE 1

EuroPython 2016

What’s the point of Object Orientation?

Iwan Vosloo

slide-2
SLIDE 2

EuroPython 2016

Introduction

About

ValidationConstraint Action Layout Button post.save()

https://goo.gl/3DMHFH

slide-3
SLIDE 3

EuroPython 2016

Introduction

About

https://goo.gl/3DMHFH

data_table = DataTable(view, ….. )

slide-4
SLIDE 4

EuroPython 2016

Introduction

About / 2

slide-5
SLIDE 5

EuroPython 2016

Introduction

OOP is not OO

Supporting mechanisms for OO Conceptual model

  • f a

problem domain Programming language map to Code

slide-6
SLIDE 6

EuroPython 2016

Introduction

Cause and effect

slide-7
SLIDE 7

EuroPython 2016

Introduction

Understand a program?

slide-8
SLIDE 8

EuroPython 2016

Introduction

Scaling up

100 lines

  • f code?

Courtesy: inkwellideas.com

slide-9
SLIDE 9

EuroPython 2016

Introduction

How about...

180 000 lines

  • f code?
slide-10
SLIDE 10

EuroPython 2016

Introduction

Understanding

  • pportunity

u s e l e s s i n f

  • r

m a t i

  • n

d a n g e r

slide-11
SLIDE 11

EuroPython 2016

Introduction

A scary thought

slide-12
SLIDE 12

EuroPython 2016

Concepts

A conceptual model

Chair

slide-13
SLIDE 13

EuroPython 2016

Concepts

Concepts as sets

Chair Desk

slide-14
SLIDE 14

EuroPython 2016

Concepts

Refinement via subsets

Chair Office Chair

slide-15
SLIDE 15

EuroPython 2016

Relations

Connecting objects

Chair Desk

slide-16
SLIDE 16

EuroPython 2016

Relations

Relation as a concept

Assignment

slide-17
SLIDE 17

EuroPython 2016

Relations

Multiplicity

Chair Desk

slide-18
SLIDE 18

EuroPython 2016

Notation

It gets complicated...

Chair Desk Assignment Office Chair

slide-19
SLIDE 19

EuroPython 2016

Notation

UML

Desk Chair OfficeChair assignment

1 *

slide-20
SLIDE 20

EuroPython 2016

More on concepts

Intangible concepts

Portfolio Investment Instrument UnitTrust

slide-21
SLIDE 21

EuroPython 2016

More on concepts

Overlapping concepts

Person Investor

slide-22
SLIDE 22

EuroPython 2016

More on concepts

Changing classification

Person Employee Investor

slide-23
SLIDE 23

EuroPython 2016

More on concepts

Concept labels

Concept Synonym Type Concept Label Chair

slide-24
SLIDE 24

EuroPython 2016

Affecting objects

Operations

Price PriceFile UnitTrust Fund

* load_prices( )

slide-25
SLIDE 25

EuroPython 2016

Summary

Operations vs methods

Operation Object 1..*

  • perates on

Method 1..* 1 is implemented by

slide-26
SLIDE 26

EuroPython 2016

Summary

OO concepts

Object Type classifies 0..* subtype 0..* Relation links 2..* Operation 1..*

  • perates on

Method implemented by 1..*

slide-27
SLIDE 27

EuroPython 2016

How can this help?

Programs are programs

Courtesy: inkwellideas.com

Concept Concept

slide-28
SLIDE 28

EuroPython 2016

How can this help?

Understanding-structured

Courtesy: inkwellideas.com

parse_file: Operation parse_xml: Method parse_csv: Method

slide-29
SLIDE 29

EuroPython 2016

How can this help?

Focus on one thing

FileFormat PriceFile

slide-30
SLIDE 30

EuroPython 2016

How can this help?

Zoom in

FileFormat Excel CSV

slide-31
SLIDE 31

EuroPython 2016

How can this help?

Zoom in more

def parse_file(price_file): for row in price_file: yield row.split(‘,’)

slide-32
SLIDE 32

EuroPython 2016

Implementing OO

OOP and Python

Object Oriented Programming Classical OOP Prototype Based

OOP implementation

slide-33
SLIDE 33

EuroPython 2016

Classical OOP & Python

Types as classes

an instance an instance an instance

slide-34
SLIDE 34

EuroPython 2016

Classical OOP & Python

Python cookies

>>> class InvestmentInstrument: ... pass >>> fund = InvestmentInstrument() >>> fund <InvestmentInstrument object at 0x7f6815559fd0> >>> isinstance(fund, InvestmentInstrument) True >>> type(fund) <class ‘InvestmentInstrument’>

slide-35
SLIDE 35

EuroPython 2016

Classical OOP & Python

Relationships as attributes

Portfolio Person Me You

  • wner
  • wner
slide-36
SLIDE 36

EuroPython 2016

Classical OOP & Python

Attributes

>>> portfolio = Portfolio() >>> someone = Person() >>> portfolio.owner = someone >>> portfolio.owner <Person instance at 0x7fd6d88100e0>

slide-37
SLIDE 37

EuroPython 2016

Classical OOP & Python

Methods as (Python)methods

Investment Instrument def activate(self): self.is_active = True indexed by class Operation

slide-38
SLIDE 38

EuroPython 2016

Classical OOP & Python

A Python method

class InvestmentInstrument: def activate(self): self.is_active = True >>> fund = InvestmentInstrument() >>> InvestmentInstrument.activate(fund) >>> fund.is_active True >>> fund.activate()

slide-39
SLIDE 39

EuroPython 2016

Classical OOP & Python

Initialising instances

class InvestmentInstrument: def __init__(self): self.is_active = False def activate(self): self.is_active = True >>> fund = InvestmentInstrument() >>> fund.is_active False >>> fund.activate()

slide-40
SLIDE 40

EuroPython 2016

Classical OOP & Python

Subtyping as inheritance

class InvestmentInstrument: def __init__(self): self.is_active = False def activate(self): self.is_active = True class UnitTrustFund(InvestmentInstrument): def value_of(self, units): return units * self.unit_price

slide-41
SLIDE 41

EuroPython 2016

Classical OOP & Python

Subtyping as inheritance

>>> fund = UnitTrustFund() >>> isinstance(fund, InvestmentInstrument) True >>> fund.activate() >>> fund.is_active True

slide-42
SLIDE 42

EuroPython 2016

Classical OOP & Python

The ghost of operations

FileFormat Excel CSV

parse( )

slide-43
SLIDE 43

EuroPython 2016

Classical OOP & Python

The ghost of operations

class CSVFileFormat(FileFormat): def parse(self, price_file): for row in price_file: yield row.split(‘,’) class ExcelFileFormat(FileFormat): def parse(self, price_file): # totally different stuff

slide-44
SLIDE 44

EuroPython 2016

Classical OOP & Python

The ghost of operations

FileFormat PriceFile for line in price_file.format.parse(price_file): # do stuff with line

slide-45
SLIDE 45

EuroPython 2016

Classical OOP & Python

The ghost of operations

if price_file.format.is_csv: lines = parse_csv(price_file) elif price_file.format.is_excel: lines = parse_excel(price_file)

else:

raise Exception(‘not supposed to get here’) for line in lines: # do stuff with line

slide-46
SLIDE 46

EuroPython 2016

Design

What’s design?

Person Employee Investor Person Role Employee Investor Company

slide-47
SLIDE 47

EuroPython 2016

Design

Worse design

CommissionCalc Bundles QuoteBasis QuoteFees Commissions AbstractCommission Bundle * QuoteBenefit Benefit Summary Calc * Benefit Grouping * * QuoteBenefit Bundles Benefit Calc * Commission Bundle Altered CommissionBundle * Lookup CommissionScale LookupCommission ScaleFactory * Calculation Constants

slide-48
SLIDE 48

EuroPython 2016

Design

Better design

QuoteScenario Benefit Category * * * * CommisionBundle

includedInAPI? negotiatedMax (R or %LOA)

Commission Scale * Product

VAT

BenefitGrouping * Commission Band

slide-49
SLIDE 49

EuroPython 2016

Design

Inheritance vs subtyping

ObjectTable ObjectTable WithFilters ObjectTable Filter

0..*

Report ObjectTable Filter

*

ObjectTable ByDate

slide-50
SLIDE 50

EuroPython 2016

Thanks

On groups.google.com:

  • reahl-discuss

www.reahl.org

iwan@reahl.org James Martin & James Odell: Object Oriented Methods: A Foundation Martin Fowler: Refactoring Slides: https://goo.gl/NPLnCf