DoubleDispatch ¶
IntegerλΌλ ν΄λμ€μ FloatλΌλ ν΄λμ€κ° μλ€. λ κ°μ²΄ κ°μ λ§μ
μ ꡬννκ³ μΆλ€. λͺκ°λ₯Ό ꡬνν΄μΌν κΉ? 4κ°λ€. μ¦, Integer + Integer, Float + Float, Integer + Float, Float + Integerμ΄λ κ² λ§μ΄λ€. μ΄λ₯Ό ν΄κ²°νκΈ° μν μ μ°¨μ λ°©λ²μ λͺ¨λ μν©μ κ±°λν case ꡬ문μ λ£λ κ²μ΄λ€. μ΄κ²μ νκ΅°λ°μλ€κ° λ‘μ§μ λ€ λ£μ μ μλ€λ μ₯μ μ΄ μμμλ λΆκ΅¬νκ³ , μ μ§λ³΄μκ° μ΄λ ΅λ€.
μ°λ¦¬μ ν΄κ²°μ± μ, κ³μ°μ μ°κ΄λ λ κ°μ²΄λ₯Ό μ»μ΄μ€λ λ©μΈμ§μ κ³μΈ΅μ μΆκ°νλ κ²μ΄λ€.(?) μ΄κ²μ λ§μ λ©μΈμ§λ₯Ό λ§λ€κ² λμ§λ§, κ·Έ 볡μ‘ν¨μ κ°μΉκ° μλ€.
μ°λ¦¬μ ν΄κ²°μ± μ, κ³μ°μ μ°κ΄λ λ κ°μ²΄λ₯Ό μ»μ΄μ€λ λ©μΈμ§μ κ³μΈ΅μ μΆκ°νλ κ²μ΄λ€.(?) μ΄κ²μ λ§μ λ©μΈμ§λ₯Ό λ§λ€κ² λμ§λ§, κ·Έ 볡μ‘ν¨μ κ°μΉκ° μλ€.
argumentμ λ©μΈμ§λ₯Ό 보λ΄λΌ. selectorμλ€κ° receiverμ ν΄λμ€ λ€μμ λ§λΆμΈλ€. receiverλ₯Ό argumentλ‘ λκΈ΄λ€. μ΄ ν¨ν΄μ μ¬μ©ν νμ Integer, Float μ½λλ λ€μκ³Ό κ°λ€.
~cpp Integer Integer::operator+(const Number& aNumber) { return aNumber.addInteger(this); } Float Float::operator+(const Number& aNumber) { return aNumber.addFloat(this); } Integer Integer::addInteger(const Integer& anInteger) { return Integer(this + anInteger); } Float Float::addFloat(const Float& aFloat) { return Float(this + aFloat); } Float Integer::addFloat(const Float& aFloat) { return asFloat().addFloat(aFloat); // Integerλ₯Ό Floatλ‘ λ°κΏμ€ λ€μ κ³μ° } Integer Float::addInteger(const Integer& anInteger) { return addFloat(anInteger.asFloat()); }
μμ μλ²½ν μ΄ν΄λ λͺ»νλ€. μ’ λ λ΄μΌ ν λ― μΆλ€.
μ½μ΄λ³΄κ³ μ 리ν΄μΌ ν μ¬μ΄νΈλ€ ¶
- http://www.object-arts.com/EducationCentre/Patterns/DoubleDispatch.htm
- http://eewww.eng.ohio-state.edu/~khan/khan/Teaching/EE894U_SP01/PDF/DoubleDispatch.PDF
- http://www-ekp.physik.uni-karlsruhe.de/~schemitz/Diploma/html/diploma/node85.html
- http://www.chimu.com/publications/short/javaDoubleDispatching.html
- http://no-smok.net/seminar/moin.cgi/DoubleDispatch