Obje c ts of Va lue Ke vlin He nne y ke vlin@c ur br alan.c om - - PowerPoint PPT Presentation

obje c ts of va lue
SMART_READER_LITE
LIVE PREVIEW

Obje c ts of Va lue Ke vlin He nne y ke vlin@c ur br alan.c om - - PowerPoint PPT Presentation

Obje c ts of Va lue Ke vlin He nne y ke vlin@c ur br alan.c om @Ke vlinHe nne y See http://programmer.97things.oreilly.com (also http://tr.im/97tepsk and http://tinyurl.com/97tepsk ) and follow @97TEPSK What Do We Mean by Value ? T he te


slide-1
SLIDE 1

Obje c ts of Va lue

Ke vlin He nne y

ke vlin@c ur br alan.c om @Ke vlinHe nne y

slide-2
SLIDE 2

See http://programmer.97things.oreilly.com (also http://tr.im/97tepsk and http://tinyurl.com/97tepsk) and follow @97TEPSK

slide-3
SLIDE 3

What Do We Mean by Value?

T

he te rm value is use d in ma ny

  • ve rla pping a nd c ontra dic tory wa ys

T

he me c ha nism of pass by value

A de c la ra tive c onstruc t, e .g ., C# str

uc ts

de fine prog ra mma tic value type s

A kind of obje c t re pre se nting fine -

g ra ine d informa tion in a doma in mode l

T

he g e ne ra l notion of qua ntity or me a sure of some thing in the re a l world

slide-4
SLIDE 4

Many objects have no conceptual

  • identity. These objects describe

some characteristic of a thing. [...] When you care only about the attributes of an element of the model, classify it as a VALUE OBJECT. Make it express the meaning of the attributes it conveys and give it related functionality. Treat the

VALUE OBJECT as immutable. Don't

give it any identity and avoid the design complexities necessary to maintain ENTITIES.

slide-5
SLIDE 5

Complementary Perspectives

T

he Pla tonic Pe rspe c tive

An ide a lise d vie w of wha t va lue s a re in

te rms of ma ths a nd the physic a l world

T

he Computa tiona l Pe rspe c tive

A mode l- ba se d vie w of wha t va lue s

a re in te rms of prog ra mming c onc e pts

T

he L a ng ua g e Pe rspe c tive

T

he c omputa tiona l vie w bound to the spe c ific s of a prog ra mming la ng ua g e

slide-6
SLIDE 6
slide-7
SLIDE 7

phenomenon (plural: phenomena): An element of what we can observe in the world. Phenomena may be individuals or relations. Individuals are entities, events, or values. Relations are roles, states, or truths. individual: An individual is a phenomenon that can be named and is distinct from every other individual: for example, the number 17, George III, or Deep Blue's first move against Kasparov. relationship: A kind of phenomenon. An association among two or more individuals, for example, Mother(Lucy, Joe). Also, generally, any pattern or structure among phenomena of a domain.

slide-8
SLIDE 8
  • Events. An event is an individual

happening, taking place at some particular point in time. Each event is indivisible and instantaneous.

  • Entities. An entity is an individual that

persists over time and can change its properties and states from one point in time to another.

  • Values. A value is an intangible individual

that exists outside time and space, and is not subject to change.

  • States. A state is a relation among

individual entities and values; it can change over time.

  • Truths. A truth is a relation among

individuals that cannot possibly change

  • ver time.
  • Roles. A role is a relation between an

event and individuals that participate in it in a particular way.

slide-9
SLIDE 9

On the Origin of Species

Va lue type s diffe r in the g e ne ra lity

a nd foc us of the ir doma in

Some a re ma the ma tic a l, e .g ., inte g e rs Some a re prog ra mma tic , e .g ., string s Some a re re a l world, e .g ., ISBNs

Va lue type s re fle c t c onstra ints

E

.g ., ISBNs ha ve a we ll- forme dne ss rule

E

.g ., int is a bounde d subse t of inte g e rs

slide-10
SLIDE 10

Systems of Values

Ope ra tions, re la tionships a nd

c onstra ints form syste ms of va lue s

E

.g ., a point in time is a va lue , a s is the diffe re nc e be twe e n two points in time , but time point, time pe r

iod a nd time inte r val a re not e quiva le nt type s

E

.g ., dista nc e divide d by time yie lds spe e d (a nd displa c e me nt divide d by time yie lds ve loc ity)

slide-11
SLIDE 11

povo, sm.

  • 1. Conjunto de indivíduos que falam (em regra) a mesma

lingua, têm costumes e hábitos idênticos, uma história e tradições communs.

  • 2. Os habitantes duma localidade ou região; povoação.
  • 3. V. povoado.
  • 4. Multidão.
  • 5. V. plebe.

Minidicionário da Língua Portuguesa

slide-12
SLIDE 12

Whole Value Whole Value

Besides using the handful of literal values offered by the language (numbers, strings, true and false) and an even smaller complement of

  • bjects normally used as values (date, time, point), you will make and

use new objects with this pattern that represent the meaningful quantities of your business. These values will carry whole, useful chunks of information from the user interface to the domain model. Construct specialized values to quantify your domain model and use these values as the arguments of their messages and as the units of input and output. Make sure these objects capture the whole quantity, with all its implications beyond merely magnitude; but keep them independent of any particular domain. (The word value here implies that these objects do not have identity of importance.) Ward Cunningham "The CHECKS Pattern Language of Information Integrity"

slide-13
SLIDE 13

Values as Objects

F

rom a prog ra mming pe rspe c tive , we c a n mode l va lue s a s obje c ts

He nc e value obje c ts a nd value type s Va lue obje c ts ha ve sig nific a nt sta te but

insig nific a nt ide ntity

But the re is no dic hotomy or c onflic t

be twe e n value s a nd obje c ts

A value obje c t is a kind or style of

  • bje c t tha t re a lise s a value
slide-14
SLIDE 14

Patterns of Value

VAL

UE

O BJE

CT

IMMUT

ABL E

VAL

UE

C OPIE

D

VAL

UE

MUT

ABL E

C OMPANION C L

ONING

C OPY C ONST

RUCT OR

C L

ASS

F

ACT ORY

ME

T HOD

C ONVE

RSION

ME

T HOD

O VE

RL OAD–

O VE

RRIDE

ME

T HOD PAIR

BRIDGE ME

T HOD

T

YPE- SPE CIF IC

O VE

RL OAD

C E

L L

VAL

UE

VAL

IDAT ING

C ONST

RUCT OR

IMPL

ICIT

WIDE

NING

C ONVE

RSION

O PE

RAT ORS F OL L OW

BUIL

T

  • INS

ME

ANINGF UL

C ONST

RUCT ION

slide-15
SLIDE 15

Identity State Behaviour

slide-16
SLIDE 16
slide-17
SLIDE 17

Ref efer erential tr ential transpar ansparenc ency and ref efer erential opaqueness ential opaqueness are properties of parts of computer programs. An expression is said to be referentially transparent if it can be replaced with its value without changing the program (in other words, yielding a program that has the same effects and output on the same input). The

  • pposite term is referentially opaque.

While in mathematics all function applications are referentially transparent, in programming this is not always the case. The importance of referential transparency is that it allows a programmer (or compiler) to reason about program behavior. This can help in proving correctness, simplifying an algorithm, assisting in modifying code without breaking it, or optimizing code by means of memoization, common subexpression elimination or parallelization.

http://en.wik http://en.wikipedia.or pedia.org/wik g/wiki/R /Ref efer eren ential_tr tial_transpar nsparenc ncy_(computer_science) y_(computer_science)

slide-18
SLIDE 18

Immutable Value Define a value object type whose instances are immutable. Copied Value Define a value object type whose instances are copyable.

slide-19
SLIDE 19
slide-20
SLIDE 20

General POLO Anatomy

Construc tion...

Construc tor re sults in va lid obje c t

Compa rison...

E

qua lity is a funda me nta l c onc e pt

T

  • ta l orde ring ma y or ma y not a pply

Cla ssific a tion a nd c onve rsion...

Ne ithe r a subc la ss nor a supe rc la ss be Ma y support c onve rsions

slide-21
SLIDE 21

Value Object Smells

Ana e mia

L

ittle be ha viour be yond fie ld a c c e ss

Role c re e p

Unc ohe sive , e xte rna l de pe nde nc ie s, ...

Inc omple te or surprising c ompa rison

E

qua lity ba se d on one a ttribute only

Unwa nte d a fforda nc e s

Non- me a ning ful a nd we a k c onstruc tor

slide-22
SLIDE 22

public final class Date implements ... { ... public int getYear() ... public int getMonth() ... public int getDayInMonth() ... public void setYear(int newYear) ... public void setMonth(int newMonth) ... public void setDayInMonth(int newDayInMonth) ... ... }

slide-23
SLIDE 23

public final class Date implements ... { ... public int getYear() ... public int getMonth() ... public int getWeekInYear() ... public int getDayInYear() ... public int getDayInMonth() ... public int getDayInWeek() ... public void setYear(int newYear) ... public void setMonth(int newMonth) ... public void setWeekInYear(int newWeek) ... public void setDayInYear(int newDayInYear) ... public void setDayInMonth(int newDayInMonth) ... public void setDayInWeek(int newDayInWeek) ... ... }

slide-24
SLIDE 24

public final class Date implements ... { ... public int getYear() ... public int getMonth() ... public int getWeekInYear() ... public int getDayInYear() ... public int getDayInMonth() ... public int getDayInWeek() ... public void setYear(int newYear) ... public void setMonth(int newMonth) ... public void setWeekInYear(int newWeek) ... public void setDayInYear(int newDayInYear) ... public void setDayInMonth(int newDayInMonth) ... public void setDayInWeek(int newDayInWeek) ... ... private int year, month, dayInMonth; }

slide-25
SLIDE 25

public final class Date implements ... { ... public int getYear() ... public int getMonth() ... public int getWeekInYear() ... public int getDayInYear() ... public int getDayInMonth() ... public int getDayInWeek() ... public void setYear(int newYear) ... public void setMonth(int newMonth) ... public void setWeekInYear(int newWeek) ... public void setDayInYear(int newDayInYear) ... public void setDayInMonth(int newDayInMonth) ... public void setDayInWeek(int newDayInWeek) ... ... private int daysSinceEpoch; }

slide-26
SLIDE 26

When it is not necessary to change, When it is not necessary to change, it is necessary not to change. it is necessary not to change. Lucius Cary

slide-27
SLIDE 27

public final class Date implements ... { ... public int getYear() ... public int getMonth() ... public int getWeekInYear() ... public int getDayInYear() ... public int getDayInMonth() ... public int getDayInWeek() ... ... }

slide-28
SLIDE 28

public final class Date implements ... { ... public int getYear() ... public Month getMonth() ... public int getWeekInYear() ... public int getDayInYear() ... public int getDayInMonth() ... public DayInWeek getDayInWeek() ... ... }

slide-29
SLIDE 29

public final class Date implements ... { ... public int year() ... public Month month() ... public int weekInYear() ... public int dayInYear() ... public int dayInMonth() ... public DayInWeek dayInWeek() ... ... }

slide-30
SLIDE 30

Language Defines a Context

De sig n is c onte xt se nsitive

And de sig n de ta il is, the re fore , a ffe c te d

by c hoic e of prog ra mming la ng ua g e

Diffe re nt la ng ua g e s e nc oura g e a nd

e na ble diffe re nt c hoic e s a nd style s

Culture a nd idioms, fe a ture s for

immuta bility, tra nspa re nc y of c opying , pre se nc e of nulls, support for ope ra tor

  • ve rloa ding , e a se of e qua lity, e tc .
slide-31
SLIDE 31

Two values of a value type are equal if and only if they represent the same abstract entity. They are representationally equal if and only if their datums are identical sequences of 0s and 1s. If a value type is uniquely represented, equality implies representational equality.

slide-32
SLIDE 32

What I propose must be “the same” after copy construction is “whatever the associated homogeneous equality comparison operator defines (documents) to be the salient attributes for that type” – i.e., “the specific attributes that must respectively compare equal in order for the

  • bjects as a whole to compare equal.”

Hence, the (observable) values of these salient attributes, and not the raw instance state used to represent them, comprise what we call the value of the object. John Lakos

Normative Language to Describe Value Copy Semantics

http://www.open-std.org/jtc1/sc22/WG21/docs/papers/2007/n2479.pdf

slide-33
SLIDE 33

Reflexivity: I am me. Symmetry: If you're the same as me, I'm the same as you. Transitivity: If I'm the same as you, and you're the same as them, then I'm the same as them too. Consistency: If there's no change, everything's the same as it ever was. Null inequality: I am not nothing. Hash equality: If we're the same, we both share the same magic numbers. No throw: If you call, I won't hang up.

slide-34
SLIDE 34
slide-35
SLIDE 35

A Note on Unit Testing

It's OK to use va lue obje c ts dire c tly

in othe r te sts

Don't moc k out va lue s — "E

ve ry time a moc k re turns a moc k, a fa iry die s"

T

he c ontra c t for e qua lity doe s not a utoma tic a lly tra nsla te to te st c a se s

Re c a st the re la tionships into a more

propositiona l form

slide-36
SLIDE 36

@Test public void identicallyConstructedValuesCompareEqual() ... @Test public void differentlyConstructedValuesCompareUnequal() ... @Test public void valuesCompareUnequalToNull() ... @Test public void identicallyConstructedValuesHaveEqualHashCodes() ...

slide-37
SLIDE 37

@Test public void identically_constructed_values_compare_equal() ... @Test public void differently_constructed_values_compare_unequal() ... @Test public void values_compare_unequal_to_null() ... @Test public void identically_constructed_values_have_equal_hash_codes() ...

slide-38
SLIDE 38

“Write that down,” the King said to the jury, and the jury eagerly wrote down all three dates

  • n their slates, and then added them up, and

reduced the answer to shillings and pence.

Lewis Carroll Alice’s Adventures in Wonderland