このコードは、異なる種類のオフィスドキュメントを表現し、それぞれのドキュメントのエクスポートや編集機能を提供しています。
しかし、各ドキュメントの種類に関連した条件分岐が多数存在し、拡張性が低く、管理が難しい状態です。
この問題を解決し、コードの保守性と拡張性を向上させるために、以下のソースコードをリファクタリングしなさい。
<?php
class Office
{
private string $title;
private string $type;
public function __construct(string $title, string $type)
{
$this->title = $title;
$this->type = $type;
}
public function export(): void
{
if ($this->type === 'spreadsheet') {
echo "Exporting Spreadsheet: $this->title" . PHP_EOL;
} elseif ($this->type === 'report') {
echo "Exporting Report: $this->title" . PHP_EOL;
}
}
public function edit(): void
{
if ($this->type === 'spreadsheet') {
echo "Editing Spreadsheet: $this->title" . PHP_EOL;
}
}
}
<?php
// 使用例
$spreadsheet = new Office("Excel Sheet", "spreadsheet");
$spreadsheet->export();
$spreadsheet->edit();
$report = new Office("Monthly Report", "report");
$report->export();