|< 1 2 3 >| | 26 Einträge, 3 Seiten |
QuoteA constraint is a limitation of possibilities.
1 2 3 4 5 6 7 8 9 10 11
my $sum1= Variable->new( "first_summand" ); my $sum2= Variable->new( "second_summand" ); my $sum3= Variable->new( "result" ); my $adder= Addierer->new(); $adder->connectFirstSummand( $sum1 ); $adder->connectSecondSummand( $sum2 ); $adder->connectResult( $sum3 ); $sum1->setValue( 5 ); $sum2->setValue( 3 ); print $sum3->getValue(); # should print 8
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;
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;
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;
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";
|< 1 2 3 >| | 26 Einträge, 3 Seiten |