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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
| #!/usr/bin/env perl
use v5.36;
# WeatherService クラス
# 国内天気サービス(統一インターフェースの基準)
package WeatherService {
use v5.36;
use Moo;
has 'name' => (is => 'ro', default => sub { '国内天気サービス' });
sub get_weather ($self, $city) {
my %weather_data = (
'東京' => { condition => '晴れ', temperature => 25 },
'大阪' => { condition => '曇り', temperature => 23 },
'札幌' => { condition => '雨', temperature => 18 },
);
return $weather_data{$city} // { condition => '不明', temperature => 0 };
}
sub show_weather ($self, $city) {
my $weather = $self->get_weather($city);
say "$city の天気: $weather->{condition}(気温: $weather->{temperature}℃)";
}
}
# OldWeatherAPI クラス
# レガシーな天気情報API
package OldWeatherAPI {
use v5.36;
use Moo;
sub fetch_weather_info ($self, $location) {
my %data = (
'東京' => '晴れ/25度',
'大阪' => '曇り/23度',
'名古屋' => '晴れ/26度',
);
return $data{$location} // '情報なし';
}
}
# OldWeatherAdapter クラス
# OldWeatherAPI用の橋渡しクラス
package OldWeatherAdapter {
use v5.36;
use Moo;
has 'old_api' => (is => 'ro', required => 1);
has 'name' => (is => 'ro', default => sub { 'レガシー天気API' });
sub get_weather ($self, $city) {
my $info = $self->old_api->fetch_weather_info($city);
if ($info eq '情報なし') {
return { condition => '不明', temperature => 0 };
}
my ($condition, $temp_str) = split '/', $info;
$temp_str =~ s/度$//;
return {
condition => $condition,
temperature => int($temp_str),
};
}
sub show_weather ($self, $city) {
my $weather = $self->get_weather($city);
say "$city の天気: $weather->{condition}(気温: $weather->{temperature}℃)";
}
}
# ForeignWeatherService クラス
# 海外天気情報サービス
package ForeignWeatherService {
use v5.36;
use Moo;
sub retrieve_conditions ($self, $city_code) {
my %data = (
'NYC' => ['Sunny', 20],
'LON' => ['Cloudy', 15],
'PAR' => ['Rainy', 12],
);
return $data{$city_code} // ['Unknown', 0];
}
sub city_codes ($self) {
return {
'ニューヨーク' => 'NYC',
'ロンドン' => 'LON',
'パリ' => 'PAR',
};
}
}
# ForeignWeatherAdapter クラス
# ForeignWeatherService用の橋渡しクラス
package ForeignWeatherAdapter {
use v5.36;
use Moo;
has 'foreign_service' => (is => 'ro', required => 1);
has 'name' => (is => 'ro', default => sub { '海外天気サービス' });
my %CONDITION_MAP = (
'Sunny' => '晴れ',
'Cloudy' => '曇り',
'Rainy' => '雨',
'Unknown' => '不明',
);
sub get_weather ($self, $city) {
my $codes = $self->foreign_service->city_codes;
my $city_code = $codes->{$city};
unless ($city_code) {
return { condition => '不明', temperature => 0 };
}
my $result = $self->foreign_service->retrieve_conditions($city_code);
my ($condition_en, $temp) = @$result;
my $condition_ja = $CONDITION_MAP{$condition_en} // '不明';
return {
condition => $condition_ja,
temperature => $temp,
};
}
sub show_weather ($self, $city) {
my $weather = $self->get_weather($city);
say "$city の天気: $weather->{condition}(気温: $weather->{temperature}℃)";
}
}
# メイン処理
package main {
use v5.36;
say "=== 複数サービス統合 天気情報ツール ===";
say "";
# 3つのサービスを準備(橋渡しクラスでラップ)
my @services = (
WeatherService->new,
OldWeatherAdapter->new(old_api => OldWeatherAPI->new),
ForeignWeatherAdapter->new(foreign_service => ForeignWeatherService->new),
);
# 各サービスの情報を表示
for my $service (@services) {
say "【" . $service->name . "】";
if ($service->name eq '海外天気サービス') {
$service->show_weather('ニューヨーク');
$service->show_weather('ロンドン');
$service->show_weather('パリ');
} else {
$service->show_weather('東京');
$service->show_weather('大阪');
}
say "";
}
say "--- 統一インターフェースの効果 ---";
say "・3つのサービスを同じメソッドで呼び出せる";
say "・ループで統一的に処理できる";
say "・新しいサービスの追加が容易";
}
|