XpdWiki

FrontPage
RecentChanges
XtC
FindPage
PageIndex
XpApprentices

Set your name in
UserPreferences

Edit this page

Referenced by
WhyTestGetAndSetM...




JSPWiki v2.0.52


UseVisitorPatternInsteadOfGetAndSetMethods


I'm quite off enumerations - they are slightly better than get/set - but again I think they are often over-used. Especially if you control the code base - we have found that cleaner code comes out of a visitor that does some operation for you and notifies you of the results. Of course its not abstract, only does exactly what you need - but thats the point, its also much easier to test.

e.g. compare: e = aCompany.getEmployeesEnumeration(); while(e.hasMoreElements()) { totalSalary+= (Employee)e.getSalary(); }

with payrollCalculator = new PayrollCalculator?(); aCompany.visitSalaries(payrollCalculator);

Notice how the enumeration example starts leaking getters, it also exposes employee objects to the outside world (which you can guard with some clone operation, but this is yet another operation you now need to test). The second example, is very clean - however its not very generic. Still its not often that you need a generic solution, so save enumerations for those times. The latter example also gives room for some very easy tests, as you can set expectations on a MockPayrollCalculator? - the former example does not test as nicely imho (I would expect that you would end up writing an Enumeration result expectation type of object). Notice also, that the passing in style of coding has automatically refied the calculation and given you another useful object -- TimM

³host³³date³May 22, 2000³agent³Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)³UseVisitorPatternInsteadOfGetAndSetMethods


Edit this page   More info...   Attach file...
This page last changed on 22-May-2000 18:19:18 BST by unknown.