Category: Modules Keywords: InsideOut Object
最近在 PerlMonks.org 很红火的一个主题便是 "Inside Out Object".xdg 简单的总结了下便有下面这么多连接:
Object-Oriented Perl - Inside-Out Technique
- Re: Where/When is OO useful?
- Re: Tutorial: Introduction to Object-Oriented Programming
- A different OO approach
- Yet Another Perl Object Model (Inside Out Objects)
- Class::InsideOut - yet another riff on inside out objects.
- Threads and fork and CLONE, oh my!
- inside-out objects using arrays?
- Array-based Inside-out Objects with Object::InsideOut
- How to use Inheritance with Inside-Out Classes and Hash Based Classes
- Inside-out objects inheriting from hash-based classes
- Seeking inside-out object implementations
- Anti-inside-out-object-ism
- Overriding bless for inside-out object safety
- Introducing Class::InsideOut
最近的一份讲演稿就简单的阐述了一下不同之处:http://dagolden.com/talks/20060117_whats_all_the_fuss.pdf
My Opionion
因为我也在学习之中,所以就写写自己的看法。不当之处请指正。
在 Damain Conway 的新书 Perl Best Practices 中有一个章节便是专门讲 "Inside Out Object", 以前看到过觉得比较麻烦就没有理会跳过去了。
最近碰到这么多帖子就想起再去看 perldoc Class::Std
在这个 perldoc 中,Damain 解释了为什么 InsideOut 要比普通的 Hash 更好一些。他认为普通的 bless \%hash $module; 对 $hash 里的东西都可以直接读写,没有保护好。这不是 OO 所应当给予的权利。比如我们有一个模块,里面提供了一个方法:
sub get_name {
my $self = shift;
return $self->{name};
}
而熟悉这个模块的人可以跳过 get_name 这个方法而直接读写 $object->{name}. 这不是一个好的 OO 习惯。当然这不是 InsideOut Object 的原意,这只是带来的好处之一。封装变量。
更多的查看那个讲演稿 PDF. 在我看来,InsideOut Object 确实有很多好处,唯一麻烦的就是大家都不太熟悉和习惯。
因为最近回了家,用我表弟的电脑在上网。他也没装 Perl 所以我也没机会对 Object::InsideOut 和 Class::InsideOut 做试验。
只好等假期过完回到学校再说了。诸位有机会还是可以试试这个的。Have fun!