跳到主要內容

發表文章

目前顯示的是 12月, 2004的文章

神奇的Reflection加上Registry Pattern

原由 : 十年前前寫結構矩陣或是Finite Element Method時,都是依照element的不同性質,代入一個相同的位移分析主程式。因此只要能將element代入主程式,就能夠在不動到主程式的情況下,動態增加element種類,也就是Reflection。問題就在於C++並沒有真正的late binding,到Smalltalk及後來的Java、.Net出現後才真正有此能力。若勉強用C++要寫,得用到function pointer之類的技巧,就又成為weak type。 以下是C的switch case範例,取自Alladin ARRAY *elmt_cst ( ARRAY *p, int isw ) { /* [a] : Jump to Task Case */ UNITS_SWITCH = CheckUnits(); switch(isw) { case PROPTY: /* CST material properties */ ..... add details of CST material properties .... break; case LOAD_MATRIX: /* compute equivalent nodal loads */ case STRESS: /* compute internal nodal loads */ ..... fill in details here .... break; case STIFF: /* compute element-level stiffness matrix */ ..... put details of element stiffness matrix here .... break; case MASS_MATRIX: /* compute element-level mass matrix */ ..... put details of element mass matrix here .... break; default: break; } 就算是使用Abstract Factory,仍然是省不了這一大串的select case,更別說source不給對方了。 最近寫另一個C#程式,剛好想起來這個問題,就可以用Registry加上Reflec