Two System Architectures: MVC and DCA Spinoff from the BabyUML project for Readable Programs Trygve Reenskaug Department of Informatics University of Oslo trygver@ifi.uio.no http://www.ifi.uio.no/~trygver MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 1 an Activity Network Planning example MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 3 My Problem Undisciplined communication → Spaghetti User tools Class actA actC + superclasses + methods → Noodles actB Project resources Project activities actD Class Class Class MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 4 BabyUML Goal: Readable Programs The price of reliability is the pursuit of the utmost simplicity (C.A.R. Hoare, 1980 Turing Award lecture) MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 5 The BabyComponent = an object encapsulating other objects resource resource «Component» «Component» network network «Component» «Component» tool «Component» tool «Component» MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 6 Baby Components Universe of Discourse «component» babydemo demoData tool «Interface» DemoDataIntf resource «Interface» Observer «Interface» ResourceIntf «Interface» NetworkIntf network «Class» ActivityDescr MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 7 The BabyDemo program demodata + network + resource + tool +IDE MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 8 Baby Components Divide and Conquer: the tool demoData tool resource «Interface» Observer «Interface» ResourceIntf «Interface» NetworkIntf network «component» babydemo «Interface» DemoDataIntf «Class» ActivityDescr MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 11 Model – View - Controller bridge gap Mental Model – Computer mental model computer model Model User MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 12 Model – View - Controller Multiple Views on same Model mental model computer model Model User 1 * View * Run Demo MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 13 Model – View - Controller Tool = Controller + Views mental model Controller computer model * 1 User * * Model View Tool Run Demo MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 14 Model View Controller Roles and Responsibilities Model View Store domain information Present and edit domain information Controller Build, manage and co-ordinate Views MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 15 Example: babydemo.tool Class structure Java.awt.Component java.awt.Panel java.applet.Applet Controller ButtonsView java.awt.Button MVC & DCA - ROOTS 2006 DependencyView GanttView ResourceView ActivityView © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 16 Example: babydemo.tool visual hierarchy (associations) Controller 1 1 1 ButtonsView DependencyView 1 1 GanttView 1 1 ResourceView 1 * * java.awt.Button MVC & DCA - ROOTS 2006 1 ActivityView © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 17 Example: babydemo.tool MVC collaboration structure Tool controller, observed, observer view, observer : ButtonsView view, observer : DependencyView model. observed view, observer : GanttView view, observer : ResourceView button : java.awt.Button view, observer : ActivityView MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 18 The Anatomy of babyDemo.tool controller : Controller view :ActivityView view :DependencyView heim.ifi.uio.no/~trygver/2006/roots-2006/babyExample4.html MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 19 MVC Synchronized Selection a view controller observer* User pointAndClickMouse actionPerformed() selectionEvent() notifyObserver() update() selectedObject present MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 20 MVC +IDE tool component MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 21 Baby Components: the network Divide and Conquer demoData tool resource «Interface» Observer «Interface» ResourceIntf «Interface» NetworkIntf network «component» babydemo «Interface» DemoDataIntf «Class» ActivityDescr MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 26 The Simple Component indisciplined internals receive message façade method actA façade method actC actD actB MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 27 The Declarative Component Member Objects in micro database newActivity () add Dependency() babyBase interface actA actB actC actD babyBase actA actC with Member Objects and Member structure actB actD MVC & DCA - ROOTS 2006 actC actD © Trygve Reenskaug 2006 void 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) 13.04.2006 16:32 Slide 28 DCA separate the whole from the Details Component looks outward Outward-looking methods and fields Member Object Inward-looking methods and fields MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 Member Objects look inward 13.04.2006 16:32 Slide 29 Three simple operations MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 30 The DCA Component Data + Communication + Algorithm receive message a DCA Component Maestro Method Maestro Method Communication ? actA actB actC actD micro “database” with Member Objects and structure MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 Algorithm Data 13.04.2006 16:32 Slide 31 The DCA Component invisible babyBase newActivity () add Dependency() babyBase interface actA actB actC maestro only actD babyBase actA actC with Member Objects and Member structure actB actD MVC & DCA - ROOTS 2006 void newActivity (String actName , …) void addDependency (String predNam , String succNam) Set<ActivityIntf> allActivities () private Activity activityNamed (String actNam) private Set<ActivityIntf> predecessorsOf (String actName) private Set<ActivityIntf> successorsOf (String actName) actC actD © Trygve Reenskaug 2006 babyBase only 13.04.2006 16:32 Slide 32 public ActivityDescr activityDescriptorFor (String actName) receive message activityDescriptorFor (“actC”) public class ActivityDescr { public String name; public Integer earlyStart, earlyFinish, duration, rank; public Color color; another public Set<String> predecessors, successors; Maestro Method Which activity? What are preds, succs?? actA actB actC actD actA actC public class ActivityImpl implements ActivityIntf { MemberBase babyBase; private String name; private Integer earlyStart, duration, rank = 0; private Color color = Color.gray; actB actD babyBase MVC & DCA - ROOTS 2006 actC actD © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 33 algorithm activityDescriptorFor() needs both predecessors and successors DescrCollab * predecessors activity * successors roles MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 34 public ActivityDescr activityDescriptorFor (String actName) receive message actA actC actD actB Maestro Method activityDescriptorFor (“actC”) DescrCollab * predecessors actA actB * activity actC successors actD actA actC micro “database” actB actD MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 Collaboration = Communication context Algorithm Data actC actD 13.04.2006 16:32 Slide 35 UML 2.1: 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.” BabyUML Collaboration + “9.3.4 CollaborationUse A collaboration use represents the application of the pattern described BabyUML by a collaboration to a specific situation involving specific classes or instances playing Query the roles of the collaboration.” MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 36 Bind roles to Member Objects with queries ActivityDescriptorCollab DescrCollab predecessors predecessors ** activity activity successors successors ** define query activity (String actNam) as select act from activities where act.name = actNam define successors (String actNam) as define predecessors (String actNam) as select succ select pred from dependencies from dependencies where pred.name = actNam where succ.name = actNam MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 37 Algorithm (maestro) ActivityImpl:: activityDescriptor() MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 38 DCA Data Communication Algorithm Data A MemberBase maintains the member (MemberBase) objects and their structure Communication Component operation realized by (Component façade 1. establishing a collaboration of method) roleplaying objects 2. triggering role interaction Algorithm Member object methods implement detailed algorithms (Member Object method) MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 42 babydemo Universe of Discourse interface NetworkIntf MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 43 maestro method void frontload (Integer firstWeek); receive message actA actC acD actD actB another Maestro Method frontload frontload (1) () frontCollab * predecessors actA actB actC frontloader actD actA actC actB actD babyBase MVC & DCA - ROOTS 2006 actC actD © Trygve Reenskaug 2006 define query frontActivities as select act from activities where act.earlyStart.isNil and ( select succ from dependencies where pred = act and pred.earlyStart.notNil ) isEmpty 13.04.2006 16:32 Slide 44 babyBase.FrontCollab protected 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; } } } } } MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 45 The DCA Component Data + Communication + Algorithm receive message a DCA Component Façade Method Façade Method Façade Method 1. creates Collaboration by binding Roles to Member Objects through dynamic Queries. roleX actA actB actB roleY actC actC 2. triggers Communication as message interaction between Roles actD actD Data structure and objects contained in micro ”database” MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 Algorithms implemented as Methods in Member Objects 13.04.2006 16:32 Slide 46 Summary and Conclusion “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. MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 48 The End Claim: MVC and DCA make for self-documenting code More info at http://heim.ifi.uio.no/~trygver MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 49 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.pdf http://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-6 http://www.simula.no/publication_one.php?publication_id=601 MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 50 ZZZThe babydemo Component Universe of Discourse «component» babydemo tool demoData resource «Interface» DemoDataIntf «Interface» ResourceIntf «Interface» NetworkIntf network «Class»ActivityDescr MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 51 Divide and Conquer the network Component «component» babydemo «Class» resource ResourceIntf «MVC-component» tool Applet «DCA-component» network NetworkIntf «Class»ActivityDescr «Class» DemoData DemoDataIntf MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 55 +IDE babydemo.NetworkIntf MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 56 public void newActivity (String actName , Integer duration, Color color); receive message newActivity () actA actB actC Maestro Method actD babyBase with Member Objects and structure MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 59 public void addDependency (String fromName , String toName); receive message add Dependency Maestro Method actA actB actC actD actA actC actB actD babyBase MVC & DCA - ROOTS 2006 actC actD © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 60 babydemo.NetworkIntf MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 61 maestro method public List<String> MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 activityNames() 13.04.2006 16:32 Slide 62 babydemo.NetworkIntf MVC & DCA - ROOTS 2006 © Trygve Reenskaug 2006 13.04.2006 16:32 Slide 63