Modern C ++ design, Spring framework and Ruby Mixin ....

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.