What is Delegation?

WireBox supports the concept of object delegation in a simple expressive DSL. In object-oriented programming, delegation refers to the evaluating a member (property or method) of one object (the receiver) to the context of another object (the sender). Basically a way to proxy calls from one object to the other and avoid the overuse of inheritance, avoid runtime mixins or traits. WireBox provides a set of rules for method lookup and method dispatching that will allow you to provide delegation easily in your CFML applications.

Untitled.png

This pattern is also known as "proxy chains". Several other design patterns use delegation - the State, Strategy and Visitor Patterns depend on it.

Why use Delegation?

In object-oriented programming, there are three ways for classes to work together:

  1. Inheritance (IS A relationships)
  2. Composition (HAS A relationships)
  3. Runtime Mixins or Traits

With inheritance, you create families of objects or hierarchies, where a parent component shares functions, properties and instance data with any component that extends it (derived class). It’s a great way to provide reuse but also one of the most widely abused approaches to reuse. It’s easy, powerful but with much power, comes great responsibility. For example, you create a base Animal class and then derived classes like: Cat, Dog, Bird, etc. You can say: A Cat IS An Animal, A Dog IS An Animal. You wouldn’t say, a Cat has an Animal.

Untitled 1.png

With composition a component will either create or have dependencies injected into them (via WireBox) which it can then use these objects to delegate work to them. This follows the has a relationship, like: A Car has Wheels, a Computer has Memory, etc. The major premise of WireBox is to provide assistance with composition.

Mixins allows you to do runtime injections of user defined functions (UDFs) and helpers from reusable objects. However, this can lead to method explosion on injected classes, collisions and just not a lot of great organization as you are just packing a class with tons of functions in order to reuse behavior. Composition is the preferred approach and the less decoupled approach. Delegation is a step further. So let’s explore it with a simple example.