double-dispatch

Double-dispatch and alternatives

谁说胖子不能爱 提交于 2019-12-05 01:05:50
I am trying to find a better way to handle some growing if constructs to handle classes of different types. These classes are, ultimately, wrappers around disparate value types (int, DateTime, etc) with some additional state information. So the primary difference between these classes is the type of data they contain. While they implement generic interfaces, they also need to be kept in homogeneous collections, so they also implement a non-generic interface. The class instances are handled according to the type of data they represent and their propogation continues or doesn't continue based on

What's the difference between Polymorphism and Multiple Dispatch?

。_饼干妹妹 提交于 2019-12-04 07:44:49
问题 ...or are they the same thing? I notice that each has its own Wikipedia entry: Polymorphism, Multiple Dispatch, but I'm having trouble seeing how the concepts differ. Edit: And how does Overloading fit into all this? 回答1: Polymorphism is the facility that allows a language/program to make decisions during runtime on which method to invoke based on the types of the parameters sent to that method. The number of parameters used by the language/runtime determines the 'type' of polymorphism

Double dispatch for collision handling with SpriteKit

删除回忆录丶 提交于 2019-12-03 20:32:52
I'm using SpriteKit's collision detection. It has a callback that looks like this: - (void)didBeginContact:(SKPhysicsContact *)contact The contact object has two physics bodies: SKPhysicsBody *bodyA; SKPhysicsBody *bodyB; My game will have lots of objects, and of course I can test the categoryBitMask to find out what collided with what. But given that I intend to have many kinds (not more than 32 of course) and might dynamically introduce new types, what's the most elegant way to do dynamic double dispatch to code the logic for collisions, explosions, points scored, etc that will result from

Polymorphically Dispatching in Java

隐身守侯 提交于 2019-12-03 11:53:53
问题 In the following, I want EventHandler to handle EventA one way, EventB another way, and any other Events (EventC, EventD) yet another way. EventReceiver receives only a reference to an Event and calls EventHandler.handle(). The version that always gets called, of course, is EventHandler.handle(Event event). Without using instanceOf, is there a way to polymorphically dispatch (perhaps via another method in EventHandler or generics) to the appropriate handle method? class EventA extends Event {

What's the difference between Polymorphism and Multiple Dispatch?

佐手、 提交于 2019-12-02 15:49:53
...or are they the same thing? I notice that each has its own Wikipedia entry: Polymorphism , Multiple Dispatch , but I'm having trouble seeing how the concepts differ. Edit: And how does Overloading fit into all this? Polymorphism is the facility that allows a language/program to make decisions during runtime on which method to invoke based on the types of the parameters sent to that method. The number of parameters used by the language/runtime determines the 'type' of polymorphism supported by a language. Single dispatch is a type of polymorphism where only one parameter is used (the

What is Single and Double Dispatch?

好久不见. 提交于 2019-12-01 03:58:18
i have wrote the visitor pattern as follow but i don't understand what is single and double dispatch. AFAIK, single dispatch is invoke a method based on caller type where double dispatch is invoke a method based on caller type and argument type. I guess double dispatch is happen in single class hierarchy but why visitor class has two class hierarchy but it still considered as double dispatch. void floppyDisk::accept(equipmentVisitor* visitor) { visitor->visitFloppyDisk(this); } void processor::accept(equipmentVisitor* visitor) { visitor->visitProcessor(this); } void computer::accept

.Net 4.0 Optimized code for refactoring existing “if” conditions and “is” operator

孤人 提交于 2019-11-30 18:09:17
问题 I have following C# code. It works fine; but the GetDestination() method is cluttered with multiple if conditions by using is operator. In .Net 4.0 (or greater) what is the best way to avoid these “if” conditions? EDIT: Role is part of the business model, and the destination is purely an artifact of one particular application using that business model. CODE public class Role { } public class Manager : Role { } public class Accountant : Role { } public class Attender : Role { } public class

How to call the correct method in Scala/Java based the types of two objects without using a switch statement?

折月煮酒 提交于 2019-11-30 03:58:27
I am currently developing a game in Scala where I have a number of entities (e.g. GunBattery, Squadron, EnemyShip, EnemyFighter) that all inherit from a GameEntity class. Game entities broadcast things of interest to the game world and one another via an Event/Message system. There are are a number of EventMesssages (EntityDied, FireAtPosition, HullBreach). Currently, each entity has a receive(msg:EventMessage) as well as more specific receive methods for each message type it responds to (e.g. receive(msg:EntityDiedMessage) ). The general receive(msg:EventMessage) method is just a switch

How to call the correct method in Scala/Java based the types of two objects without using a switch statement?

删除回忆录丶 提交于 2019-11-29 01:41:25
问题 I am currently developing a game in Scala where I have a number of entities (e.g. GunBattery, Squadron, EnemyShip, EnemyFighter) that all inherit from a GameEntity class. Game entities broadcast things of interest to the game world and one another via an Event/Message system. There are are a number of EventMesssages (EntityDied, FireAtPosition, HullBreach). Currently, each entity has a receive(msg:EventMessage) as well as more specific receive methods for each message type it responds to (e.g

How does double dispatch work in Visitor pattern?

末鹿安然 提交于 2019-11-27 20:36:22
问题 I was looking into other questions related to the visitor pattern but couldn't understand the implementation of double dispatch in visitor pattern. Please refer to the link Visitor Pattern How does double dispatch work in the Visitor pattern? 回答1: The element object's accept method receives a visitor object and it calls the visit method on the visitor object. As the visitor object has several visit methods, based on the element type the appropriate visit method is called. Here we have two