重构代码的方法技巧
重构太经典了!
重构里面比较简单的又算是Extract Method,这是我的代码里的一个类,经过我Extract Method后,我自己是越看越喜欢:P
本来createControl方法看起来很晕得,重构够变得非常简单了,几乎不需要注释,每个方法名就是注释.
当然有时候Extract Method是很麻烦的,比如碰到很多其它外部变量时.
对于这个简单的例子,我的心得就是取个好的方法名!
/**
*@authorRey
*/
publicclassAutoSqlDataSetPageextendsWizardPage{
publicstaticfinalStringID="AutoSqlDataSetPage";
privatestaticfinalintOK=0;
privatestaticfinalintCANCEL=-1;
privateListtableList=null;
privateDataTableinitDataTable=null,resultDataTable=null;
privateTableViewerinitTableViewer=null,resultTableViewer=null;
privateString[]names=newString[]{""};
publicAutoSqlDataSetPage(){
super(ID);
}
privateintindex=1;//input
privateintclick=-1;
privateImageimage=null;
privateintWIDTH=40,HEIGHT=40;
privateCanvascanvas=null;
privateColumnMoveCommandcolumnMoveCommand=null;
publicvoidcreateControl(finalCompositeparent){
setTitle("AutoSqlDataSetWizard");
setMessage("XXXXXXXXXXXXXXXX",IMessageProvider.INFORMATION);
Compositecomposite=newComposite(parent,SWT.NULL);
GridLayoutglayout=newGridLayout(9,true);
composite.setLayout(glayout);
CompositemainBtnsComposite=createMainBtnsComposite(composite);
ButtonmainBtnAdd=createMainBtn(mainBtnsComposite,"Add");
ButtonmainBtnRemove=createMainBtn(mainBtnsComposite,"Remove");
ButtonmainBtnProperties=createMainBtn(mainBtnsComposite,"Properties");
ButtonmainBtnPreview=createMainBtn(mainBtnsComposite,"Preview");
createSeparator(composite);
createImage(composite);
createCanvas(composite);
addListener2Canvas(parent);
TableinitTable=createTable(composite);
initTableViewer=newTableViewer(initTable);
initTableViewer.setContentProvider(newColumnContentProvider());
initTableViewer.setLabelProvider(newColumnLabelProvider());
CompositetableBtnsComposite=createTableBtnsComposite(composite);
finalButtonadd=createTableBtn(tableBtnsComposite,">>");
finalButtonremove=createTableBtn(tableBtnsComposite,"<<");
finalButtonaddAll=createTableBtn(tableBtnsComposite,">>>");
finalButtonremoveAll=createTableBtn(tableBtnsComposite,"<<<");
finalButtonundo=createUndoAndRedoBtn(tableBtnsComposite,"undo");
finalButtonredo=createUndoAndRedoBtn(tableBtnsComposite,"redo");
TableresultTable=createTable(composite);
resultTableViewer=newTableViewer(resultTable);
resultTableViewer.setContentProvider(newColumnContentProvider());
resultTableViewer.setLabelProvider(newColumnLabelProvider());
/***********************************************************************
*MainBtns'SelectionListener
**********************************************************************/
addListener2MainBtnAdd(parent,mainBtnAdd);
/***********************************************************************
*TableandColumnBtns'SelectionListener
**********************************************************************/
addListener2AddBtn(add,undo,redo);
addListener2RemoveBtn(remove,undo,redo);
addListener2UndoBtn(undo);
addListener2RedoBtn(redo);
addListener2AddAllBtn(addAll,undo,redo);
addListener2RemoveAllBtn(removeAll,undo,redo);
setControl(composite);
setPageComplete(true);
}
privateCompositecreateMainBtnsComposite(Compositecomposite){
GridLayoutglayout;
CompositemainBtnsComposite=newComposite(composite,SWT.NULL);
GridDatagd=newGridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan=9;
mainBtnsComposite.setLayoutData(gd);
glayout=newGridLayout(9,true);
mainBtnsComposite.setLayout(glayout);
returnmainBtnsComposite;
}
privateButtoncreateMainBtn(CompositemainBtnsComposite,Stringtext){
GridDatagd;
ButtonmainBtnAdd=newButton(mainBtnsComposite,SWT.PUSH);
mainBtnAdd.setText(text);
gd=newGridData(GridData.FILL_HORIZONTAL);
mainBtnAdd.setLayoutData(gd);
returnmainBtnAdd;
}
privatevoidcreateSeparator(Compositecomposite){
GridDatagd;
Labellabel=newLabel(composite,SWT.SEPARATOR|SWT.HORIZONTAL);
gd=newGridData(GridData.HORIZONTAL_ALIGN_FILL);
gd.horizontalSpan=9;
label.setLayoutData(gd);
}
privatevoidcreateCanvas(Compositecomposite){
GridDatagd;
canvas=newCanvas(composite,SWT.NO_REDRAW_RESIZE|SWT.V_SCROLL
|SWT.H_SCROLL);
gd=newGridData(GridData.FILL_BOTH);
gd.horizontalSpan=9;
canvas.setLayoutData(gd);
}
privatevoidaddListener2Canvas(finalCompositeparent){
finalPointorigin=newPoint(0,0);
finalScrollBarhBar=canvas.getHorizontalBar();
hBar.addListener(SWT.Selection,newListener(){
publicvoidhandleEvent(Evente){
inthSelection=hBar.getSelection();
intdestX=-hSelection-origin.x;
Rectanglerect=image.getBounds();
canvas.scroll(destX,0,0,0,rect.width,rect.height,false);
origin.x=-hSelection;
}
});
finalScrollBarvBar=canvas.getVerticalBar();
vBar.addListener(SWT.Selection,newListener(){
publicvoidhandleEvent(Evente){
intvSelection=vBar.getSelection();
intdestY=-vSelection-origin.y;
Rectanglerect=image.getBounds();
canvas.scroll(0,destY,0,0,rect.width,rect.height,false);
origin.y=-vSelection;
}
});
canvas.addListener(SWT.Resize,newListener(){
publicvoidhandleEvent(Evente){
Rectanglerect=image.getBounds();
Rectangleclient=canvas.getClientArea();
hBar.setMaximum(rect.width);
vBar.setMaximum(rect.height);
hBar.setThumb(Math.min(rect.width,client.width));
vBar.setThumb(Math.min(rect.height,client.height));
inthPage=rect.width-client.width;
intvPage=rect.height-client.height;
inthSelection=hBar.getSelection();
intvSelection=vBar.getSelection();
if(hSelection>=hPage){
if(hPage<=0)
hSelection=0;
*@authorRey
*/
publicclassAutoSqlDataSetPageextendsWizardPage{
publicstaticfinalStringID="AutoSqlDataSetPage";
privatestaticfinalintOK=0;
privatestaticfinalintCANCEL=-1;
privateListtableList=null;
privateDataTableinitDataTable=null,resultDataTable=null;
privateTableViewerinitTableViewer=null,resultTableViewer=null;
privateString[]names=newString[]{""};
publicAutoSqlDataSetPage(){
super(ID);
}
privateintindex=1;//input
privateintclick=-1;
privateImageimage=null;
privateintWIDTH=40,HEIGHT=40;
privateCanvascanvas=null;
privateColumnMoveCommandcolumnMoveCommand=null;
publicvoidcreateControl(finalCompositeparent){
setTitle("AutoSqlDataSetWizard");
setMessage("XXXXXXXXXXXXXXXX",IMessageProvider.INFORMATION);
Compositecomposite=newComposite(parent,SWT.NULL);
GridLayoutglayout=newGridLayout(9,true);
composite.setLayout(glayout);
CompositemainBtnsComposite=createMainBtnsComposite(composite);
ButtonmainBtnAdd=createMainBtn(mainBtnsComposite,"Add");
ButtonmainBtnRemove=createMainBtn(mainBtnsComposite,"Remove");
ButtonmainBtnProperties=createMainBtn(mainBtnsComposite,"Properties");
ButtonmainBtnPreview=createMainBtn(mainBtnsComposite,"Preview");
createSeparator(composite);
createImage(composite);
createCanvas(composite);
addListener2Canvas(parent);
TableinitTable=createTable(composite);
initTableViewer=newTableViewer(initTable);
initTableViewer.setContentProvider(newColumnContentProvider());
initTableViewer.setLabelProvider(newColumnLabelProvider());
CompositetableBtnsComposite=createTableBtnsComposite(composite);
finalButtonadd=createTableBtn(tableBtnsComposite,">>");
finalButtonremove=createTableBtn(tableBtnsComposite,"<<");
finalButtonaddAll=createTableBtn(tableBtnsComposite,">>>");
finalButtonremoveAll=createTableBtn(tableBtnsComposite,"<<<");
finalButtonundo=createUndoAndRedoBtn(tableBtnsComposite,"undo");
finalButtonredo=createUndoAndRedoBtn(tableBtnsComposite,"redo");
TableresultTable=createTable(composite);
resultTableViewer=newTableViewer(resultTable);
resultTableViewer.setContentProvider(newColumnContentProvider());
resultTableViewer.setLabelProvider(newColumnLabelProvider());
/***********************************************************************
*MainBtns'SelectionListener
**********************************************************************/
addListener2MainBtnAdd(parent,mainBtnAdd);
/***********************************************************************
*TableandColumnBtns'SelectionListener
**********************************************************************/
addListener2AddBtn(add,undo,redo);
addListener2RemoveBtn(remove,undo,redo);
addListener2UndoBtn(undo);
addListener2RedoBtn(redo);
addListener2AddAllBtn(addAll,undo,redo);
addListener2RemoveAllBtn(removeAll,undo,redo);
setControl(composite);
setPageComplete(true);
}
privateCompositecreateMainBtnsComposite(Compositecomposite){
GridLayoutglayout;
CompositemainBtnsComposite=newComposite(composite,SWT.NULL);
GridDatagd=newGridData(GridData.FILL_HORIZONTAL);
gd.horizontalSpan=9;
mainBtnsComposite.setLayoutData(gd);
glayout=newGridLayout(9,true);
mainBtnsComposite.setLayout(glayout);
returnmainBtnsComposite;
}
privateButtoncreateMainBtn(CompositemainBtnsComposite,Stringtext){
GridDatagd;
ButtonmainBtnAdd=newButton(mainBtnsComposite,SWT.PUSH);
mainBtnAdd.setText(text);
gd=newGridData(GridData.FILL_HORIZONTAL);
mainBtnAdd.setLayoutData(gd);
returnmainBtnAdd;
}
privatevoidcreateSeparator(Compositecomposite){
GridDatagd;
Labellabel=newLabel(composite,SWT.SEPARATOR|SWT.HORIZONTAL);
gd=newGridData(GridData.HORIZONTAL_ALIGN_FILL);
gd.horizontalSpan=9;
label.setLayoutData(gd);
}
privatevoidcreateCanvas(Compositecomposite){
GridDatagd;
canvas=newCanvas(composite,SWT.NO_REDRAW_RESIZE|SWT.V_SCROLL
|SWT.H_SCROLL);
gd=newGridData(GridData.FILL_BOTH);
gd.horizontalSpan=9;
canvas.setLayoutData(gd);
}
privatevoidaddListener2Canvas(finalCompositeparent){
finalPointorigin=newPoint(0,0);
finalScrollBarhBar=canvas.getHorizontalBar();
hBar.addListener(SWT.Selection,newListener(){
publicvoidhandleEvent(Evente){
inthSelection=hBar.getSelection();
intdestX=-hSelection-origin.x;
Rectanglerect=image.getBounds();
canvas.scroll(destX,0,0,0,rect.width,rect.height,false);
origin.x=-hSelection;
}
});
finalScrollBarvBar=canvas.getVerticalBar();
vBar.addListener(SWT.Selection,newListener(){
publicvoidhandleEvent(Evente){
intvSelection=vBar.getSelection();
intdestY=-vSelection-origin.y;
Rectanglerect=image.getBounds();
canvas.scroll(0,destY,0,0,rect.width,rect.height,false);
origin.y=-vSelection;
}
});
canvas.addListener(SWT.Resize,newListener(){
publicvoidhandleEvent(Evente){
Rectanglerect=image.getBounds();
Rectangleclient=canvas.getClientArea();
hBar.setMaximum(rect.width);
vBar.setMaximum(rect.height);
hBar.setThumb(Math.min(rect.width,client.width));
vBar.setThumb(Math.min(rect.height,client.height));
inthPage=rect.width-client.width;
intvPage=rect.height-client.height;
inthSelection=hBar.getSelection();
intvSelection=vBar.getSelection();
if(hSelection>=hPage){
if(hPage<=0)
hSelection=0;
本文地址:http://www.45fan.com/dnjc/67220.html