rc166 010d ModularityPatterns - 0 PDF
rc166 010d ModularityPatterns - 0 PDF
JPA 2.0
n
JDBC Properties
n
Access Mode
n
Mappings
n
Shared Cache
n
Additional API and more... By Mike Keith
Getting Started with JPA 2.0
CONTENTS INCLUDE:
Module frameworks are gaining traction on the Java platform. Though The runtime model focuses on how to manage software systems at
modularity isn’t a new concept, it promises to change the way we runtime. A module system, such as OSGi, is required to take advantage of
develop software applications. You’ll only be able to realize the benefits the runtime model.
of modularity if you understand how to design more modular software
systems. The development model deals with how developers create modular
software. The development model can be broken down into two sub-
The modularity patterns lay the foundation necessary to incorporate categories. The programming model is how you interact with a module
modular design thinking into your development initiatives. No module framework to take advantage of the runtime benefits of modularity. The
framework is necessary to use these patterns, and you already have many design paradigm is the set of patterns you apply to design great modules.
of the tools you need to design modular software. This refcard provides a
quick reference to the 18 modularity patterns discussed in the book Java A module framework gives you runtime support and a programming
Application Architecture: Modularity Patterns with Examples Using OSGi. model for modularity. But a module framework won’t help you design
great software modules. The patterns in this refcard address the design
The modularity patterns are not specific to the Java platform. They can paradigm and help you design modular software.
be applied on any platform by treating the unit of release and deployment
as the module. Each pattern, except for base patterns, includes a diagram Module Defined
description, and implementation guidance. A software module is a deployable, manageable, natively reusable,
composable, stateless unit of software that provides a concise interface
Base Patterns: Fundamental modular design concepts upon which several to consumers. On the Java platform, a module is a JAR file, as depicted in
other patterns exist. the diagram. The patterns in this refcard help you design modular software
and realize the benefits of modularity.
Dependency Patterns: Used to help you manage dependencies between
modules.
Usability Patterns: Used to help you design modules that are easy to use.
Extensibility Patterns: Used to help you design flexible modules that you
can extend with new functionality.
Logical design is just one piece of the software design and architecture
challenge, however. The other is modular design, which focuses on
the physical entities and the relationships between them. Identifying
the entities containing your logical design constructs and managing
dependencies between the units of deployment are examples of modular
design. Without modular design, you may not realize the benefits you
expect from your logical design. The modularity patterns help you:
The base patterns are the fundamental elements upon which the other The dependency patterns focus on managing the relationships between
patterns exist. They establish the conscientious thought process that modules. They provide guidance on managing coupling that increase the
goes into designing systems with a modular architecture. They focus on likelihood of module reuse.
modules as the unit of reuse, dependency management, and cohesion.
Acyclic Relationships
Module relationships must be acyclic.
Manage Relationships
Design module relationships.
Description
A relationship between two modules exists when a class within one module
imports at least a single class within another module. In other words:
Hot If changing the contents of a module, M2, may impact the contents
of another module, M1, we can say that M1 has a physical
Tip dependency on M2.
Implementation Guidance
• Separate horizontal modules (those that span business domains)
from vertical modules (those specific to a business domain).
Cohesive Modules
Module behavior should serve a singular purpose.
Description
Cohesion is a measure of how closely related and focused the various
responsibilities of a module are. Modules that lack cohesion are more
difficult to maintain. Description
Levelization is similar to layering, but is a finer-grained way to manage
Implementation Guidance acyclic relationships between modules. With levelization, a single layer may
• Pay careful attention to how you allocate classes to their contain multiple module levels. To levelize modules, do the following:
respective modules.
Assign external modules level 0. Modules dependent only on level 0
• Classes changing at the same rate and typically reused together modules are assigned level 1. Modules dependent on level 1 are assigned
belong in the same module. level 2. Modules dependent on level n are assigned level n + 1.
• Classes changing at different rates and typically not reused
together belong in separate modules.
Physical Layers
Module relationships should not violate the conceptual layers.
Description
The less outgoing dependencies a module has, the easier the module
is to reuse. A module with no outgoing dependencies is independently
deployable and can be reused without the worry of identifying which
additional modules might be necessary. Lower-level modules inherently
have fewer outgoing dependencies and increase the opportunity for reuse.
Implementation Guidance
• Not all modules can be independently deployable. Some module
dependencies are always necessary.
Description
Layering a system helps ease maintenance and testability of the • In addition to reducing outgoing dependencies, container
application. Common layers include presentation (i.e., user interface), dependencies must also be minimized for those modules that are
domain (i.e., business), and data access. Any conceptually layered software independently deployable.
system can be broken down into modules that correspond to these
conceptual layers. Physical layers helps increase reusability because each • Highly cohesive modules are easier to make independently
layer is a deployable unit. deployable units.
Implementation Guidance
• Begin by creating a single coarse-grained module for each layer. USABILITY PATTERNS
• Enforce the layers using Levelize Build. We want modules that other developers find easy to interact with. The
usability patterns help design modules that are easy to understand and
• Break out each layer into more cohesive modules and use use.
Levelize Modules to understand and manage the relationships
within the layer. Published Interface
Make a module’s published interface well known.
• It’s fine if modules within a layer have relationships between
them. These modules will be at different levels.
Container Independence
Modules should be independent of the runtime container.
Description
Description
Heavyweight modules are dependent upon a specific runtime environment
Modules should encapsulate implementation details so that other modules
and are difficult to reuse across contexts. Environmental dependencies also
don’t need to understand the implementation to use the module. A
negatively affect your ability to test modules. Modules independent of the
module’s published interface exposes the capabilities you want to make
runtime container are more likely reused, and are more easily maintained
available to other developers.
and testable.
Implementation Guidance A published interface consists of the public methods within the
• Avoid importing container-dependent packages in your module’s
code. Hot public classes within the “exported” packages that other modules
are able to invoke.
External Configuration
Modules should be externally configurable.
Description
Module initialization typically requires configuring the module to its Description
environmental context. Externalizing the configuration decreases context Fine-grained and lightweight modules are inherently more reusable.
dependencies and allows you to use the module across a wider array of But fine-grained modules are also typically dependent on several other
environments. External configuration increases a module’s reuse, but modules. A Module Façade defines a higher level API that coordinates the
makes it more difficult to use because developers must understand how to work of a set of fine-grained modules. The façade emphasizes ease of use
configure the module. while the finer-grained modules emphasize reuse.
• Include a configuration file within the module that defines a • Place context and environmental dependencies in the façade.
default configuration, making the module easier to use.
• Use the façade as an entry point for your integration tests.
• Remain cognizant of the tradeoff between increased reuse
and decreased usability. In other words, maximizing reuse
EXTENSIBILITY PATTERNS
complicates use.
Default Implementation We want software that is easy to extend without modifying the existing
Provide modules with a default implementation. codebase. We also want to deploy this new functionality without affecting
other areas of the system. The extensibility patterns help us achieve this
goal.
Abstract Modules
Depend upon the abstract elements of a module.
Description
Depending on the abstract elements of a module gives you greater
opportunities to extend the system by defining new modules with classes
Description
that implement or extend the abstraction. Any clients of the module also
To maximize reuse, a module must be flexible enough so that it can
have the ability to define their own implementations and to plug them into
function in a variety of different operating environments. Yet, making a
the module.
module easier to use leads us to incorporate more functionality into a
module so developers are required to do less when using the module. A
Implementation Guidance
default implementation with well-defined extension points helps address
• Use an Implementation Factory to create a module’s underlying
this tension.
implementation.
Implementation Guidance
• When defining a Default Implementation, depend upon the • Use Abstract Modules when you have many incoming module
abstract elements of a module (see Abstract Modules or Separate dependencies and you want the flexibility to swap out underlying
Abstractions). implementations.
• Include a default configuration in the module, but make the • Strive to make the abstraction within a module as stable as
module externally configurable, as well. possible. That is, avoid changes since it will have many many
other modules that are dependent upon it.
• Always create a Test Module to test the default implementation.
Implementation Factory
Use factories to create a module’s implementation classes. Hot Keep the abstraction closer to the classes that depend upon it and
further from the classes that extend or implement it.
Tip
Implementation Guidance
UTILITY PATTERNS
Description
Any module whose classes depend upon the abstract elements of another The utility patterns are additional tools and techniques that aid modular
module should avoid referencing any implementation classes. Doing so development. They help you enforce your modular design and ensure
will compromise your module design. Consider the following rule. quality.
Implementation Guidance
Levelize Build
Execute the build in accordance with module levelization.
Description
Separating abstractions from their implementation offers the greatest
flexibility to provide new implementations that completely replace existing
implementations. With Separate Abstractions, you can define new behavior
and plug it into your system without affecting existing system modules.
Separate Abstractions can be used to develop a plug-in architecture. As a
general guideline, apply the following rule.
Description Description
Enforcing module relationships is difficult. Though conceptually you may Test modules contain all of the tests for the classes in a specific module.
believe you have an acyclic module structure and fully comprehend the They allow you to test a module’s underlying implementation. A Test
module relationships, a single build target with everything on the classpath Module may contain unit tests that test a module’s classes, as well as
allows undesirable cycles and dependencies to creep in. A levelized build integration tests that test the entire module’s functionality.
helps you enforce your module dependencies. Any dependencies that
violate your defined module structure will result in a build failure. Implementation Guidance
• Depending on Abstract Modules makes it easier to define mocks
Implementation Guidance
and stubs for testing a module independently.
• Avoid a full classpath build, where all classes are built using a
single compile target.
• For larger test suites, or situations where performance is
paramount, separate different types of tests (i.e., unit tests,
• Define separate build targets for modules in different levels. Level
integration tests, performance tests, etc.) out into separate test
1 modules can be built with only external level 0 modules. At
modules.
higher levels, include only the modules from lower levels that are
required for a successful build.
• Ideally, you’ll only include the test module and the module under
test in the classpath when executing the tests. Pragmatically,
• Defining new module dependencies will require modifying the
some modules may require other dependent modules.
build for that module. This is not necessarily undesirable.
Test Module
Each module should have a corresponding test module.
DZone, Inc.
150 Preston Executive Dr.
Suite 201
Cary, NC 27513
DZone communities deliver over 6 million pages each month to 888.678.0399
more than 3.3 million software developers, architects and decision 919.678.0300
makers. DZone offers something for everyone, including news,
Refcardz Feedback Welcome
$7.95
tutorials, cheat sheets, blogs, feature articles, source code and more.
“"DZone is a developer's dream",” says PC Magazine. [email protected]
Copyright © 2012 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval Sponsorship Opportunities
system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior
[email protected]
Version 1.0
written permission of the publisher.