what s the point of object orientation
play

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,


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

  2. Introduction About ValidationConstraint Layout Button Action post.save() https://goo.gl/3DMHFH EuroPython 2016

  3. Introduction About data_table = DataTable(view, ….. ) https://goo.gl/3DMHFH EuroPython 2016

  4. Introduction About / 2 EuroPython 2016

  5. Introduction OOP is not OO Conceptual model map to of a problem domain Code Supporting Programming mechanisms language for OO EuroPython 2016

  6. Introduction Cause and effect EuroPython 2016

  7. Introduction Understand a program? EuroPython 2016

  8. Introduction Scaling up 100 lines of code? Courtesy: inkwellideas.com EuroPython 2016

  9. Introduction How about... 180 000 lines of code? EuroPython 2016

  10. 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

  11. Introduction A scary thought EuroPython 2016

  12. Concepts A conceptual model Chair EuroPython 2016

  13. Concepts Concepts as sets Desk Chair EuroPython 2016

  14. Concepts Refinement via subsets Chair Office Chair EuroPython 2016

  15. Relations Connecting objects Desk Chair EuroPython 2016

  16. Relations Relation as a concept Assignment EuroPython 2016

  17. Relations Multiplicity Chair Desk EuroPython 2016

  18. Notation It gets complicated... Desk Chair Assignment Office Chair EuroPython 2016

  19. Notation UML Chair 1 * assignment OfficeChair Desk EuroPython 2016

  20. More on concepts Intangible concepts Portfolio Investment Instrument UnitTrust EuroPython 2016

  21. More on concepts Overlapping concepts Person Investor EuroPython 2016

  22. More on concepts Changing classification Person Employee Investor EuroPython 2016

  23. More on concepts Concept labels Label Synonym Concept Chair Concept Type EuroPython 2016

  24. Affecting objects Operations load_prices( ) UnitTrust * Price PriceFile Fund EuroPython 2016

  25. Summary Operations vs methods operates on 1..* Operation Object 1 is implemented by Method 1..* EuroPython 2016

  26. Summary OO concepts 2..* 1..* Object 0..* operates on links 0..* subtype classifies Type Relation Operation Method 1..* implemented by EuroPython 2016

  27. How can this help? Programs are programs Concept Concept Courtesy: inkwellideas.com EuroPython 2016

  28. How can this help? Understanding-structured parse_file: Operation parse_xml: Method parse_csv: Method Courtesy: inkwellideas.com EuroPython 2016

  29. How can this help? Focus on one thing PriceFile FileFormat EuroPython 2016

  30. How can this help? Zoom in FileFormat Excel CSV EuroPython 2016

  31. How can this help? Zoom in more def parse_file (price_file): for row in price_file: yield row.split(‘,’) EuroPython 2016

  32. Implementing OO OOP and Python Object Oriented Programming Classical Prototype OOP implementation OOP Based EuroPython 2016

  33. Classical OOP & Python Types as classes an instance an instance an instance EuroPython 2016

  34. 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

  35. Classical OOP & Python Relationships as attributes Person Portfolio owner Me owner You EuroPython 2016

  36. Classical OOP & Python Attributes >>> portfolio = Portfolio() >>> someone = Person() >>> portfolio.owner = someone >>> portfolio.owner <Person instance at 0x7fd6d88100e0> EuroPython 2016

  37. Classical OOP & Python Methods as (Python) methods Investment indexed by class Instrument def activate (self): self.is_active = True Operation EuroPython 2016

  38. 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

  39. 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

  40. 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

  41. Classical OOP & Python Subtyping as inheritance >>> fund = UnitTrustFund() >>> isinstance(fund, InvestmentInstrument) True >>> fund.activate() >>> fund.is_active True EuroPython 2016

  42. Classical OOP & Python The ghost of operations parse( ) FileFormat Excel CSV EuroPython 2016

  43. 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

  44. Classical OOP & Python The ghost of operations PriceFile FileFormat for line in price_file.format.parse(price_file): # do stuff with line EuroPython 2016

  45. 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

  46. Design What’s design? Person Person Role Employee Employee Investor Investor Company EuroPython 2016

  47. 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

  48. Design Better design Commission * Commission QuoteScenario Product Scale Band VAT * * Benefit Category * CommisionBundle * includedInAPI? BenefitGrouping negotiatedMax (R or %LOA) * EuroPython 2016

  49. Design Inheritance vs subtyping 0..* ObjectTable ObjectTable Filter ObjectTable Report ObjectTable WithFilters * Filter ObjectTable ByDate EuroPython 2016

  50. 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