Java Idioms

(http://c2.com/ppr/wiki/JavaIdioms/html.zip -- fast download of all these pages.)

Architectural idioms: * ParameterClasses -- parameterise the behaviour of a Java program based on external data * InterfacesIndependentOfImplementation * AppletsAreApplicationsToo -- ensure an applet can run stand-alone, too * PackagePerLayer -- structuring m-tier applications * FallBackOnReflection

Memory management and reference/value semantics: * ResourceReleasesResource, which came from ReleasingResourcesInJava * ReturnNewObjectsFromAccessorMethods -- Avoid aliasing object state by returning copies of state data from accessors * ImmutableValue * ValueObjectsShouldBeImmutable * ReturnImmutablesFromAccessorMethods * CopyMutableParameters * ImmutableCollection -- concurrently walk over and modify a collection * ImmutableInterface * EviscerateParameters * CrossSection * ThrowawayObject * PassParameterInArray * PassParameterInWrapperObject * NoNullBeyondMethodScope * JavaImmutableStruct -- but also see DumbDataObject for a second opinion. --JeffBay

Object creation and initialisation idioms * TestWhetherInConstructionPhase * EncapsulateMultiStageConstruction * UseFactoriesToBuildObjects * HelperMethodForInitializations * ParameterFileNotHtmlParams * ParameterClasses * ComplexInterfacesNeedCloneable -- implement clone() when dealing with non-trivial interfaces * AlwaysDeclareSerialVersionUid * ImmutableValue * ArgumentAccumulator

Enumeration and collection idioms: * ReturnEnumerationsNotCollections * UseEnumerationsNotForLoops * UseClosuresNotEnumerations * ImmutableCollection * DuplicateCollectionsForModificationSafety * BlocksInJava * FlyweightEnum * EnumeratedTypesInJava * TheLclass

How to use interfaces: * BuildInterfaceImplementationPairs * MaskingInterfaces * InterfacesIndependentOfImplementation * ComplexInterfacesNeedCloneable -- implement clone() when dealing with non-trivial interfaces * InterfacesForDefiningConstants * UseClosuresNotEnumerations * ImmutableInterface -- emulate the C++ const keyword in Java * TagInterface -- empty interface to tag a class * AdaptDontImplement -- use of the Adaptor pattern instead of implementing interfaces * FallBackOnReflection

Exception idioms: * DontThrowGenericExceptions * ConvertExceptions ** HomogenizeExceptions *** RefineExceptions * RefactorExceptionHandlers * ThrowDontCatch * UnhandledException * BouncerPattern * ExceptionTunneling * NestedException * GeneralizeOnExceptionBehavior * DeclareThrowsExceptionByDefault * EmptyCatchClause (an AntiIdiom) * perhaps UseFinallyInsteadOfCatch? will come from JavaExceptionsAreParticularlyEvil

Concurrency idioms * ImmutableCollection * ImmutableValue * ImmutableInterface * ValueObjectsShouldBeImmutable * PrivateMutex

Type-safety idioms * EnumeratedTypesInJava * AvoidFinalStringsForUniqueTypes * MaskingInterfaces * BuildInterfaceImplementationPairs * RangedInt -- Range-limited integer in Java

Performance * CatchDontCheck (Rather an Antipattern as described on CatchDontCheckRefuted) * CheckDontCatch * BetterForLoopConstruct * FallBackOnReflection

Documentation and commenting * FixmeComment -- on the usage of comments for marking code that needs attention

InnerClasses * InnerClassesAreSecondaryAbstractions * NoPublicInnerClasses * InnerClassInInterface -- or NestedClassInInterface

Networking, Remote Objects & Remote Method Invokation * ObserversAndRmi * RmiPatterns

General workarounds, idioms, references and miscellanea: * BeansNotObserverObservable * ConditionalCompilationInJava * EfficientContractsInJava * ReturnBooleanEvaluations * UseObjectsToReturnMultipleValues * ClosuresThatWorkAroundFinalLimitation * LimitConstructorArguments * AbstractStaticVariables * CreatePrivatelyPublishLater * BankOfEngland * JavaPackageNames -- how to name a package * BlocksInJava -- HigherOrderFunctions and FunctorObjects? in Java * ObserverObservable (a pattern implementation, not an idiom) * IdiomOrPattern -- what's an idiom * JavaPatterns * ServletAndJspPatterns

AntiPatterns & AntiIdioms:

* EmptyCatchClause * DoubleCheckedLockingIsBroken * DontCreateGarbage

Related Work:

* "Java in Practice: Design Styles and Idioms for Effective Java", ISBN 0201360659 * "EssentialJavaStyle: Patterns for Implementation", ISBN 0130850861 * "EffectiveJava: Programming Language Guide", ISBN 0201310058

* "Patterns in Java, Volume 1", ISBN 0471258393 . http://www.mindspring.com/~mgrand/pattern_synopses.htm * "Patterns in Java, Volume 2", ISBN 0471258415 . http://www.mindspring.com/~mgrand/pattern_synopses2.htm * "Patterns in Java, Volume 3", ISBN 0471333158 . http://www.mindspring.com/~mgrand/pattern_synopses3.htm

Caveat: read the reviews on Amazon to check that the above two books are aimed at your level of experience. Also, you can find several of the patterns in these books, and the upcoming volume 3, on this page and elsewhere on the WikiWikiWeb.Volume 3 is out and claims to cover J2EE design patterns.

* "Concurrency : State Models & Java Programs", ISBN 0471987107
This book has a chapter on architectural styles for concurrent programs.

* "Concurrent Programming in Java: Design Principles and Patterns, ISBN 0201695812
The second edition of this book, which radically altered the chapter structure, is ISBN 0201310090

* "Refactoring : Improving the Design of Existing Code", ISBN 0201485672
Strictly more about good coding than idioms but very relevant


Please add any that you think of, and feel free to refactor the groupings. IdiomOrPattern ?
CategoryIndex CategoryJava
EditText of this page (last edited January 10, 2005)
FindPage by browsing or searching

This page mirrored in JavaIdioms as of October 22, 2005