Sunday, November 15, 2015

Searching for Flyweight

As part of Design Patterns in Dart, I would very much like to include examples of the various patterns in the wild. I have not quite figured out how to go about doing that, so if anyone knows where to find examples of the Flyweight pattern in the wild, I would appreciate it.

I know from the very excellent Game Programming Patterns (seriously, it's great, I just bought another copy so I could read it in epub) that graphics programming tries to make extensive use of the Flyweight pattern. I dug around a bit today, but it does not seem that WebGL supports it yet. Even if did, the Flyweight would be in the WebGL API, not in Dart, so that seems unlikely

Instead of playing around with forced examples in Three.dart, I change tact. I clone a bunch of Dart repositories that are trending on GitHub, then grep through them in the hope of finding a mention of the word "flyweight":
➜  trending-dart-repos  ack -i flyweight
➜  trending-dart-repos  echo $?
No luck there. Still, not a complete waste of time, as I expect to find other patterns in the top results:I will add to that list over time, but that seems a good start. I have looked through code in several of those already and know that there are plenty of pattern examples.

That does not help with my goal tonight. What does help, is the search interface on GitHub, which allows me to search all Dart projects by keyword:

Most of those results look to be from goofballs compiling examples of design patterns. One in the list, however, does include an actual Flyweight pattern. The Dart Web Toolkit, which is based on the same idea as GWT. The projects looks un/lightly maintained, so it's not a strong example, but the Flyweight pattern is definite present.

The idea is to cache event mappings between native and DWT events. The DomEvent class caches these in the private _registered map:
abstract class DomEvent extends DwtEvent implements HasNativeEvent {
  // ...
  static Map<String, DomEventType> _registered;
  // ...
Whenever a new type is created by the factory class, it registers the DWT / native event pair under the string description of the event:
class DomEventType<H> extends EventType<H> {
  String eventName;
  DomEvent flyweight;

  DomEventType(this.eventName, this.flyweight) {
    DomEvent.getRegistered()[eventName] = this;
Every single event in the library then registers itself at runtime when the class is evaluated:
class TouchMoveEvent extends TouchEvent {
   * The event type.
  static DomEventType<TouchMoveHandler> TYPE = new DomEventType<TouchMoveHandler>(BrowserEvents.TOUCHMOVE, new TouchMoveEvent());
  // ...
This way, whenever the framework needs a touch-move event, it can use the flyweight registered when the class assigns the TYPE variable. Nice!

As I said, I am unsure if the qualifies as a strong example. I suppose if I can find that folks are still using the library, then it qualifies. Regardless, I am happy to have rediscovered the GitHub language search. I have the feeling this will come in quite handy.

Day #4

No comments:

Post a Comment