Preventive program maintenance in Demeter/Java

PreventiveProgramMaintenanceinDemeter/JavaKarlJ.Lieb erherr,DougOrleansNortheasternUniversity,CollegeofComputerScience,161CNBoston,MA02115-9959USA(617)3732077Fax:5121flieb erherr,dougog@ccs.neu.eduDemeterhomepage:http://www.ccs.neu.edu/research/de meterPublishedinthePro ceedingsofICSE'97,Copyright1997ACMPress.ABSTRACTDemeter/JavaisanimplementationofAdaptivePro-gramming(AP)forJava.Demeter/Jaaprogramscon-sistofJavaco detogetherwithprogramsinsmalllan-guagetoexpresstraversals,visitormetho ds,andclassdiagrams.Demeter/Javaprogramsaretypicallywrit-teninapreventivemaintenancestyleusingtraversalstrategiesforexpressingtraversals.Theresultingpro-gramsareveryrobustunderchangestotheclassdia-gramsandvisitors.Thispreventivemaintenancestylealsoleadstosimplerprograms,makingb othsoftwaredevelopmentandmaintenanceeasier.Keywordssoftwareevolution,separationofconcerns,asp ect-orientedprogrammingINTRODUCTIONAdaptiveProgramming(AP)hasb eendescrib edinsev-eralpap ers,thesesandab o ok[5].TheideaofAPistowriteprogramsintermsoftraversalstrategies,andtocustomizeindetailedtraversalsusingsp eci cclassdiagrams.Thetraversalstrategiesaresimplerthanthedetailedtraversals,leadingtomoreexibleandsimplerprograms.Demeter/JavaisanimplementationofAPforJaintheformofanecientlyexecutabledesignlanguage.InDemeter/JavaweaddaninnoationtoAP:im-provedformoftheVisitorpatternfrom[2]isvidedbythedesignlanguage.TheVisitorpatternallowsonetoexpressatraversalthroughob jectsde nedbyclassdiagramandtoreusethetraversalwithseveralvisitorob jects.Thevisitorob jectsde netheb ehaviorwhichneedstob eimplementedontopofthetraversal.TheVisitorpatternhasthedisadvantagethattraversalco deisspreadoutacrosstheclassdiagram,andchangestotheclassdiagramleadmanytediousup datesoftraversalsandvisitors.InDemeter/Java,theVisitorpatternisimproedbynotwritingthetraversalsexplicitlyinanob ject-orientedlanguage,butbyusingatraversalsp eci cationlan-guage.Thisleadstoaseparationb ehaviorsandter-connectionsb etweenthem.Thevisitorob jectsdescrib etheimp ortantb ehavior,andtraversalsp eci cationsap-proximatelysp ecifyhowvisitorsareconnected.Finally,aUML(Uni edMo delingLanguage)[1]classdiagramprovidesallthedetailstocustomizeatraversalsp eci -cationintoadetailedtraversalexpressedanob ject-orientedlanguage.TRAVERSALSTraersalsareubiquitousinob ject-orientedapplica-tions.Atraersalisanavigationthroughgroupofrelatedob jectswiththepurp oseofaccomplishingsometask.Asinglemetho disadegeneratetraversalwhichvisitsonlyoneob ject.Butmostofthetime,ataskneedsthecollab orationofseveralob jectswhichknowab outeachother.Thewaytheob jectsknoab outeachotherisb oundtochangeasapro jectevolves.Forexample,supp osean-othertaskrequiresdi erentob jectrelationshipswhichneedtob eintegratedwiththecurrentob jectrelation-ships.Butthenmanyofthetaskswhichusedoldob jectrelationshipsneedtob eup dated.Thisproblemoffragileob jectrelationshipsiscalledthesmallmetho dsproblemorstructuralanomaly[8,6].Whileeachclasshasminimalcouplingandworksonitsprivatedata,theimplementationofataskisusuallyspreadacrossmanyclasses.Theassumesparticularclassorganization thathard-wiresstructuralknowledgepathsintheprogram.Thisleadseakeningofencap-sulationandasigni cantmaintenanceproblem.ThisdemonstrationpresentsasolutiontothestructuralanomalyforJavasoftwaredevelop ers.Theideaistolettheprogrammersexpressatraversalstrategyinsteadofadetailedtraversal.Writingtheersalstrategyisaformofpreventivemaintenancesinceitsimpli esmaintenancelater.Preventivesoundslikmoreinitialworkfortheprogrammer,butindeedopp ositeistruesincetheprogramsb ecomesimpler.Thesimplestformofatraversalsp eci cationis:fromCompanytoSalary,whereandareclassesinsomeclassdiagram.GivenaCompany-ob ject,1