我注意到的第一件事是你没有为你的
类。
$self->{price} = 0;
这违反了使用 Moose 提供的大部分封装。驼鹿解决方案
至少会使price成为一个实际的属性。
use 5.10.0; # for given/when
has '_price' => ( is => 'rw' );
sub price {
my ( $self, $item ) = @_;
given ($item) {
when ('A') { $self->_price($PRICE_OF_A) }
default { $self->_price(0) }
}
}
但是,您提供的代码的主要问题是您不是
真正建模了 Kata 描述的问题。首先是 Kata
声明您需要在每次调用
结帐对象。所以我们知道我们需要将这个状态保存在其他东西中
而不是一系列 ReadOnly 类变量。
has pricing_rules => (
isa => 'HashRef',
is => 'ro',
traits => ['Hash'],
default => sub { {} },
handles => {
price => 'get',
has_price => 'exists'
},
);
您会看到这里的 price 方法现在是使用 Moose 的“Native
属性”委托。这将在项目和规则之间执行查找。
但是,这不会处理不查找元素的情况
存在。 Kata 提供的单元测试之一就是这样
一种查找:
is( price(""), 0 ); # translated to Test::More
所以我们需要稍微修改价格方法来处理这种情况。
基本上我们检查是否有定价规则,否则返回 0。
around 'price' => sub {
my ( $next, $self, $item ) = @_;
return 0 unless $self->has_price($item);
$self->$next($item);
};
接下来,我们需要在扫描项目时对其进行跟踪,以便我们建立一个总数。
has items => (
isa => 'ArrayRef',
traits => ['Array'],
default => sub { [] },
handles => {
scan => 'push',
items => 'elements'
},
);
“Native Attributes”委托再次提供了scan 方法,
正在寻找 Kata 中的测试。
# Translated to Test::More
my $co = Checkout->new( pricing_rules => $RULES );
is( $co->total, 0 );
$co->scan("A");
is( $co->total, 50 );
最后一个total 方法使用List::Util 的sum 函数是微不足道的。
sub total {
my ($self) = @_;
my @prices = map { $self->price($_) } $self->items;
return sum( 0, @prices );
}
这段代码没有完全实现 Kata 的解决方案,但它确实
提出一个更好的问题状态模型并展示更多
“Moosey”解决方案。
完整的代码和翻译的测试如下所示。
{
package Checkout;
use Moose;
our $VERSION = '0.01';
use namespace::autoclean;
use List::Util qw(sum);
has pricing_rules => (
isa => 'HashRef',
is => 'ro',
traits => ['Hash'],
default => sub { {} },
handles => {
price => 'get',
has_price => 'exists'
},
);
around 'price' => sub {
my ( $next, $self, $item ) = @_;
return 0 unless $self->has_price($item);
$self->$next($item);
};
has items => (
isa => 'ArrayRef',
traits => ['Array'],
default => sub { [] },
handles => {
scan => 'push',
items => 'elements'
},
);
sub total {
my ($self) = @_;
my @prices = map { $self->price($_) } $self->items;
return sum( 0, @prices );
}
__PACKAGE__->meta->make_immutable;
}
{
package main;
use 5.10.0;
use Test::More;
our $RULES = { A => 50 }; # need a full ruleset
sub price {
my ($goods) = @_;
my $co = Checkout->new( pricing_rules => $RULES ); # use BUILDARGS the example API
for ( split //, $goods ) { $co->scan($_) }
return $co->total;
}
TODO: {
local $TODO = 'Kata 9 not implemented';
is( price(""), 0 );
is( price("A"), 50 );
is( price("AB"), 80 );
is( price("CDBA"), 115 );
is( price("AA"), 100 );
is( price("AAA"), 130 );
is( price("AAAA"), 180 );
is( price("AAAAA"), 230 );
is( price("AAAAAA"), 260 );
is( price("AAAB"), 160 );
is( price("AAABB"), 175 );
is( price("AAABBD"), 190 );
is( price("DABABA"), 190 );
my $co = Checkout->new( pricing_rules => $RULES );
is( $co->total, 0 );
$co->scan("A");
is( $co->total, 50 );
$co->scan("B");
is( $co->total, 80 );
$co->scan("A");
is( $co->total, 130 );
$co->scan("A");
is( $co->total, 160 );
$co->scan("B");
is( $co->total, 175 );
}
done_testing();
}