Accessor patterns

Julia objects are transparent. What does that mean? Well, currently, the Julia language does not have the ability to apply access control over the fields of an object. Therefore, people coming from a C++ or Java background may find it a little uneasy. In this section, we will explore a number of ways in which to make the language more acceptable for those users who are seeking more access control.

So, perhaps we should define our requirements first. While we write up the requirements, we will also ask ourselves why we want to have them in the first place. Let's just consider any object in a Julia program:

  • Some fields need to be hidden from the outside world: Some fields are considered to be part of the public interface and are, therefore, fully documented and supported. Other fields are considered implementation details, and they may not be used because they are subject to change in the future.
  • Some fields require validation before being mutated: Some fields may only accept a range of values. For example, an age field of a Person object may reject anything less than 0 or greater than 120! Avoiding invalid data is paramount to building robust systems.
  • Some fields require a trigger before they can be read: Some fields may be lazily loaded, which means they are not loaded until the value is read. Another reason is that some fields may contain sensitive data, and the use of such fields must be logged for audit purposes.

We will now discuss how to address these requirements.

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

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