© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 113.04.2006 16:32
Two System Architectures:Two System Architectures:MVC and DCAMVC and DCA
Trygve ReenskaugDepartment of InformaticsUniversity of Oslo
[email protected]://www.ifi.uio.no/~trygver
Spinoff from Spinoff from the BabyUML projectthe BabyUML projectfor for Readable ProgramsReadable Programs
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 313.04.2006 16:32
an Activity Network Planningan Activity Network Planningexampleexample
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 413.04.2006 16:32
Projectactivities
actDactB
actCactA
Projectresources
Usertools
My ProblemMy Problem
Undisciplined communication → Spaghetti
ClassClass
Class
actB
Class + superclasses + methods → Noodles
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 513.04.2006 16:32
BabyUML Goal:BabyUML Goal:Readable ProgramsReadable Programs
The price of reliability is the pursuit of the utmost simplicity
(C.A.R. Hoare, 1980 Turing Award lecture)
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 613.04.2006 16:32
The BabyComponent =The BabyComponent =an object encapsulating other objectsan object encapsulating other objects
network «Component»
resource «Component»
tool «Component»
network «Component»
resource «Component»
tool «Component»
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 713.04.2006 16:32
Baby ComponentsBaby ComponentsUniverse of DiscourseUniverse of Discourse
demoData
resource
network
«component» babydemo
tool
«Interface»Observer
«Interface»DemoDataIntf
«Interface»NetworkIntf
«Class»ActivityDescr
«Interface»ResourceIntf
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 813.04.2006 16:32
The BabyDemo programThe BabyDemo programdemodata + network + resource + tooldemodata + network + resource + tool
+IDE
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1113.04.2006 16:32
Baby ComponentsBaby ComponentsDivide and Conquer: the toolDivide and Conquer: the tool
demoData
resource
network
«co
mp
on
en
t» ba
by
de
mo
tool
«Interface»Observer
«Interface»DemoDataIntf
«Interface»NetworkIntf
«Class»ActivityDescr
«Interface»ResourceIntf
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1213.04.2006 16:32
Model – View - ControllerModel – View - Controllerbridge gapbridge gap Mental Model – ComputerMental Model – Computer
ModelUser
mental model
computer model
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1313.04.2006 16:32
Model – View - ControllerModel – View - ControllerMultiple Views on same ModelMultiple Views on same Model
ModelUser
mental model
computer model
Run Demo
View
1
**
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1413.04.2006 16:32
Model – View - ControllerModel – View - ControllerTool = Controller + ViewsTool = Controller + Views
ModelUser
View
Controller
*1
*
*
Tool
mental model
computer model
Run Demo
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1513.04.2006 16:32
MModelodel V Viewiew C ControllerontrollerRoles and ResponsibilitiesRoles and Responsibilities
Model Store domain information
View Present and edit domain information
Controller Build, manage and co-ordinate Views
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1613.04.2006 16:32
Example: babydemo.toolExample: babydemo.toolClass structureClass structure
Java.awt.Component java.applet.Appletjava.awt.Panel
Controller
ResourceViewButtonsView
java.awt.Button ActivityView
GanttViewDependencyView
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1713.04.2006 16:32
Example: babydemo.toolExample: babydemo.tool visual hierarchy visual hierarchy (associations)(associations)
Controller
ResourceViewButtonsView
java.awt.Button ActivityView
GanttViewDependencyView
1 111
1
*
1111
*
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1813.04.2006 16:32
Example: babydemo.toolExample: babydemo.toolMVC collaboration structureMVC collaboration structure
model.observed
view, observer: ResourceView
button: java.awt.Button
Tool
controller,observed, observer
view, observer: ButtonsView
view, observer: DependencyView
view, observer: GanttView
view, observer: ActivityView
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 1913.04.2006 16:32
The Anatomy ofThe Anatomy ofbabyDemo.toolbabyDemo.toolcontroller : Controller
view :DependencyViewview
:ActivityView
heim.ifi.uio.no/~trygver/2006/roots-2006/babyExample4.html
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 2013.04.2006 16:32
MVCMVCSynchronized SelectionSynchronized Selection
Usera view
actionPerformed()
observer*
pointAndClickMouse
controller
present
update()
selectedObject
selectionEvent()
notifyObserver()
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 2113.04.2006 16:32
MVCMVCtool componenttool component
+IDE
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 2613.04.2006 16:32
Baby Components: the networkBaby Components: the networkDivide and ConquerDivide and Conquer
demoData
resource
network
«co
mp
on
en
t» ba
by
de
mo
tool
«Interface»Observer
«Interface»DemoDataIntf
«Interface»NetworkIntf
«Class»ActivityDescr
«Interface»ResourceIntf
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 2713.04.2006 16:32
The Simple ComponentThe Simple Componentindisciplined internalsindisciplined internals
façademethod
façademethod
receive message
actB
actA actC actD
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 2813.04.2006 16:32
The Declarative ComponentThe Declarative ComponentMember Objects in micro databaseMember Objects in micro database
addDependency()
actDactC
actDactB
actCactA
actA actB actC actD
babyBase with Member Objectsand Member structure
newActivity()
newActivity()
addDependency()
babyBase interfacevoid newActivity
(String actName , …) void addDependency
(String predNam , String succNam) Set<ActivityIntf> allActivities
() Activity activityNamed
(String actNam)Set<ActivityIntf> predecessorsOf
(String actName) Set<ActivityIntf> successorsOf
(String actName)
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 2913.04.2006 16:32
DCADCAseparate the whole from the Detailsseparate the whole from the Details
Inward-lookingmethods and fields
Outward-lookingmethods and fields
Componentlooks outward
Member Objectslook inward
Member Object
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3013.04.2006 16:32
Three simple operationsThree simple operations
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3113.04.2006 16:32
a DCA Component
The DCA ComponentThe DCA ComponentDData + ata + CCommunication + ommunication + AAlgorithmlgorithm
MaestroMethod
MaestroMethod
receive message
micro “database” with Member Objects and structure
actA actB actC actD
Data
Algorithm
Communication
?
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3213.04.2006 16:32
The DCA ComponentThe DCA Componentinvisible babyBaseinvisible babyBase
addDependency()
actDactC
actDactB
actCactA
actA actB actC actD
babyBase with Member Objectsand Member structure
newActivity()
babyBase interfacevoid newActivity
(String actName , …) void addDependency
(String predNam , String succNam) Set<ActivityIntf> allActivities
() privateprivate Activity activityNamed
(String actNam)privateprivate Set<ActivityIntf> predecessorsOf
(String actName) privateprivate Set<ActivityIntf> successorsOf
(String actName)
maestroonly
babyBaseonly
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3313.04.2006 16:32
public ActivityDescr public ActivityDescr activityDescriptorFor activityDescriptorFor (String actName)(String actName)
another Maestro Method
receive message
babyBase actDactC
actDactB
actCactA
actA actB actC actD
activityDescriptorFor(“actC”)
public class ActivityDescr { public String name; public Integer earlyStart, earlyFinish, duration, rank; public Color color; public Set<String> predecessors, successors;
public class ActivityImpl implements ActivityIntf { MemberBase babyBase; private String name; private Integer earlyStart, duration, rank = 0; private Color color = Color.gray;
Which activity?What are preds, succs??
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3413.04.2006 16:32
DescrCollab
activity successors predecessors * *
algorithmalgorithm activityDescriptorFor()activityDescriptorFor()needs both predecessors and successorsneeds both predecessors and successors
roles
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3513.04.2006 16:32
public public ActivityDescr ActivityDescr activityDescriptorFor activityDescriptorFor (String actName)(String actName)
MaestroMethod
receive message
micro“database” actDactC
actDactB
actCactA
actA actB actC actD
activityDescriptorFor(“actC”)
actB
actA actC actD
Data
Algorithm
Collaboration =Communication
context
actA actB actC actD
activity successors predecessors * *
DescrCollab
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3613.04.2006 16:32
“9.3.4 CollaborationUseA collaboration use represents the application of the pattern described by a collaboration to a specific situation involving specific classes or instances playing the roles of the collaboration.”
“9.3.3 CollaboratioA collaboration describes a structure of collaborating elements (roles), each performing a specialized function,
which collectively accomplish some desired functionality…
Thus, details, such as the identity or precise class of the actual participating instances are suppressed.”
UML 2.1: CollaborationUML 2.1: Collaboration
BabyUMLCollaboration
BabyUMLQuery
+
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3713.04.2006 16:32
Bind roles to Member Objects with queriesBind roles to Member Objects with queries
ActivityDescriptorCollab
activity successors predecessors * *
DescrCollab
activity successors predecessors * *
define successors (String actNam) asselect succfrom dependencieswhere pred.name = actNam
define query activity (String actNam) asselect actfrom activitieswhere act.name = actNam
define predecessors (String actNam) asselect predfrom dependencieswhere succ.name = actNam
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 3813.04.2006 16:32
Algorithm Algorithm (maestro)(maestro)ActivityImpl:: activityDescriptor()ActivityImpl:: activityDescriptor()
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 4213.04.2006 16:32
D C AD C ADDataata CCommunicationommunication AAlgorithmlgorithm
Data(MemberBase)
A MemberBase maintains the member objects and their structure
Communication(Component façade method)
Component operation realized by
1. establishing a collaboration of roleplaying objects
2. triggering role interaction
Algorithm(Member Object method)
Member object methods implement detailed algorithms
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 4313.04.2006 16:32
babydemobabydemo Universe of Discourse Universe of Discourseinterface interface NetworkIntfNetworkIntf
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 4413.04.2006 16:32
actB
actA actC actD
maestro methodmaestro methodvoidvoid frontload frontload (Integer firstWeek);(Integer firstWeek);
another Maestro Method
babyBase
receive message
actDactC
actDactB
actCactA
actA actB actC actD
frontload ( )
frontloaderpredecessors *frontCollab
define query frontActivities asselect actfrom activitieswhere
act.earlyStart.isNiland (
select succfrom dependencieswhere
pred = actand
pred.earlyStart.notNil) isEmpty
actB
actA actC acD
frontload (1)
actB
actA actC actD
actB
actA actC actD
actB
actA actC actD
actA actB actCactB actDactCactB
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 4513.04.2006 16:32
babyBase.FrontCollabbabyBase.FrontCollabprotected class FrontCollab { ActivityIntf frontloader; Set<ActivityIntf> predecessors; FrontCollab() {
// activity that is ready to do : not done and all predecessors done frontloader = null; predecessors = new HashSet<ActivityIntf> (0); for (ActivityIntf act : activities) {
if (act.earlyStart() == null) { Set<ActivityIntf> preds = predecessorsOf(act.name()); boolean predsDone = true; for ( ActivityIntf pred : preds) {
if (pred.earlyStart() == null) { predsDone = false; break;
} }
if (predsDone) { frontloader = act; predecessors = preds;
break; } } } } }
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 4613.04.2006 16:32
The DCA ComponentThe DCA ComponentDData + ata + CCommunication + ommunication + AAlgorithmlgorithm
FaçadeMethod
receive messagea DCA Component
Algorithms implemented as Methods in Member Objects
Data structure and objectscontained in micro ”database”
actA actB actC actD
Façade Method
1. creates Collaboration by binding Roles to Member Objectsthrough dynamic Queries.
2. triggers Communication as message interaction between Roles
FaçadeMethod
roleX roleY
actDactCactB
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 4813.04.2006 16:32
• Reduce coupling by enforcing component encapsulation.• MVC to separate domain information and presentation
– Model components hold domain information– Tool gives user direct access to information
• View objects let user see and edit• Controller object coordinates views
• DCA to organize component internal Member Objects– Data: Member objects administred by BabyBase that ensured data
integrity• Conceptual Schema; BabyBase defines universe of discourse.• External schemas: BabyBase creates collaborations for the external
operations• (Internal schema is the Member Object classes).
– Communication: Maestro methods in Component object triggers Member Object behavior
– Algorithms: What’s left in the Member Objects.
Summary and ConclusionSummary and Conclusion““make it so simple that there are obviously no deficiencies”make it so simple that there are obviously no deficiencies”
• Reduce coupling by enforcing component encapsulation.• MVC to separate domain information and presentation
– Model components hold domain information– Tool gives user direct access to information
• View objects let user see and edit• Controller object coordinates views
• DCA to organize component internal Member Objects– Data: Member objects administred by BabyBase that ensured data
integrity• Conceptual Schema; BabyBase defines universe of discourse.• External schemas: BabyBase creates collaborations for the external
operations• (Internal schema is the Member Object classes).
– Communication: Maestro methods in Component object triggers Member Object behavior
– Algorithms: What’s left in the Member Objects.
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 4913.04.2006 16:32
The EndThe End
More info at http://heim.ifi.uio.no/~trygver
Claim: MVC and DCA make for self-documenting code
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 5013.04.2006 16:32
More reading ….More reading ….http://www.ifi.uio.no/~trygver mailto: trygver ‘at’ ifi.uio.no
Trygve Reenskaug: The BabyUML discipline of programming (where A Program = Data + Communication + Algorithms). SoSym 5,1 (April 2006). DOI: 10.1007/s10270-006-0008-x.
Trygve Reenskaug: The BabyUML Discipline of Programming. ECMDA 2005 keynote. http://heim.ifi.uio.no/~trygver/2005/babyuml/ECMDA2005-keynote.ppt
Edsger Dijkstra: A Discipline of Programming, 1976
Charles Antony Richard Hoare: The Emperor's Old Clothes. 1980 Turing Award lecture. Comm.ACM 24, 2 (Feb. 1981)
[UML] Unified Modeling Language: Superstructure. Version 2.1. Object Management Group (OMG) document ptc/06-04-02. http://www.omg.org
Trygve Reenskaug: Empowering People with BabyUML: A sixth GenerationProgramming Language. Opening talk, ECOOP 2004, Oslo.http://heim.ifi.uio.no/~trygver/2004/ECOOP-04/EcoopHandout.pdf
Trygve Reenskaug: Original MVC notes from Xerox PARC:http://heim.ifi.uio.no/~trygver/1979/mvc-1/1979-05-MVC.pdfhttp://heim.ifi.uio.no/~trygver/1979/mvc-2/1979-12-MVC.pdf
Cattell, Barry: The Object Data Standard: ODMG 3.0. Academic Press,London, 2000. ISBN 1-55860-647-4
Erik Arisholm and Dag Sjøberg, A Controlled Experiment with Professionals to Evaluate the Effect of a Delegated versus Centralized Control Style on the Maintainability of Object-Oriented Software, Simula Research Laboratory Technical Report 2003-6http://www.simula.no/publication_one.php?publication_id=601
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 5113.04.2006 16:32
ZZZTheZZZThe babydemo Componentbabydemo ComponentUniverse of DiscourseUniverse of Discourse
demoData
resource
network
«component» babydemo
tool
«Interface» ResourceIntf
«Interface» DemoDataIntf
«Interface» NetworkIntf
«Class»ActivityDescr
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 5513.04.2006 16:32
Divide and ConquerDivide and Conquerthethe networknetwork ComponentComponent
«Class» DemoData
DemoDataIntf
«Class» resource
ResourceIntf
«DCA-component»network
NetworkIntf
«component» babydemo
«MVC-component»tool
Applet
«Class»ActivityDescr
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 5613.04.2006 16:32
babydemo.babydemo.NetworkIntfNetworkIntf+IDE
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 5913.04.2006 16:32
public void public void newActivitynewActivity(String actName , Integer duration, Color color);(String actName , Integer duration, Color color);
MaestroMethod
newActivity()
receive message
babyBase with Member Objects and structure
actA actB actC actD
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 6013.04.2006 16:32
public void public void addDependencyaddDependency(String fromName , String toName);(String fromName , String toName);
addDependency
MaestroMethod
receive message
babyBase actDactC
actDactB
actCactA
actA actB actC actD
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 6113.04.2006 16:32
babydemo.babydemo.NetworkIntfNetworkIntf
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 6213.04.2006 16:32
maestro methodmaestro methodpublic public List<String> activityNames()List<String> activityNames()
© Trygve Reenskaug 2006 MVC & DCA - ROOTS 2006 Slide 6313.04.2006 16:32
babydemo.babydemo.NetworkIntfNetworkIntf