== 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 ["MoreEffectiveC++"] 에서 [http://zeropage.org/wiki/MoreEffectiveC_2b_2b_2fTechniques3of3#head-a44e882d268553b0c56571fba06bdaf06618f2d0 Item31] 에서도 언급됨. ---- [SBPPSummary]