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