What’s 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, ….. ) https://goo.gl/3DMHFH EuroPython 2016
Introduction About / 2 EuroPython 2016
Introduction OOP is not OO Conceptual model map to of a problem domain Code Supporting Programming mechanisms language for OO 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 s s n e o d l e i a t s a n u m g e r o r f n i opportunity EuroPython 2016
Introduction A scary thought EuroPython 2016
Concepts A conceptual model Chair EuroPython 2016
Concepts Concepts as sets Desk Chair EuroPython 2016
Concepts Refinement via subsets Chair Office Chair EuroPython 2016
Relations Connecting objects Desk Chair EuroPython 2016
Relations Relation as a concept Assignment EuroPython 2016
Relations Multiplicity Chair Desk EuroPython 2016
Notation It gets complicated... Desk Chair Assignment Office Chair EuroPython 2016
Notation UML Chair 1 * assignment OfficeChair Desk EuroPython 2016
More on concepts Intangible concepts Portfolio Investment Instrument UnitTrust EuroPython 2016
More on concepts Overlapping concepts Person Investor EuroPython 2016
More on concepts Changing classification Person Employee Investor EuroPython 2016
More on concepts Concept labels Label Synonym Concept Chair Concept Type EuroPython 2016
Affecting objects Operations load_prices( ) UnitTrust * Price PriceFile Fund EuroPython 2016
Summary Operations vs methods operates on 1..* Operation Object 1 is implemented by Method 1..* EuroPython 2016
Summary OO concepts 2..* 1..* Object 0..* operates on links 0..* subtype classifies Type Relation Operation Method 1..* implemented by EuroPython 2016
How can this help? Programs are programs Concept Concept Courtesy: inkwellideas.com EuroPython 2016
How can this help? Understanding-structured parse_file: Operation parse_xml: Method parse_csv: Method Courtesy: inkwellideas.com EuroPython 2016
How can this help? Focus on one thing PriceFile FileFormat 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 Object Oriented Programming Classical Prototype OOP implementation OOP Based EuroPython 2016
Classical OOP & Python Types as classes an instance an instance an instance 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’> EuroPython 2016
Classical OOP & Python Relationships as attributes Person Portfolio owner Me owner You EuroPython 2016
Classical OOP & Python Attributes >>> portfolio = Portfolio() >>> someone = Person() >>> portfolio.owner = someone >>> portfolio.owner <Person instance at 0x7fd6d88100e0> EuroPython 2016
Classical OOP & Python Methods as (Python) methods Investment indexed by class Instrument def activate (self): self.is_active = True Operation 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 () 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 () 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 EuroPython 2016
Classical OOP & Python Subtyping as inheritance >>> fund = UnitTrustFund() >>> isinstance(fund, InvestmentInstrument) True >>> fund.activate() >>> fund.is_active True EuroPython 2016
Classical OOP & Python The ghost of operations parse( ) FileFormat Excel CSV 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 EuroPython 2016
Classical OOP & Python The ghost of operations PriceFile FileFormat for line in price_file.format.parse(price_file): # do stuff with line 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 EuroPython 2016
Design What’s design? Person Person Role Employee Employee Investor Investor Company EuroPython 2016
Design Worse design * CommissionCalc Benefit Benefit * * QuoteBasis QuoteBenefit Bundles Summary Grouping Calc * * QuoteBenefit Benefit Bundles QuoteFees Calc Commissions AbstractCommission * Bundle LookupCommission Calculation ScaleFactory Constants Commission Altered * Bundle CommissionBundle Lookup CommissionScale EuroPython 2016
Design Better design Commission * Commission QuoteScenario Product Scale Band VAT * * Benefit Category * CommisionBundle * includedInAPI? BenefitGrouping negotiatedMax (R or %LOA) * EuroPython 2016
Design Inheritance vs subtyping 0..* ObjectTable ObjectTable Filter ObjectTable Report ObjectTable WithFilters * Filter ObjectTable ByDate EuroPython 2016
Thanks James Martin & James Odell: Object Oriented Methods: A Foundation Martin Fowler: Refactoring iwan@reahl.org On groups.google.com : ● reahl-discuss Slides : https://goo.gl/NPLnCf www.reahl.org EuroPython 2016
Recommend
More recommend