essence of dispatch
play

Essence of Dispatch Taking Pharo Booleans as Example Damien Cassou, - PowerPoint PPT Presentation

Essence of Dispatch Taking Pharo Booleans as Example Damien Cassou, Stphane Ducasse and Luc Fabresse W3S01 http://www.pharo.org Objectives Understanding of message passing (late binding) the heart of OOP more an OOP lecture than a


  1. Essence of Dispatch Taking Pharo Booleans as Example Damien Cassou, Stéphane Ducasse and Luc Fabresse W3S01 http://www.pharo.org

  2. Objectives � Understanding of message passing (late binding) ◦ the heart of OOP ◦ more an OOP lecture than a Pharo one � Insight at how beautiful Pharo’s implementation is W3S01 2 / 21

  3. Context: Booleans In Pharo, Booleans have a superb implementation! � & , | , not (eager) � or: , and: (lazy) � ifTrue:ifFalse: , ifFalse:ifTrue: W3S01 3 / 21

  4. Three Exercises 1. Implement not (Not) 2. Implement | (Or) 3. What is the goal of these exercises? W3S01 4 / 21

  5. Exercise 1: Implement Not Propose an implementation of Not in a world where: � You have: true , false � You only have objects and messages � How would you implement the message not ? false not − > true true not − > false W3S01 5 / 21

  6. Hint 1: No conditionals The solution does not use conditionals (i.e., no if ) W3S01 6 / 21

  7. Hint 2: With Three Classes � The solution uses three classes: ◦ Boolean (abstract), True and False � true is the singleton instance of True � false is the singleton instance of False W3S01 7 / 21

  8. Hint 2: Three Classes W3S01 8 / 21

  9. Hint 3: How do We Express Choice in OOP? In OOP , choice is expressed � By defining classes with compatible methods � By sending a message to an instance of such class Example x open � x can be a file, a window, a tool,... � The method is selected based on x ’s class W3S01 9 / 21

  10. Implementation of Not in Two Methods False >> not "Negation −− answer true since the receiver is false." ^ true True >> not "Negation −− answer false since the receiver is true." ^ false W3S01 10 / 21

  11. Implementation Hierarchy W3S01 11 / 21

  12. Message Lookup is Choosing the Right Method W3S01 12 / 21

  13. Boolean Implementation � Boolean is abstract � Subclasses are True and False and implement ◦ logical operations & , not ◦ control structures and: , or: , ifTrue: , ifFalse: , ifTrue:ifFalse: , ifFalse:ifTrue: Boolean>>not "Abstract method. Negation: Answer true if the receiver is false, answer false if the receiver is true." self subclassResponsibility W3S01 13 / 21

  14. Behavior of Or true | true − > true true | false − > true true | anything − > true false | true − > true false | false − > false false | anything − > anything W3S01 14 / 21

  15. Implementation of Or in Boolean Boolean >> | aBoolean "Abstract method. Evaluating Or: Evaluate the argument. Answer true if either the receiver or the argument is true." self subclassResponsibility W3S01 15 / 21

  16. Implementation of Or in Class False false | true − > true false | false − > false false | anything − > anything False >> | aBoolean "Evaluating Or −− answer with the argument, aBoolean." ^ aBoolean W3S01 16 / 21

  17. Implementation of Or in Class True true | true − > true true | false − > true true | anything − > true True >> | aBoolean "Evaluating Or −− answer true since the receiver is true." ^ true W3S01 17 / 21

  18. Real Implementation of Or in Class True The object true is the receiver of the message! True>> | aBoolean "Evaluating disjunction (Or) −− answer true since the receiver is true." ^ true So we can write it like the following: True >> | aBoolean "Evaluating disjunction (Or) −− answer true since the receiver is true." ^ self W3S01 18 / 21

  19. Or Implementation in Two Methods W3S01 19 / 21

  20. Summary � The solution to implement booleans’ operations: ◦ does NOT use conditionals (if) ◦ lets the receiver decide � Do not ask, tell W3S01 20 / 21

  21. A course by and in collaboration with Inria 2016 Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France https://creativecommons.org/licenses/by-nc-nd/3.0/fr/

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend