SlideShare a Scribd company logo
1 of 4
Download to read offline
126 Java™
Report | JU N E 2 00 0 http://www.java re po rt.com
I
DENTITY, STATE, AND behavior can typify an
object. For example, value objects1,2
—used for
representing simple pieces of information in a
system, such as dates, strings, and quantities—
have state, simple behavior, and transparent identity.
Objects with significant identity typically represent
the load-bearing structures in a system, such as per-
sistent data and graphical objects. They are also typi-
cally objects with significant life cycles that respond
to method calls quite differently, depending on what
general state or mode their content represents. Objects
that do not have significant identity tend to have less
interesting life cycles. A
stateless service object has
no state and therefore no
life cycle; a value-based
object tends to have either
a trivial life cycle model or
no life cycle model (e.g.,
Immutable Value1, 2
).
The life cycle of an
object can be modeled
in UML using a Harel
statechart.3
Statechart di-
agrams can tell developers
a lot about the behavior of objects over time, and un-
like many conventional class models, the move to
code is far from trivial. Java offers features that sim-
plify the implementation of object life cycle models.
General Pattern
Objects for States is a general-purpose (as opposed to
domain-specific) pattern4
that can be used to move
from a documented state model to a practical im-
plementation. It demonstrates a classic use of del-
egation into a class hierarchy. Note that the name
Objects for States is used here in preference to its
common name of State, as this is both a more accu-
rate description of its structure—State describes
broad intent, whereas Objects for States describes
structure—and does not give the reader the impres-
sion that this is the only solution style possible for
object life cycles; i.e., it is “a state pattern” not “the
state pattern”.*
State Anatomy. Although often presented as a sin-
gle pattern, many have recognized5
that there is a
great deal more to implementing such a collabora-
tion than can be elegantly captured in a single pat-
tern. A pattern that covers a great deal of complexity
or describes a number of implementation alterna-
tives can often be opened up to reveal a more in-
volved decision structure better represented through
a pattern language.
Pattern languages combine a number of patterns
into a coherent whole, connecting them together
through a set of decisions, with one pattern effective-
ly completing another.1, 2
A pattern that plays a role
in a language does not belong exclusively to that lan-
guage: It may be used to resolve similar problems in
other pattern languages and contexts.
The Patterns. A pattern language describes how to
build a system or an aspect of a system. This article
looks at the Objects for States pattern from a Java
perspective. It shows a fragment of a protolanguage
(i.e., a work in progress), focusing specifically on how
the mode of the object is represented—as opposed to,
say, how its transitions are managed. Figure 1 depicts
the basic flow, showing that once Objects for States
has been selected we have an either/or decision as to
how to best represent the objects housing the actual
delegated behavior.
Stateful Object and Stateless Object have applica-
bility outside this context, e.g., in considering the
statefulness (or otherwise) of a transactional middle
tier. They are here woven into the language through
common links and a shared example. The patterns
are described using a simple intent-problem-
solution-example form, with a simplified example.
Kevlin Henney is an independent consultant and trainer based
in the UK.
*Unfortunately, I have seen the havoc this misconception can
wreak on a system: Every single statechart was converted un-
questioningly into an implementation of “the State” pattern. Pat-
terns solve particular problems well; if applied inappropriately
they reduce rather than increase communication and under-
standing. A clear name counts for a lot.
KevlinHenney/ kevlin@curbralan.com
Patterns in Java
Matters of state
128 Java™
Report | JU N E 2 00 0 http://www.java re po rt.com
Patterns in Java / Kevlin Henney
Objects for
States
Allow an object to
alter its behavior
significantly by
delegating state-
based behavior to
a separate object.
Problem. How
can an object sig-
nificantly change its behavior, depending on its internal
state, without hardwired multipart conditional code? The
temptation is to use a flag internal to the object and in each
method to switch to the correct behavior accordingly. This
creates long and complex methods. It becomes difficult
to locate the behavior for a particular mode, and harder still
to modify the behavioral model correctly.
Solution. Separate the behavior from the main class,
which holds the context, into a separate class hierarchy,
where each class represents the behavior in a particular
state. The context object—the instance of the main class
with which the client communicates—aggregates an ob-
ject that is used to represent the behavior in one of its
modes. Method calls on the context are forwarded to the
mode object; responsibility for implementing behavior
in a particular state is therefore delegated and localized.
This behavioral mode object can be implemented as ei-
ther a Stateful Object or a Stateless Object.
The transition to a different state means replacement of
the behavior object by another of a different class. Poly-
morphism replaces the explicit conditional lookup. Tran-
sitions between states can be managed either by the
behavioral mode objects themselves or by a centralized
mechanism such as a table lookup.
To move with confidence from an existing design that
uses a type switch of some kind to one that takes advan-
tage of Objects for States, the Replace Type Code With
State/Strategy refactoring6
can be applied. The separa-
tion and increased messaging through forwarding meth-
ods means that this design is not suitable for distribution;
i.e., mode objects should live in the same process as their
context objects.
Example. Consider the life cycle for a simple clock shown
in Figure 2.
Leaving aside the issue of transitions and time
updates, a tempting procedural implementation can be
seen in Listing 1. Such a control-flow-based approach is
error prone and scatters the state-based behavior across
many methods.
Using Objects for States turns the basic structure inside
out through delegation. All behavior relevant to a particu-
lar mode is encapsulated in an object and the entire selec-
tion is expressed through polymorphism (see Figure 3).
Stateful Object
Allow a behavioral object that is separate from the data
object on which it operates access to that data by providing
a field with a reference back to it.
Problem. Given a separation of behavior from the state
that the behavior operates on, so that the state is one ob-
ject and the behavior in another, what is the simplest and
most direct design for the behavioral class? The separa-
tion means that whereas the behavior and data were pre-
viously in the same object—and therefore the behavior
could act directly on it—the behavior can no longer di-
rectly manipulate the data.
Solution. Implement the behavioral class so that an in-
stance has access to the contents of the object on which it
operates. Because of the knowledge of its context, it has
state of its own. At any one time, a behavior object is ded-
icated to a single context object.
Figure 1.The Objects for States pattern and
the construction ofits representation.
Figure 2. Statechart representing the life cycle of a simple
clock object.
LISTING 1.
Implementation of the clock’s life cycle using a flag
and explicit switching.
public class Clock
{
public synchronized void changeMode() ...
public synchronized void increment()
{
switch(mode)
{
case displayingTime:
throw new Exception(“Change mode to change time”);
break;
case settingHours:
++hours;
break;
case settingMinutes:
++minutes;
break;
}
}
public synchronized void cancel() ...
...
private static final int displayingTime = 0;
private static final int settingHours = 1;
private static final int settingMinutes = 2;
private int hours, minutes;
private int mode = settingHours;
}
The simplest and most direct approach is to use inner
classes so that the context object’s state is implicitly visible
to the behavior classes. Alternatively, an explicit back ref-
erence from the behavior object to the context object can be
defined, with either raw access to the data—through pack-
age or class-level visibility—or via additional query and
modifier methods.
This retains the separation between behavior and
the data of interest, but means that a behavioral object is
now tied to a single data object at a time. This can lead
to increased object creation and collection costs, espe-
cially if the behavior object is changed for another and
the previous object is discarded rather than cached.
If this is significant, implementing the behavioral mode
object as a Stateless Object represents an alternative.
Example. Implementing the clock class with a Stateful
Object for the mode leads to Listing 2. An improvement in
the creation/collection performance of the program would
be to preinstantiate all the required mode objects for an
object and cache them in an array.
Stateless Object
Allow a behavioral object that is separate from the data ob -
ject on which it operates access to that data by passing a
reference back to it as an argument with each method call.
Problem. Given a separation of behavior from the state it
operates on, so that the state is in one object and the be-
havior in another, what design for the behavioral class min-
imizes object creation and collection costs? A Stateful Ob-
ject is simple to implement and manage, but can lead to
increased object numbers and reduced performance if the
objects are created and discarded frequently.
Solution. Implement the behavioral class so that it has
no data in it whatsoever. The context object is now
passed in as an argument to each of the delegated meth-
ods of the behavioral objects. The simplest approach that
LI ST IN G 2.
Implementation of the clock’s life cycle using a
Stateful Object for the mode.
public class Clock
{
public synchronized void changeMode() { mode.changeMode();
}
public synchronized void increment() { mode.increment(); }
public synchronized void cancel() { mode.cancel(); }
private interface Mode
{
void changeMode();
void increment();
void cancel();
}
private class DisplayingTime implements Mode ...
private abstract class SettingTime implements Mode
{
public void cancel() { mode = new DisplayingTime(); }
}
private class SettingHours extends SettingTime ...
private class SettingMinutes extends SettingTime
{
public void changeMode() { mode = new DisplayingTime();
}
public void increment() { ++minutes; }
}
private int hours, minutes;
private Mode mode = new SettingHours();
}
Figure 3.Representation of the clock state model using Objects
for States.
129http://www.java re po rt.com JU N E 2 00 0 | Java™
Report
Kevlin Henney / Patterns in Java
LI STIN G 3.
Implementation of the clock’s life cycle using a
Stateless Object for the mode.
public class Clock
{
public synchronized void changeMode()
{ mode.changeMode(this); }
public synchronized void increment()
{ mode.increment(this); }
public synchronized void cancel()
{ mode.cancel(this); }
private interface Mode
{
void changeMode(Clock context);
void increment(Clock context);
void cancel(Clock context);
}
private static class DisplayingTime implements Mode ...
private static abstract class SettingTime implements Mode
...
private static class SettingHours extends SettingTime ...
private static class SettingMinutes extends SettingTime
{
public void changeMode(Clock context)
{ context.mode = displayingTime; }
public void increment(Clock context)
{ ++context.minutes; }
}
private int hours, minutes;
private Mode mode = settingHours;
private static final Mode
displayingTime = new DisplayingTime(),
settingHours = new SettingHours(),
settingMinutes = new SettingMinutes();
}
allows behavioral objects to see the encapsulated content
of the context objects is to declare them as static top-lev-
el classes in the context class. Alternatively, package-lev-
el visibility or additional query and modifier methods
must be provided.
Such behavioral objects are stateless, so they may be
treated as Flyweight4
objects that can be shared transpar-
ently among all instances of the context class. The ab-
sence of state also means that they are implicitly
threadsafe, and therefore need no synchronization. Giv-
en that only a single instance of a behavioral class is ever
required, Singleton4
can be used to good effect when the
definition of the behavioral class is outside the context
class. Otherwise, Singleton is overkill where private static
data would suffice.
The entire selection of behavior is now expressed pure-
ly through polymorphism and callbacks on a separate ob-
ject, and the state now resides only in the context object.
If the behavioral model is quite stable, the Visitor pattern
may be used to put all the behavioral code back into the
context object, with the behavioral mode object doing se-
lection only and calling the relevant method on the con-
text object to do all of the work. However, although this is
sometimes an option, it can make the implementation un-
wieldy for large state models: The context class suffers
method explosion.
Example. Implementing the clock class with a Stateless Ob-
ject for the mode leads to Listing 3.
Conclusion
A common pattern for implementing object life cycle mod-
els can be expressed in terms of other more granular pat-
terns. They may be linked together in a language; i.e., the
main pattern can be said to contain or be completed by the
other patterns. In this article, the focus was on the repre-
sentation of the Objects for States pattern in Java, as opposed
to a more detailed view of the whole pattern.5
The Stateful Object and Stateless Object patterns assist in
the realization of Objects For States, but are not tied exclu-
sively to it: The issues raised and resolutions given can be
found in many object systems whether local or distributed,
(e.g., EJB). s
References
1. Henney, K., “Patterns of value,” Java Report, Vol. 5, No. 2,
Feb. 2000, pp. 64–68.
2.Henney, K., “Value added,” Java Report, Vol. 5, No. 4, Apr.
2000, pp. 74–82.
3. Rumbaugh, J., I. Jacobson, and G. Booch, The Unified Mod -
eling Language Reference Manual, Addison–Wesley, 1999.
4. Gamma, E. et al., Design Patterns: Elements of Reusable Ob -
ject-Oriented Software, Addison–Wesley, 1995.
5. Dyson, P. and Anderson, B. “State Patterns,” Pattern Lan -
guages of Program Design 3, R. Martin, D. Riehle, and F.
Buschmann, Eds., Addison–Wesley, 1998.
6.Fowler, M., Refactoring: Improving the Design of Existing
Code, Addison–Wesley, 1999.
130 Java™
Report | JUN E 20 0 0 http://www.java re po rt.com
Patterns in Java / Kevlin Henney

