Você está na página 1de 3

7/12/13 MVC vs.

PAC | GarfieldTech
www.garfieldtech.com/blog/mvc-vs-pac 1/3
MVCvs.PAC
31December20065:42pmLarry
OneofthemostcommonmistakesIseepeoplemakewhentalkingaboutwebarchitectureiswithregardstoMVC.Generallyitcomesdowntoastatementsuchasthis:
It'sawebapp,sowehavetouseMVC.Thatwayweseparatethelogicandpresentation,whichmeanskeepingPHPoutofourdisplaylayer.Alltheimportantprojects
doitthatway.
Ofcourse,suchastatementisfalse.ItdemonstratesalackofunderstandingaboutMVC,aboutwebapplications,about"importantprojects",andaboutsoftwarearchitectureingeneral.
Let'strytoclarify,withalittlehelpfromWikipedia.
Modulardesign
ModulardesignisagenerallyrecognizedGoodThing(tm)insoftwareengineering.Asinscienceingeneral,breakingaproblemdowntosmaller,bitesizedpiecesmakesiteasiertosolve.It
alsoallowsdifferentpeopletosolvedifferentpartsoftheproblemandstillhaveitallworkcorrectlyintheend.Eachcomponentisthenselfcontainedand,aslongastheinterfacebetween
differentcomponentsremainsconstant,canbeextendedorevenguttedandrewrittenasneededwithoutcausingachaoticmess.
Ishouldnotethatmodulardesignisnotthesamethingaspluginbaseddesign,whichiswhatmanymanyopensourceprojectsuse.Drupal"modules"areactuallyplugins.TheLinux
kernel,Apache,Eclipse,andmanyotherhighprofileprojectswith"loadablemodules"arepluginbasedarchitectures.Pluginbasedarchitecturesarenotincompatiblewithmodulardesign
andinfactcomplementitwell,butIdigress...
Mostsoftwarearchitecturalpatternsarebasedaroundtheideathatmodulardesignisagoodthing,ratherbydefinition.Therearelotsofarchitecturalpatternsforsystemsdependingon
whatitisthey'resupposedtodo.Forinteractivesystems,theusualbreakdownofcomponentsis"displaycomponent","datastoragecomponent",and"businesslogic".
MVC
ThemostcommonlyknowninteractivesystemarchitectureisModelViewController,orMVC.MostgooddesktopapplicationsuseMVCoravariantofit,sometimeswiththe
ControllerpartiallymergedintotheView.InMVC,astheprettypictureattheotherendofthatlinksshows,theModelholdsdata,theViewistheparttheusersees,andtheControlleris
anintermediaryforbusinesslogic.Seemsreasonable,right?Nowtakeacloserlook.
InMVC,theViewcomponenthasdirectaccesstotheModel.TheControlleritselfdoesn'tenterthepictureunlessthereisactualchangetothedata.Simplyreadinganddisplayingdatais
doneentirelybytheViewcomponentitself.Asaresult,asystemcanhavemanyViewcomponentsactiveatonce,allreadinganddisplayingdatainavarietyofdifferentways,evenon
differentsystemsorindifferentlanguagesordifferentmodes(GUIvs.textualvs.web).Ontheflipside,however,thatmeanstheViewcomponenthastohavesomerathercomplexlogicin
it.ItneedstoknowhowtopulldataoutoftheModel,whichmeansitneedstoknowwhatthedatastructureis(oratleastarichAPIinfrontoftheModel).Itneedstobeabletohandle
userinteractionitself,withitsowneventloop.
Well,thatrulesoutmostwouldbewebMVCsetups.Oneofthemostcommoncriesofsuchsystemsisto"getdatabasestuffawayfromHTML".Instead,everythingishandledthrougha
smartcontrollerthatusesatemplatelayertorenderanddisplayoutput.That'snotabaddesignnecessarily,butitisnotMVC.Ifthedisplaycomponentdoesnothavedirect,random
7/12/13 MVC vs. PAC | GarfieldTech
www.garfieldtech.com/blog/mvc-vs-pac 2/3
access,pullaccesstothedatastore,thenitisnotMVC.
PAC
AlesspublicizedbutstillwidelyusedarchitectureisPresentationAbstractionControl,orPAC.ThetwomaindifferencesbetweenMVCandPACarethatinPACthePresentation
componentis"dumb"whilealltheintelligenceresidesintheControllerandPACislayered.Again,seetheprettypicture.
You'llnoticethatthePresentationandAbstractioncomponentsneverspeaktoeachother.TheControllertakesinput,notthedisplaycomponent.TheControllerhasallthebusinesslogic
androutinginformation.ThePresentationcomponentisessentiallyjustafilterthattakesrawdatathattheControllerpushesthroughitandrendersittoHTML(orWML,orXML,or
text,oraniconinagraphicalmonitoringsystem,orwhatever).It'sjustatemplatingsystem.
TheclassicexampleofaPACarchitectureisanairtrafficcontrolsystem.OnePACAgenttakesinputfromaradarsystemaboutthelocationofanincoming747,andusesthePresentation
componenttopaintapictureofthatbliponacanvas(screen).AnotherAgentindependentlytakesinputaboutaDC10thatistakingoff,andpaintsthatbliptothecanvasaswell.Still
anothertakesinweatherdataandpaintsclouds,whileanothertrackstheincomingenemybomberandpaintsaredblipinstead.(Er,wait...)
TheWebandDrupal
Awebapplicationdoesn'tmapnicelytoeitherMVCorPAC.There'snoconsistenteventloopfortheViewcomponentofMVC.ForPACtheinputiscomingfromthesamesourceasthe
display,thatis,inviathePresentationlayer(kinda).MostwebapplicationsI'veseen(andwritten)tendtouseasortofhybridapproach,whichthengets(wrongly)calledMVC.Iblame
Sunforthatconfusion,mostly,butthere'senoughblametogoaround,too.
I'mgoingtopickonDrupalherebecauseit'swhatI'mmostfamiliarwith,butthisisn'taDrupalspecificobservation.DrupalisverymuchaPACarchitecture.Infact,it'sarathergood
PACarchitecture.ThemenusystemactsastheController.Itacceptsinputviaasinglesource(HTTPGETandPOST),routesrequeststotheappropriatehelperfunctions,pullsdataout
oftheAbstraction(nodesand,inDrupal5,forms),andthenpushesitthroughafiltertogetaPresentationofit(thethemesystem).Itevenhasmultiple,parallelPACagentsintheformof
blocksthatpushdataouttoacommonconvas(page.tpl.php).
That'sallwellandgood,butweneedtoremember,then,thatDrupalisaPAC,notMVCframework.IfDrupalwereanMVCframework,thentheme_*functionswouldhavenode_load()
callsscatteredthroughoutthem.That'sonereasonwhy,forinstance,Adrian'sFAPI3plansscareme.Hetalksinhispresentationabout"amodifiedMVCbutwiththeModelandView
somewhatmerged".Well,tostartwithDrupalisn'tMVCinthefirstplace.Secondly,theModelandViewarethelastpiecesyouwanttomerge.MergingathinControllerintoathick
Viewmakessomesense,butmergingarichModelandrichViewtogethermakesarichmess.Thatlosestheseparationofdataanddisplaythatwasthewholepointinthefirstplace.
CurrentlytheclosestDrupalcomestoMVCisthePanelsmodule.Panelsallowsthesiteadmintosetupacustomlayoutwithcustomregionsandthenpullblocks,Views,ornodesintoit
inthoseregions.Currentlythat'stheonlyrealpullbasedlogicthatDrupalsupportsfordisplaygiventheactivediscouragementofdatabaseaccess(evenviaaccessorslikenode_load())in
theme_functionsandtemplatefiles.Eventhat,however,isstilllimitedtospecificpanelpages.There'snosupportedwaytorandomlypullablockintoanodepage,forinstance.Iknow
Earl"merlin"MilesisdeeplyengrossedinPanels2.0,TheNextGeneration,butIdon'tbelieveitcurrentlyinvolvesturningPanelsinsideout.Heis,ofcourse,welcometocorrectmeifhe
does.)
There'salsotheViewfieldmoduleforturningaViewintoafieldofanode,butascoolaconceptasthatisit'sstillPACthinking,oratbestaveryweirdhybridmuddle.
7/12/13 MVC vs. PAC | GarfieldTech
www.garfieldtech.com/blog/mvc-vs-pac 3/3
Conclusion
There'smoretosayonthissubject,butit'sgettinglatesoIwillfindaconvenientstoppingpointfornow.Whenthinkingaboutyourwebapplications,though,trynottofallbackonthe
default"separationisgoodsowe'reusingMVCwhichmeansseparation"logic.Onlythefirstpartofthatstatement,"separationisgood",istrue.Therearelotsofwaystobuildamodular
system,allofthemappropriateindifferentplaces.MVCisnottheonly,noreventhemostwidelyusedofthem.Ifyouaren'tcallingyourdatastoredirectlyfromyourdisplaylayer,in
fact,you'renotusingitatall.
I'llbebacknextyearwithmorethoughtsonhowtoaddresstheMVCvs.PACquestion,andtheimpacttheyhaveonteamdevelopmentprocesses.Untilthen,HappyNewYearand
HappyCoding!
Bookmark/Searchthispostwith

Você também pode gostar