Applying dynamic method dispatch with JavaScript Proxy

Dynamic method dispatch is a powerful feature in object-oriented programming languages that allows you to invoke different implementations of a method based on the type of the object at runtime. While JavaScript is not a strictly object-oriented language, we can utilize the Proxy object to achieve dynamic method dispatch and add this functionality to our code.

In JavaScript, a Proxy object is used to define custom behavior for fundamental operations such as property lookup, assignment, function invocation, and more. We can leverage these capabilities to implement dynamic method dispatch.

Let’s imagine we have a base class Animal and two subclasses Dog and Cat. Each subclass has its own implementation of the makeSound method.

class Animal {
  makeSound() {
    console.log('Generic animal sound');
  }
}

class Dog extends Animal {
  makeSound() {
    console.log('Woof!');
  }
}

class Cat extends Animal {
  makeSound() {
    console.log('Meow!');
  }
}

To apply dynamic method dispatch, we’ll create a Proxy object that intercepts method calls and redirects them to the appropriate method based on the object’s type.

function createAnimalProxy(animal) {
  return new Proxy(animal, {
    get(target, property) {
      if (property === 'makeSound') {
        // Determine the actual type of the object
        const objectType = target.constructor.name;
        
        // Choose the appropriate method based on the object type
        if (objectType === 'Dog') {
          return target.makeSound.bind(target); // Bind the method to the target object
        }
        
        if (objectType === 'Cat') {
          return () => console.log('Purr!');
        }
      }
      
      return target[property];
    }
  });
}

Now, let’s create instances of Dog and Cat and use the createAnimalProxy function to create proxies for dynamic method dispatch.

const dog = createAnimalProxy(new Dog());
dog.makeSound(); // Output: Woof!

const cat = createAnimalProxy(new Cat());
cat.makeSound(); // Output: Purr!

In this example, the createAnimalProxy function creates a Proxy object that intercepts the makeSound method call. Depending on the object type, it either returns the original method or a custom implementation.

By utilizing JavaScript’s Proxy object, we can achieve dynamic method dispatch. This approach allows us to write more flexible and extensible code, particularly in cases where we may have multiple subclasses with different method implementations.

#Javascript #Proxy