More Related Content

What's hot

The Fuzzy Logical Databases
The Fuzzy Logical DatabasesThe Fuzzy Logical Databases
The Fuzzy Logical DatabasesAlaaZ
 
From Multiple Data Models To One Modeling Language V1
From Multiple Data Models To One Modeling Language  V1From Multiple Data Models To One Modeling Language  V1
From Multiple Data Models To One Modeling Language V1Andries_vanRenssen
 
Cluster Analysis Assignment 2013-2014(2)
Cluster Analysis Assignment 2013-2014(2)Cluster Analysis Assignment 2013-2014(2)
Cluster Analysis Assignment 2013-2014(2)TIEZHENG YUAN
 
Object oriented analysis and design using uml classes and objects
Object oriented analysis and  design using uml  classes and objectsObject oriented analysis and  design using uml  classes and objects
Object oriented analysis and design using uml classes and objectsLikhithaMadddula
 
Marketing analytics - clustering Types
Marketing analytics - clustering TypesMarketing analytics - clustering Types
Marketing analytics - clustering TypesSuryakumar Thangarasu
 
Barzilay & Lapata 2008 presentation
Barzilay & Lapata 2008 presentationBarzilay & Lapata 2008 presentation
Barzilay & Lapata 2008 presentationRichard Littauer
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects yndaravind
 
