2 |
## $Id$ |
## $Id$ |
3 |
## -------------------------------------------------------------------------------- |
## -------------------------------------------------------------------------------- |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.13 2003/07/01 23:26:37 joko |
6 |
|
## croak to STDERR if module loading fails! |
7 |
|
## |
8 |
|
## Revision 1.12 2003/05/13 08:39:22 joko |
9 |
|
## autocalling constructor after instantiation |
10 |
|
## added pod documentation |
11 |
|
## |
12 |
|
## Revision 1.11 2003/02/21 08:38:21 joko |
13 |
|
## + additional checks |
14 |
|
## + raising exceptions |
15 |
|
## |
16 |
|
## Revision 1.10 2003/02/20 20:50:32 joko |
17 |
|
## + small exception handling: now inheriting from little Exception object |
18 |
|
## |
19 |
|
## Revision 1.9 2003/02/18 18:35:30 joko |
20 |
|
## + encapsulated/abstracted some more functionality: sub load_single |
21 |
|
## |
22 |
|
## Revision 1.8 2003/02/14 14:20:05 joko |
23 |
|
## + modified mixin behaviour |
24 |
|
## |
25 |
|
## Revision 1.7 2003/02/11 10:34:19 joko |
26 |
|
## + loaded module may now lack 'mixin::with' declaration |
27 |
|
## + this gets us the possibility to load modules from any perl namespace |
28 |
|
## + enabled this mechanism |
29 |
|
## |
30 |
|
## Revision 1.6 2003/02/09 16:22:51 joko |
31 |
|
## + pseudo constructor mechanism via options |
32 |
|
## |
33 |
## Revision 1.5 2003/01/31 01:19:50 root |
## Revision 1.5 2003/01/31 01:19:50 root |
34 |
## + fixed: doesn't need Log::Dispatch any more, but uses it if available |
## + fixed: doesn't need Log::Dispatch any more, but uses it if available |
35 |
## |
## |
54 |
use strict; |
use strict; |
55 |
use warnings; |
use warnings; |
56 |
|
|
57 |
use base qw( DesignPattern::Object ); |
use base qw( |
58 |
|
DesignPattern::Object |
59 |
|
DesignPattern::Exception |
60 |
|
); |
61 |
|
|
62 |
|
|
63 |
use Data::Dumper; |
use Data::Dumper; |
64 |
|
|
65 |
## ======== object inheritance ======== |
=pod |
66 |
|
|
67 |
|
=head1 NAME |
68 |
|
|
69 |
|
DesignPattern::Bridge |
70 |
|
|
71 |
|
|
72 |
|
=head1 DESCRIPTION |
73 |
|
|
74 |
|
This acts as a generic backplane for plugins using "Mix-in inheritance". |
75 |
|
|
76 |
|
|
77 |
|
=head2 BACKGROUND |
78 |
|
|
79 |
|
Mix-in inheritance |
80 |
|
Please visit [http://citeseer.nj.nec.com/bracha90mixinbased.html]. |
81 |
|
|
82 |
|
[...] |
83 |
|
Abstract: The diverse inheritance mechanisms provided by Smalltalk, Beta, |
84 |
|
and CLOS are interpreted as different uses of a single underlying construct. |
85 |
|
Smalltalk and Beta differ primarily in the direction of class hierarchy growth. |
86 |
|
These inheritance mechanisms are subsumed in a new inheritance model |
87 |
|
based on composition of mixins, or abstract subclasses. This form of |
88 |
|
inheritance can also encode a CLOS multiple-inheritance hierarchy, although |
89 |
|
changes to the encoded hierarchy that would violate... (Update) |
90 |
|
[...] |
91 |
|
|
92 |
|
...level, and can naturally span component boundaries. Jiazzi supports |
93 |
|
the composition of class extending components; e.g. mixins [1]. Mixins |
94 |
|
and cyclic component linking can be combined into an open class pattern, |
95 |
|
which allows independent features that cross cut class... |
96 |
|
|
97 |
|
=head3 History of "mixins" |
98 |
|
|
99 |
|
=head4 G. Bracha, W. Cook. Mixin-based inheritance. |
100 |
|
|
101 |
|
G. Bracha and W. Cook, "Mixin-Based Inheritance", ECOOP/OOPSLA 1990, 303-311. |
102 |
|
G. Bracha, W. Cook. Mixin-based inheritance. In OOPSLA/ECOOP '90 Conference Proceedings. ACM SIGPLAN Notices 25, 10 (Oct. 1990). |
103 |
|
|
104 |
|
@inproceedings{ bracha90mixinbased, |
105 |
|
author = "Gilad Bracha and William Cook", |
106 |
|
title = "Mixin-Based Inheritance", |
107 |
|
booktitle = "Proceedings of the Conference on Object-Oriented Programming: Systems, Languages, and Applications / Proceedings of the European Conference on Object-Oriented Programming", |
108 |
|
publisher = "ACM Press", |
109 |
|
address = "Ottawa, Canada", |
110 |
|
editor = "Norman Meyrowitz", |
111 |
|
pages = "303--311", |
112 |
|
year = "1990", |
113 |
|
url = "citeseer.nj.nec.com/bracha90mixinbased.html" } |
114 |
|
|
115 |
|
=head4 D. Duggan and C. Sourelis. Mixin modules. |
116 |
|
|
117 |
|
http://users.hol.gr/~csourelis/thesis.html |
118 |
|
|
119 |
|
@misc{ duggan96mixin, |
120 |
|
author = "D. Duggan and C. Sourelis", |
121 |
|
title = "Mixin modules", |
122 |
|
text = "D. Duggan and C. Sourelis. Mixin modules. In Intl. Conf. on Functional |
123 |
|
Programming, Philadelphia, May 1996. ACM Press.", |
124 |
|
year = "1996", |
125 |
|
url = "citeseer.nj.nec.com/sourelis95mixin.html" } |
126 |
|
|
127 |
|
=head4 Google on your own: |
128 |
|
|
129 |
|
http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&q=mix-in+inheritance |
130 |
|
http://www.google.com/search?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&q=Moo86+mixins |
131 |
|
|
132 |
|
|
133 |
|
=head3 Discussion |
134 |
|
|
135 |
|
http://citeseer.nj.nec.com/context/6117/0 |
136 |
|
|
137 |
|
=head2 References |
138 |
|
|
139 |
|
The Jigsaw Framework: |
140 |
|
http://citeseer.nj.nec.com/bracha92programming.html |
141 |
|
|
142 |
|
http://citeseer.nj.nec.com/114070.html |
143 |
|
http://citeseer.nj.nec.com/445396.html |
144 |
|
http://citeseer.nj.nec.com/tichelaar97coordination.html |
145 |
|
http://citeseer.nj.nec.com/meijler98beyond.html |
146 |
|
|
147 |
|
http://citeseer.nj.nec.com/89291.html |
148 |
|
http://citeseer.nj.nec.com/sousa91runtime.html |
149 |
|
http://citeseer.nj.nec.com/banavar95application.html |
150 |
|
http://citeseer.nj.nec.com/sourelis95mixin.html |
151 |
|
http://citeseer.nj.nec.com/hirschowitz02mixin.html |
152 |
|
|
153 |
|
Java: http://csis.pace.edu/~bergin/patterns/multipleinheritance.html |
154 |
|
C++: http://www.oofile.com.au/oofdoc/samples/ooftst19.htm |
155 |
|
|
156 |
|
=head3 Related |
157 |
|
|
158 |
|
http://citeseer.nj.nec.com/nrelated/6117/23339 |
159 |
|
|
160 |
|
|
161 |
|
=head3 Traits - Composable Units of Behavior |
162 |
|
|
163 |
|
via: http://citeseer.nj.nec.com/context/22076/0 |
164 |
|
|
165 |
|
Traits: Composable Units of Behavior - Nathanael Schrli Stphane (Correct) |
166 |
|
....is not expressive enough to factor out common features (i.e. instance |
167 |
|
variables and methods) shared by classes in a complex hierarchy. As a |
168 |
|
consequence, language designers have proposed various forms of multiple |
169 |
|
inheritance [Mey88] Kee89] Str86] as well as other mechanisms, such as |
170 |
|
mixins [Moo86] BC90] FKF98] that allow classes to be composed incrementally |
171 |
|
from sets of features. Despite the passage of nearly twenty years, neither |
172 |
|
multiple inheritance nor mixins have achieved wide acceptance [Tai96] |
173 |
|
Summarizing Alan Snyder s contribution to the inheritance panel discussion |
174 |
|
at OOPSLA .... |
175 |
|
|
176 |
|
....complete. But as a unit of reuse, a class should be small. These properties |
177 |
|
often conflict. Furthermore, the role of classes as instance generators requires |
178 |
|
that each class have a unique place in the class hierarchy, whereas units of |
179 |
|
reuse should be applicable at arbitrary places. Moon s Flavors [Moo86] were |
180 |
|
an early attempt to address this problem: Flavors are small, not necessarily |
181 |
|
complete, and they can be mixed in at arbitrary places in the class hierarchy. |
182 |
|
More sophisticated notions of mixins were subsequently developed by Bracha |
183 |
|
and Cook [BC90] and Flatt, Krishnamurthi and Felleisen .... |
184 |
|
|
185 |
|
David A. Moon. Object-oriented programming with flavors. In Proceedings |
186 |
|
OOPSLA '86, ACM SIGPLAN Notices, pages 1--8, November 1986. |
187 |
|
Published as Proceedings OOPSLA '86, ACM SIGPLAN Notices, volume 21, number 11. |
188 |
|
|
189 |
|
|
190 |
|
from: http://citeseer.nj.nec.com/566972.html |
191 |
|
|
192 |
|
Abstract: Inheritance is the fundamental reuse mechanism in object-oriented |
193 |
|
programming languages; its most prominent variants are single inheritance, |
194 |
|
multiple inheritance, and mixin inheritance. In the first part of this paper, |
195 |
|
we identify and illustrate the conceptual and practical reusability problems |
196 |
|
that arise with these forms of inheritance. We then present a simple |
197 |
|
compositional model for structuring object-oriented programs, which we call traits. |
198 |
|
Traits are essentially groups of methods... (Update) |
199 |
|
|
200 |
|
|
201 |
|
=head3 Hybrid - A Language for Programming with Active Objects |
202 |
|
|
203 |
|
http://citeseer.nj.nec.com/561934.html |
204 |
|
http://citeseer.nj.nec.com/nierstrasz92tour.html |
205 |
|
http://www.iam.unibe.ch/~scg/Archive/NFS/cao.html |
206 |
|
|
207 |
|
=head3 Active Objects and Oberon |
208 |
|
|
209 |
|
|
210 |
|
=head2 DETAILS |
211 |
|
|
212 |
|
This attempts to be a runtime wrapper around mixin.pm, written by Michael G Schwern |
213 |
|
and available on CPAN. |
214 |
|
Please visit: |
215 |
|
http://magnonel.guild.net/~schwern/talks/Design_Patterns/full_slides/slide036.html |
216 |
|
|
217 |
|
|
218 |
|
=head2 NOTES |
219 |
|
|
220 |
|
## ======== object inheritance ======== |
221 |
|
|
222 |
|
2002-12: |
223 |
|
TODO / REFACTORING PROPOSAL |
224 |
|
leading from Data::Storage to code abstracted out into this module - DesignPattern::Bridge |
225 |
|
- this is no inheritance and it doesn't have to be |
226 |
|
- implement this module as a bridge to its sub-modules |
227 |
|
- use the BridgePattern (http://c2.com/cgi/wiki?BridgePattern) |
228 |
|
Using patterns was already successfully done with Data::Storage::Handler |
229 |
|
by implementing the AdapterPattern (http://c2.com/cgi/wiki?AdapterPattern) |
230 |
|
with Perl's AUTOLOAD-mechanism |
231 |
|
- try to use Perl's "tie" command to implement this functionality here instead of using AUTOLOAD! |
232 |
|
- sub getChildNodes |
233 |
|
- sub run |
234 |
|
|
235 |
|
2003-02-11, joko: Does this have anything in parallel with CPAN's Class::Inner? No! |
236 |
|
|
237 |
|
|
238 |
|
=head1 TODO |
239 |
|
|
240 |
|
o use Class::Loader |
241 |
|
o rename to mixin::runtime? |
242 |
|
|
243 |
|
|
244 |
|
=cut |
245 |
|
|
|
# TODO: |
|
|
# - this is no inheritance and it doesn't have to be |
|
|
# - implement this module as a bridge to its sub-modules |
|
|
# - use the BridgePattern (http://c2.com/cgi/wiki?BridgePattern) |
|
|
# Using patterns was already successfully done with Data::Storage::Handler |
|
|
# by implementing the AdapterPattern (http://c2.com/cgi/wiki?AdapterPattern) |
|
|
# with Perl's AUTOLOAD-mechanism |
|
|
# - try to use Perl's "tie" command to implement this functionality here instead of using AUTOLOAD! |
|
|
# - sub getChildNodes |
|
|
# - sub run |
|
246 |
|
|
247 |
# get logger instance |
# get logger instance |
248 |
my $logger = eval { Log::Dispatch::Config->instance; }; |
my $logger = eval { Log::Dispatch::Config->instance; }; |
249 |
|
|
250 |
my $meta; |
my $meta; |
251 |
|
|
252 |
|
|
253 |
|
sub DEBUG { 1 } |
254 |
|
|
255 |
|
|
256 |
## ======== object constructor ======== |
## ======== object constructor ======== |
257 |
sub new { |
sub new { |
258 |
my $invocant = shift; |
my $invocant = shift; |
259 |
my $class = ref($invocant) || $invocant; |
my $class = ref($invocant) || $invocant; |
260 |
my @args = (); |
my @args = (); |
261 |
@_ && (@args = @_); |
@_ && (@args = @_); |
262 |
$logger->debug( __PACKAGE__ . "->new(@args)" ) if $logger; |
$logger->debug( __PACKAGE__ . "->new(@args)" ) if $logger; |
263 |
my $self = { @_ }; |
my $self = { @_ }; |
264 |
|
|
265 |
|
# trace |
266 |
#print "class: $class", "\n"; |
#print "class: $class", "\n"; |
267 |
bless $self, $class; |
|
268 |
##if (my $bizWorks = shift) { |
# create instance |
269 |
##$self->boot($bizWorks); |
bless $self, $class; |
270 |
##} |
|
271 |
|
# NEW: 2003-04-16 |
272 |
return $self; |
$self->constructor() if $self->can('constructor'); |
273 |
} |
|
274 |
|
return $self; |
275 |
|
} |
276 |
|
|
277 |
|
|
278 |
## ======== method overrider ======== |
## ======== method overrider ======== |
307 |
|
|
308 |
# build full package name |
# build full package name |
309 |
my $self_classname = ref $self; |
my $self_classname = ref $self; |
310 |
my $package = $self_classname . '::' . $modulename_load; |
# name |
311 |
|
my $package = $modulename_load; |
312 |
|
|
313 |
|
# if package is absolute, cut away prefix ('/' or '::') |
314 |
|
if ($package !~ s/^:://) { |
315 |
|
# else: prefix with base classname if above name is relative (lacks of '/' or '::') |
316 |
|
$package = $self_classname . '::' . $package |
317 |
|
} |
318 |
|
|
319 |
return $package; |
return $package; |
320 |
} |
} |
321 |
|
|
322 |
sub load { |
sub load { |
323 |
|
|
324 |
my $self = shift; |
my $self = shift; |
325 |
|
my $modulename = shift; |
326 |
|
my $options = shift; |
327 |
|
|
328 |
|
if (ref $modulename eq 'ARRAY') { |
329 |
|
foreach (@$modulename) { |
330 |
|
$self->load_single($_, $options); |
331 |
|
} |
332 |
|
} else { |
333 |
|
$self->load_single($modulename, $options); |
334 |
|
} |
335 |
|
|
336 |
|
} |
337 |
|
|
338 |
|
sub load_single { |
339 |
|
|
340 |
|
my $self = shift; |
341 |
my $modulename_load = shift; |
my $modulename_load = shift; |
342 |
|
|
343 |
|
my $options = shift; |
344 |
|
|
345 |
my $self_modulename = ref $self; |
my $self_modulename = ref $self; |
346 |
my $package = $self->_getPluginPackage($modulename_load); |
my $package = $self->_getPluginPackage($modulename_load); |
347 |
|
|
355 |
|
|
356 |
# this is the module testing phase - use mixin doesn't seem to propagate errors by default |
# this is the module testing phase - use mixin doesn't seem to propagate errors by default |
357 |
eval("use $package;"); |
eval("use $package;"); |
358 |
|
$self->checkExceptions(); |
359 |
|
|
360 |
|
=pod |
361 |
if ($@) { |
if ($@) { |
362 |
$meta->{loaded}->{$package} = 0; |
$meta->{loaded}->{$package} = 0; |
363 |
# include caller information |
# include caller information |
370 |
print $msg, "\n"; |
print $msg, "\n"; |
371 |
} |
} |
372 |
} |
} |
373 |
|
=cut |
374 |
|
|
375 |
#print "ref-1: ", ref $self, "\n"; |
#print "ref-1: ", ref $self, "\n"; |
376 |
#print "ref-2: ", ref $self::SUPER, "\n"; |
#print "ref-2: ", ref $self::SUPER, "\n"; |
379 |
#bless $self, $package; |
#bless $self, $package; |
380 |
|
|
381 |
# V2: |
# V2: |
|
# switch into foreign package and mixin plugin-module |
|
382 |
$self->mixinPackage($package); |
$self->mixinPackage($package); |
383 |
|
|
384 |
|
if (my $method = $options->{method}) { |
385 |
|
$self->$method(); |
386 |
|
} |
387 |
|
|
388 |
return 1; |
return 1; |
389 |
|
|
390 |
} |
} |
391 |
|
|
392 |
|
sub mixinPackage { |
393 |
|
my $self = shift; |
394 |
|
my $package = shift; |
395 |
|
|
396 |
|
# switch into foreign package and prepare for mixin |
397 |
|
$self->mixin_prepare($package); |
398 |
|
|
399 |
|
# switch into local package (scope which uses DesignPattern::Bridge) and mixin plugin-module |
400 |
|
$self->mixin_do($package); |
401 |
|
|
402 |
|
} |
403 |
|
|
404 |
# TODO: maybe refactor to DesignPattern::Object? what about the '$logger'? |
# TODO: maybe refactor to DesignPattern::Object? what about the '$logger'? |
405 |
sub mixinPackage { |
sub mixin_prepare { |
406 |
|
my $self = shift; |
407 |
|
my $package = shift; |
408 |
|
my $self_classname = ref $self; |
409 |
|
eval("package $package; use mixin::with '$self_classname';"); |
410 |
|
|
411 |
|
# FIXME: --- this is redundant --- |
412 |
|
if ($@) { |
413 |
|
$meta->{loaded}->{$package} = 0; |
414 |
|
my $msg = __PACKAGE__ . "->load: $@"; |
415 |
|
print STDERR $msg, "\n" if DEBUG; |
416 |
|
$logger->error( $msg ) if $logger; |
417 |
|
} else { |
418 |
|
$meta->{loaded}->{$package} = 1; |
419 |
|
} |
420 |
|
# FIXME: --- this is redundant --- |
421 |
|
|
422 |
|
} |
423 |
|
|
424 |
|
sub mixin_do { |
425 |
my $self = shift; |
my $self = shift; |
426 |
my $package = shift; |
my $package = shift; |
427 |
# switch into foreign package and mixin plugin-module |
# switch into foreign package and mixin plugin-module |
428 |
my $self_classname = ref $self; |
my $self_classname = ref $self; |
429 |
eval("package $self_classname; use mixin '$package';"); |
eval("package $self_classname; use mixin '$package';"); |
430 |
#eval("use mixin_all '$package';"); |
#eval("use mixin_all '$package';"); |
431 |
|
|
432 |
|
# FIXME: --- this is redundant --- |
433 |
if ($@) { |
if ($@) { |
434 |
$meta->{loaded}->{$package} = 0; |
$meta->{loaded}->{$package} = 0; |
435 |
$logger->error( __PACKAGE__ . "->load: $@" ) if $logger; |
$logger->error( __PACKAGE__ . "->load: $@" ) if $logger; |
436 |
} else { |
} else { |
437 |
$meta->{loaded}->{$package} = 1; |
$meta->{loaded}->{$package} = 1; |
438 |
} |
} |
439 |
|
# FIXME: --- this is redundant --- |
440 |
|
|
441 |
} |
} |
442 |
|
|
443 |
sub unload { |
sub unload { |
465 |
my $self = shift; |
my $self = shift; |
466 |
my $includefile = shift; |
my $includefile = shift; |
467 |
my $package = shift; |
my $package = shift; |
468 |
# TODO: do better error-detection here / prevent dies under all circumstances! |
|
469 |
require $includefile; |
# pre-flight checks |
470 |
|
if (!$includefile) { |
471 |
|
$self->raiseException('Filename for inclusion was empty.'); |
472 |
|
return; |
473 |
|
} |
474 |
|
|
475 |
|
# go for it ... |
476 |
|
eval("require '$includefile';"); |
477 |
|
# ... and handle errors afterwards catching every message from perl itself ... |
478 |
|
return if $self->checkExceptions(); |
479 |
|
|
480 |
|
# ... otherwise continue assuming everything is fine |
481 |
$self->mixinPackage($package) if $package; |
$self->mixinPackage($package) if $package; |
482 |
|
|
483 |
} |
} |
484 |
|
|
485 |
1; |
1; |
486 |
|
__END__ |