Models and Reality: Master Program Execution with DCI UML China 2010

advertisement
Models and Reality:
Master Program Execution
with DCI
UML China 2010
Trygve Reenskaug
Dept. of Informatics
University of Oslo
heim.ifi.uio.no/~trygver/themes/babyide
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 1
Two commonly believed
Fallacies
End users cannot understand modern programs
FALSE BECAUSE:
Object Models are understandable
BUT:
Code must implement user mental model
Programmers cannot read modern programs
FALSE BECAUSE:
Object systems can be coded to be
readable and checkable
BUT:
Code must describe program runtime structure
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 3
Reality
program execution creates value
Personal
plans
Project
control
Accounting
Calender
IDE
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 4
End User System Experience
End User mental model
domain model
(what the system IS)
use cases
(what the system DOES)
Runtime
(objects and interactions)
Chasm
Compile time
(classes, superclasses)
code
class model
Programmer mental model
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 5
Plan
The User’s mental model
• Object Orientation
• CRC Cards
• Lean and Agile
The Programmer’s mental model
Defect removal
Conclusion
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 6
Object-Orientation:
networks of communicating objects
Alan Kay:
Each Smalltalk object is a recursion
on the entire possibilities of the computer.
Thus its semantics are a bit like having
thousands and thousands of computers
all hooked together by a very fast network
http://gagne.homedns.org/~tgagne/contrib/EarlyHistoryST.html
The essence of object orientation
is the interchange of messages
that takes place in the space between the objects
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 7
True Object Orientation
Objects as participants in commnication
receive
receive
receive
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 8
End User performs a Money Transfer
What the System DOES
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 9
End User’s Mental Model
What the system DOES
Customer
Bank
FromAccount
ToAccount
transfer (
amount,
fromAccount,
toAccount)
transfer (...)
deposit (...)
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 10
Three Use Case Executions
Money Transfers
use case
execution
use case
execution
use case
execution
Class
ATM
Class
SavingsAccount
Class
CheckingAccount
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 11
Name the objects by the
roles they play in the Interaction
use case executions
ID
662454
136585
748534
995124
254967
245872
554782
221483
254967
UML-China 2010
© Trygve Reenskaug 2010
Class
ATM
SavingsAccount
CheckingAccount
ATM
CheckingAccount
SavingsAccount
ATM
CheckingAccount
CheckingAccount
---
29.05.2016 05:18
Role
Bank
FromAccount
ToAccount
Bank
FromAccount
ToAccount
Bank
FromAccount
ToAccount
---
Slide 12
CRC
Class – Responsibility – Collaboration
Wikipedia: Sloppy thinking
CRC cards are usually created from index cards on which are written:
• The class name
• Its Super and Sub classes (if applicable)
• The responsibilities of the class.
• The names of other classes with which the class will collaborate
to fulfill its responsibilities.
• Author
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 13
CRC
Candidate – Responsibility – Collaboration
CRC cards are usually created from index cards on which are written:
• The role name.
• The responsibilities of the roles (and the objects that play them).
• The names of other roles with which the role will collaborate
to fulfill its responsibilities.
• Author.
Candidate role – Responsibility -- Collaboration
Wirfs-Brock; McKean:
Object Design. Roles, Responsibilities, and Collaborations
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 14
CRC Cards Example
role name = Bank
responsibility =
collaborators =
To serve the
customer with
a transfer of
funds
FromAccount
role name = FromAccount
role name = ToAccount
responsibility =
collaborators =
To transfer
funds from
this account to
its collaborator
ToAccount
UML-China 2010
responsibility =
To receive funds
as part of a
funds transfer
© Trygve Reenskaug 2010
collaborators =
none
29.05.2016 05:18
Slide 15
Summary
Object Models are understandable
End user mental model explains how the system works.
End user mental model is an object model:
Objects
• Can store data
• Can handle received messages
• Can send messages to other objects
Objects interact to to achieve a use case
Objects are named by the role they play in the interaction
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 16
Coplien, Bjørnvig: Lean Architecture
ISBN: 978-0-470-68420-7
Building Software as if people mattered
No waste
Get it right the first time!
Be prepared for changes!
Lean Principle:
”everybody, all together, from early on.”
System architecture
shall reflect the end user's mental model.
Architecture has two parts:
What the system IS: Its state
What the system DOES: Its behavior
Architecture
a skeleton program
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 17
Plan
The User’s mental model
•
Object Orientation
•
CRC Cards
•
Lean and Agile
The Programmer’s mental model
• The Chasm betweenCode and Execution
• What the system IS – What the system DOES
• Separation of concerns: Contexts with Roles
• DCI: Data – Context – Interaction
Defect removal
Conclusion
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 18
Gamma, Helm, Johnson, Vlissides:
Design Patterns
An object-oriented program's run-time structure
often bears little resemblance to its code structure.
• The code structure is frozen at compile-time; it consists of
classes in fixed inheritance relationships.
• The run-time structure consists of rapidly changing
networks of communicating objects.
It's clear that code won't reveal everything
about how a system will work.
[GOF p.22, 23]
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 19
The Essence of Object Orientation
 a class reveals everything about how an instance