Domain vs. (Data) Type, Class vs. Relation
Domain vs. (Data) Type, Class vs. RelationDomain vs. (Data) Type, Class vs. Relation
Domain vs. (Data) Type, Class vs. RelationFabian Pascal
 
Cluster analysis
Cluster analysisCluster analysis
Cluster analysis緯鈞 沈
 
INCREASING ACCURACY THROUGH CLASS DETECTION: ENSEMBLE CREATION USING OPTIMIZE...
INCREASING ACCURACY THROUGH CLASS DETECTION: ENSEMBLE CREATION USING OPTIMIZE...INCREASING ACCURACY THROUGH CLASS DETECTION: ENSEMBLE CREATION USING OPTIMIZE...
INCREASING ACCURACY THROUGH CLASS DETECTION: ENSEMBLE CREATION USING OPTIMIZE...IJCSEA Journal
 
Ooad notes
Ooad notesOoad notes
Ooad notesNancyJP
 

What's hot (16)

The Fuzzy Logical Databases
The Fuzzy Logical DatabasesThe Fuzzy Logical Databases
The Fuzzy Logical Databases
 
G04835759
G04835759G04835759
G04835759
 
State pattern
State patternState pattern
State pattern
 
Oops
OopsOops
Oops
 
From Multiple Data Models To One Modeling Language V1
From Multiple Data Models To One Modeling Language  V1From Multiple Data Models To One Modeling Language  V1
From Multiple Data Models To One Modeling Language V1
 
