4 ms·
The best explanation I've ever seen for design patterns is that they are workarounds for weaknesses of the programming language. Usually Java and its contempora
by matheusmoreira 6y ago
The best explanation I've ever seen for design patterns is that they are workarounds for weaknesses of the programming language. Usually Java and its contemporaries.
http://journal.stuffwithstuff.com/2010/09/18/futureproofing-uniform-access-and-masquerades/ http://journal.stuffwithstuff.com/2010/09/18/futureproofing-...
Factories, for example. They're essentially just constructors. So why not use the new keyword? Turns out new has limitations:
- Dependency on the constructed type at the ABI level
- Cannot return subtypes of the constructed type
- Special keyword, not orthogonal with the rest of the language
Singletons, for example. They're essentially classes with static methods and state. Why not use these language features instead of coming up with incredibly convoluted ways to ensure only a single instance of an object has been created? Turns out classes have limitations:
- Classes aren't objects
- Classes cannot be passed as parameters
- Classes cannot implement interfaces
The need for all of these patterns simply disappears in a better designed language. In Ruby for example everyone uses factory methods without even realizing it and classes are fully featured objects that can be used in any context.
- catlifeonmars 6y agoInterestingly enough, the same phenomenon arises in languages with fewer features. In go, for example, there are no constructors, only functions that return structs. Singletons are just global variables referencing a struct, etc etc.
- commandlinefan 6y agoEvery programming language has “singletons”: they’re just global variables.
- wruza 6y agoSingleton’s main feature is not that it is globally accessible, but in that it represents a state that cannot be naturally duplicated. The pattern itself lies in a controlled instantiation, where a method/constructor/factory returns a cached value on a second call. Globals are just that – globals.
- virtue3 6y agomy (very tongue in cheek) favorite description of singletons - an object oriented way to break object orientedness.
- bvrmn 6y agoGlobal variable is naturally can't be duplicated. Or do you assume dark corners of initialization spec for c++?
- AnimalMuppet 6y agoAt global level: ThingThereCanBeOnlyOneOf a; ThingThereCanBeOnlyOneOf b; Oops. That's not a singleton. That depends on nobody, ever, anywhere in the code creating another one. In contrast, a true singleton can enforce that there is only one, no matter what the callers do.
- wott 6y agoGlobals like a and b are fields of the singleton, not instance of singleton(s). The global space is the singleton (or a juxtaposition of singletons, if we let aside the lack of visibility protection), and global variables and functions are the fields (members) and the methods of the singleton.
- AnimalMuppet 6y agoYou can look at it that way, I suppose, though I consider it to be a fairly odd perspective. But it doesn't help at all with the problem of enforcing that there be only one ThingThereCanBeOnlyOneOf. Therefore globals are not a true solution to the problem that singletons are trying to solve.
- bcrosby95 6y agoJust use static methods.
- dwaite 6y agoNot quite - variables are references to objects, there's nothing that restricts global variables themselves to only reference a single _instance_ of an object. That said, a global singleton (and global state in general) is usually an anti-pattern; having pretty much any code be able to depend on the state makes it very difficult to evaluate the impact of a change.
- kitkat_new 6y agois the Abstract Factory showing a weakness of all languages in which it is useful? What is the underlying weakness?
- anonymoushn 6y agoThe question is, "I have some code that wants to make some different kinds of a thing in the same way, then use the different kinds in the same way, being unaware that they are different and not depending on any particular kind. How?" The answer is typically that you can pass a function to that code which accepts some stuff and returns the thing it is concerned with. You may say "Thats an Abstract Factory" but when we write like 4 LoC instead of 3 classes in different files we often don't feel a need to give it a name. Edit: Also, depending on the circumstances, you can probably just pass the class or its constructor instead of passing a newly written function. That would get you down to ~0 LOC involved in replicating the functionality of this pattern.
- anonymoushn 6y agoI guess if we want to get really abstract, we can say that the Abstract Factory pattern is a shim from a kind of late binding that is implemented in the language ("object orientation" a.k.a. dynamic dispatch on the first argument to a function) to one that is not (late binding of classes, constructors returning null, etc.). This isn't the whole picture though, because sometimes late binding isn't even an important part of what we're getting from the Abstract Factory pattern! For many use cases, we could imagine implementing the function that consumes the Abstract Factory using C++ templates or Zig comptime, and our compiler would generate separate subroutines for each factory type and statically determine which one to use at each call site and we would still be doing the Abstract Factory thing. Another pattern called the Visitor Pattern is similarly a shim from dynamic dispatch on one argument to dynamic dispatch on two arguments. You just really hope you don't need dynamic dispatch on five arguments one day.
- galaxyLogic 6y ago> - Classes aren't objects Right. Except in Smalltalk they are
- grzm 6y agoRuby, too, IIRC. And likely others.