The delegation pattern

Delegation is a pattern that is commonly applied in software engineering. The primary objective is to leverage the capabilities of an existing component by wrapping it via a has-a relationship.

The delegation pattern is widely adopted, even in the object-oriented programming community. In the early days of object-oriented programming, people thought that code reuse could be achieved beautifully using inheritance. However, people came to realize that this promise couldn't be completely fulfilled due to a variety of issues related to inheritance. Since then, many software engineers prefer composition over inheritance. The concept of composition is to wrap one object within another. In order to reuse existing functions, we must delegate functions calls to the wrapped object. This section will explain how delegation can be implemented in Julia.

The concept of composition is to wrap one object within another. In order to reuse existing functions, we must delegate functions calls to the wrapped object.

One way is to enhance an existing component with new features. This may sound good, but it could be challenging in practice. Consider the following situations:

  • The existing component comes from a vendor product and the source code is not available. Even if the code is available, the vendor's license may not allow us to make custom changes.
  • The existing component is developed and used by another team for a mission-critical system and changes are neither welcome nor applicable for that system. 
  • The existing component contains a lot of legacy code and new changes may compromise the component's stability and require a lot of testing effort.

If modifying an existing component's source code is not an option, then we should at least be able to use the component via its published programming interface. That is the virtue of the delegation pattern.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.15.94