Cluster Analysis Assignment 2013-2014(2)
Cluster Analysis Assignment 2013-2014(2)Cluster Analysis Assignment 2013-2014(2)
Cluster Analysis Assignment 2013-2014(2)
 
Object oriented analysis and design using uml classes and objects
Object oriented analysis and  design using uml  classes and objectsObject oriented analysis and  design using uml  classes and objects
Object oriented analysis and design using uml classes and objects
 
Virtual Enterprise Model
Virtual Enterprise ModelVirtual Enterprise Model
Virtual Enterprise Model
 
Viva extented final
Viva extented finalViva extented final
Viva extented final
 
Marketing analytics - clustering Types
Marketing analytics - clustering TypesMarketing analytics - clustering Types
Marketing analytics - clustering Types
 
Barzilay & Lapata 2008 presentation
Barzilay & Lapata 2008 presentationBarzilay & Lapata 2008 presentation
Barzilay & Lapata 2008 presentation
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
 
Domain vs. (Data) Type, Class vs. Relation
Domain vs. (Data) Type, Class vs. RelationDomain vs. (Data) Type, Class vs. Relation
Domain vs. (Data) Type, Class vs. Relation
 
Cluster analysis
Cluster analysisCluster analysis
Cluster analysis
 
INCREASING ACCURACY THROUGH CLASS DETECTION: ENSEMBLE CREATION USING OPTIMIZE...
INCREASING ACCURACY THROUGH CLASS DETECTION: ENSEMBLE CREATION USING OPTIMIZE...INCREASING ACCURACY THROUGH CLASS DETECTION: ENSEMBLE CREATION USING OPTIMIZE...
INCREASING ACCURACY THROUGH CLASS DETECTION: ENSEMBLE CREATION USING OPTIMIZE...
 
Ooad notes
Ooad notesOoad notes
Ooad notes
 

Similar to Matters of State

Collections for States
Collections for StatesCollections for States
Collections for StatesKevlin Henney
 
Major and Minor Elements of Object Model
Major and Minor Elements of Object ModelMajor and Minor Elements of Object Model
Major and Minor Elements of Object Modelsohailsaif
 
Unit two concept of classes and objects
Unit two concept of classes and objects Unit two concept of classes and objects
Unit two concept of classes and objects Dr Chetan Shelke
 
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...Sakthi Durai
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Sakthi Durai
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysisMahesh Bhalerao
 
