Source and discussion: http://www.blogjava.net/calvin/archive/2005/11/01/17678.html
Author: Jiangnan Baiyi
The policy-base class in modern c ++ design is the same as that of cpper;
The dependency injection of spring-type IOC container is the same thing that javaer is proud;
Ruby's Mixin feature is just like rubyer's.
What's interesting is that each of their proud tasks points to the same problem: a combination of multiple behaviors.
1. Policy-base is achieved by mixing the template with multiple inheritance of C ++:
The final class inherits the template class multiple times, and the template passes in the actual behavior class ).
Template <class checkingpolicy>
Class smartptr: Public checkingpolicy
{...};
Typedef smartptr <nochecking> nocheckptr;
The previous Code passes the nochecking class into smartptr during compilation and combines it into the nocheckptr class. In practice, "template parameter "....
2. Java follows the oldest delegate mode to achieve this:
The final class has a pointer to the behavior class that actually executes various behaviors. All methods of the final class are delegated to the behavior class for execution. If the behavior is diverse, use interface-based programming. At the same time, the Reflection Characteristics of Java are used, and IOC container is used to automate injection according to configuration.
3. Ruby relies on its own dynamic features to directly build the Mixin feature in the language.
Module greetable
Def greet
Puts "your age is," + self. Age
End
End
Class person
Include greetable
Def initialize (name, age)
@ Name = Name
@ Age = Age
End
Attr_reader: Age
End
Person = person. New ("Bruce", 40)
Person. Greet
Your age is 40.
This code entry adds the greetable class Minxin to the person class, giving it the GREET method.
Thanks to Ruby's dynamic nature, greetable can print a person attribute that is unknown to itself.
Just because the same thing still involves the selection of C ++ templates, traditional delegate and emerging dynamic Minxin, which makes us feel a little free and fun.