what’s the point of object orientation? - europython 2016 · what’s the point of object...

50
EuroPython 2016 What’s the point of Object Orientation? Iwan Vosloo

Upload: vuonghanh

Post on 19-Jul-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

EuroPython 2016

What’s the point ofObject Orientation?

Iwan Vosloo

EuroPython 2016

Introduction

About

ValidationConstraint

Action

Layout

Button

post.save()

https://goo.gl/3DMHFH

EuroPython 2016

Introduction

About

https://goo.gl/3DMHFH

data_table = DataTable(view, ….. )

EuroPython 2016

Introduction

About / 2

EuroPython 2016

Introduction

OOP is not OO

Supporting mechanisms

for OO

Conceptual modelof a

problem domain

Programminglanguage

map to

Code

EuroPython 2016

Introduction

Cause and effect

EuroPython 2016

Introduction

Understand a program?

EuroPython 2016

Introduction

Scaling up

100 lines

of code?

Courtesy: inkwellideas.com

EuroPython 2016

Introduction

How about...

180 000 lines

of code?

EuroPython 2016

Introduction

Understanding

opportunity

useless

informationdanger

EuroPython 2016

Introduction

A scary thought

EuroPython 2016

ConceptsA conceptual model

Chair

EuroPython 2016

Concepts

Concepts as sets

ChairDesk

EuroPython 2016

Concepts

Refinement via subsetsChair

OfficeChair

EuroPython 2016

Relations

Connecting objectsChair Desk

EuroPython 2016

Relations

Relation as a concept

Assignment

EuroPython 2016

Relations

MultiplicityChair Desk

EuroPython 2016

Notation

It gets complicated...

ChairDesk

AssignmentOfficeChair

EuroPython 2016

Notation

UML

Desk

Chair

OfficeChairassignment 1*

EuroPython 2016

More on concepts

Intangible concepts

PortfolioInvestmentInstrument

UnitTrust

EuroPython 2016

More on concepts

Overlapping concepts

Person

Investor

EuroPython 2016

More on concepts

Changing classification

Person

EmployeeInvestor

EuroPython 2016

More on concepts

Concept labels

Concept

Synonym

Type

Concept

Label

Chair

EuroPython 2016

Affecting objects

Operations

Price PriceFileUnitTrustFund

*

load_prices( )

EuroPython 2016

Summary

Operations vs methods

Operation Object1..*operates on

Method1..*

1

is implemented by

EuroPython 2016

Summary

OO conceptsObject

Type classifies

0..*

subtype0..*

Relation

links

2..*

Operation

1..*

operates on

Method

implemented by1..*

EuroPython 2016

How can this help?

Programs are programs

Courtesy: inkwellideas.com

Concept

Concept

EuroPython 2016

How can this help?

Understanding-structured

Courtesy: inkwellideas.com

parse_file:Operation

parse_xml:Method

parse_csv:Method

EuroPython 2016

How can this help?

Focus on one thing

FileFormatPriceFile

EuroPython 2016

How can this help?

Zoom in

FileFormat

Excel CSV

EuroPython 2016

How can this help?

Zoom in more

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

EuroPython 2016

Implementing OO

OOP and Python

ObjectOriented

Programming

ClassicalOOP

PrototypeBased

OOP implementation

EuroPython 2016

Classical OOP & PythonTypes as classes

an instance

an instance

an instance

EuroPython 2016

Classical OOP & PythonPython cookies

>>> class InvestmentInstrument:... pass

>>> fund = InvestmentInstrument()>>> fund<InvestmentInstrument object at 0x7f6815559fd0>

>>> isinstance(fund, InvestmentInstrument)True

>>> type(fund)<class ‘InvestmentInstrument’>

EuroPython 2016

Classical OOP & PythonRelationships as attributes

Portfolio Person

Me

You

owner

owner

EuroPython 2016

Classical OOP & PythonAttributes

>>> portfolio = Portfolio()>>> someone = Person()>>> portfolio.owner = someone

>>> portfolio.owner<Person instance at 0x7fd6d88100e0>

EuroPython 2016

Classical OOP & PythonMethods as (Python)methods

InvestmentInstrument

def activate(self): self.is_active = True

indexed by class

Operation

EuroPython 2016

Classical OOP & PythonA Python method

class InvestmentInstrument:

def activate(self): self.is_active = True

>>> fund = InvestmentInstrument()>>> InvestmentInstrument.activate(fund)

>>> fund.is_activeTrue >>> fund.activate()

EuroPython 2016

Classical OOP & PythonInitialising instances

class InvestmentInstrument: def __init__(self): self.is_active = False

def activate(self): self.is_active = True

>>> fund = InvestmentInstrument()>>> fund.is_activeFalse>>> fund.activate()

EuroPython 2016

Classical OOP & PythonSubtyping 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

EuroPython 2016

Classical OOP & PythonSubtyping as inheritance

>>> fund = UnitTrustFund()>>> isinstance(fund, InvestmentInstrument)True

>>> fund.activate()>>> fund.is_activeTrue

EuroPython 2016

Classical OOP & PythonThe ghost of operations

FileFormat

Excel CSV

parse( )

EuroPython 2016

Classical OOP & PythonThe 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

EuroPython 2016

Classical OOP & PythonThe ghost of operations

FileFormatPriceFile

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

EuroPython 2016

Classical OOP & PythonThe 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

EuroPython 2016

Design

What’s design?Person

Employee

Investor

Person Role

Employee Investor

Company

EuroPython 2016

Design

Worse designCommissionCalc

Bundles QuoteBasis

QuoteFeesCommissions

AbstractCommissionBundle

*

QuoteBenefit BenefitSummary

Calc

* BenefitGrouping

*

*QuoteBenefit

BundlesBenefit

Calc

*

CommissionBundle

AlteredCommissionBundle

*

LookupCommissionScale

LookupCommissionScaleFactory

*

CalculationConstants

EuroPython 2016

Design

Better designQuoteScenario

Benefit Category*

*

*

*

CommisionBundle

includedInAPI?negotiatedMax (R or %LOA)

CommissionScale

*Product

VAT

BenefitGrouping*

CommissionBand

EuroPython 2016

Design

Inheritance vs subtyping

ObjectTable

ObjectTableWithFilters

ObjectTable Filter0..*

Report ObjectTable

Filter*

ObjectTableByDate

EuroPython 2016

Thanks

On groups.google.com:● reahl-discuss

www.reahl.org

[email protected]

James Martin & James Odell:Object Oriented Methods: A Foundation

Martin Fowler: Refactoring

Slides: https://goo.gl/NPLnCf