will handle an incoming message
and reveals nothing about its collaborator
or why the message was sent.
 an object by itself is not interesting
 the essence of object orientation is that
objects communicate to achieve a common goal
 We need explicit code that clearly specifies
the Contexts,
i.e., the networks of communicating roles
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 20
Class Hierachy is Irrelevant
for program execution
Wikipedia:
Code refactoring is the process of
changing a computer program's
source code without modifying its
external functional behavior.
Object
copy
Shape
center
area
F_Circle
Circle
radius
UML-China 2010
copy
center
area
radius
Polygon
vertices
© Trygve Reenskaug 2010
F_Polygon
copy
center
area
vertices
29.05.2016 05:18
Slide 21
The Chasm between
End User and Programmer
End User mental model
domain model
(what the system IS)
use cases
(what the system DOES)
Runtime
(objects and interactions)
Chasm
Compile time
(classes, superclasses)
code
class model
Programmer mental model
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 22
Use Case
Context and Roles
Environment
Customer
transfer (
amount,
fromAccount,
toAccount)
BankTransferContext
role
Bank
role
FromAccount
role
ToAccount
transfer (..)
deposit (..)
role methods
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 23
Each Use Case
Is Executed in a Context
Context-2
Context-1
Context-3
Role binding
+Role Method injection
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 24
Three Perspectives
Data – Context - Interaction
Compile time
Data, Context, Interaction
Object model
code
Data – What-The-System-IS
Implementation of user’s domain model
Dumb, dumb classes and superclasses without interaction code.
Context – The Roles Participating in a Use Case
Roles and role structure
Select objects to play the roles.
Inject role methods into selected objects (or their classes)
Trigger Interaction.
Interaction – What-The-System-DOES
Role Methods direct the execution of the Use Case
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 25
Programmer’s Mental Model
Data Perspective
domain
model
End User
Conceptual
schema
«realize»
Class attributes
+ local methods
«instantiate classes to object»
data objects
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 26
Programmer’s Mental Model
Context Perspective
End User
use cases
Environment
use case
Context
Role
Role
Role
Bind roles to objects
data objects
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 27
Programmer’s Mental Model
Interaction Perspective
Context Role Method definitions
RoleA>> public void frontloadNetwork(Integer startWeek) {
RoleB.frontload(startWeek);
setChanged();
notifyObservers("ACTIVITY");
}
RoleB>>public void frontload(Integer startWeek) {
// reset all
for (Activity act : AllActivities) {
act.setEarlyStart(null);
}
// frontload all
Activity frontloader;
while (CurrentContext.rebind(); Activity != null) {
Integer earlyStart = startWeek;
for (Activity pred : Predecessors) {
earlyStart = Math.max(earlyStart, pred.earlyFinish() + 1);
}
Activity.setEarlyStart(earlyStart);
}
}
Class definitions
package babydemo3.model;
import babydemo3.model.Model;
package babydemo3.model.algorithm;
import babydemo3.model.collaboration.*;
import babydemo3.model.Model;
import babydemo3.model.algorithm.*;
import babydemo3.model.data.Activity;
import babydemo3.model.data.*;
import babydemo3.model.collaboration.FrontloadCollab;
import java.util.*;
public class Case {
import java.awt.Color;
private
Plan model;
public class Model
extends
Observable {
}
private NetBase netBase;
private Resource resource;
public void frontload(Integer startWeek) {
public Model()//{}reset all
forresource()
(Activity act
public Resource
{} : AllActivities) {
act.setEarlyStart(null);
public void reset()
{}
}
public void addActivity(String
name, Integer duration, Color color) {}
// frontload all
public void addDependency(String
predName, String succName) {}
Activity frontloader;
while (CurrentContext.reselect(); Activity != null) {
public void frontloadNetwork(Integer
startWeek) {
Integer earlyStart = startWeek;
RoleB.frontload(startWeek);
for (Activity pred : Predecessors) {
setChanged();
earlyStart = Math.max(earlyStart, pred.earlyFinish() + 1);
notifyObservers("ACTIVITY");
}
}
Activity.setEarlyStart(earlyStart);
}
}
}
inject role methods
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 28
Programmer’s Mental Model
The Whole Story
domain
model
use cases
Conceptual
schema
Environment
use case task
Context
realize
Class attributes
+ local methods
+ role methods
Role
Inject role
methods
«instantiate classes to objects»
Role
Role
«bind roles to objects»
interaction
data objects
UML-China 2010
End User
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 29
Plan
The User’s mental model
•
Object Orientation
•
CRC Cards
•
Lean and Agile
The Programmer’s mental model
•
The Chasm betweenCode and Execution
•
What the system IS – What the system DOES
•
Separation of concerns: Contexts with Roles
•
DCI: Data – Context – Interaction
Defect removal
• Lean: Get it right the first time
• Chunkable, readable code. BabyIDE.
• Software Peer Review
Conclusion
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 30
Edsger W. Dijkstra on Bugs
"Program testing can be used to show the
presence of bugs,
but never to show their absence!“
"Simplicity
is prerequisite for reliability.“
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 31
Peer Review
Datamation Magazine ca. 1967
•
•
•
•
It’s hard to find bugs in own code
It’s fun to find bugs in other people’s code
Coder learns from reviewer’s comments
Reviewer learns by reading the code
Wikipedia: savings exceeds costs by 4 to 1".
• Get it right the first time!
• Test to confirm no blunder!
--- but execute all statements
Code must be Chunkable! Readable!
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 32
Code must be Chunkable! Readable!
DCI Separation Of Concerns:
• Data:
Data classes greatly simplified; realize ’pure’ data model.
• Context:
One Context for each use case, isolated from other Contexts.
• Interaction:
Roles only meaningful within their Context
Role to object binding done within one chunk of methods.
The Role Methods specify system behavior
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 33
BabyIDE
DCI Integrated Development Environment
2
1
3
4
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 34
Plan
The User’s mental model
•
Object Orientation
•
CRC Cards
•
Lean and Agile
The Programmer’s mental model
•
The Chasm betweenCode and Execution
•
What the system IS – What the system DOES
•
Separation of concerns: Contexts with Roles
•
DCI: Data – Context – Interaction
Defect removal
•
Lean: Get it right the first time
•
Chunkable, readable code. BabyIDE.
•
Software Peer Review
Conclusion
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 35
Conclusion – 1 of 4
DCI = Readable Code
The DCI paradigm:
Work with program in different perspectives
• D ata Classes for what the system IS
• C
• I
UML-China 2010
ontext for the network of communicating roles
nteraction for what the system DOES
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 36
Conclusion – 2 of 4
DCI = Changeable Code
• Data perspective compact; system state only.
• Role/object selection separates
system state and system behavior.
• Existing Contexts can be changed independently.
• New Contexts can be added to realize new funtionality.
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 37
Conclusion – 3 of 4
End User Mental Model = Code
End User mental model
domain model
(what the system IS)
use cases
(what the system DOES)
Runtime
(objects play roles)
instantiate data classes
to objects
context selects objects
to play roles
inject role methods
Chasm Bridged!
Compile time
(Data, Context, Interaction)
Object model
code
Programmer mental model
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 38
Conclusion – 4 of 4
State of DCI
DCI
Implementation examples in
Smalltalk/Squeak,
C++, Java, C#, Ruby, Scala, Python.
Real applications in Java (Qi4j, Rickard Øberg).
New DCI Programming Language being developed.
BabyIDE
Squeak Demo version available
Basic technology is freely available for toolmakers
For now:
Read the “Lean Architecture” book
Read Wikipedia article
Never forget the end user of your code
Never forget the reader of your code
DCI Community at object-composition@googlegroups.com
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 39
Comments?
Questions?
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 40
More reading
http://www.ifi.uio.no/~trygver
mailto:
Coplien, J.O., Bjørnvig, G; Lean Architecture for Agile Software Development;
Wiley, Chichester, UK, 2010; ISBN 978-0-470-68420-7
Reenskaug, T; Wold, P.;, Lehne, O. A.: Working With Objects; Manning, Greenwich, CT 06830;1996;
ISBN 1-884777-10-4; (Prentice Hall ISBN 0-13-452930-8).
This book is out of print. An early .pdf version kan be downloaded free from
[web page] http://folk.uio.no/trygver/1996/book/book11d.pdf
Important Web resources:
Kay on object orientation: http://gagne.homedns.org/~tgagne/contrib/EarlyHistoryST.html
The DCI defining reference: http://en.wikipedia.org/wiki/Data,_Context,_and_Interaction
A technical article: http://heim.ifi.uio.no/~trygver/2008/commonsense.pdf
More online resources: http://www.leansoftwarearchitecture.com/home/more-online-resources
Reenskaug, T.: Programming with Roles and Classes; the BabyUML Approach;
Chapter in Klein, A.D.; Computer Software Engineering Research; Nova Science Publishers, New York, 2007;
ISBN-13: 978-1-60021-774-6;pp.45-88;
[web page] http://folk.uio.no/trygver/2007/babyUML.pdf
Reenskaug, T,: The BabyUML discipline of programming
(where A Program = Data + Communication + Algorithms).
SoSym 5,1 (April 2006). DOI: 10.1007/s10270-006-0008-x.
[web page] http://heim.ifi.uio.no/~trygver/2006/SoSyM/trygveDiscipline.pdf
Wirfs-Brock, R.; McKean, A.; Object Design. Roles, Responsibilities, and Collaborations.
ISBN 0-201-37943-0; Addison-Wesley; Boston, MA, 2003.
Andersen, E. P.; Conceptual Modeling of Objects. A Role Modeling Approach.
D.Scient thesis, November 1997, University of Oslo.
[web page] http://heim.ifi.uio.no/~trygver/1997/EgilAndersen/ConceptualModelingOO.pdf
Gamma et.al. (GOF) Design Patterns; ISBN 0-201-63361-; Addison-Wesley, Reading, MA. 1995.
UML-China 2010
© Trygve Reenskaug 2010
29.05.2016 05:18
Slide 41
Download