Re: [PW-dev] Static methods - was Widget factory
Status: Alpha
Brought to you by:
spadkins
|
From: Stephen A. <ste...@of...> - 2001-06-12 03:01:08
|
At 05:08 PM 6/11/2001 -0500, you wrote:
>"Jay Lawrence" <Ja...@La...> wrote:
>>James and Stephen,
>>
>>Could you please expand on what you mean by static methods?
>
>These would be methods that are not tied to an instance -- the `new' method
>(basically, any constructor) is an example of a usually static method. Any
>method that requires an instance of an object class is not a static method.
>
>Perl blurs the line between static and non-static methods. C++ is very
>explicit and requires the `static' keyword before the function declaration.
>
>The static methods do not have an object to get any information from, so any
>configuration information they get would have to either come from the
function
>arguments or from globals.
>--
>James Smith <JG...@TA...>, 979-862-3725
>Texas A&M CIS Operating Systems Group, Unix
>
James is correct on all points.
For example
$wc = Widget->controller();
is a "static" method on the Widget package. No instance of a "Widget" class
was required in order to call the controller() method. Any routine called
from the package name (i.e. Package::Name->method_name()) is a static method.
(The terminology indeed comes from C++ and Java.)
A "normal" method call (dynamic?) is called on an instance of a class. i.e.
$query = CGI->new(); # this is a static method call, called from
package
print $query->param("x"); # this is a normal (dynamic) method call,
called from object
The "Factory" pattern is a standard way of constructing objects without
knowing
what type of objects they actually are. In order to instantiate an object,
you
normally need to know the class of the object. With a factory, you can let
the
factory decide. In this way,
$wc = Widget->controller();
is letting the static method, "controller()", in the Widget package decide
which
class it should instantiate when returning a Controller.
Similarly, with
$date_widget = $wc->widget("date");
we are letting the controller decide (based on config and runtime information)
what actual class to instantiate.
Thus, both the Widget package and Widget::Controller objects are acting as
factories
in the PWL.
Stephen
|