I’m writing Ruby code with a C++ accent, while Ruby’s core is small, it’s programming style is much more fluid and harder to get used, especially after coding in C++/Java/C# for the last 6 years.
Archive for September, 2006
This is something that had been bothering me ever since I got my own copy of “Design Patterns,” it started after I finished reading the Creational Patterns. “Why is this so difficult?” I thought to myself, patterns felt incomplete, they weren’t solutions, more like frameworks for solutions.
In Data Structures, we created Abstract Data Types in C using structs and typedefs, these ADTs provide encapsulation in C, similar to Classes in C++, except they are more difficult to implement. We were following the “Class” pattern in C, trying to overcome a limitation of the C programming language. It wasn’t until C++ that we were finally able to implement encapsulation easily with Classes.
Back to the Factory family of patterns. After programming with Ruby and Python I realized we can use Meta Classes, classes that define classes at run time, as a substitute for the Factory patterns.
With this type of programming, we don’t need to have factories and products, because these meta-classes are the factories. Languages such as Python and Rubysupport Meta programming, don’t suffer from the limitations of Java/C++/C#. If meta programming were available to us in C++/Java/C#, then we wouldn’t have to rely on Factory patterns, instead we would use meta-classes.
Also, while thumbing through my Java Design Patterns book, I finally realized that sometimes you have to use the Adapter pattern because Java doesn’t allow multiple inheritance. I understand that multiple inheritance can be dangerous, but, it is still a limitation of the language.
I’m sure other patterns from the GoF have their own counterparts as language features in other languages, but I don’t know them, yet.
I’m posting this here so I’ll remember it, and because it’s one of those simple things that people in software development often forget. A method of an object should invoke only the methods of the following kinds of
objects:
- itself,
- its parameters,
- any objects it creates/instantiates;
- its direct component objects.
I read about the Law of Demeter at javaguy.org