AllComputationIsPerformedWithinanActor
CarlHewittcalledtheactorthefundamentalunitofcomputation.
1
Whatdoesitmeantobethe
fundamentalunitofcomputation?ItmeansthatwhenyoubuildasystemusingtheActor
Model,everythingisanactor.WhetheryouarecomputingaFibonaccisequenceor
maintainingthestateofauserinyoursystem,youdosowithinanactor,ormultipleactors.
Thisideathateverythingisanactorisnotwithoutdifficulties,though.Ifeverycomputation
needstohappenwithinanactor,thisimpliesthateveryfunctionandeverystatevariablecould
beitsownactor.Andeventhoughthisistechnicallypossible,it’snotalwayspragmatic.Often,
wehavegroupsofrelatedfunctionsanditisusuallymoreconvenienttowrapallthose
functionswithinasingleactor.Doingsodoesn’tviolatetheActorModel.Buthowdowe
decidewheretodrawtheline?WewilldiscussthisinmoredetailinChapter3whenwelearn
aboutdomain-drivendesign(DDD).ThebuildingblocksweintroduceforDDDareexcellent
candidatestoturnintoactorsandwecanusethemasguidingprinciplesforwhentocreatea
newactor.
ActorsintheActorModelembodynotonlystatebutalsobehavior.Thismightsound
suspiciouslylikethedefinitionofOOP—infactthetwoareverycloselyrelated.AlanKay,
whocoinedtheterm“object-orientedprogramming”andwasoneoftheoriginalcreatorsof
theSmalltalklanguage,waslargelyinfluencedbytheActorModel.AndwhileSmalltalkand
OOPeventuallyevolvedawayfromtheirActorModelroots,manyofthebasicprinciplesof
theActorModelremaininourmoderninterpretationofOOP.Intruth,theoriginalfocusof
OOPwasnottheobjectsthemselvesbutratherthemessagesflowingbetweenthem.
NOT E
OOPmodelsthefundamentalunitofcomputationasanobject(aninstanceofaclass),andisfamiliarto
developerscomingfromJavaandsimilarlanguages.Functionalprogramming,ontheotherhand,models
computationaroundfunctionsandtheirapplications,andisseeninlanguagessuchasLispandHaskell.
Anotheraspectofthemodelthatisusefulforhighlyconcurrentapplicationsistheideathatan
actor ’sstateisisolated.Itisneverdirectlyexposedtotheoutsideworld.Wedon’tallow
actorstolookatormodifythestateofanotheractor,exceptindirectlythroughmessages.
Thisisolationappliesequallytotheactor ’sbehavior.Theinternalmethodsormechanismsof
theactorareneverexposedtootheractors.Infact,stateandbehaviorcanlargelybetreatedas
thesamethingwithinanactor(moreonthislater).
Actorsinthemodelcantakemanydifferentforms.Theycanexistashighlytechnical
constructslikeadatabaseaccesslayer,ortheycanbedomain-specificconstructslikeaperson
orschedule.Theycanevenbeusedtoperformsimplemathematicaloperations.Anythingin
thesystemthatneedstoperformanyamountofcomputationisanactor.