logo

Navigation:First_Community->Software->Java Goto:New TopicSettingSearch
JBuilder2005实现重构之类继承体系间
Posted by: keven Date: April 4, 2008 12:28AM
  在JBuilder中通过重构将值域、方法在类的继承体系中上推下移,是非常容易的操作。这了说明这种重构功能,我们先来看下面的类继承体系:

CopyRight owned by the original author.--(www.MegaEntry.com)

图 15 类的继承类图
  Animal类是Cat和Horse和父类,Animal中包含了两个值域和一个方法。可以将Animal中的值域有选择地下移到某个子类中。此外,还可以为Animal提炼超类和接口。  1、搬迁方法

CopyRight owned by the original author.--(www.MegaEntry.com)

  打开Animal.java并将光标定位到howl()方法处,通过Ctrl+Shift+R->Push Down Method调出下移方法对话框:
图 16 下移方法
  ・Method:下移的方法。

MegaEntry - Social networking and discussion site!

  ・Keep as abstract in current class:表示在下移方法的同时将类转为抽象类。  ・Selected subclasses:列出当前类的所有子类,默认情况下方法将下移到所有子类中,我们取消Cat,仅将howl()下移到Horse中。   方法howl()中引用到Animal类的一个私有变量private int volume,在下移howl()后,该变量变为protected int volume。

MegaEntry - Social networking and discussion site!

  上推和下移是相反的操作,负责将子类中的方法推到父类中去。保存并重新编译工程,打开Horse.java将光标置到howl()方法处:Ctrl+Shift+R->Pull Up Method调出上推的对话框:
图 17
  上推方法

MegaEntry - Social networking and discussion site!

  如果需上推的方法引用到了类中的其他方法,这些被引用的方法列在Select dependent members to moves列表中,一般情况下你需要将这些被引用的方法一起上移到父类中。  面向对象的一个重要特性即是子类共享父类的代码,所以当你发现子类的方法或值域是共享性的代码时,你就需要着手将这些代码推到父类中去了,这样代码的复用性将得到提升。  2、搬迁值域

MegaEntry - Social networking and discussion site!

  Animal类中有两个值域:
1. protected int weight ;2. protected int volume = weight ;
  我们希望将volume下移到子类Cat中,光标定位到volume处:Ctrl+Shift+R>Push Down Field调出下移值域的对话框:

MegaEntry - Social networking and discussion site!

图 18 下移值域
  ・Field:需下移的值域。  ・Select subclasses:当前类的子类。在默认情况下,值域下移到所有的子类中,可以通过子类前的复选框作出选择,这里我们去除Horse类前的勾选,即仅下移到Cat中。

MegaEntry - Social networking and discussion site!

  ・Select dependent members to move:和下移值域相关的所有值域,JBuilder通过下移值域右边的赋值语句找出所有关联的值域,通过关联值域前的复选框决定是否需要一起下移到子类。默认情况下,JBuilder将关联的属性weight随volume一起下移。  按OK后完成下移。编译工程,打开Horse.java,我们执行下移值域的反操作:上推值域到Animal类中。  光标定位在Horse.java的volume值域处:Ctrl+Shift+R->Pull Up Field调出上推值域对话框,如下图所示:

CopyRight owned by the original author.--(www.MegaEntry.com)

图 19
  上推值域  点击OK后,volume值域被上推到Horse父类Animal中。

CopyRight owned by the original author.--(www.MegaEntry.com)

  提示:  遗憾的是,JBuilder上推值域并不会像下移值域一样列出所有相关值域,只是简单的将值域声明代码行上推到父类中。所以如果在值域的定义代码行中引用了类中其它的值域,在上推到父为中时,将会发生语法错误,需要你手工解决。  3、提炼超类

CopyRight owned by the original author.--(www.MegaEntry.com)

  几个类具有相同的功能时,就需要提炼出一个超类出来。如随着需求的变化,我们不但需要对四脚野兽(Animal)予以关注,还需要对鸟类(Bird)投注关怀,那么在Bird和Animal之上抽象出一个Creature类出来是再适合不过的了。  打开Animal.java,光标移至类名称Animal处:Ctrl+Shift+R->Introduce Superclass for “Animal” 调出提炼超类的对话框,如下图所示:
图 20 提炼超类对话框

CopyRight owned by the original author.--(www.MegaEntry.com)

  在Superclass name中指定超类名Creature,在Package中指定超类所在的包名,默认为当前类所在的包,按OK按钮,JBuilder创建Creature超类,并使Animal类继承这个超类:  代码清单 7 Creature超类
1. package myrefactor ;2.

MegaEntry - Social networking and discussion site!

3. public class Creature4. {5.  public Creature()6.  {7.  }8. }

MegaEntry - Social networking and discussion site!

  Animal代码做如下调整  代码清单 8 提炼超类
1. package myrefactor ;2.

CopyRight owned by the original author.--(www.MegaEntry.com)

3. public class Animal extends Creature4. {5.  …6. }
  由于Creature生成后并未编译,所以Animal类在编辑器中会提示语法错误, 出现在控制槽上。你可以通过重构信息窗口的 按钮编译重构相关的类以消除这个语法错误。本文后续内容还会碰到相似的情况,解决方法相同,将不再赘述。

CopyRight owned by the original author.--(www.MegaEntry.com)

  4、提炼接口  假设我们需要为动物定义一些共同的习性,可以将类的若干方法提炼出来,用一个接口来表达。打开Animal.java,将光标移到类名Animal处:Ctrl+Shift+R->Extract Interface from Class “Animal”调出提炼接口对话框,如下所示:
图 21 提炼接口对话框

MegaEntry - Social networking and discussion site!

  ・Interface name:接口名称,填入Habitable。  ・Package:包名,接受默认值。  ・Available methods:JBuilder将Animal类中所有的public方法列在该列表中,你可以选择需要将哪些方法抽取到接口中。

MegaEntry - Social networking and discussion site!

  点击OK完成Habitable接口的提炼。JBuilder创建Habitable接口,并使Animal实现这个接口。  代码清单 9 Habitable接口
1. package myrefactor ;2.

MegaEntry - Social networking and discussion site!

3. public interface Habitable4. {5.  public void howl() ;6. }
  Animal类调整如下:

CopyRight owned by the original author.--(www.MegaEntry.com)

  代码清单 10 提炼接口
1. package myrefactor ;2. public class Animal extends Creature implements Habitable3. {4.  …

CopyRight owned by the original author.--(www.MegaEntry.com)

5. }
  假设你仅希望将类中的某个方法提炼到一个接口中,你可以直接将光标移至方法名处:Ctrl+Shift+R->Extract to Interface Method更直接地完成这个操作。


Reply To This Message
Subject: 

Copyright 2005-2006 megaentry,All Rights Reserved
IE 6.0 or above is perfect