Behavioural design pattern
Behavioural design patternBehavioural design pattern
Behavioural design patternBiruk Mamo
 
L ab # 07
L ab # 07L ab # 07
L ab # 07Mr SMAK
 
ArchitectureOfAOMsWICSA3
ArchitectureOfAOMsWICSA3ArchitectureOfAOMsWICSA3
ArchitectureOfAOMsWICSA3Erdem Sahin
 
Omg Fundamental Certification 4
Omg Fundamental Certification 4Omg Fundamental Certification 4
Omg Fundamental Certification 4Ricardo Quintero
 
IT440.pdf
IT440.pdfIT440.pdf
IT440.pdfAhTh3
 
s123.pdf
s123.pdfs123.pdf
s123.pdfAhTh3
 
s123.pdf
s123.pdfs123.pdf
s123.pdfAhTh3
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! EdholeEdhole.com
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! EdholeEdhole.com
 

Similar to Matters of State (20)

Collections for States
Collections for StatesCollections for States
Collections for States
 
State
StateState
State
 
Major and Minor Elements of Object Model
Major and Minor Elements of Object ModelMajor and Minor Elements of Object Model
Major and Minor Elements of Object Model
 
Observer pattern
Observer patternObserver pattern
Observer pattern
 
Ch14
Ch14Ch14
Ch14
 
Unit two concept of classes and objects
Unit two concept of classes and objects Unit two concept of classes and objects
Unit two concept of classes and objects
 
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
Java Progamming Paradigms, OOPS Concept, Introduction to Java, Structure of J...
 
Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1 Java Programming Paradigms Chapter 1
Java Programming Paradigms Chapter 1
 
Object oriented analysis
Object oriented analysisObject oriented analysis
Object oriented analysis
 
Behavioural design pattern
Behavioural design patternBehavioural design pattern
Behavioural design pattern
 
L ab # 07
L ab # 07L ab # 07
L ab # 07
 
ArchitectureOfAOMsWICSA3
ArchitectureOfAOMsWICSA3ArchitectureOfAOMsWICSA3
ArchitectureOfAOMsWICSA3
 
Uml - An Overview
Uml - An OverviewUml - An Overview
Uml - An Overview
 
ecoop99
ecoop99ecoop99
ecoop99
 
Omg Fundamental Certification 4
Omg Fundamental Certification 4Omg Fundamental Certification 4
Omg Fundamental Certification 4
 
IT440.pdf
IT440.pdfIT440.pdf
IT440.pdf
 
s123.pdf
s123.pdfs123.pdf
s123.pdf
 
s123.pdf
s123.pdfs123.pdf
s123.pdf
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! Edhole
 
Mba ebooks ! Edhole
Mba ebooks ! EdholeMba ebooks ! Edhole
Mba ebooks ! Edhole
 

More from Kevlin Henney

The Case for Technical Excellence
The Case for Technical ExcellenceThe Case for Technical Excellence
The Case for Technical ExcellenceKevlin Henney
 
Empirical Development
Empirical DevelopmentEmpirical Development
Empirical DevelopmentKevlin Henney
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that LetterKevlin Henney
 
Solid Deconstruction
Solid DeconstructionSolid Deconstruction
Solid DeconstructionKevlin Henney
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayKevlin Henney
 
Structure and Interpretation of Test Cases
Structure and Interpretation of Test CasesStructure and Interpretation of Test Cases
Structure and Interpretation of Test CasesKevlin Henney
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to ImmutabilityKevlin Henney
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-InKevlin Henney
 
Giving Code a Good Name
Giving Code a Good NameGiving Code a Good Name
Giving Code a Good NameKevlin Henney
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Kevlin Henney
 
Thinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantThinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantKevlin Henney
 

More from Kevlin Henney (20)

Program with GUTs
Program with GUTsProgram with GUTs
Program with GUTs
 
The Case for Technical Excellence
The Case for Technical ExcellenceThe Case for Technical Excellence
The Case for Technical Excellence
 
Empirical Development
Empirical DevelopmentEmpirical Development
Empirical Development
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Lambda? You Keep Using that Letter
Lambda? You Keep Using that LetterLambda? You Keep Using that Letter
Lambda? You Keep Using that Letter
 
Solid Deconstruction
Solid DeconstructionSolid Deconstruction
Solid Deconstruction
 
Get Kata
Get KataGet Kata
Get Kata
 
Procedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went AwayProcedural Programming: It’s Back? It Never Went Away
Procedural Programming: It’s Back? It Never Went Away
 
Structure and Interpretation of Test Cases
Structure and Interpretation of Test CasesStructure and Interpretation of Test Cases
Structure and Interpretation of Test Cases
 
Agility ≠ Speed
Agility ≠ SpeedAgility ≠ Speed
Agility ≠ Speed
 
Refactoring to Immutability
Refactoring to ImmutabilityRefactoring to Immutability
Refactoring to Immutability
 
Old Is the New New
Old Is the New NewOld Is the New New
Old Is the New New
 
Turning Development Outside-In
Turning Development Outside-InTurning Development Outside-In
Turning Development Outside-In
 
Giving Code a Good Name
Giving Code a Good NameGiving Code a Good Name
Giving Code a Good Name
 
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
Clean Coders Hate What Happens To Your Code When You Use These Enterprise Pro...
 
Thinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation QuadrantThinking Outside the Synchronisation Quadrant
Thinking Outside the Synchronisation Quadrant
 
Code as Risk
Code as RiskCode as Risk
Code as Risk
 
Software Is Details
Software Is DetailsSoftware Is Details
Software Is Details
 
Game of Sprints
Game of SprintsGame of Sprints
Game of Sprints
 
Good Code
Good CodeGood Code
Good Code
 

Recently uploaded

What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 

Recently uploaded (20)

What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 

