To create a simple class Foo derived from Class::DBI, one can do something like:
Now to get an instance of the Foo class, I can simply call the retrieve() method implemented in the base class(i.e.,Class::DBI), and call any method on the derived class(i.e., Foo):package Foo; use strict; use Class::DBI; use base 'Class::DBI'; Foo->set_db('Main',"url","user","password"); Foo->table('Foo'); Foo->columns(All=>qw/id name/); sub method1{} sub method2{}
This is all wonderful and natural, for perl. But I realized I can't do that easily in a static language, say Java: if retrieve() is defined/implemented in the base class, its return type has to be the base class, so unless one explicitly cast the returned object to the derived class, one can't call any method of the derived class. In fact, one can look at a general persistent framework like Hibernate, the query/finder method always requires casting ( the save() method is ok since it returns void.) One can further look at EJB, where the finder method is always part of a bean's home interface, there can't be a general finder method (unless some casting is involved). I always feel it's bad design if one has to do much casting. This seems to be at least one advantage of OO for dynamic languages: there is no static type to restrict what method you can call, so you can have at least a general finder method.my $foo = Foo->retrieve(1); # for row with id 1. $foo->method1();
In reply to perl, Class::DBI and OO for dynamic languages by johnnywang
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |