dci - data, context and interaction @ jug lugano may 2011

121
DCI Data, Context and Interaction Fabrizio Giudici, Senior Java Architect Tidalwave s.a.s - [email protected] Wednesday, May 18, 2011

Upload: fabriziogiudici

Post on 29-Nov-2014

2.537 views

Category:

Technology


2 download

DESCRIPTION

My personal take on the Data, Context and Interaction best practice.

TRANSCRIPT

Page 1: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCIData, Context and

InteractionFabrizio Giudici, Senior Java Architect

Tidalwave s.a.s - [email protected]

Wednesday, May 18, 2011

Page 2: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

About the speaker

2

Wednesday, May 18, 2011

Page 3: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 4: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Agenda

3

Wednesday, May 18, 2011

Page 5: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Agenda

• A real world example

3

Wednesday, May 18, 2011

Page 6: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Agenda

• A real world example

• DCI - Basic concepts

3

Wednesday, May 18, 2011

Page 7: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Agenda

• A real world example

• DCI - Basic concepts

• DCI - How to implement?

3

Wednesday, May 18, 2011

Page 8: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Agenda

• A real world example

• DCI - Basic concepts

• DCI - How to implement?

• Some simple examples

3

Wednesday, May 18, 2011

Page 9: DCI - Data, Context and Interaction @ Jug Lugano May 2011

A real world example

Wednesday, May 18, 2011

Page 10: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Shameless Plug

5

Wednesday, May 18, 2011

Page 11: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Shameless Plug

• Exercises of Design - my design book...

5

Wednesday, May 18, 2011

Page 12: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Shameless Plug

• Exercises of Design - my design book...

• Just started!

5

Wednesday, May 18, 2011

Page 13: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Shameless Plug

• Exercises of Design - my design book...

• Just started!

• http://exercisesofdesign.java.net

5

Wednesday, May 18, 2011

Page 14: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 15: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 16: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 17: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI 7

Wednesday, May 18, 2011

Page 18: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 19: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI 9

Wednesday, May 18, 2011

Page 20: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

So, what about a Bird?

10

Wednesday, May 18, 2011

Page 21: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

So, what about a Bird?

• Both a Taxon and an Observable

10

Wednesday, May 18, 2011

Page 22: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

So, what about a Bird?

• Both a Taxon and an Observable

• Multiple inheritance?

10

Wednesday, May 18, 2011

Page 23: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

So, what about a Bird?

• Both a Taxon and an Observable

• Multiple inheritance?

• Multiple interface implementation?

10

Wednesday, May 18, 2011

Page 24: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

So, what about a Bird?

• Both a Taxon and an Observable

• Multiple inheritance?

• Multiple interface implementation?

• Composition

10

Wednesday, May 18, 2011

Page 25: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 26: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 27: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI - Basic Concepts

Wednesday, May 18, 2011

Page 28: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Data, Context and Interaction

12

Wednesday, May 18, 2011

Page 29: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Data, Context and Interaction

• OOD best practice formalized by Trygve Reenskaug

12

Wednesday, May 18, 2011

Page 30: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Data, Context and Interaction

• OOD best practice formalized by Trygve Reenskaug

• The formalizer of MVC

12

Wednesday, May 18, 2011

Page 31: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 32: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 33: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 34: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 35: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - aims

13

Wednesday, May 18, 2011

Page 36: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - aims

• Improve readability of a OO system

13

Wednesday, May 18, 2011

Page 37: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - aims

• Improve readability of a OO system

• Give system behaviour first-class status

13

Wednesday, May 18, 2011

Page 38: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 39: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 40: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 41: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 42: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 43: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Data

14

Wednesday, May 18, 2011

Page 44: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Data

• What the system is

14

Wednesday, May 18, 2011

Page 45: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Data

• What the system is

• Relatively static with relations

14

Wednesday, May 18, 2011

Page 46: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Data

• What the system is

• Relatively static with relations

• Domain structure implemented with “conventional” classes

14

Wednesday, May 18, 2011

Page 47: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 48: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 49: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 50: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 51: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 52: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Context (and Roles)

15

Wednesday, May 18, 2011

Page 53: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Context (and Roles)

• Associated to a use case, user story, scenario, or algorithm

15

Wednesday, May 18, 2011

Page 54: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 55: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 56: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 57: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 58: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 59: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 60: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Quick terminology note

16

Wednesday, May 18, 2011

Page 61: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Quick terminology note

• Methodless role: abstract role

16

Wednesday, May 18, 2011

Page 62: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Quick terminology note

• Methodless role: abstract role

• Interface, abstract class

16

Wednesday, May 18, 2011

Page 63: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Quick terminology note

• Methodless role: abstract role

• Interface, abstract class

• Methodful role: concrete role

16

Wednesday, May 18, 2011

Page 64: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Quick terminology note

• Methodless role: abstract role

• Interface, abstract class

• Methodful role: concrete role

• Concrete class

16

Wednesday, May 18, 2011

Page 65: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Interaction

17

Wednesday, May 18, 2011

Page 66: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Interaction

• What the system does

17

Wednesday, May 18, 2011

Page 67: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Interaction

• What the system does

• Occurs among roles, which act as adapters among objects

17

Wednesday, May 18, 2011

Page 68: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 69: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 70: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 71: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 72: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 73: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Execution Model

18

Wednesday, May 18, 2011

Page 74: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Execution Model

• The Context finds object participants

18

Wednesday, May 18, 2011

Page 75: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

DCI - Execution Model

• The Context finds object participants

• Then, it assigns (injects?) roles to them

18

Wednesday, May 18, 2011

Page 76: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 77: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 78: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 79: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Some issues

19

Wednesday, May 18, 2011

Page 80: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Some issues

• What about anemic objects?

19

Wednesday, May 18, 2011

Page 81: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Some issues

• What about anemic objects?

• Perhaps not a problem considering clusters object + roles

19

Wednesday, May 18, 2011

Page 82: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Some issues

• What about anemic objects?

• Perhaps not a problem considering clusters object + roles

• Object schizophrenia

19

Wednesday, May 18, 2011

Page 83: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 84: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 85: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 86: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 87: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 88: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI - How to implement?

Wednesday, May 18, 2011

Page 89: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Implementation issues

21

Wednesday, May 18, 2011

Page 90: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Implementation issues

• Static vs dynamic

21

Wednesday, May 18, 2011

Page 91: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Implementation issues

• Static vs dynamic

• Java

21

Wednesday, May 18, 2011

Page 92: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Implementation issues

• Static vs dynamic

• Java

• AOP, annotation-driven code generation (e.g. Qi4J)

21

Wednesday, May 18, 2011

Page 93: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Implementation issues

• Static vs dynamic

• Java

• AOP, annotation-driven code generation (e.g. Qi4J)

• Other languages

21

Wednesday, May 18, 2011

Page 94: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 95: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 96: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 97: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 98: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 99: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 100: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

NetBeans Platform Lookup; as()

23

Wednesday, May 18, 2011

Page 101: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

NetBeans Platform Lookup; as()

• Role role = object.getLookup().lookup(Role.class) ...

23

Wednesday, May 18, 2011

Page 102: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 103: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 104: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 105: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 106: DCI - Data, Context and Interaction @ Jug Lugano May 2011

Some examples

Wednesday, May 18, 2011

Page 107: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Some example reusable roles

25

Wednesday, May 18, 2011

Page 108: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Some example reusable roles

• Displayable:

25

Wednesday, May 18, 2011

Page 109: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Some example reusable roles

• Displayable:

• String s = myObject.as(Displayable).getDisplayName();

25

Wednesday, May 18, 2011

Page 110: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Some example reusable roles

• Displayable:

• String s = myObject.as(Displayable).getDisplayName();

• IconProvider:

25

Wednesday, May 18, 2011

Page 111: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 112: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 113: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 114: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 115: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 116: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 117: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 118: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 119: DCI - Data, Context and Interaction @ Jug Lugano May 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

Page 120: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Example: decorating roles

27

Wednesday, May 18, 2011

Page 121: DCI - Data, Context and Interaction @ Jug Lugano May 2011

DCI

Q & A

• Question Time

28

Wednesday, May 18, 2011