Saturday, December 5, 2015

The Caretaker, Mel, and One Velvet Fog Machine to Rule Them All

Once upon a time there was a wise a benevolent creature known as the Caretaker:

The Caretaker was responsible for playing the ultimate selections of Mel Tormé music on the magnificent Velvet Fog Machine:

Every now and then the Caretaker got special coins that remembered a particular song at a particular time:

The Caretaker could used these to got back to these very special moments.

The Caretaker had no idea how the Velvet Fog Machine knew what to do with these coins and did not care. Mel Tormé played all the time and special moments could be had a plenty.

But one day something strange happened. The Caretaker tried something a little different and... all of the sudden there were three identical (but for the hashCode) velvet fog machines:

This was confusing to the Caretaker and seemed likely to cause more problems in a life that should be dedicated to Melvie. So that Caretaker decided that the VelvetFogMachine needed to be a singleton. Happily, the Caretaker favored bliss and so was programming with Dart. And so a factory constructor was able to resolve these woes:
class VelvetFogMachine {
  // ...
  static final VelvetFogMachine _vfm = new VelvetFogMachine._internal();
  factory VelvetFogMachine() => _vfm;
  // ...
With that, the originator of the magical best-of-Mel coins, the VelvetFogMachine would always return the same _vfm instance of itself. So there was one instance of the velvet fog machine no matter how the Caretake might interact with it:

And the Caretakee could enjoy Mel until the end of days. And the Caretaker was happy.

Day #24

No comments:

Post a Comment