Multimethods in a Multimethods in a Single Dispatch Single Dispatch Language Language Brian Foote Brian Foote Ralph Johnson Ralph Johnson Dept. of Computer Science Dept. of Computer Science University of Illinois at Urbana-Champaign University of Illinois at Urbana-Champaign 201 N. Goodwin, Urbana, IL 61801, USA 201 N. Goodwin, Urbana, IL 61801, USA [email protected][email protected][email protected][email protected]James Noble James Noble School of Mathematical and Computing School of Mathematical and Computing Sciences Sciences Victoria University of Wellington Victoria University of Wellington P.O. Box 600, Wellington, New Zealand P.O. Box 600, Wellington, New Zealand [email protected][email protected]
27
Embed
Efficient Multimethods in a Single Dispatch Language Brian Foote Brian Foote Ralph Johnson Dept. of Computer Science Dept. of Computer Science University.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Efficient Multimethods in a Efficient Multimethods in a Single Dispatch LanguageSingle Dispatch Language
Brian FooteBrian Foote
Ralph JohnsonRalph JohnsonDept. of Computer ScienceDept. of Computer Science
University of Illinois at Urbana-ChampaignUniversity of Illinois at Urbana-Champaign
201 N. Goodwin, Urbana, IL 61801, USA201 N. Goodwin, Urbana, IL 61801, USA
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 22
LandmarksLandmarks Motivation for MultimethodsMotivation for Multimethods Syntax MattersSyntax Matters A Tale of Two MOPsA Tale of Two MOPs Two ImplementationsTwo Implementations Enduring SignificanceEnduring Significance
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 33
Let’s Get SeriousLet’s Get SeriousOne day an Englishman, a One day an Englishman, a ScotsmanScotsman, and , and
an Irishman walked into a pub together. an Irishman walked into a pub together. They each called for a dram of whiskey. They each called for a dram of whiskey. As they were about to enjoy their As they were about to enjoy their libations, three flies landed, one in each libations, three flies landed, one in each of their drinks, and became stuck inside of their drinks, and became stuck inside the glasses. The Englishman pushed his the glasses. The Englishman pushed his shot away in disgust. The Scotsman shot away in disgust. The Scotsman fished the fly out of his glass, and fished the fly out of his glass, and continued drinking it, as if nothing had continued drinking it, as if nothing had happened. The Irishman, too, picked the happened. The Irishman, too, picked the fly out of his drink, held it out over the fly out of his drink, held it out over the glass, and started yelling, "SPIT IT OUT, glass, and started yelling, "SPIT IT OUT, SPIT IT OUT YOU BASTARD!!!!" SPIT IT OUT YOU BASTARD!!!!"
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 44
Motivation: A Simple ScotsmanMotivation: A Simple Scotsmanpublic class Scotsman extends Carouserpublic class Scotsman extends Carouser{{
public void imbibe(ScotchWhiskey libation)public void imbibe(ScotchWhiskey libation) {{
public void imbibe (IrishWhiskey libation)public void imbibe (IrishWhiskey libation){{
stomach.add(libation);stomach.add(libation); bloodAlchoholLevel += 0.01;bloodAlchoholLevel += 0.01; System.out.println("Sure and begora...");System.out.println("Sure and begora...");
}}
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 66
A Simple, but Naïve TestA Simple, but Naïve Test public void testOverloads()public void testOverloads() {{ Irishman paddy = new Irishman();Irishman paddy = new Irishman(); Scotsman angus = new Scotsman();Scotsman angus = new Scotsman(); System.out.System.out.
Irishman paddy = new Irishman();Irishman paddy = new Irishman();Scotsman angus = new Scotsman();Scotsman angus = new Scotsman();
Carouser carouser = paddy;Carouser carouser = paddy; System.out.println("--> testBreakOverloads()...");System.out.println("--> testBreakOverloads()..."); Dram dram = new IrishWhiskey();Dram dram = new IrishWhiskey(); // You can't really do this properly...// You can't really do this properly...
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 99
A Type CaseA Type Case public void imbibe(Dram libation)public void imbibe(Dram libation) {{ if (libation instanceof ScotchWhiskey)if (libation instanceof ScotchWhiskey) {{ emptyStomach();emptyStomach(); System.out.println("Caledonian Swill...");System.out.println("Caledonian Swill..."); }} else if (libation instanceof IrishWhiskey)else if (libation instanceof IrishWhiskey) {{ stomach.add(libation);stomach.add(libation); bloodAlchoholLevel += 0.01;bloodAlchoholLevel += 0.01; System.out.println("Sure and begora...");System.out.println("Sure and begora..."); }} elseelse System.out.println("Mother of God...");System.out.println("Mother of God..."); }}
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 1010
The Olfactory MethodThe Olfactory Method
Kent Beck: May be Best Remembered as the Man Kent Beck: May be Best Remembered as the Man Who brought Scatology and Software Who brought Scatology and Software Engineering together…Engineering together…
If it stinks, change it!If it stinks, change it!--Grandma Beck--Grandma Beck
Code Smells are (not so) subtle indications a piece Code Smells are (not so) subtle indications a piece of code is in need of attention… …and is a likely of code is in need of attention… …and is a likely candidate for refactoring…candidate for refactoring…
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 1111
Double DispatchDouble Dispatchpublic class Irishman extends Carouserpublic class Irishman extends Carouser{{ public void imbibe(Dram libation)public void imbibe(Dram libation) {{ libation.whenImbibedByIrishman(this);libation.whenImbibedByIrishman(this); }}
public class IrishWhiskey extends Drampublic class IrishWhiskey extends Dram{{
public void whenImbibedByIrishman(Irishman irishman)public void whenImbibedByIrishman(Irishman irishman) {{ irishman.stomach.add(this);irishman.stomach.add(this); irishman.bloodAlchoholLevel += 0.01;irishman.bloodAlchoholLevel += 0.01; System.out.println("Sure and begora...");System.out.println("Sure and begora..."); }}}}
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 1212
Dynamic Multidispatch?Dynamic Multidispatch?public class Scotsman extends Carouserpublic class Scotsman extends Carouser{{
public void imbibe(<public void imbibe(<ScotchWhiskeyScotchWhiskey> libation)> libation) {{
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 2525
LessonsLessons The Beauty of Smalltalk The Beauty of Smalltalk The Elegance of the CLOS The Elegance of the CLOS
MOPMOP Building Languages of Building Languages of
ObjectsObjects The Power of The Power of
MultimethodsMultimethods
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 2626
I Have Nothing to DeclareI Have Nothing to Declare
End to End ArgumentEnd to End Argument Impact of Dynamic Types and LanguagesImpact of Dynamic Types and Languages The Arrogance of Closed WorldsThe Arrogance of Closed Worlds Reflection as a School of ArchitectureReflection as a School of Architecture
Foote, Johnson, and Noble -- Foote, Johnson, and Noble -- 2727
AcknowledgementsAcknowledgements Ralph JohnsonRalph Johnson James NobleJames Noble
John BrantJohn Brant Don RobertsDon Roberts
Richard P. GabrielRichard P. Gabriel Andrew BlackAndrew Black StStééphane Ducassephane Ducasse Christophe DonyChristophe Dony Anonymous ECOOP ReviewersAnonymous ECOOP Reviewers UIUC Software Architecture GroupUIUC Software Architecture Group