Re:Crafting Interpreters: Superclasses
by matheusmoreira on Wednesday March 18, 2020 @10:50PM
(#22623905)
Completely agree. Instruction sets are very inflexible. If a language has a new keyword or instruction, developers will spend a lot of time avoiding it at all costs and hiding it behind methods.
The new keyword requires a concrete class to instantiate. Abstractions such as interfaces cannot be used with new. The resulting instruction will directly reference that concrete class which makes it part of the software's binary interface. Its behavior can't be overridden: a constructor will always return an instance of the constructed class, never a subtype.
The factory design pattern was invented to solve these problems. Replacing new with normal method calls pretty much takes care of everything. The author himself has written about this on his blog:
The new keyword requires a concrete class to instantiate. Abstractions such as interfaces cannot be used with new. The resulting instruction will directly reference that concrete class which makes it part of the software's binary interface. Its behavior can't be overridden: a constructor will always return an instance of the constructed class, never a subtype.
The factory design pattern was invented to solve these problems. Replacing new with normal method calls pretty much takes care of everything. The author himself has written about this on his blog:
http://journal.stuffwithstuff.com/2010/09/18/futureproofing-...
http://journal.stuffwithstuff.com/2010/12/14/the-trouble-wit...