Convert Static to Dynamic Construction -- 静态到动态构造转换
2013-03-17 23:31
288 查看
RefactoringcontributedbyGerardM.Davison
Youhaveclassesthathavestaticcompiletimedependenciesonclassesthatcanonlybebuiltonaspecificplatform.
你有一些平台独立的静态编译时期的类
Makeuseofthejava.lang.reflecttobreakthestaticdependency.
利用java反射打破静态依赖。
ForexampleaclasstoaccessWinHelpneedstogetthewindowhandleviathesun.awt.windows.WWindowPeerclasses.
有时候你会根据情况提供不同功能的驱动程序。
Theremightalsobethedesiretoonlyprovidecertianfunctionalitytoagivensub-setofusers.Thisistheequivalentofbeingabletocompileoutcodewithouthavingtoalterthesourceorusepre-compilers.
你可能期望给予用户不同的功能子集,这就相当于不用修改源码或者预编译就能达到这种目的。
Thismethodcanbeusedwithgoodeffectwithapplicationframeworkswhereyoudonotknowwhichclassneedtobeusedatcompiletime.
在应用程序框架中使用反射是一种的好的方式,譬如在编译期你不知道到底需要选择哪个类时。
Makesurethatthereisacommoninterfaceandmethodofconstruction.UtiliseExtractInterfaceoruseExtractSuperclasstocreateasuitableabstractiontoworkwith.
Replacetheclassselectionprocesswithonethatinsteadselectsforthestringnameoftheclassinquestion.
Removeanyimportstatementsthatreferethetheclassesbeingconstructed.
Addcodeintoloadandinstantiatetheclass.Formoreparameterisedcontructors,makeuseofthejava.lang.reflectpackage.
Dealwithanyexcpetiongenerated.
Youarenowreadytocompileandtestthecode.Makesurethatanydependantcodeisalsotestedproperly.
Therefactoringiscomplete,butyoushouldmakesurethatthisrun-timedependencyisproperlydocumentedandthatalldevelopersknowtoinstantiatetheclassesusingtheneworalteredmethod.
Firstwehavetochangetheselectioncodetoreturnaclassnameratherthananactualinstance.Wecansafelyremovetheimportstatementthatreferestothisclass.
Wecannowinstantiatetheclass,inthissimplecasetheconstructorhasnoparameters,sowedonothavetomakeuseoftheextendedjava.lang.reflectpackage.
Wenowhavetoaddthecodetodealwiththepossibleexceptioncases.HereIhavechosentoconvertthemtotheequivalentJavaErrorsinordertomaintaintheinterface.Ifthisisnotrequiredthensimplercodeis
possible.
Compileandtestatthispointaswehavecodethatiscomplete.
Whenthisisfinishedandalldependentclassesarere-tested,therefactoringiscomplete.
Youhaveclassesthathavestaticcompiletimedependenciesonclassesthatcanonlybebuiltonaspecificplatform.
你有一些平台独立的静态编译时期的类
Makeuseofthejava.lang.reflecttobreakthestaticdependency.
利用java反射打破静态依赖。
importorg.davison.data.jdbc.JDBCProvider;
.
.
.
DataProviderdp=newJDBCProvider();
try
{
DataProviderdp=(DataProvider)
Class.forName("org.davison.data.jdbc.JDBCProvider").newInstance();
}
catch(IllegalAccessExceptioniae)
{
//Convertexceptiontoerrortopresevetheinterface.
//
thrownewIllegalAccessError(iae.getMessage());
}
catch(InstantiationExceptionie)
{
//Convertexceptiontoerrortopresevetheinterface.
//
thrownewInstantiationError(ie.getMessage());
}
catch(ClassNotFoundExceptioncnfe)
{
//Convertexceptiontoerrortopresevetheinterface.
//
thrownewNoClassDefFoundError(cnfe.getMessage());
}
Motivation
Insomecasesyouhavetoprovidedriverstoprovidedifferentfunctionalitydependingonthesituation.Youmighthaveclassesthatonagivenplatformneedtoaccesssomesun.*classestoperformaspecificfunction.ForexampleaclasstoaccessWinHelpneedstogetthewindowhandleviathesun.awt.windows.WWindowPeerclasses.
有时候你会根据情况提供不同功能的驱动程序。
Theremightalsobethedesiretoonlyprovidecertianfunctionalitytoagivensub-setofusers.Thisistheequivalentofbeingabletocompileoutcodewithouthavingtoalterthesourceorusepre-compilers.
你可能期望给予用户不同的功能子集,这就相当于不用修改源码或者预编译就能达到这种目的。
Thismethodcanbeusedwithgoodeffectwithapplicationframeworkswhereyoudonotknowwhichclassneedtobeusedatcompiletime.
在应用程序框架中使用反射是一种的好的方式,譬如在编译期你不知道到底需要选择哪个类时。
Mechanics
Identifytheplacewherethedifferentclassesareinstantiated.Ifthereismorethanoneplace,youmightliketomakeuseofcodeconsolidationrefactoringstocreateoneentrypoint.Makesurethatthereisacommoninterfaceandmethodofconstruction.UtiliseExtractInterfaceoruseExtractSuperclasstocreateasuitableabstractiontoworkwith.
Replacetheclassselectionprocesswithonethatinsteadselectsforthestringnameoftheclassinquestion.
Removeanyimportstatementsthatreferethetheclassesbeingconstructed.
Addcodeintoloadandinstantiatetheclass.Formoreparameterisedcontructors,makeuseofthejava.lang.reflectpackage.
Dealwithanyexcpetiongenerated.
Youarenowreadytocompileandtestthecode.Makesurethatanydependantcodeisalsotestedproperly.
Therefactoringiscomplete,butyoushouldmakesurethatthisrun-timedependencyisproperlydocumentedandthatalldevelopersknowtoinstantiatetheclassesusingtheneworalteredmethod.
Example
Startwiththiscode:importorg.davison.data.jdbc.JDBCProvider;
.
.
.
DataProviderdp=newJDBCProvider();
Firstwehavetochangetheselectioncodetoreturnaclassnameratherthananactualinstance.Wecansafelyremovetheimportstatementthatreferestothisclass.
Class.forName("org.davison.data.jdbc.JDBCProvider")
Wecannowinstantiatetheclass,inthissimplecasetheconstructorhasnoparameters,sowedonothavetomakeuseoftheextendedjava.lang.reflectpackage.
DataProviderdp=(DataProvider)
Class.forName("org.davison.data.jdbc.JDBCProvider").newInstance();
Wenowhavetoaddthecodetodealwiththepossibleexceptioncases.HereIhavechosentoconvertthemtotheequivalentJavaErrorsinordertomaintaintheinterface.Ifthisisnotrequiredthensimplercodeis
possible.
try
{
DataProviderdp=(DataProvider)
Class.forName("org.davison.data.jdbc.JDBCProvider").newInstance();
}
catch(IllegalAccessExceptioniae)
{
//Convertexceptiontoerrortopresevetheinterface.
//
thrownewIllegalAccessError(iae.getMessage());
}
catch(InstantiationExceptionie)
{
//Convertexceptiontoerrortopresevetheinterface.
//
thrownewInstantiationError(ie.getMessage());
}
catch(ClassNotFoundExceptioncnfe)
{
//Convertexceptiontoerrortopresevetheinterface.
//
thrownewNoClassDefFoundError(cnfe.getMessage());
}
Compileandtestatthispointaswehavecodethatiscomplete.
Whenthisisfinishedandalldependentclassesarere-tested,therefactoringiscomplete.
相关文章推荐
- Convert Dynamic to Static Construction -- 动态到静态构造转换
- Linux - 动态(Dynamic)与静态(Static)函数库
- Delphi对象的方法能定义- 静态(static)、虚拟(virtual)、动态(dynamic)或消息处理(message)
- 静态(Static)、虚拟(Virtual)、动态(dynamic)方法区别
- Delphi静态(static)、虚拟(virtual)、动态(dynamic)或消息处理(message)
- 静态(static)、虚拟(virtual)、动态(dynamic)或消息处理(message)
- Linux - 动态(Dynamic)与静态(Static)函数库
- 小东吖 之 java 构造方法 static关键字 静态变量 静态方法
- c++四种转换 static_cast,const_cast,dynamic_cast,reinterpret_cast
- 标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast(转载)
- 谈谈C++的类型转换符 static_cast、dynamic_cast、const_cast、reinterpret_cast
- 标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast(转载
- 标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
- define a static method to convert datatime to a formatted string with uesr-defined
- 标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast
- 使用标准C++的类型转换符--static_cast、dynamic_cast、reinterpret_cast、const_cast
- 数据类型转换(static_cast, const_cast ,reinterpret_cast ,dynamic_cast )
- 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】
- 使用标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast、和const_cast。(转)
- 【C编程练习】2013华为校园招聘机试题9月10日题1:把整数转换成字符串 void ConvertIntToStr(int nVal, char* pStr);