Ok, wenn wir gleich posten dürfen...
Variable.pm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package Variable;
sub new{
my ($class) = @_;
my $self = {value => 0};
bless $self,$class;
return $self;
}
sub setValueAdderVar{
my ($self,$val) = @_;
$self->{old} = $self->{value};
$self->{value} = $val * $val;
$self->{Adder}->refresh($self->{old},$self->{value});
}
sub connectAdder{
my ($self,$var) = @_;
$self->{Adder} = $var;
}
sub connectSqrt{
my ($self,$var) = @_;
$self->{Sqrt} = $var;
}
sub setValueSqrtVar{
my ($self,$val) = @_;
$self->setValue($val);
$self->{Sqrt}->refresh($self->getValue());
}
sub setValue{
my ($self,$val) = @_;
$self->{value} = $val;
}
sub getValue{
my ($self) = @_;
return $self->{value};
}
1;
Adder.pm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package Adder;
sub new{
my ($class) = @_;
my $self = {sum => 0};
bless $self,$class;
return $self;
}
sub connectSum{
my ($self,$var) = @_;
$self->{SumVar} = $var;
}
sub refresh{
my ($self,$old,$new) = @_;
$self->{sum} += ($new - $old);
$self->{SumVar}->setValueSqrtVar($self->{sum});
}
1;
Sqrt.pm:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package Sqrt;
sub new{
my ($class) = @_;
my $self = {};
bless $self,$class;
return $self;
}
sub connectRes{
my ($self,$var) = @_;
$self->{Result} = $var;
}
sub refresh{
my ($self,$val) = @_;
$self->{Result}->setValue(sqrt($val));
}
1;
Skript:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#! /usr/bin/perl
use strict;
use warnings;
use lib qw(.);
use Variable;
use Adder;
use Sqrt;
my $alpha = Variable->new();
my $beta = Variable->new();
my $gamma = Variable->new();
my $delta = Variable->new();
my $adder = Adder->new();
my $sqrt = Sqrt->new();
$alpha->connectAdder($adder);
$beta->connectAdder($adder);
$adder->connectSum($gamma);
$gamma->connectSqrt($sqrt);
$sqrt->connectRes($delta);
$alpha->setValueAdderVar(3);
$beta->setValueAdderVar(4);
print $delta->getValue(),"\n";
@kabel: so wie Du das kurz umrissen hast, würde nicht die veränderte Variable sich bei der nächsten Instanz (z.B. Adder oder Sqrt) melden, sondern die nächste Instanz müsste sich von der vorherigen Instanz die Werte holen...
Ist wahrscheinlich keine besonders gute Lösung, aber mal eine Diskussionsgrundlage ;)