Extract Package -- 抽取包
Refactoring contributed by Gerard M. Davison
A package either has too many classes to be easily understandable or it suffers from the 'Promiscuous packages' smell.
一个包有太多类以至于不易理解或者有种“混杂包”的坏味道。
Extract a sub package depending on their gross dependencies or usages.
根据依赖关系或者用法进行子包的抽取。
The way I've done this is to move all the classes in one go. (I'm prepared to take the big step first here since all the errors can be found with compiling.)
有时候我利用抽取包重构手段一口气移动了所有的类。(因为在编译期可以发现所有错误,所以使我能够大胆的进行一大步重构)
I ensure the original package is dependent on the extracted package but the extracted package is independent of the original. Once I've done the move I compile the original package. The errors here can be fixed with import statements in the offending classes. I fix these errors until the original package compiles
我应该保证原始包依赖于抽取出来的包,但是抽取出来的包必须是独立于原始包的。抽取包之后,编译原始包,利用import语句解决编译错误。
Then I work on the extracted package. This may compile fine, the problem lies if you have a class in the extracted package referring to a class in the original package. I move it back if I can. If not I use extractInterface to create an interface which captures the way the classes in the extracted pacakge refer to the original class. I then place the extracted interface in the extracted package.
然后我开始处理抽取出来的包。如果抽取出来的包引用了原始包,尽可能重构回退,不行的话,利用extractInterface创建一个接口,这个接口主要是抽取包引用原始包的接口,然后将这个接口放到抽取出来的包里。
Martin Fowler