Fun with Stamps. Episode 10. My stamp mental model
Hello. I’m developer Vasyl Boroviak and welcome to the tenth episode of Vasyl Boroviak presents Fun with Stamps.
One of the many problems of classic inheritance looks like this:
Luckily, most of the design patterns are obsolete with stamps. Stamps solve the Animal-Robot problem like this:
Typically, the Java and C# developers solve the above problem like this:
- Put everything to the base GameEntity class, or
- Use has-a composition with or without the Strategy design pattern, or
- Use is-a composition with help of interfaces, or
- else.
For example, in .NET they went the first way — put everything to the base class(es). The simplest UI element in .NET Framework v4.0 is System.Windows.Controls.Label.
According to the MSDN the class has:
- 7 parent classes
- 1 constructor
- 211 methods
- 130 properties
- 55 events (which essentially are properties too)
- 1 static field
- and 1 attribute (aka annotation in Java)
A bit too much to display a simple text on a screen, right?
Another example is present in the Wikipedia “Composition over inheritance” article. It is using both “is-a” and “has-a” composition:
Looks too messy, right?
The big problem with classic OOP is that a developer should know and distinguish multiple various design patterns and specific language tricks. In other words — you better hire an experienced developer in order to have a well designed application.
I wish most of those yellow squares above were stamps, so that I can:
- MallardDuck = compose(Duck, Quack, FlyWithWings)
- RedheadDuck = compose(Duck, Quack, FlyTurkeyLike)
- RubberDuck = compose(Duck, Squeak, CannotFly)
- DecoyDuck = compose(Duck, DuckCall, CannotFly)
My stamp mental model
Those Duck, Quack, Squeak, DuckCall, FlyWithWings, FlyTurkeyLike, CannotFly and GameEntity, Biter, Barker, BatteryPowered, Killer bring some information about your future objects. I call the information THE METADATA.
Actually, the “compose” function does nothing else but simply merges the metadata of the provided stamps.
That’s it! That’s my stamp mental model!
Stamps are chunks of metadata. By merging those chunks together we can build new chunks which have the features combined.
The rest of the episodes:
- Episode 1. Stamp basics
- Episode 2. Dependency injection in FP
- Episode 3. Comparing with the ES2015 classes
- Episode 4. Implementing stamps yourself in 30 LOC
- Episode 5. Composition design pattern
- Episode 6. Statics — properties on stamps
- Episode 7. Early and late dependency injection
- Episode 8. Tracking and overriding composition
- Episode 9. Detaching compose()
- Episode 10. My stamp mental model (this article)
- Episode 11. Interfering composition
- Episode 12. New @stamp home
- Episode 13. Method collision control
- Episode 14. New @stamp/it as a replacement of Stampit
- Episode 15. The @stamp/ modules ecosystem
- Episode 16. TypeScript mix-in classes vs Stamps
- Episode 17. Easy 100% unit test coverage in JS
- Episode 18. Dependency injection paradise
- Episode 19. Java/C# abstract methods in JavaScript
- Episode 20. Stampit v4
- Episode 21. Private data in JavaScript. 4 ways using stamps
- Episode 22. JavaScript instanceof as composable stamp
- Episode 23. New stampit.js.org with all the docs
- Episode 24. New “name” feature