4 ms·
These patterns are almost always working around language shortcomings. For example, factories work around the new keyword. The new keyword in Java emits the co
by matheusmoreira 7y ago
These patterns are almost always working around language shortcomings.
For example, factories work around the new keyword. The new keyword in Java emits the constructed type into the bytecode, making it a hard ABI dependency. So people invented factories: they hide the new keyword behind methods that return interfaces. In better languages such as Smalltalk, new is just a method that can be overridden.
Singletons work around the fact only objects can implement interfaces. Classes are natural singletons and yet they are second class citizens of most languages. It is not possible to pass the class itself to code expecting some interface. So people are forced to create an object and add complicated boilerplate code to prevent more than one instance from ever being created. In better languages, classes are the same as objects, they can conform to interfaces and be passed around normally.
- LgWoodenBadger 7y agoIn Smalltalk, how would how would the consumer be given differing implementations of this "new" method? And wouldn't that just make the "new" method a factory?
- riffraff 7y agoyes it would be a factory, the difference being that you don't have to use a different syntax (`Klass.build/new Klass` would be `Klass new` in both cases)
- matheusmoreira 7y ago> In Smalltalk, how would how would the consumer be given differing implementations of this "new" method? Classes can simply override that method. The default implementation of new is: ^ self basicNew initialize And basicNew is a method that allocates and returns an instance of the receiver. So a custom implementation can easily change self to some other class, chain more messages or replace initialize with something else, add more logic before an object is returned and so on. > And wouldn't that just make the "new" method a factory? Smalltalk actually predates the discovery of object-oriented design patterns by a couple of decades so it's the factory methods that are like the new method. For some reason language designers turned it into a magic keyword and people rediscovered the fact that methods are better.
- LgWoodenBadger 7y agoIf my BubbleMachine currently makes SoapBubbles, but I want it to be able to make GumBubbles as well, who, out of those three, is responsible for overriding the “new” method to create GumBubbles instead of SoapBubbles?
- spankalee 7y agoA Bubbles class, which is basically the same as a factory. I don't see a huge difference in practice myself.
- matheusmoreira 7y agoAre these all subclasses of a Bubbles class? I think that'd be the natural place for a custom new method that figures out which subclass to construct based on the parameters. In Java, an interface could have a static method that returns concrete implementations of itself.
- scroot 7y agoIt's even better. If it's just making stuff, you don't even need `BubbleMachine` if you have the `Bubbles` base class. You can add creation method on the class side of `Bubbles` like so: `Bubbles class >> #newGum` ^ GumBubbles new `Bubbles class >> #newSoap` ^ SoapBubbles new The difference here is that the base class still serves as a true base: it will have all the common functionality for various kinds of bubbles
- RaiseProfits 7y agoI wouldn’t call hyper object oriented languages like smalltalk, io, and ruby “better“, just different. The ABI is only a shortcoming if you value aesthetics over other considerations.
- matheusmoreira 7y agoIt's not just aesthetics. I mentioned the ABI issue because it is the technical reason why factories exist. Writing "new X" makes X part of the compiled code. This makes it impossible to swap X for Y and delete X later without breaking binary and even source compatibility. This is important for libraries and code reuse. I don't think it is controversial to say those languages are better. A language with limitations that must be constantly worked around must be worse than a language without those problems.
- RaiseProfits 7y agoLanguages are only good or bad in the context of performing a task. Ranking them in abstract is a silly waste of time that engineers love to emotionally engage in. The ABI is also important for performance! You aren’t making this comparison in good faith. You can easily acknowledge the entire trade off rather than just calling one better.
- matheusmoreira 7y agoYou're right. I acknowledge that languages do pay a price for their flexibility and dynamism: efficient implementation becomes much harder.
- RaiseProfits 7y agoI also acknowledge that smalltalk has literally the dream ide and tooling. Ruby will take decades to catch up.
- riffraff 7y agosee classic "design patterns are missing language features" presentation by peter norvig, aka "design patterns in dynamic languages"[0], which is from 1998. [0] https://norvig.com/design-patterns/ https://norvig.com/design-patterns/
- matheusmoreira 7y agoNice! I didn't know this idea went that far back. What convinced me were these posts: http://journal.stuffwithstuff.com/2010/09/18/futureproofing-uniform-access-and-masquerades/ http://journal.stuffwithstuff.com/2010/09/18/futureproofing-... http://journal.stuffwithstuff.com/2012/12/19/the-impoliteness-of-overriding-methods/ http://journal.stuffwithstuff.com/2012/12/19/the-impolitenes... http://journal.stuffwithstuff.com/2010/08/01/class-in-the-front-proto-in-the-back/ http://journal.stuffwithstuff.com/2010/08/01/class-in-the-fr... http://journal.stuffwithstuff.com/2010/12/14/the-trouble-with-constructors/ http://journal.stuffwithstuff.com/2010/12/14/the-trouble-wit...
- riffraff 7y agoI think people tend to re-discover this over and over :) I myself thought I had a great insight on this topic while learning CLOS[0], only to realize I was a few decades late. [0] https://en.wikipedia.org/wiki/Common_Lisp_Object_System https://en.wikipedia.org/wiki/Common_Lisp_Object_System
- skybrian 7y agoI don't know about Smalltalk, but in other languages, a constructor is just a function, and you can "override" it by passing in a different function. The flaw in Java is that constructors aren't functions, so you need to wrap them. Wrapping is easy in simple cases but gets annoying fast when you have lots of arguments. To get around this, you can group arguments using a struct, and sometimes there's a natural way to do it, but that gets annoying in languages without lightweight, easy-to-use structs.
- xfer 7y agoIn this case DI seems to be compensating the lack of a proper module system as in SML.