CS246 Lecture 20: Lesson 20

46 views4 pages

Document Summary

Visitor pattern is often used to implement a concept called double dispatch. Using this method, we can implement a program where action can be based upon two different objects. To achieve this goalm we can combine overriding and overloading. Basic video game with enemies and weapons using the visitor pattern. We want a different action to occur when different weapons are used on different enemies. We want something like virtual void(enemy, weapon)::strike(); virtual void bestruckby(weapon &w) = 0; class enemy { class weapon{ }; //each of these only does hal the job virtual void strike(enemy &e); Now overload strike that takes in both turtle ref and a bullet ref: //we also want enemy to know what"s striking it. //we want weapon to know what it"s striking class weapon { public: virtual void strike(turtle &t) = 0; virtual void strike(bullet &b) = 0; class stick : public weapon { public: void strike(turtle &t){

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers