# 任务6：创建子类

在上一步任务中，我们把重复魔法字符串收缩到了`isXXX()`的判断函数及对象创建函数`Item.createXXX()`里，增强了它的可维护性。随着这项重构的完成，我们也发现了一些别的征兆：有些商品需要一些特殊的处理，证据正是`isAgedBrie()`、`isBackstagePass()`以及`isSulfuras()`这几个刚刚抽取出来的函数。它们被用于判断商品的具体类型，然后执行不同的操作。

这与规格文档里的描述不谋而合！根据需求规格的描述，陈年干酪（aged brie）、后台门票（backstage pass）等确实是些特殊的商品，有不同的过期和保质规则。虽然，重构进行到这里，我们还很难从代码里看清这些“不同的操作”具体是什么，但“有几类特殊的商品”一事是确定的。既然它们有不同的行为，那就值得为它们创建不同的子类。这样未来识别出属于他们各自的行为时，就可以将商品特定的行为搬移到子类中去。

## 你的任务

Java：

```
git checkout task-6-subclassing
./gradlew clean build
```

JavaScript：

```
git checkout task-6-subclassing
npm test
```

1. 针对三类特别的商品，分别创建对应的子类
2. 将`isAgedBrie()`等三个条件的判断搬移到子类中去
3. 要求小步前进，每一步修改之后马上运行所有测试，确保软件行为不变

## 参考

* 《重构 2》10.4 以多态取代条件表达式（Replace Conditional with Polymorphism）


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ethan-lin.gitbook.io/refactoring/xiang-jin-mei-gui/8-task-6.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