Matters of State

  • 1. 126 Java™ Report | JU N E 2 00 0 http://www.java re po rt.com I DENTITY, STATE, AND behavior can typify an object. For example, value objects1,2 —used for representing simple pieces of information in a system, such as dates, strings, and quantities— have state, simple behavior, and transparent identity. Objects with significant identity typically represent the load-bearing structures in a system, such as per- sistent data and graphical objects. They are also typi- cally objects with significant life cycles that respond to method calls quite differently, depending on what general state or mode their content represents. Objects that do not have significant identity tend to have less interesting life cycles. A stateless service object has no state and therefore no life cycle; a value-based object tends to have either a trivial life cycle model or no life cycle model (e.g., Immutable Value1, 2 ). The life cycle of an object can be modeled in UML using a Harel statechart.3 Statechart di- agrams can tell developers a lot about the behavior of objects over time, and un- like many conventional class models, the move to code is far from trivial. Java offers features that sim- plify the implementation of object life cycle models. General Pattern Objects for States is a general-purpose (as opposed to domain-specific) pattern4 that can be used to move from a documented state model to a practical im- plementation. It demonstrates a classic use of del- egation into a class hierarchy. Note that the name Objects for States is used here in preference to its common name of State, as this is both a more accu- rate description of its structure—State describes broad intent, whereas Objects for States describes structure—and does not give the reader the impres- sion that this is the only solution style possible for object life cycles; i.e., it is “a state pattern” not “the state pattern”.* State Anatomy. Although often presented as a sin- gle pattern, many have recognized5 that there is a great deal more to implementing such a collabora- tion than can be elegantly captured in a single pat- tern. A pattern that covers a great deal of complexity or describes a number of implementation alterna- tives can often be opened up to reveal a more in- volved decision structure better represented through a pattern language. Pattern languages combine a number of patterns into a coherent whole, connecting them together through a set of decisions, with one pattern effective- ly completing another.1, 2 A pattern that plays a role in a language does not belong exclusively to that lan- guage: It may be used to resolve similar problems in other pattern languages and contexts. The Patterns. A pattern language describes how to build a system or an aspect of a system. This article looks at the Objects for States pattern from a Java perspective. It shows a fragment of a protolanguage (i.e., a work in progress), focusing specifically on how the mode of the object is represented—as opposed to, say, how its transitions are managed. Figure 1 depicts the basic flow, showing that once Objects for States has been selected we have an either/or decision as to how to best represent the objects housing the actual delegated behavior. Stateful Object and Stateless Object have applica- bility outside this context, e.g., in considering the statefulness (or otherwise) of a transactional middle tier. They are here woven into the language through common links and a shared example. The patterns are described using a simple intent-problem- solution-example form, with a simplified example. Kevlin Henney is an independent consultant and trainer based in the UK. *Unfortunately, I have seen the havoc this misconception can wreak on a system: Every single statechart was converted un- questioningly into an implementation of “the State” pattern. Pat- terns solve particular problems well; if applied inappropriately they reduce rather than increase communication and under- standing. A clear name counts for a lot. KevlinHenney/ kevlin@curbralan.com Patterns in Java Matters of state
  • 2. 128 Java™ Report | JU N E 2 00 0 http://www.java re po rt.com Patterns in Java / Kevlin Henney Objects for States Allow an object to alter its behavior significantly by delegating state- based behavior to a separate object. Problem. How can an object sig- nificantly change its behavior, depending on its internal state, without hardwired multipart conditional code? The temptation is to use a flag internal to the object and in each method to switch to the correct behavior accordingly. This creates long and complex methods. It becomes difficult to locate the behavior for a particular mode, and harder still to modify the behavioral model correctly. Solution. Separate the behavior from the main class, which holds the context, into a separate class hierarchy, where each class represents the behavior in a particular state. The context object—the instance of the main class with which the client communicates—aggregates an ob- ject that is used to represent the behavior in one of its modes. Method calls on the context are forwarded to the mode object; responsibility for implementing behavior in a particular state is therefore delegated and localized. This behavioral mode object can be implemented as ei- ther a Stateful Object or a Stateless Object. The transition to a different state means replacement of the behavior object by another of a different class. Poly- morphism replaces the explicit conditional lookup. Tran- sitions between states can be managed either by the behavioral mode objects themselves or by a centralized mechanism such as a table lookup. To move with confidence from an existing design that uses a type switch of some kind to one that takes advan- tage of Objects for States, the Replace Type Code With State/Strategy refactoring6 can be applied. The separa- tion and increased messaging through forwarding meth- ods means that this design is not suitable for distribution; i.e., mode objects should live in the same process as their context objects. Example. Consider the life cycle for a simple clock shown in Figure 2. Leaving aside the issue of transitions and time updates, a tempting procedural implementation can be seen in Listing 1. Such a control-flow-based approach is error prone and scatters the state-based behavior across many methods. Using Objects for States turns the basic structure inside out through delegation. All behavior relevant to a particu- lar mode is encapsulated in an object and the entire selec- tion is expressed through polymorphism (see Figure 3). Stateful Object Allow a behavioral object that is separate from the data object on which it operates access to that data by providing a field with a reference back to it. Problem. Given a separation of behavior from the state that the behavior operates on, so that the state is one ob- ject and the behavior in another, what is the simplest and most direct design for the behavioral class? The separa- tion means that whereas the behavior and data were pre- viously in the same object—and therefore the behavior could act directly on it—the behavior can no longer di- rectly manipulate the data. Solution. Implement the behavioral class so that an in- stance has access to the contents of the object on which it operates. Because of the knowledge of its context, it has state of its own. At any one time, a behavior object is ded- icated to a single context object. Figure 1.The Objects for States pattern and the construction ofits representation. Figure 2. Statechart representing the life cycle of a simple clock object. LISTING 1. Implementation of the clock’s life cycle using a flag and explicit switching. public class Clock { public synchronized void changeMode() ... public synchronized void increment() { switch(mode) { case displayingTime: throw new Exception(“Change mode to change time”); break; case settingHours: ++hours; break; case settingMinutes: ++minutes; break; } } public synchronized void cancel() ... ... private static final int displayingTime = 0; private static final int settingHours = 1; private static final int settingMinutes = 2; private int hours, minutes; private int mode = settingHours; }
  • 3. The simplest and most direct approach is to use inner classes so that the context object’s state is implicitly visible to the behavior classes. Alternatively, an explicit back ref- erence from the behavior object to the context object can be defined, with either raw access to the data—through pack- age or class-level visibility—or via additional query and modifier methods. This retains the separation between behavior and the data of interest, but means that a behavioral object is now tied to a single data object at a time. This can lead to increased object creation and collection costs, espe- cially if the behavior object is changed for another and the previous object is discarded rather than cached. If this is significant, implementing the behavioral mode object as a Stateless Object represents an alternative. Example. Implementing the clock class with a Stateful Object for the mode leads to Listing 2. An improvement in the creation/collection performance of the program would be to preinstantiate all the required mode objects for an object and cache them in an array. Stateless Object Allow a behavioral object that is separate from the data ob - ject on which it operates access to that data by passing a reference back to it as an argument with each method call. Problem. Given a separation of behavior from the state it operates on, so that the state is in one object and the be- havior in another, what design for the behavioral class min- imizes object creation and collection costs? A Stateful Ob- ject is simple to implement and manage, but can lead to increased object numbers and reduced performance if the objects are created and discarded frequently. Solution. Implement the behavioral class so that it has no data in it whatsoever. The context object is now passed in as an argument to each of the delegated meth- ods of the behavioral objects. The simplest approach that LI ST IN G 2. Implementation of the clock’s life cycle using a Stateful Object for the mode. public class Clock { public synchronized void changeMode() { mode.changeMode(); } public synchronized void increment() { mode.increment(); } public synchronized void cancel() { mode.cancel(); } private interface Mode { void changeMode(); void increment(); void cancel(); } private class DisplayingTime implements Mode ... private abstract class SettingTime implements Mode { public void cancel() { mode = new DisplayingTime(); } } private class SettingHours extends SettingTime ... private class SettingMinutes extends SettingTime { public void changeMode() { mode = new DisplayingTime(); } public void increment() { ++minutes; } } private int hours, minutes; private Mode mode = new SettingHours(); } Figure 3.Representation of the clock state model using Objects for States. 129http://www.java re po rt.com JU N E 2 00 0 | Java™ Report Kevlin Henney / Patterns in Java LI STIN G 3. Implementation of the clock’s life cycle using a Stateless Object for the mode. public class Clock { public synchronized void changeMode() { mode.changeMode(this); } public synchronized void increment() { mode.increment(this); } public synchronized void cancel() { mode.cancel(this); } private interface Mode { void changeMode(Clock context); void increment(Clock context); void cancel(Clock context); } private static class DisplayingTime implements Mode ... private static abstract class SettingTime implements Mode ... private static class SettingHours extends SettingTime ... private static class SettingMinutes extends SettingTime { public void changeMode(Clock context) { context.mode = displayingTime; } public void increment(Clock context) { ++context.minutes; } } private int hours, minutes; private Mode mode = settingHours; private static final Mode displayingTime = new DisplayingTime(), settingHours = new SettingHours(), settingMinutes = new SettingMinutes(); }
  • 4. allows behavioral objects to see the encapsulated content of the context objects is to declare them as static top-lev- el classes in the context class. Alternatively, package-lev- el visibility or additional query and modifier methods must be provided. Such behavioral objects are stateless, so they may be treated as Flyweight4 objects that can be shared transpar- ently among all instances of the context class. The ab- sence of state also means that they are implicitly threadsafe, and therefore need no synchronization. Giv- en that only a single instance of a behavioral class is ever required, Singleton4 can be used to good effect when the definition of the behavioral class is outside the context class. Otherwise, Singleton is overkill where private static data would suffice. The entire selection of behavior is now expressed pure- ly through polymorphism and callbacks on a separate ob- ject, and the state now resides only in the context object. If the behavioral model is quite stable, the Visitor pattern may be used to put all the behavioral code back into the context object, with the behavioral mode object doing se- lection only and calling the relevant method on the con- text object to do all of the work. However, although this is sometimes an option, it can make the implementation un- wieldy for large state models: The context class suffers method explosion. Example. Implementing the clock class with a Stateless Ob- ject for the mode leads to Listing 3. Conclusion A common pattern for implementing object life cycle mod- els can be expressed in terms of other more granular pat- terns. They may be linked together in a language; i.e., the main pattern can be said to contain or be completed by the other patterns. In this article, the focus was on the repre- sentation of the Objects for States pattern in Java, as opposed to a more detailed view of the whole pattern.5 The Stateful Object and Stateless Object patterns assist in the realization of Objects For States, but are not tied exclu- sively to it: The issues raised and resolutions given can be found in many object systems whether local or distributed, (e.g., EJB). s References 1. Henney, K., “Patterns of value,” Java Report, Vol. 5, No. 2, Feb. 2000, pp. 64–68. 2.Henney, K., “Value added,” Java Report, Vol. 5, No. 4, Apr. 2000, pp. 74–82. 3. Rumbaugh, J., I. Jacobson, and G. Booch, The Unified Mod - eling Language Reference Manual, Addison–Wesley, 1999. 4. Gamma, E. et al., Design Patterns: Elements of Reusable Ob - ject-Oriented Software, Addison–Wesley, 1995. 5. Dyson, P. and Anderson, B. “State Patterns,” Pattern Lan - guages of Program Design 3, R. Martin, D. Riehle, and F. Buschmann, Eds., Addison–Wesley, 1998. 6.Fowler, M., Refactoring: Improving the Design of Existing Code, Addison–Wesley, 1999. 130 Java™ Report | JUN E 20 0 0 http://www.java re po rt.com Patterns in Java / Kevlin Henney