Post on 29-Nov-2014
description
transcript
DCIData, Context and
InteractionFabrizio Giudici, Senior Java Architect
Tidalwave s.a.s - fabrizio.giudici@tidalwave.it
Wednesday, May 18, 2011
DCI
About the speaker
2
Wednesday, May 18, 2011
DCI
About the speaker
• Senior Software Architect, Mentor, Technical Writer
• Fourteen years of Java experience (JSE, JEE, JME, etc...)
• Sun partner since 1998, Oracle consultant since 2010
• Author of a number of open source projects
• Speaker at JavaOne, Devoxx, Jazoon, JAX and other events
• Member of the NetBeans Dream Team
• Co-leader of JUG Milano
• Java.Net blogger at http://www.java.net/blogs/fabriziogiudici
2
Wednesday, May 18, 2011
DCI
Agenda
3
Wednesday, May 18, 2011
DCI
Agenda
• A real world example
3
Wednesday, May 18, 2011
DCI
Agenda
• A real world example
• DCI - Basic concepts
3
Wednesday, May 18, 2011
DCI
Agenda
• A real world example
• DCI - Basic concepts
• DCI - How to implement?
3
Wednesday, May 18, 2011
DCI
Agenda
• A real world example
• DCI - Basic concepts
• DCI - How to implement?
• Some simple examples
3
Wednesday, May 18, 2011
A real world example
Wednesday, May 18, 2011
DCI
Shameless Plug
5
Wednesday, May 18, 2011
DCI
Shameless Plug
• Exercises of Design - my design book...
5
Wednesday, May 18, 2011
DCI
Shameless Plug
• Exercises of Design - my design book...
• Just started!
5
Wednesday, May 18, 2011
DCI
Shameless Plug
• Exercises of Design - my design book...
• Just started!
• http://exercisesofdesign.java.net
5
Wednesday, May 18, 2011
DCI
The Observation API
• A core component of blueBill Mobile
• An application about recording bird observations
• Designed with a high degree of abstraction
6
Wednesday, May 18, 2011
DCI
The Observation API
• A core component of blueBill Mobile
• An application about recording bird observations
• Designed with a high degree of abstraction
6
Wednesday, May 18, 2011
DCI
The Observation API
• A core component of blueBill Mobile
• An application about recording bird observations
• Designed with a high degree of abstraction
6
Wednesday, May 18, 2011
DCI 7
Wednesday, May 18, 2011
DCI
The Taxonomy API
• Models the taxonomy of a philogenetic tree
• The standard way of biology for representing (bird) species
8
Wednesday, May 18, 2011
DCI 9
Wednesday, May 18, 2011
DCI
So, what about a Bird?
10
Wednesday, May 18, 2011
DCI
So, what about a Bird?
• Both a Taxon and an Observable
10
Wednesday, May 18, 2011
DCI
So, what about a Bird?
• Both a Taxon and an Observable
• Multiple inheritance?
10
Wednesday, May 18, 2011
DCI
So, what about a Bird?
• Both a Taxon and an Observable
• Multiple inheritance?
• Multiple interface implementation?
10
Wednesday, May 18, 2011
DCI
So, what about a Bird?
• Both a Taxon and an Observable
• Multiple inheritance?
• Multiple interface implementation?
• Composition
10
Wednesday, May 18, 2011
DCI
So, what about a Bird?
• Both a Taxon and an Observable
• Multiple inheritance?
• Multiple interface implementation?
• Composition
• Taxon and Observable are roles
10
Wednesday, May 18, 2011
DCI
So, what about a Bird?
• Both a Taxon and an Observable
• Multiple inheritance?
• Multiple interface implementation?
• Composition
• Taxon and Observable are roles
• A Bird, in some contexts, could be a Taxon and/or an Observable
10
Wednesday, May 18, 2011
DCI - Basic Concepts
Wednesday, May 18, 2011
DCI
Data, Context and Interaction
12
Wednesday, May 18, 2011
DCI
Data, Context and Interaction
• OOD best practice formalized by Trygve Reenskaug
12
Wednesday, May 18, 2011
DCI
Data, Context and Interaction
• OOD best practice formalized by Trygve Reenskaug
• The formalizer of MVC
12
Wednesday, May 18, 2011
DCI
Data, Context and Interaction
• OOD best practice formalized by Trygve Reenskaug
• The formalizer of MVC
• Seen by somebody as an evolution of MVC
12
Wednesday, May 18, 2011
DCI
Data, Context and Interaction
• OOD best practice formalized by Trygve Reenskaug
• The formalizer of MVC
• Seen by somebody as an evolution of MVC
• But it doesn’t replace it
12
Wednesday, May 18, 2011
DCI
Data, Context and Interaction
• OOD best practice formalized by Trygve Reenskaug
• The formalizer of MVC
• Seen by somebody as an evolution of MVC
• But it doesn’t replace it
• It rather broadens the analysis scope
12
Wednesday, May 18, 2011
DCI
Data, Context and Interaction
• OOD best practice formalized by Trygve Reenskaug
• The formalizer of MVC
• Seen by somebody as an evolution of MVC
• But it doesn’t replace it
• It rather broadens the analysis scope
• http://www.artima.com/articles/dci_vision.html
12
Wednesday, May 18, 2011
DCI
DCI - aims
13
Wednesday, May 18, 2011
DCI
DCI - aims
• Improve readability of a OO system
13
Wednesday, May 18, 2011
DCI
DCI - aims
• Improve readability of a OO system
• Give system behaviour first-class status
13
Wednesday, May 18, 2011
DCI
DCI - aims
• Improve readability of a OO system
• Give system behaviour first-class status
• Recover readability of system properties on the whole
13
Wednesday, May 18, 2011
DCI
DCI - aims
• Improve readability of a OO system
• Give system behaviour first-class status
• Recover readability of system properties on the whole
• Separate responsibilities for behaviour and domain
13
Wednesday, May 18, 2011
DCI
DCI - aims
• Improve readability of a OO system
• Give system behaviour first-class status
• Recover readability of system properties on the whole
• Separate responsibilities for behaviour and domain
• Behaviour: what the system does
13
Wednesday, May 18, 2011
DCI
DCI - aims
• Improve readability of a OO system
• Give system behaviour first-class status
• Recover readability of system properties on the whole
• Separate responsibilities for behaviour and domain
• Behaviour: what the system does
• Domain: what the system is
13
Wednesday, May 18, 2011
DCI
DCI - aims
• Improve readability of a OO system
• Give system behaviour first-class status
• Recover readability of system properties on the whole
• Separate responsibilities for behaviour and domain
• Behaviour: what the system does
• Domain: what the system is
• Be close to people’s mental model
13
Wednesday, May 18, 2011
DCI
DCI - Data
14
Wednesday, May 18, 2011
DCI
DCI - Data
• What the system is
14
Wednesday, May 18, 2011
DCI
DCI - Data
• What the system is
• Relatively static with relations
14
Wednesday, May 18, 2011
DCI
DCI - Data
• What the system is
• Relatively static with relations
• Domain structure implemented with “conventional” classes
14
Wednesday, May 18, 2011
DCI
DCI - Data
• What the system is
• Relatively static with relations
• Domain structure implemented with “conventional” classes
• Hmm... perhaps anemic classes?
14
Wednesday, May 18, 2011
DCI
DCI - Data
• What the system is
• Relatively static with relations
• Domain structure implemented with “conventional” classes
• Hmm... perhaps anemic classes?
• Typically it includes persistence
14
Wednesday, May 18, 2011
DCI
DCI - Data
• What the system is
• Relatively static with relations
• Domain structure implemented with “conventional” classes
• Hmm... perhaps anemic classes?
• Typically it includes persistence
• Comes from the mental model of system stakeholders
14
Wednesday, May 18, 2011
DCI
DCI - Data
• What the system is
• Relatively static with relations
• Domain structure implemented with “conventional” classes
• Hmm... perhaps anemic classes?
• Typically it includes persistence
• Comes from the mental model of system stakeholders
• Close to the “model” in MVC
14
Wednesday, May 18, 2011
DCI
DCI - Data
• What the system is
• Relatively static with relations
• Domain structure implemented with “conventional” classes
• Hmm... perhaps anemic classes?
• Typically it includes persistence
• Comes from the mental model of system stakeholders
• Close to the “model” in MVC
• E.g.: BankAccount with increase(), decrease(); no deposit()
14
Wednesday, May 18, 2011
DCI
DCI - Context (and Roles)
15
Wednesday, May 18, 2011
DCI
DCI - Context (and Roles)
• Associated to a use case, user story, scenario, or algorithm
15
Wednesday, May 18, 2011
DCI
DCI - Context (and Roles)
• Associated to a use case, user story, scenario, or algorithm
• Identifies objects participating in a scenario
15
Wednesday, May 18, 2011
DCI
DCI - Context (and Roles)
• Associated to a use case, user story, scenario, or algorithm
• Identifies objects participating in a scenario
• Assign to each object one or more stateless roles
15
Wednesday, May 18, 2011
DCI
DCI - Context (and Roles)
• Associated to a use case, user story, scenario, or algorithm
• Identifies objects participating in a scenario
• Assign to each object one or more stateless roles
• Objects can have multiple roles at the same time
15
Wednesday, May 18, 2011
DCI
DCI - Context (and Roles)
• Associated to a use case, user story, scenario, or algorithm
• Identifies objects participating in a scenario
• Assign to each object one or more stateless roles
• Objects can have multiple roles at the same time
• Decompose the scenario into roles, not objects
15
Wednesday, May 18, 2011
DCI
DCI - Context (and Roles)
• Associated to a use case, user story, scenario, or algorithm
• Identifies objects participating in a scenario
• Assign to each object one or more stateless roles
• Objects can have multiple roles at the same time
• Decompose the scenario into roles, not objects
• Contrast this with polymorphism and classic OO decomposition
15
Wednesday, May 18, 2011
DCI
DCI - Context (and Roles)
• Associated to a use case, user story, scenario, or algorithm
• Identifies objects participating in a scenario
• Assign to each object one or more stateless roles
• Objects can have multiple roles at the same time
• Decompose the scenario into roles, not objects
• Contrast this with polymorphism and classic OO decomposition
• E.g.: MoneyTransfer.{.withDraw(),.deposit()} with srcAccount and destAccount
15
Wednesday, May 18, 2011
DCI
Quick terminology note
16
Wednesday, May 18, 2011
DCI
Quick terminology note
• Methodless role: abstract role
16
Wednesday, May 18, 2011
DCI
Quick terminology note
• Methodless role: abstract role
• Interface, abstract class
16
Wednesday, May 18, 2011
DCI
Quick terminology note
• Methodless role: abstract role
• Interface, abstract class
• Methodful role: concrete role
16
Wednesday, May 18, 2011
DCI
Quick terminology note
• Methodless role: abstract role
• Interface, abstract class
• Methodful role: concrete role
• Concrete class
16
Wednesday, May 18, 2011
DCI
DCI - Interaction
17
Wednesday, May 18, 2011
DCI
DCI - Interaction
• What the system does
17
Wednesday, May 18, 2011
DCI
DCI - Interaction
• What the system does
• Occurs among roles, which act as adapters among objects
17
Wednesday, May 18, 2011
DCI
DCI - Interaction
• What the system does
• Occurs among roles, which act as adapters among objects
• Roles are bound in different ways for each context
17
Wednesday, May 18, 2011
DCI
DCI - Interaction
• What the system does
• Occurs among roles, which act as adapters among objects
• Roles are bound in different ways for each context
• Roles life cycle is likely to be bound to a given interaction
17
Wednesday, May 18, 2011
DCI
DCI - Interaction
• What the system does
• Occurs among roles, which act as adapters among objects
• Roles are bound in different ways for each context
• Roles life cycle is likely to be bound to a given interaction
• Roles should be generic
17
Wednesday, May 18, 2011
DCI
DCI - Interaction
• What the system does
• Occurs among roles, which act as adapters among objects
• Roles are bound in different ways for each context
• Roles life cycle is likely to be bound to a given interaction
• Roles should be generic
• Interaction should be explicit given roles nature
17
Wednesday, May 18, 2011
DCI
DCI - Interaction
• What the system does
• Occurs among roles, which act as adapters among objects
• Roles are bound in different ways for each context
• Roles life cycle is likely to be bound to a given interaction
• Roles should be generic
• Interaction should be explicit given roles nature
• Hmm.... rather than emergent as in agile design?
17
Wednesday, May 18, 2011
DCI
DCI - Execution Model
18
Wednesday, May 18, 2011
DCI
DCI - Execution Model
• The Context finds object participants
18
Wednesday, May 18, 2011
DCI
DCI - Execution Model
• The Context finds object participants
• Then, it assigns (injects?) roles to them
18
Wednesday, May 18, 2011
DCI
DCI - Execution Model
• The Context finds object participants
• Then, it assigns (injects?) roles to them
• Roles are discovered by type (methodless roles)
18
Wednesday, May 18, 2011
DCI
DCI - Execution Model
• The Context finds object participants
• Then, it assigns (injects?) roles to them
• Roles are discovered by type (methodless roles)
• Then, it triggers a method on the first role
18
Wednesday, May 18, 2011
DCI
DCI - Execution Model
• The Context finds object participants
• Then, it assigns (injects?) roles to them
• Roles are discovered by type (methodless roles)
• Then, it triggers a method on the first role
• Interaction goes on among other roles until the job is done
18
Wednesday, May 18, 2011
DCI
Some issues
19
Wednesday, May 18, 2011
DCI
Some issues
• What about anemic objects?
19
Wednesday, May 18, 2011
DCI
Some issues
• What about anemic objects?
• Perhaps not a problem considering clusters object + roles
19
Wednesday, May 18, 2011
DCI
Some issues
• What about anemic objects?
• Perhaps not a problem considering clusters object + roles
• Object schizophrenia
19
Wednesday, May 18, 2011
DCI
Some issues
• What about anemic objects?
• Perhaps not a problem considering clusters object + roles
• Object schizophrenia
• Which is the identity of an object in a cluster?
19
Wednesday, May 18, 2011
DCI
Some issues
• What about anemic objects?
• Perhaps not a problem considering clusters object + roles
• Object schizophrenia
• Which is the identity of an object in a cluster?
• What about equals() / hashcode()? Are roles parts of identity?
19
Wednesday, May 18, 2011
DCI
Some issues
• What about anemic objects?
• Perhaps not a problem considering clusters object + roles
• Object schizophrenia
• Which is the identity of an object in a cluster?
• What about equals() / hashcode()? Are roles parts of identity?
• Possible solution is an Identifiable role
19
Wednesday, May 18, 2011
DCI
Some issues
• What about anemic objects?
• Perhaps not a problem considering clusters object + roles
• Object schizophrenia
• Which is the identity of an object in a cluster?
• What about equals() / hashcode()? Are roles parts of identity?
• Possible solution is an Identifiable role
• Injected by context
19
Wednesday, May 18, 2011
DCI
Some issues
• What about anemic objects?
• Perhaps not a problem considering clusters object + roles
• Object schizophrenia
• Which is the identity of an object in a cluster?
• What about equals() / hashcode()? Are roles parts of identity?
• Possible solution is an Identifiable role
• Injected by context
• Objects with the same identity are “equals”
19
Wednesday, May 18, 2011
DCI - How to implement?
Wednesday, May 18, 2011
DCI
Implementation issues
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
• Java
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
• Java
• AOP, annotation-driven code generation (e.g. Qi4J)
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
• Java
• AOP, annotation-driven code generation (e.g. Qi4J)
• Other languages
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
• Java
• AOP, annotation-driven code generation (e.g. Qi4J)
• Other languages
• Traits, mix-ins
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
• Java
• AOP, annotation-driven code generation (e.g. Qi4J)
• Other languages
• Traits, mix-ins
• First, acknowledge that DCI is a best practice
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
• Java
• AOP, annotation-driven code generation (e.g. Qi4J)
• Other languages
• Traits, mix-ins
• First, acknowledge that DCI is a best practice
• Some languages can fit better than others
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
• Java
• AOP, annotation-driven code generation (e.g. Qi4J)
• Other languages
• Traits, mix-ins
• First, acknowledge that DCI is a best practice
• Some languages can fit better than others
• Frameworks might help... but they force a new nature
21
Wednesday, May 18, 2011
DCI
Implementation issues
• Static vs dynamic
• Java
• AOP, annotation-driven code generation (e.g. Qi4J)
• Other languages
• Traits, mix-ins
• First, acknowledge that DCI is a best practice
• Some languages can fit better than others
• Frameworks might help... but they force a new nature
• My point: DCI must be addressed in design
21
Wednesday, May 18, 2011
DCI
Abstracting and sweetening
• Would be nice to be technology independent
• Would be nice to have some syntactic sugar
• Note: not central in this presentation, but needed for code examples
22
Wednesday, May 18, 2011
DCI
NetBeans Platform Lookup; as()
23
Wednesday, May 18, 2011
DCI
NetBeans Platform Lookup; as()
• Role role = object.getLookup().lookup(Role.class) ...
23
Wednesday, May 18, 2011
DCI
NetBeans Platform Lookup; as()
• Role role = object.getLookup().lookup(Role.class) ...
• NetBeans Platform’s Lookup, as a bag of roles
23
Wednesday, May 18, 2011
DCI
NetBeans Platform Lookup; as()
• Role role = object.getLookup().lookup(Role.class) ...
• NetBeans Platform’s Lookup, as a bag of roles
• Allows both static implementation and dynamic injection
23
Wednesday, May 18, 2011
DCI
NetBeans Platform Lookup; as()
• Role role = object.getLookup().lookup(Role.class) ...
• NetBeans Platform’s Lookup, as a bag of roles
• Allows both static implementation and dynamic injection
• Role role = object.as(Role);
23
Wednesday, May 18, 2011
DCI
NetBeans Platform Lookup; as()
• Role role = object.getLookup().lookup(Role.class) ...
• NetBeans Platform’s Lookup, as a bag of roles
• Allows both static implementation and dynamic injection
• Role role = object.as(Role);
• My syntactic sugar around Lookup
23
Wednesday, May 18, 2011
Some examples
Wednesday, May 18, 2011
DCI
Some example reusable roles
25
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
25
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
25
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
25
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
• Icon i = myObject.as(IconProvider).getIcon(16);
25
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
• Icon i = myObject.as(IconProvider).getIcon(16);
25
“ask” approach
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
• Icon i = myObject.as(IconProvider).getIcon(16);
25
“ask” approach
“tell” approach
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
• Icon i = myObject.as(IconProvider).getIcon(16);
• Renderable:
25
“ask” approach
“tell” approach
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
• Icon i = myObject.as(IconProvider).getIcon(16);
• Renderable:
• PrintWriter pw = ... //myObject.as(TextRenderable).render(pw);
25
“ask” approach
“tell” approach
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
• Icon i = myObject.as(IconProvider).getIcon(16);
• Renderable:
• PrintWriter pw = ... //myObject.as(TextRenderable).render(pw);
• JLabel label = ... // from SwingmyObject.as(JLabelRenderable).render(label);
25
“ask” approach
“tell” approach
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
• Icon i = myObject.as(IconProvider).getIcon(16);
• Renderable:
• PrintWriter pw = ... //myObject.as(TextRenderable).render(pw);
• JLabel label = ... // from SwingmyObject.as(JLabelRenderable).render(label);
• TextView textView = ... // from SwingmyObject.as(TextViewRenderable).render(textView);
25
“ask” approach
“tell” approach
Wednesday, May 18, 2011
DCI
Some example reusable roles
• Displayable:
• String s = myObject.as(Displayable).getDisplayName();
• IconProvider:
• Icon i = myObject.as(IconProvider).getIcon(16);
• Renderable:
• PrintWriter pw = ... //myObject.as(TextRenderable).render(pw);
• JLabel label = ... // from SwingmyObject.as(JLabelRenderable).render(label);
• TextView textView = ... // from SwingmyObject.as(TextViewRenderable).render(textView);
25
“ask” approach
“tell” approach
Roles can be dynamically injected,so myObject does not depend on them
Wednesday, May 18, 2011
DCI
Examples from blueBill
• ObservationSet set = ... ;set.as(ObservationSetTraverser) .visit(new KMLReportGenerator()); // visitor pattern
• ObservationItem item = ...;TextView textView = ...;item.as(Taxon) .as(TextViewRenderable) .render(textView);
26
Wednesday, May 18, 2011
DCI
Example: decorating roles
27
Wednesday, May 18, 2011
DCI
Q & A
• Question Time
28
Wednesday, May 18, 2011