<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Modular Architecture &#187; Part 2</title>
	<atom:link href="http://www.kirkk.com/modularity/category/part-2/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.kirkk.com/modularity</link>
	<description>Patterns of Modular Architecture</description>
	<lastBuildDate>Tue, 07 Sep 2010 21:09:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>External Configuration</title>
		<link>http://www.kirkk.com/modularity/2009/12/external-configuration/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/external-configuration/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 20:22:54 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Part 2]]></category>
		<category><![CDATA[Usability Pattern]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=378</guid>
		<description><![CDATA[Statement
Modules should be externally configurable.
Description
The ability to configure a module to it&#8217;s usage context increases our ability to reuse the module across contexts, whereas tightly coupling configuration to the module prohibits reuse. External configuration allows a module to be reconfigured across environemtns, and even within it&#8217;s existing environment, without demanding redeploying  the module. Figure 1 [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Modules should be externally configurable.</p>
<h2>Description</h2>
<p>The ability to configure a module to it&#8217;s usage context increases our ability to reuse the module across contexts, whereas tightly coupling configuration to the module prohibits reuse. External configuration allows a module to be reconfigured across environemtns, and even within it&#8217;s existing environment, without demanding redeploying  the module. Figure 1 illustrates External Configuration, where an XML configuration file is used by the Client class to configure the client.jar module.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-381" style="border: 0pt none;" title="ExternalConfiguration" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/ExternalConfiguration.jpg" alt="ExternalConfiguration" width="557" height="99" /></p>
<p style="text-align: center;"><strong>Figure 1: External Configuration</strong></p>
<h2>Implementation Variations</h2>
<p>Different configuration files can be used for different contexts. The configuration file can be included in the module or included in a separate module, depending on the flexibility desired. Figure 2 illustrates a configuration file that is not deployed with the module. A flexible alternative is to provide a default configuration file with the module, but allow for the module to be configured with an alternative configuration file. Including a default configuration file within the module is one aspect of providing a module&#8217;s Default Implementation.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-386" style="border: 0pt none;" title="ExternalConfiguration2" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/ExternalConfiguration2.jpg" alt="ExternalConfiguration2" width="557" height="177" /></p>
<p style="text-align: center;"><strong>Figure 2: External Configuration File</strong></p>
<h2>Consequences</h2>
<h2>Sample Code</h2>
<p>Show example using Spring to configure a module with a uid/pwd combination.</p>
<p>For instance, many applications must provide credentials to connect to a database. Web applications often use a standard userid and password combination so that a pool of connections are available to clients. If a module encpasulates this information</p>
<h2>Wrapping Up</h2>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/external-configuration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Module Facade</title>
		<link>http://www.kirkk.com/modularity/2009/12/module-facade/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/module-facade/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 20:08:52 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Part 2]]></category>
		<category><![CDATA[Usability Pattern]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=373</guid>
		<description><![CDATA[Statement
Create a facade serving as a coarse-grained entry point to the modules underlying implementation.
Description
Often times, configuring fine-grained modules can be a burden, especially when multiple fine-grained modules are typically used in conjunction but forces prevent module designers from combining the fine-grained module into a single coarse-grained entity. In these situations, a module facade can be [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Create a facade serving as a coarse-grained entry point to the modules underlying implementation.</p>
<h2>Description</h2>
<p>Often times, configuring fine-grained modules can be a burden, especially when multiple fine-grained modules are typically used in conjunction but forces prevent module designers from combining the fine-grained module into a single coarse-grained entity. In these situations, a module facade can be used to expose a subset of the functionality contained within a group of fine-grained modules. A module facade allows a group of fine-grained modules to operate as a coarse-grained module. Figure 1 illustrates a Module Facade.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-374" style="border: 0pt none;" title="ModuleFacade" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/ModuleFacade.jpg" alt="ModuleFacade" width="699" height="572" /></p>
<p style="text-align: center;"><strong>Figure 1: Module Facade</strong></p>
<h2>Implementation Variations</h2>
<p>The module facade can configure the fine-grained modules.</p>
<p>Module facade can also act as a mediator to help break dependencies between modules. This is quite similar to escalation.</p>
<h2>Consequences</h2>
<p>All fine-grained modules must be deployed whenever the coarse-grained module is deployed.</p>
<h2>Sample Code</h2>
<h2>Wrapping Up</h2>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/module-facade/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Colocate Exceptions</title>
		<link>http://www.kirkk.com/modularity/2009/12/colocate-exceptions/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/colocate-exceptions/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 19:51:32 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Dependency Pattern]]></category>
		<category><![CDATA[Part 2]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=367</guid>
		<description><![CDATA[Statement
Exceptions should be close to the class or interface that throw them.
Description
Sadly, dealing with exceptions in enterprise software systems is often an afterthought. But allocation of exceptions to modules has significant implications on the modularity, and more specifically the dependencies between modules, within our software system. Placing the exception close to a class that catches [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Exceptions should be close to the class or interface that throw them.</p>
<h2>Description</h2>
<p>Sadly, dealing with exceptions in enterprise software systems is often an afterthought. But allocation of exceptions to modules has significant implications on the modularity, and more specifically the dependencies between modules, within our software system. Placing the exception close to a class that catches it will often cause a cyclic dependency between the module containing the class that throws the exception and the module containing the class that catches the exception. Exceptions should always be placed in a module closer to the class that throws the exception than a module closer to the class that catches the exception. Figure 1 illustrates this scenario.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-369" style="border: 0pt none;" title="ColocateExceptions" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/ColocateExceptions.jpg" alt="ColocateExceptions" width="557" height="225" /></p>
<p style="text-align: center;"><strong>Figure 1: Colocate Exceptions</strong></p>
<p style="text-align: center;">
<h2>Implementation Variations</h2>
<p>Whereas separate abstractions says that interfaces should be close to the classes that use them, exceptions should be close to the class that throws them.</p>
<p>If multiple classes from separate modules throw the same exception, the demotion should be used to manage the dependencies between the two modules.</p>
<p>Similar to separate abstractions &#8211; that is where we place the interface.</p>
<h2>Consequences</h2>
<h2>Sample Code</h2>
<h2>Wrapping Up</h2>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/colocate-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Container Independence</title>
		<link>http://www.kirkk.com/modularity/2009/12/container-independence/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/container-independence/#comments</comments>
		<pubDate>Mon, 28 Dec 2009 19:28:50 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Dependency Pattern]]></category>
		<category><![CDATA[Part 2]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=355</guid>
		<description><![CDATA[Statement
Modules should be independent of the runtime container.
Description
Modules with excessive runtime container dependencies are heavyweight modules that cannot execute outside the confines of the runtime container. A good example of a heavyweight technology is Enterprise JavaBeans (EJB), and the meteoric rise in popularity of lighter weight frameworks, such as Spring, are the direct result of [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Modules should be independent of the runtime container.</p>
<h2>Description</h2>
<p>Modules with excessive runtime container dependencies are heavyweight modules that cannot execute outside the confines of the runtime container. A good example of a heavyweight technology is Enterprise JavaBeans (EJB), and the meteoric rise in popularity of lighter weight frameworks, such as Spring, are the direct result of the shortcomings of heavyweight technologies. While lightweight modules are not container dependent, they are still able to leverage the infrastructure capabilities (e.g., security, transactions) of the container. Lightweight modules with no container dependencies have two significant advantages.</p>
<ul>
<li>They are portable across runtime environments. For instance, a lightweight module can be deployed in a Java EE environment, but it can also run directly atop the JVM. Or, a module might be designed to work in a runtime supporting OSGi, but it can also function if an OSGi framework isn&#8217;t available.</li>
<li>The are testable. Because they lack environmental dependencies, they can be tested in isolation, outside the confines of the environment to which they&#8217;ll ultimately be deployed.</li>
</ul>
<p>Additionally, it&#8217;s easy to argue that lighter weight modules are also easier to maintain, since their contents aren&#8217;t polluted with excessive dependencies. Developing light weight modules demands that container dependencies be abstracted away. Figure 1 illustrates how the client.jar module is made independent of the runtime.jar through the use of the abstraction.jar module, which encapsulates the container dependencies and coordinates the interaction of client.jar module in the runtime environment. As a result, the Client class will have no dependencies on the runtime.jar nor the abstraction.jar modules&#8217; APIs.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-383" style="border: 0pt none;" title="ContainerIndependence" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/ContainerIndependence.jpg" alt="ContainerIndependence" width="557" height="225" /></p>
<p style="text-align: center;"><strong>Figure 1: Container Independence</strong></p>
<h2>Implementation Variations</h2>
<p>External configuration can be used to configure a module so that it operates correctly in a specific runtime environment. In these situations, External Configuration is a pattern that is used to achieve Container Independence since the configuration dependencies are moved to an external configuration file. A superceding framework then manages module configuration. However, there are subtle differences between External Configuration and Container Independence that validates pattern individuality. External configuration is used to configure a module so that it can be used across contexts. Container Independence is leveraged to ensure a module is portable across containers.</p>
<p>For instance, configuring a module with a userid and password combination so that it can be used to access different database instances is an example of external configuration, not container independence. While poor practice, hardcoding the userid and password combination wouldn&#8217;t create dependencies on any runtime container, though obviously would limit the context in which the module could be reuse.</p>
<p>In many cases, developers don&#8217;t need to roll their own code to abstract away the container dependencies. Instead, a framework is used. The most common mechanism to achieve container independence is through dependency injection, where a module&#8217;s dependencies are injected at runtime. Many dependency injection frameworks also provide important infrastructure capabilities that help ensure the module remains container independent. For instance, in the example below that uses Spring DM, the framework provides dependency injection capabilities but also provides critical functionality surrounding OSGi service management.</p>
<h2>Consequences</h2>
<p>Container Independence significantly reduces the amount of infrastructure code a developers needs to write. While this provides significant benefits that enable testing and increases portability, it can also result in complex infrastructure code that is complex to write and manage. When a dependency injection framework is used, xml configuration files tend to proliferate. Fortunately, the tradeoff is often worth the benefit.</p>
<h2>Sample Code</h2>
<p>Let&#8217;s consider two very simple modules &#8211; a client and a service, as illustrated in Figure 1. Also shown is a configuration file that will be used to help us realize container independence.</p>
<p style="text-align: center;"><img class="aligncenter size-large wp-image-623" style="border: 0pt none;" title="ContainerIndependenceSample" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/ContainerIndependenceSample-1024x566.jpg" alt="ContainerIndependenceSample" width="574" height="317" /></p>
<p style="text-align: center;"><strong>Figure 1 &#8211; Simple Client Module Using a Service Module</strong></p>
<p>Listing 1 illustrates the start method of a class in the service module that is registered as an OSGi service, enabling other classes to discover the class when requesting the service. As can be seen, the code is heavily dependent on the OSGi framework packages, and this prohibits the code from being used outside the context of an OSGi runtime.</p>
<pre class="brush: java;">
import java.util.Properties;
import com.extensiblejava.hello.service.HelloService;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceEvent;
import org.osgi.framework.ServiceRegistration;

public class HelloServiceImpl implements HelloService, BundleActivator {

	private ServiceRegistration registration;

	public void start(BundleContext context) {
		Properties props = new Properties();
		props.put(&quot;Language&quot;, &quot;English&quot;);
		registration = context.registerService(HelloService.class.getName(), this, props);
        }
        public String sayHello() {
		return &quot;Hello World!! &quot;;
	}

	public String sayGoodbye() {
		return &quot;Goodbye World!!&quot;;
	}
}
</pre>
<p style="text-align: center;"><strong>Listing 1: Heavy Container Dependencies</strong></p>
<p>Any client that hopes to use this class that has been registered with the OSGi service registry also needs to leverage the OSGi framework. Listing 2 illustrates a sample client in the client module that interacts with OSGi to obtain a reference to the service registered in Listing 1.</p>
<pre class="brush: java;">
package com.extensiblejava.hello.client;

import com.extensiblejava.hello.service.HelloService;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

public class HelloConsumer implements BundleActivator {

	private ServiceTracker helloWorldTracker;
	private HelloService helloService;

	public void setService(HelloService helloService) {
		this.helloService = helloService;
	}

	public void removeService() {
		this.helloService = null;
	}

	public void start(BundleContext context) throws Exception {
		helloWorldTracker = new ServiceTracker(context, HelloService.class.getName(), null);
		helloWorldTracker.open();
		HelloService hello = (HelloService) helloWorldTracker.getService();

		if (hello == null) {
			System.out.println(&quot;Hello service unavailable on HelloConsumer start&quot;);
		} else {
			System.out.println(hello.sayHello());
		}
	}

	public void stop(BundleContext context) {
		HelloService hello = (HelloService) helloWorldTracker.getService();
		if (hello == null) {
			System.out.println(&quot;Hello service unavailable on HelloConsumer stop&quot;);
		} else {
			System.out.println(hello.sayGoodbye());
		}

		helloWorldTracker.close();
    }

}
</pre>
<p style="text-align: center;"><strong>Listing 2: Heavyweight Client</strong></p>
<p>The heavyweight nature of this code prevents it from being used outside the context of the OSGi framework, and also prohibits unit testing. Keeping our exact same module structure as illustrated in Figure 1, we leverage Spring DM to remove all dependencies on the OSGi framework. Listings 3 and 4 show the updated code that leverages Spring DM, illustrating all OSGi dependencies have been removed. To achieve this, we&#8217;ve needed to deploy the Spring DM modules to the OSGi runtime, and have also introduced the appropriate configuration file that informs Spring DM how to treat these modules. The configuration file is shown in Listing 5.</p>
<pre class="brush: java;">
package com.extensiblejava.hello.service.impl;

import java.util.Properties;
import com.extensiblejava.hello.service.HelloService;

public class HelloServiceImpl implements HelloService {

	public String sayHello() {
		return &quot;Hello OSGi Spring World!! &quot;;
	}

	public String sayGoodbye() {
		return &quot;Goodbye OSGi Spring World!!&quot;;
	}
}
</pre>
<p style="text-align: center;"><strong>Listing 3: Independent HelloServiceImpl Class</strong></p>
<pre class="brush: java;">
package com.extensiblejava.hello.client;

import com.extensiblejava.hello.service.HelloService;

public class HelloConsumer {
	private HelloService hello;

	public void setHelloService(HelloService hello) {
		this.hello = hello;
	}

	public void start() throws Exception {
       System.out.println(hello.sayHello());
	}

	public void stop() throws Exception {
		System.out.println(hello.sayGoodbye());
    }

}
</pre>
<p style="text-align: center;"><strong>Listing 4: Independent Consumer Class</strong></p>
<pre class="brush: xml;">
&lt;osgi:reference id=&quot;helloService&quot; interface=&quot;com.extensiblejava.hello.service.HelloService&quot;/&gt;

&lt;bean name=&quot;helloConsumer&quot; class=&quot;com.extensiblejava.hello.client.HelloConsumer&quot;
		init-method=&quot;start&quot; destroy-method=&quot;stop&quot;&gt;
  		&lt;property name=&quot;helloService&quot; ref=&quot;helloService&quot;/&gt;
&lt;/bean&gt;
</pre>
<p style="text-align: center;"><strong>Listing 5: Spring XML Configuration File</strong></p>
<h2>Wrapping Up</h2>
<p>Container dependencies result in heavyweight modules that are difficult to reuse and maintain. Eliminating these dependencies helps improve the ease with which modules can be tested. Modules with no container dependencies are also portable across runtime environments and can be used in a variety of different contexts. Dependency injection is a common technique used to help ensure modules aren&#8217;t container dependent.</p>
<p><span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: small;"><span style="line-height: 19px; white-space: normal;"> </span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/container-independence/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Levelize Modules</title>
		<link>http://www.kirkk.com/modularity/2009/12/levelize-modules/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/levelize-modules/#comments</comments>
		<pubDate>Sun, 27 Dec 2009 18:10:25 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Dependency Pattern]]></category>
		<category><![CDATA[Part 2]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=218</guid>
		<description><![CDATA[Statement
Module relationships should be levelized.
Description
Levelized modules demands that module relationships be acyclic. Any cycles in module relationships therefore prevents levelization. There is a close relationship between levelized modules and physical layers, though the two are not the same. Physical layers aims to create one or more modules that are functionally equivalent to the typical layers [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Module relationships should be levelized.</p>
<h2>Description</h2>
<p>Levelized modules demands that module relationships be acyclic. Any cycles in module relationships therefore prevents levelization. There is a close relationship between levelized modules and physical layers, though the two are not the same. Physical layers aims to create one or more modules that are functionally equivalent to the typical layers composing an application. Levelized modules are more fine-grained than physical layers. While a typical software system may have only three or four layers, within each layer might exist multiple levels.</p>
<p>To levelize modules relationships, do the following:</p>
<ul>
<li>External modules are assigned level zero.</li>
<li>Modules dependent only on level zero modules are assigned level one.</li>
<li>Modules dependent on level one are assigned level two.</li>
<li>Modules dependent on level <em>n</em> are assigned level <em>n + 1</em>.</li>
</ul>
<p>Figure 1 shows levelized modules. Even though the topclient.jar module is dependent on client.jar, a Level 1 module, topclient.jar is also dependent on the midlevel.jar module, which is a Level 2 module. Because of this, topclient.jar module is a Level 3 module. More generally, if a module is dependent on an <em>n &#8211; 1</em> leve module, the module is an <em>n</em> level module, even if it&#8217;s also dependent on modules lower in the level hierarchy.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-343" style="border: 0pt none;" title="LevelizedModules" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/LevelizedModules.jpg" alt="LevelizedModules" width="468" height="468" /></p>
<p style="text-align: center;">
<p style="text-align: center;"><strong>Figure 1: Levelized Modules</strong></p>
<h2>Implementation Variations</h2>
<p>Levels and <a href="http://www.kirkk.com/modularity/2009/12/physical-layers/">layers</a>, what&#8217;s the difference? Layers have much more do with responsibility, whereas levels have much more to do with understanding the system structure, especially the dependencies. Levels are more granular than layers. Within a single layer might exist multiple levels, but multiple layers can never exist at a single level.</p>
<p>The Levelized Modules pattern sits squarely between Physical Layers and Acyclic Relationships (not just in the book, but in concept, as well). Levelized Modules demands that the module structure be acyclic. If it weren&#8217;t, the cycle would make it impossible to clearly identify the levels. And as we discussed above, levels tend to be a bit more granular than layers.</p>
<p>In fact, there aren&#8217;t many implementation variations of Levelized Modules, which brings into question it&#8217;s value as a pattern to begin with. But I&#8217;ve decided to include it because it can be valuable to levelize modules as it provides an extra mechanism to more easily understand module relationships within the conceptual layers. Either a system&#8217;s modules are levelized, or they aren&#8217;t! But if the modules are levelized, than it&#8217;s a guarantee that the modules also exhibit acyclic relationships, though levelization does not guarantee layers because, again, levelization emphasizes dependencies between modules and layers emphasizes responsibilities. There are a few important things to think about when levelizing modules, though.</p>
<p>Coarse grained modules at lower levels of the system have a significant impact on the ability to easily reuse these modules. In general, lower level modules should have fewer outgoing dependencies and more incoming dependencies. In other words, modules in lower level layers should be lower level modules, and these modules should be finer grained and lighter weight than higher level modules. Likewise, modules in higher level layers will likely be higher level modules, though it&#8217;s perfectly acceptable that a Level 1 module exist in a higher level layer. For instance, a UI Utility module may have no outgoing dependencies, and therefore be a Level 1 module, but exist in the UI layer because of it&#8217;s behavior. At this point, it&#8217;s easy to question why this is valuable. We&#8217;ll see shortly!</p>
<p>An especially important piece of criteria to consider when levelizing modules is whether you&#8217;ll allow module levels to span layers. The decision to enforce a strict levelization scheme over allowing a relaxed scheme will have a significant architectural impact. In theory, a strict scheme might seem better, but in practice a relaxed scheme is easier. A mixture of the two is likely most pragmatic, and it must be a conscious decision each time you decide to allow a levelized module to span multiple layers.</p>
<h2>Consequences</h2>
<p>Levelization can provide insight to the complexity of dependencies within various layers. For instance, a level 5 module found in the data access layer indicates a rather complex set of dependencies for a module that is very likely used heavily throughout the system. Excessive dependencies in lower level modules have the greatest capacity to increase overall cost of maintaining a system. A good candidate for refactoring, perhaps!</p>
<p>Levelized Modules also offer the opportunity to perform a <a href="http://www.kirkk.com/modularity/2009/12/levelized-build/">Levelized Build</a>. If a team cannot easily identify the levels within the system, the only opportunity to perform a Levelized Build exists across the Physical Layers, which may not offer the flexibility necessary. For very large and complex systems, a Levelized Build might be necessary to keep build time fast, which is especially important for teams practicing continuous integration.</p>
<p>Levelization also help improve the module testability. Test Modules can be created for each module, and the levelization will provide guidance on the module test strategy. Lower level modules are inherently easier to test that higher level modules because they lack the complex dependency structure. For instance, modules in the UI layer can be perceived as more difficult to test independently because we have to create mocks and stubs for all of their dependencies. But a Level 1 module in the UI layer indicates no outgoing dependencies, and therefore provides insight to how easy the module will be to bring under test.</p>
<p>Levelization improves ability to understand the system structure. The team has the ability to extract and understand the module relationships in a large software system, allowing us to efficiently and accurately verify throughout the development lifecycle that these dependencies are consistent with our overall architectural vision. Levelization also provides insight to how reusable modules are. For example, reusing a level 3 module comes with the implicit knowledge that one or more level 2 and one or more level 1 modules must also be included in the application.</p>
<p>Levelization can serve as the foundation for an enterprise reuse strategy. Look at it this way. If modules are levelized, lower level modules are inherently easier to reuse because they have no outgoing dependencies. Because they have no outgoing dependencies, we can build these modules independent of the rest of the system. This separate build means that the module can evolve independently. Development of the module can exist outside the context of the application, in parallel, and each time the module is built, it can be included as part of the application (picture would really help here illustrating what I&#8217;m talking about &#8211; separate IDE project, VC project, build project, binary pulled into IDE, not source).</p>
<h2>Sample</h2>
<p>As mentioned, levelizing modules is slightly different than physical layers. Figure 2 illustrates this difference. As can be seen here, the levels don&#8217;t necessarily coincide with layers. At least, a single layer isn&#8217;t necessarily confined to contain a specific set of levels. The billpayutils.jar module is in the control layer based on the set of responsibilities it provides. However, it&#8217;s lack of dependence on any level 3 or level 4 modules in the domain layer Even though the billpayutils.jar module is in the control layer make it a level 2 component because it&#8217;s only dependency is on the level 1 dbutils.jar module.</p>
<p>Because the modules are levelized, we now have the opportunity to perform a levelized build, and we can more clearly understand build order. The Level 1 dbutils.jar module must be built first. Once finished, possibly as part of a completely separate build, the the billdata.jar and findata.jar modules can be built. Yet so too can the billpayutils.jar module, even though it&#8217;s in the Control Layer. Unless we understand module levelization, the opportunity to maximize the efficiency of the build may be lost. So too might we lose the opportunity to realize many of the other advantages of levelization.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-572" style="border: 0pt none;" title="LevelizedWithLayers" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/LevelizedWithLayers1.jpg" alt="LevelizedWithLayers" width="703" height="640" /></p>
<p style="text-align: center;">
<p style="text-align: center;">
<p style="text-align: center;"><strong>Figure 2: Levelized Modules and Physical Layers</strong></p>
<h2>Wrapping Up</h2>
<p>Levelize Modules sits squarely between Acyclic Modules and Physical Layers. If the module structure is acyclic, then module relationships can be levelized. Levelization helps understand the dependency structure of the application, and it&#8217;s benefits include build optimization, reuse, ease of testing, and a clear understanding of system structure.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/levelize-modules/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Cohesive Modules</title>
		<link>http://www.kirkk.com/modularity/2009/12/cohesive-modules/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/cohesive-modules/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 02:11:24 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Base Pattern]]></category>
		<category><![CDATA[Part 2]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=310</guid>
		<description><![CDATA[Statement
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. In the worst case scenario, little emphasis is placed on the allocation of classes to modules. Instead, modules are assembled at random, and the likelihood that modules suffer from lack of cohesion [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Module behavior should serve a singular purpose.</p>
<h2>Description</h2>
<p>Cohesion is a measure of how closely related and focused the various responsibilities of a module are. In the worst case scenario, little emphasis is placed on the allocation of classes to modules. Instead, modules are assembled at random, and the likelihood that modules suffer from lack of cohesion is high. In the best case scenario, modules exhibit high degrees of functional cohesion and the entities composing the module each work in conjunction to fulfill module behavior.</p>
<p>In general, cohesion is a qualitative measurement that is difficult to measure. Instead, we typically refer to a software module as either possessing high cohesion or low cohesion. Modules with higher degrees of cohesion are preferred. There are key difficulties that arise when modules suffer from low cohesion, including the inability to understand the software system and difficulty maintaining the software system.</p>
<p>Reusing modules that lack cohesion is also difficult. Module consumers rarely need all of the random behaviors provided by modules lacking cohesion, and the random behaviors often increase dependencies on other modules. In general, modules that lack cohesion degrade the overall quality of a software system&#8217;s architecture.</p>
<h2>Implementation Variations</h2>
<p>There are two key elements that affect module cohesion. These follow:</p>
<ul>
<li>The rate at which the software entities within a module change.</li>
<li>The likelihood that the software entities within a module are reused together.</li>
</ul>
<p>Based on these statements, it&#8217;s easy to draw the following conclusion:</p>
<ul>
<li>Classes which change at different rates belong in separate modules.</li>
<li>Classes that change at the same rate belong in the same module.</li>
<li>Classes not reused together belong in separate modules.</li>
<li>Classes reused together belong in the same module.</li>
</ul>
<p>Unfortunately, this simple logic is flawed because it doesn&#8217;t consider the intricate relation between rate of change, reuse, and the natural shifts that occur throughout the development lifecycle. It&#8217;s possible, even likely, that classes change at different rates but are reused together, or that classes change at the same rate but are rarely reused together. Indeed, there is a tension between rate of change and reuse, and we must consider the following combinations.</p>
<ul>
<li>Classes within a module that change at the same rate and are reused together.</li>
<li>Classes within a module that change at a different rate and are reused together.</li>
<li>Classes within a module that change at the same rate and are not reused together.</li>
<li>Classes within a module that change at a different rate and are not reused together.</li>
</ul>
<p>The first and fourth scenarios are the easiest to accommodate. When classes change at the same rate and are reused together, it&#8217;s logical that they belong in the same module. Likewise, classes that change at different rates and are not reused together belong in separate modules. The final two scenarios, which tend to be most common, are also the most challenging.</p>
<p>Fortunately, the natural shifts that occur throughout the development lifecycle provide insight to dealing with the second and third scenarios, and encourage us to emphasize one of these aspects more than the other. Early in the development lifecycle, when the system is volatile and change is rampant, packaging the classes based on rate of change should supersede packaging based on reuse. As the system stabilizes, we should focus on packaging the classes based on reuse since the rate of change is much less.</p>
<h2>Consequences</h2>
<p>Early in the development lifecycle, when change is rampant, we are encouraged to package classes into more coarse-grained module. This is logical. Because change is rampant and widespread, packaging into coarse-grained modules means less module management as change occurs. We don&#8217;t have to worry as much about managing dependencies between modules. Unfortunately, this can lead to dire consequences if not dealt with on an ongoing basis. Failure to refactor the modules as the system stabilizes results in a software system composed of coarse-grained modules.</p>
<p>The ramifications of coarse-grained modules are discussed in Chapter 6, Section 6.1. Coarse-grained modules make the modules easier to use, but the increased ease of use comes at the price of reusability. It&#8217;s vital to recognize that shifts will occur throughout development As the rate of change lessens, it&#8217;s imperative to turn our attention toward the cohesive properties of the modules that affect reuse.</p>
<h2>Sample</h2>
<p>Figure 1 illustrates the initial version of our system, where Bill instances are routed to an appropriate place so they can be handled (ie., rejected or paid). Unfortunately, the bill.jar module lacks cohesion, since the Bill and Router instances are each bundled and deployed in that module. The lack of cohesion affects reuse, as we cannot reuse Bill without the Router nor reuse the Router to handle other types of entites that might require routing.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-535" style="border: 0pt none;" title="CohesiveModulesv1" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/CohesiveModulesv12.jpg" alt="CohesiveModulesv1" width="582" height="173" /></p>
<p style="text-align: center;"><strong>Figure 1: Module Lacking Cohesion</strong></p>
<p style="text-align: left;">The code for the Bill class is shown in Listing 1. As can be seen, a Router is passed to the Bill constructor, which is used when the Bill&#8217;s route method is called. The Router is an abstract class with an abstract route method on it. The TypeARouter shown in Figure 1 extends the Router and provides an implementation for the route method.</p>
<pre class="brush: java;">package com.extensiblejava.bill;

import java.math.*;
import com.extensiblejava.route.*;

public class Bill {
	private String type;
	private String location;
	private BigDecimal amount;
	private Router router;

	public Bill(String type, String location, BigDecimal amount, Router router) {
		this.type = type;
		this.location = location;
		this.amount = amount;
		this.router = router;
	}
	public String getType() {return this.type;}
	public String getLocation() {return this.location;}
	public BigDecimal getAmount() {return this.amount;}
	public String route() { return this.router.route(this); }
}
}</pre>
<p style="text-align: center;"><strong>Listing 1: The Bill Class</strong></p>
<p style="text-align: left; "><strong></strong>To increase cohesion of the bill.jar module, routing functionality can be moved to it&#8217;s own module, as shown in Figure 2.</p>
<p style="text-align: center; "><img class="aligncenter size-full wp-image-538" style="border: 0pt none;" title="CohesiveModulesv2" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/CohesiveModulesv2.jpg" alt="CohesiveModulesv2" width="530" height="191" /><strong></strong></p>
<p style="text-align: center; "><strong>Figure 2: Cohesive Bill and Route Modules</strong></p>
<p>The only change required to bundle the functionality separately was to modify the build script. Previously, the build bundled all classes into a single module. The modified script, shown in Listing 2 with the lines responsible for creating the bundles highlight, illustrates how the functionality is allocated to the separate bundles.</p>
<pre class="brush: xml; highlight: [2,3,4];">
&lt;target name=&quot;dist&quot; depends=&quot;compile&quot;&gt;
   &lt;jar jarfile=&quot;${dist}/bill.jar&quot; basedir=&quot;${bin}&quot; includes=&quot;com/extensiblejava/bill/**&quot;/&gt;
   &lt;jar jarfile=&quot;${dist}/route.jar&quot; basedir=&quot;${bin}&quot; includes=&quot;com/extensiblejava/route/**&quot;/&gt;
   &lt;jar jarfile=&quot;${dist}/bill-test.jar&quot; basedir=&quot;${bin}&quot; includes=&quot;com/extensiblejava/test/**&quot;/&gt;
   &lt;junit printsummary=&quot;yes&quot; haltonfailure=&quot;yes&quot;&gt;
      &lt;classpath&gt;
         &lt;pathelement path=&quot;${dist}/route.jar&quot;/&gt;
         &lt;pathelement path=&quot;${dist}/bill.jar&quot;/&gt;
         &lt;pathelement path=&quot;${dist}/bill-test.jar&quot;/&gt;
         &lt;pathelement path=&quot;${lib}/junit.jar&quot;/&gt;
      &lt;/classpath&gt;
      &lt;test name=&quot;com.extensiblejava.test.RouterTest&quot; outfile=&quot;junitresults&quot;&gt;
         &lt;formatter type=&quot;plain&quot;/&gt;
      &lt;/test&gt;
   &lt;/junit&gt;
&lt;/target&gt;
</pre>
<p style="text-align: center;"><strong>Listing 2: Modified Build Script</strong></p>
<p style="text-align: left;">Unfortunately, there is still a glaring problem with the module structure illustrated in Figure 2. A cyclic dependency exists between the bill.jar and route.jar modules. Applying the AcyclicRelationships pattern will help eliminate the cyclic dependency. When applying the AcyclicRelationships pattern to break the cycle between the bill.jar and route.jar modules, we also applied the SeparateAbstractions pattern. Otherwise, the cycle would have persisted. Upon doing so, the final module structure would resemble what&#8217;s illustrated in Figure 3. Note the addition of the Routable interface, which is implemented by Bill. It&#8217;s the Routable interface, and it&#8217;s placement in the route.jar module that eliminates the cycle.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-541" style="border: 0pt none;" title="CohesiveModulesv3" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/CohesiveModulesv3.jpg" alt="CohesiveModulesv3" width="583" height="323" /></p>
<p style="text-align: center;"><strong>Figure 3: Cohesive Modules with Acyclic Relationships</strong></p>
<h2>Wrapping Up</h2>
<p>Highly cohesive modules are easier to understand, maintain, and reuse. In many cases though, it can be difficult to create cohesive modules early in the development lifecycle, when the team may not have a clear understanding of system behavior. As this insight is gained, the development team should structure the system to ensure highly cohesive modules are available.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/cohesive-modules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementation Factory</title>
		<link>http://www.kirkk.com/modularity/2009/12/implementation-factory/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/implementation-factory/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 00:53:57 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Extensibility Pattern]]></category>
		<category><![CDATA[Part 2]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=297</guid>
		<description><![CDATA[Statement
Use factories to create a modules implementation classes.
Description
One of the challenges we face with abstract coupling is creation of the implementation class. A client class cannot create an implementation class if we want the two classes abstractly coupled. Figure 1 illustrates the Implementation Factory pattern.


Figure 1: Implementation Factory Pattern
Implementation Variations
Implementation Factory is often used with [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Use factories to create a modules implementation classes.</p>
<h2>Description</h2>
<p>One of the challenges we face with abstract coupling is creation of the implementation class. A client class cannot create an implementation class if we want the two classes abstractly coupled. Figure 1 illustrates the Implementation Factory pattern.</p>
<p style="text-align: center;"><img class="aligncenter" style="border: 0pt none;" title="ImplementationFactory" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/ImplementationFactory.jpg" alt="ImplementationFactory" width="595" height="301" /></p>
<p style="text-align: center;">
<p style="text-align: center;"><strong>Figure 1: Implementation Factory Pattern</strong></p>
<h2>Implementation Variations</h2>
<p>Implementation Factory is often used with Abstract Module and Separate Abstractions.</p>
<p>Where does the factory live.</p>
<p>What is the factory? Class or framework such as Spring.</p>
<p>Injection or Lookup.</p>
<h2>Consequences</h2>
<h2>Sample Code</h2>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-328" style="border: 0pt none;" title="LoanSampleImplementationFactory" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/LoanSampleImplementationFactory.jpg" alt="LoanSampleImplementationFactory" width="976" height="897" /></p>
<p style="text-align: center;"><strong>Figure 1: Implementation Factory using Spring</strong></p>
<p>In abstract modules, we were left wondering how to create the implementation class without referencing the concrete class within the client.jar module.</p>
<h2>Wrapping Up</h2>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/implementation-factory/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Separate Abstractions</title>
		<link>http://www.kirkk.com/modularity/2009/12/separate-abstractions/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/separate-abstractions/#comments</comments>
		<pubDate>Wed, 23 Dec 2009 00:42:40 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Extensibility Pattern]]></category>
		<category><![CDATA[Part 2]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=289</guid>
		<description><![CDATA[Statement
Place abstractions and the classes that implement them in separate modules.
Description
You create an abstract class or interface to help reduce coupling between classes. This offers the ability to create new implementations of the abstraction without impacting clients dependent on that abstraction. In this sense, abstract coupling allows you to add new classes to your system [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Place abstractions and the classes that implement them in separate modules.</p>
<h2>Description</h2>
<p>You create an abstract class or interface to help reduce coupling between classes. This offers the ability to create new implementations of the abstraction without impacting clients dependent on that abstraction. In this sense, abstract coupling allows you to add new classes to your system without modifying existing classes, and it does so by reducing the dependency relationship between classes.</p>
<p>But coupling classes abstractly does not help reduce the dependency structure between components, especially if the abstraction and the classes that realize the abstraction reside in the same component. So while abstract coupling allows you to effectively reduce coupling between classes, it does not help to eliminate coupling between components. The abstract modules patterns is a prime example of this, where the client.jar module is still tightly coupled to the service.jar module even though the Client concrete class is not directly coupled to the ServiceImpl class.</p>
<p>The ability to deploy client.jar without service.jar does not exist given the structure in Figure 10.1.1. But what if I have a situation where I need to use client.jar without service.jar, or possibly with a new implementation. Even if I create new implementations of the Service interface, I&#8217;m still required to deploy service.jar with client.jar due to the placement of Service interface. In order to avoid this nasty problem, I can move the Service interface to the client.jar. This still allows me to configure the Client class with the appropriate Service implementation, yet the client.jar is not dependent on the service.jar module. In fact, the direction of the relationship has been inversed. Instead of client.jar depending on service.jar, the service.jar is now dependent on client.jar.</p>
<p>By simply changing the placement of classes, I&#8217;ve changed the dependency relationship between my modules. We saw an example of how module relationships can be inverted in the Module Relationships pattern Sample Code section. But inverting the dependency relationship may not always be what we&#8217;re interested in doing. Certainly, we know have the ability to use client.jar without service.jar, but can no longer use service.jar without client.jar. If we need the ability to use both client and service independent of each other, I need a different structure that accommodates this need. When this situation arises, the Separate Abstraction pattern can be used.</p>
<p>Fortunately, it&#8217;s as easy as moving the Service interface to a new component that both client.jar and service.jar modules are dependent upon. Figure 1 illustrates the new structure. In most cases where you take this approach, the new module will be an entirely abstract module. We refer to an abstract module as a specification module.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-290" style="border: 0pt none;" title="SeparateAbstractionsPattern" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/SeparateAbstractionsPattern.jpg" alt="SeparateAbstractionsPattern" width="557" height="336" /></p>
<p style="text-align: center;"><strong>Figure 1: Separate Abstractions Pattern</strong></p>
<h2>Implementation Variations</h2>
<p>SeparateAbstractions solves two different problems using a common technique. If there are two modules involved in the relationship, by separating the abstraction from the implementation, you are inverting the relationship between the two components. Using an ImplementationFactory will help maintain this separation. When more than three components are involved, SeparateAbstractions allows you to physically decouple two components where no relationship exists between the two components in either direction.</p>
<h2>Consequences</h2>
<p>Invariably more complex structure to manage.</p>
<p>The two key ingredients when you SeparateAbstractions is deciding where you should place the abstraction and where you should place the implementation. Since there could be many different implementations, they key to making this decision is figuring out your dependencies keeping in mind that the components containing the implementation will be dependent on the component containing the abstraction.</p>
<p>Since all implementations will only have a single abstraction, the placement of the abstraction is key to creating the component relationships that you want. In general, you want to keep the abstraction as close as possible to the classes that depend upon it. Here are some general guidelines to making this happen.</p>
<ul>
<li>If only a single class, or set of classes in the same package, rely upon the abstraction, then place the abstraction in that package.</li>
<li>If a set of classes that span multiple packages all rely upon the abstraction, but all packages exist within the same component, place the abstraction in a separate package in that component.</li>
<li>If a set of classes that span multiple components all rely upon the abstraction, and you want to keep each of those components independent of each other, then place the abstraction in separate component.</li>
</ul>
<p>SeparateAbstractions allows you to eliminate a module relationship. The module depending on the abstraction needs to be injected with an implementation or have some way to lookup the implementation at run-time. Creation of the appropriate implementation is another important consideration. If a lookup approach is taken, the lookup mechanism should use reflection to avoid introducing the dependency. If injection is used, you&#8217;ll need a separate component that performs the creation. SeparateAbstractions is concerned with flexibility, but comes at the price of complexity.</p>
<p>When assembling components, you may find that two otherwise independent components share common state. You&#8217;ll likely want to avoid having each component initialize and manage that state separately due to overhead, and possibly unreliable processing. Doing so also incurs  and unneccessary performance burden. If the state that each component relies upon is simply data that each requires to perform some processing, then it&#8217;s fairly easy to populate a bean defined by each component and pass the bean into the class requiring that data. But in situations involving more than state, each component may need to call back on an external entity. In this case, you cannot put the functionality on the bean since the bean would then require the ability to talk to the component. In order to accommodate this scenario, each component would need to define it&#8217;s own abstraction (the approach taken in Figure 10.1.2) or each component would need to depend on another component containing the abstraction (the approach taken in Figure 10.1.3).</p>
<h2>Sample Code</h2>
<p>In Abstract Modules, we depend upon the abstract elements of a module. In Implementation Factory, we used a factory to create the implementation class. Now, by moving the abstract class to a separate module, we can completely eliminate the relationship between the two modules.</p>
<p>Inverting the relationships allows us to deploy the customer.jar module independent of the billing.jar module. Again, it’s all about need. But I’d like to explore another option based on another important need – the ability to test and deploy modules independently. Before inverting the relationships, I am able to test and deploy the billing.jar module independently. After inverting the relationships, I can test and deploy the customer.jar module independently. But what if I want to test or deploy both modules independently? To do this, I need to completely eliminate the relationship altogether.</p>
<p>As it turns out, because I’ve got a pretty flexible class structure after I inverted the relationships (lot’s of abstract coupling), I can do this by simply bundling the two interfaces – Bill and DiscountCalculator – into a separate module. No other coding changes required. I start by moving them to a new package, which we’ll case base. (<a href="http://code.google.com/p/kcode/source/browse/#svn/trunk/edcie/eliminated/src/com/kirkk/base">moving them to a new package called base</a>). Then, I modify my build script (<a href="http://code.google.com/p/kcode/source/browse/trunk/edcie/eliminated/build.xml#34">modify my build script</a>) to bundle these two interfaces into a separate base.jar module, and we have successfully eliminated the relatinonship between the bill.jar and cust.jar modules. This is illustrated below in Figure 1.</p>
<p style="text-align: center;"><img class="aligncenter size-large wp-image-330" style="border: 0pt none;" title="LoanSampleSeparateAbstractions" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/LoanSampleSeparateAbstractions-1024x789.jpg" alt="LoanSampleSeparateAbstractions" width="1024" height="789" /></p>
<p style="text-align: center;">
<p style="text-align: center;">
<p style="text-align: center;">
<p style="text-align: center;"><strong>Figure 1: Separating Abstractions to another Module</strong></p>
<h2>Wrapping Up</h2>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/separate-abstractions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Abstract Modules</title>
		<link>http://www.kirkk.com/modularity/2009/12/abstract-modules/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/abstract-modules/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 21:31:29 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Extensibility Pattern]]></category>
		<category><![CDATA[Part 2]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=277</guid>
		<description><![CDATA[Statement
Depend upon the abstract elements of a module.
Description
Modules  heavily depended upon have many incoming dependencies. In other words, you may have many modules that all depend on a single modules. On one hand, this is a good thing because you&#8217;ve managed to maximize reuse. But reuse has it&#8217;s challenges. If the modules you&#8217;re reusing heavily [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Depend upon the abstract elements of a module.</p>
<h2>Description</h2>
<p>Modules  heavily depended upon have many incoming dependencies. In other words, you may have many modules that all depend on a single modules. On one hand, this is a good thing because you&#8217;ve managed to maximize reuse. But reuse has it&#8217;s challenges. If the modules you&#8217;re reusing heavily requires a change, the ramifications of that change can ripple throughout all dependent modules. Changing a heavily reused module can be quite a maintenance headache.</p>
<p>There are two types of changes a modules can undergo. If you change a method signature of a public method on a public class, you can introduce compile errors into other areas of the application. While this might seem the most severe, it&#8217;s the easiest to correct. The negative ramifications of this type of change are fairly obvious, and if you do change a method signature, it&#8217;s important that you search for all referencing classes and make the correction. As a general rule, instead of changing the method signature, I&#8217;ll typically deprecate the old version and create the new version of the method with the updated parameters. This allows other developers to incrementally make changes.</p>
<p>You might feel safer if you only have to change a method implementation, or possibly a private method. But changing an implementation can have a farther reaching impact than you might expect. While you&#8217;ll certainly isolate all compile errors to the module, a much more stealth problem arises. Something must have driven the functional change being made. In the case of changing a module that is heavily depended upon, at least one of the dependent modules is the driver for that change. But you cannot only consider that single context when testing the change, but must also consider all other dependent modules. No compiler will tell you if you&#8217;ve introduced a change that won&#8217;t work with other modules not considered by the decision that drove the change. It&#8217;s much easier to make a change if you know that the effect of change is contained to just a single module.</p>
<p>Some modules within an application are very widely used. A module that can be used by any other module is a global package. An example of a global module is one containing utility classes that provide helpful methods to manipulate strings and format dates. You have to be very cautious when changing global modules. A small problem can cause widespread failure.</p>
<p>When you&#8217;re designing the dependencies between modules, depending upon only the abstract elements increases flexibility. It gives you the ability to more easily extend and maintain areas of your application by defining new modules with classes that implement or extend the abstraction. Most important, clients of the modules now have the freedom to receive different implementations because they are no longer coupled to an implementation, but instead are coupled to an abstraction (ie. an abstract class or interface).</p>
<p>In Figure 1, you can see the client1 package is dependent on the service package. Examining the contents of client1, you&#8217;ll notice that the dependency of the Client class is on the Service interface. This is supported by the associated code. Therefore, the client1 package is dependent upon only the abstract elements in service. This relationship is enforced, and cannot be violated, because the ServiceImpl class is given only package scope.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-286" style="border: 0pt none;" title="AbstractModule" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/AbstractModule.jpg" alt="AbstractModule" width="557" height="225" /></p>
<p style="text-align: center;"><strong>Figure 1: Depending on the Abstract Elements of a Module</strong></p>
<p>The stability [MARTIN] of a package is a metric used to help determine the likelihood that a package will experience change. In Chapter 14, I examine this stability metric. Stable packages resist change, and are desirably the most heavily depended upon packages in the system. In Figure 7.4.1, you see the service package used by three other packages. The service package should be as stable, or resistant to change, as possible.</p>
<h2>Implementation Variations</h2>
<p>Depending on only the abstract elements of a package carries a price. Creating the implementing class can no longer be done by using the new keyword to create an instance of the implementing class. Instead, you have to consider some of the following options:</p>
<ul>
<li>Object Factory – To avoid dependencies on the concrete elements of a package, an object factory can be used to create the appropriate concrete instances. This offers a few advantages. First, the factory is the only area of the application referencing the concrete class. Adding new concrete classes that extend the abstraction is much easier. Second, if there are rules associated with creating the instances, these rules are well encapsulated within the factory. If the rules change, you&#8217;ll have a single maintenance point.</li>
<li>Dynamic Creation – In some situations, you&#8217;ll find that using the Class class is more appropriate than an object factory. I&#8217;ve found this approach to work best in a couple of cases. In a web application, if I need to create certain classes at server startup, I&#8217;ll use the Class class and specify the concrete class to instantiate in a startup properties file. This allows me to create new classes that can be plugged into my application when the server starts up by simply defining the new class and then specifying it&#8217;s fully qualified name in the appropriate properties file. The second scenario is when I&#8217;m using an Abstract Factory [GOF]. Specifying how the appropriate concrete factories get created is also useful to specify in a properties file. In most other cases, I&#8217;ll use the object factory approach described above.</li>
</ul>
<p>I don&#8217;t think it&#8217;s imperative that all dependencies on a package be abstract all the time. There are situations where referencing the concrete class directly is appropriate, and it eases the burden of designing a creation mechanism. However, you can certainly depend on some abstractions in a package, while also referencing concrete classes in that same package. It&#8217;s no coincidence that the most stable packages are packages that also have the majority of incoming dependencies on abstract classes or interfaces. This leads to two interesting observations.</p>
<p>Abstract classes or interfaces should absorb most incoming dependencies, whereas outgoing dependencies should originate from concrete classes. While simpler, you&#8217;ll want to avoid a heavily dependent package with a lot of concrete classes  because a single change can have a significant ripple effect across all dependent packages. You&#8217;ll also want to avoid packages with a lot of abstract packages that have no incoming dependencies. I&#8217;ll leave it as an exercise to the reader to determine why such a package is utterly worthless. In Chapter 12, I&#8217;ll explore some metrics that can help evaluate the robustness of your package structure.</p>
<p>The example in Heuristic 7.4 clearly illustrated the value of a dependency on only the abstract elements of a package. I&#8217;m not going to rehash that material here. You might, however, be interested in referring to Heuristic 11.2, which is closely related to Heuristic 7.5, but offers an additional perspective.</p>
<h2>Consequences</h2>
<p>If you have to change a stable module, you&#8217;ll experience a ripple effect throughout much of the application. Unless it&#8217;s a very simple change, the ripple effect is almost certain to cause a problem somewhere. Of course, you&#8217;ll probably have a gut feeling telling you what to expect of your change, and you should listen. The more you have to change stable packages, the less maintainable you&#8217;ll find the application.</p>
<h2>Sample Code</h2>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-326" style="border: 0pt none;" title="LoanSampleAbstractModules" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/LoanSampleAbstractModules.jpg" alt="LoanSampleAbstractModules" width="950" height="838" /></p>
<p><strong>Figure 1: Abstract Module</strong></p>
<h2>Wrapping Up</h2>
<p>If Heuristic 7.2 is arguably the most important, Heuristic 7.5 arguably yields the most flexibility. While the resulting design is a bit more complex, you&#8217;ll find you can achieve some pretty amazing results when applying patterns in conjunction with Heuristic 7.5. When layering an application, your resulting package structure should be uni-directional. But in some cases, you might have a very strong desire to violate your layered relationships because of the perceived need to reference a method on a class in an upper level layer. Heuristics 11.2 and 11.3 offer interesting perspectives, and some interesting techniques that will allow you  to make explicit calls to methods on classes in upper layers without jeopardizing the hard work you&#8217;ve put into designing your package relationships.</p>
<p>The focus on package relationships helped me establish some clear and concise goals. Because the underlying datasource might change, I wanted to avoid coupling to a relational database, and this meant avoiding any dependency on java.sql. The desire to remain independent of java.sql create the need for a few additional classes to ensure I realized my goals. While not the only contributor, package relationships certainly played a large role in driving my class design.</p>
<p>As you&#8217;ve seen with most of the package heuristics, layering is a central theme. But a number of other design patterns have also emerged. In our example, the SelectDAO is a Strategy [GOF],  the FileDataCache is a Facade[GOF], and the BaseDAO serves as a factory for it&#8217;s implementers.</p>
<p>There are also a few other situations in the examples that  could use some attention. Passing the String and SortedMap as parameters to the retrieve method on the SelectDAO lacks  flexibility. While outside the scope of this discussion, I discuss some options in Heuristic 11.1.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/abstract-modules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test Module</title>
		<link>http://www.kirkk.com/modularity/2009/12/test-module-2/</link>
		<comments>http://www.kirkk.com/modularity/2009/12/test-module-2/#comments</comments>
		<pubDate>Tue, 22 Dec 2009 19:46:04 +0000</pubDate>
		<dc:creator>kirk knoernschild</dc:creator>
				<category><![CDATA[Part 2]]></category>
		<category><![CDATA[Utility Pattern]]></category>

		<guid isPermaLink="false">http://www.kirkk.com/modularity/?p=252</guid>
		<description><![CDATA[Statement
Each module should have a corresponding test module.
Description
Writing tests is one of the most important activities you should perform as a developer. Create a robust suite of tests has significant advantages, both long and short term. Short term, tests help you verify that the code you write does as you intend. Creating tests also helps [...]]]></description>
			<content:encoded><![CDATA[<h2>Statement</h2>
<p>Each module should have a corresponding test module.</p>
<h2>Description</h2>
<p>Writing tests is one of the most important activities you should perform as a developer. Create a robust suite of tests has significant advantages, both long and short term. Short term, tests help you verify that the code you write does as you intend. Creating tests also helps you design your system. Since classes should be independently testable, a natural by-product of test driven development is that your classes will exhibit lower degrees of coupling.</p>
<p>Test driven development has long-term benefits too. When refactoring your code, or adding new functionality to an existing code base, a suite of tests can be repeatedly run to guarantee that your changes haven&#8217;t broken anything that once worked. Inevitably though, bugs will surface and tests can help tremendously in proving the source of the bug and then help you fix it. When bugs do surface, most developers form a mental picture of the cause of the bug, and this helps them determine the place in the system to begin looking. But it always seems that some developers have better luck fixing bugs than others. It&#8217;s a frustrating experience when you&#8217;re told a bug has been fixed, only to find that either it hasn&#8217;t been fixed correctly, or that fixing the bug has caused another area of the system to break. Why do some developers have better luck fixing bugs that others? A few simple scenarios should yield some answers.</p>
<p>Lazy developers will operate on their initial hunch and simply change the area of the system they suspect is causing the error, and release those changes without proper test to other developers. Lazy developers typically experience very little consistent success with fixing bugs.</p>
<p>A better approach taken by some developers is to duplicate the error by running the application using the same conditions as when the bug was encountered. Once the error is duplicated, they&#8217;ll typically use a debugger or logging statements to nail down the exact location of the bug. After the location is nailed down and the code has been corrected, they&#8217;ll run the application again to prove the bug has been fixed. While some testing is better than no testing, this approach is also seriously flawed. Running the application doesn&#8217;t allow you to focus specifically on the source of the bug because you are testing the application as a whole.  Additionally, without a suite of test cases, you may be able to prove that your change fixed the problem in the scenario it was discovered, but you cannot guarantee that your changes didn&#8217;t break something elsewhere in the application. It&#8217;s unrealistic to expect each developer to test the entire application each time a change is made. Few developers are disciplined enough to repeatedly perform such a mundane exercise. Another disadvantage is that this approach is not automated and repeatable, and you&#8217;ll always run the risk that future changes will break it again. You may fix the problem initially, only to find that future changes introduce a similar problem. Proving that you fixed the problem today doesn&#8217;t prove that the problem won&#8217;t surface again tomorrow.  Developers adopting this approach typically introduce more bugs than they fix.</p>
<p>Those developers who use test driven development typically have a greater degree of success in fixing bugs since a robust suite of test cases already exist that prove other areas of the system still function as intended. However, you still have to be very judicious in your approach to correcting the error. Simply fixing the source of the bug and running the existing suite of tests to prove that the bug has been corrected won&#8217;t always work.</p>
<p>The most successful developers typically have a very regimented approach. The approach goes something like this. Their first step is to create a new test case that reproduces the problem. The reasoning here is that if the existing suite of tests don&#8217;t reproduce the problem, then the existing suite of tests don&#8217;t test that condition, and a new test is needed. Once the problem has been reproduced, developers will fix the bug and run the test again. This time, the test should pass. If the test does pass, you can start feeling pretty good that you&#8217;ve fixed the error. But you can&#8217;t stop here. You should also run all other tests within the application to prove that your changes haven&#8217;t broken something else. Only after you do this can you feel reasonably comfortable knowing the problem has been fixed.</p>
<p>Adopting a sound approach to testing involves much more than simply saying that you write tests. A robust testing strategy means that you&#8217;re working hard to write classes that can be tested independently.  A unit test emphasizes testing classes as a standalone unit. This typically implies that you want to decouple the class you&#8217;re testing from any outside forces, such as a database or legacy integration with a backend system. Testing classes independently implies that you are devoting effort to decoupling your classes by injecting dependencies into the class under test. This means that the classes you&#8217;re testing must depend on abstract classes or interfaces for the services they use, and the test case will inject a stub into the class under test. The stub is a simple implementation of the abstraction that exists for testing purposes only, and isn&#8217;t used in versions of the system running outside of the test suite.</p>
<p>Test ing classes independently using unit tests is valuable, but it&#8217;s also valuable to test the integration aspect of multiple classes or components. For instance, you might create an integration test that retrieves information from a database table to prove connectivity with the backend database, or to prove that when two components are wired together, they continue to function as expected. Any test that requires access to a force external to the class originally under test is considered an integration test. While integration tests are useful, you should be cautious to avoid testing business logic in your integration tests. You should not check that the result of the test is a specific data value, but instead focus on the structure of the information being what you expect, that the connectivity between two entities is functional, or to ensure the integration point is still what you expect. Becoming “test infected” means that you aren&#8217;t writing tests because you&#8217;ve been told you need to, but that you create a suite of unit and integrations tests to help you write higher quality code and design more resilient software.</p>
<p>There are other types of tests that can be very useful as well. A design test allows you to make assertions about the design metrics of the system. Tools such as JDepend can be used to create tests that check for dependencies between packages. Performance tests allow you to verify the response time of a test. JunitPerf offers extensions to the Junit testing framework that allows you to write performance tests. Finally, functional tests allow you to create user level tests. Tools such as JwebUnit or Fitnesse allow you to create functional tests.</p>
<p>Our discussion to this point has served as primarily a review of sound testing strategies for class level testing. Assuming you are devoted to creating robust test suites, there is no magic in creating a TestComponent. If you&#8217;re applying some of the other useful patterns in this book, a TestComponent is a very natural by-product, and is mainly an exercise in how you bundle your test cases. For any set of classes bundled in a component, the corresponding test cases are bundled in a separate TestComponent. But similar to a unit test case that should test a class independently, a TestComponent should also be able to test a component while only depending on components required by the component under test. You should avoid introducing any new dependencies from within the test component. Otherwise, you cannot say for certain that you&#8217;re testing only the component&#8217;s functionality. A good rule of thumb when creating a TestComponent is that the TestComponent should be one level higher than the component under test. This helps ensure you aren&#8217;t introducing additional dependencies that aren&#8217;t required.</p>
<p>Recall that levelizing your component hierarchy requires that you have AcyclicRelationships between all components. TestComponents should also have AcyclicRelationships. If you find that the TestComponent is more than a single level higher, it&#8217;s a  good indication that the TestComponent is using  another component&#8217;s classes as stubs for the component under test. You have to give careful consideration to the placement of integration, design, performance, and functional tests. It&#8217;s very likely that these types of tests will introduce undesirable TestComponent dependencies. Strategies for dealing with the placement of these other types of tests will be discussed next. Figure 1 illustrates a Test Module.</p>
<p><img class="aligncenter size-full wp-image-433" title="TestModule" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/TestModule1.jpg" alt="TestModule" width="557" height="226" /></p>
<p style="text-align: center;">
<p style="text-align: center;"><strong>Figure 1: Test Module</strong></p>
<h2>Implementation Variations</h2>
<p>You&#8217;ll want to share some utility classes across TestComponents. In this situation, you can either duplicate the utility classes, or create a lower level test utility component that contains the test utility classes.</p>
<p>It is certainly tempting for a TestComponent to define dependencies on other components that contain implementations for classes within the component under test. While resisting this temptation is difficult, it&#8217;s also important. Defining additional dependencies on other components within your test component will save you a bit of time in creating the test cases, but it will also compromise the integrity of your test. By using classes in other components as your stubs, you cannot be certain that a failing test case is the result of a flaw in the component under test, or a bug in the component classes you are using as your stubs.</p>
<p>Notes on Integration vs. Unit tests here<br />
If you&#8217;re creating integration, performance, functional or design tests, you might consider splitting these integration tests out into another separate TestComponent if the integration tests introduce dependencies on other components. Using a LevelizedBuild can help in determining where these other tests should be placed.<br />
There are some cases, however, where a TestComponent will need, and even want, to introduce additional dependencies. For instance, if the component under test relies heavily upon database interaction, it&#8217;s likely that the connections to the database are obtained from your J2EE containers javax.sql.DataSource. Ideally though, you&#8217;ll execute your test outside the container&#8217;s environment. In such situations, you&#8217;ll find you want to introduce new dependencies to components that allow you to execute the tests standalone. To deal with database connections, a good approach would be to use the Commons Database Connection Pool (DBCP) component. Of course, another approach well worth considering is creating your classes so that they can be tested independent of a database connection.</p>
<p>If you leverage Abstract Modules, you&#8217;ll be able to create mocks or stubs to ensure modules can be test independently of each other. Figure 2 illustrates this.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-435" style="border: 0pt none;" title="TestModuleWithAbstractModule" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/TestModuleWithAbstractModule.jpg" alt="TestModuleWithAbstractModule" width="557" height="464" /></p>
<p style="text-align: center;"><strong>Figure 2: Test Module with Abstract Module</strong></p>
<h2>Consequences</h2>
<p>Test modules increase testability by allowing you to test modules independent of each other. Once a modules functions correctly as an independent unit, any error is the result of integration with other modules. Application level integration tests can be helpful in this situation.</p>
<h2>Sample Code</h2>
<p>Recall the example using a Callback in the discussion on the Acyclic Relationships pattern. Figure 3 illustrates the relationship between the customer.jar and billing.jar modules resulting from the relationship between the Customer and Bill classes.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-188" style="border: 0pt none;" title="CustBillAcyclic" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/CustBillAcyclic1.jpg" alt="CustBillAcyclic" width="600" height="216" /></p>
<p style="text-align: center;"><strong>Figure 3: Module Relationships</strong></p>
<p style="text-align: left;">Based on these module relationships, we can already create a separate test module for the billing.jar module. Listing 1 illustrates the test case to test the payment functionality of Bill.</p>
<pre class="brush: java;">
package com.kirkk.test;

import junit.framework.TestCase;
import java.math.BigDecimal;
import java.util.*;
import com.kirkk.cust.*;
import com.kirkk.bill.*;

public class PaymentTest extends TestCase {

   public PaymentTest(String arg0) {
      super(arg0);
   }

   public static void main(String[] args) {}

   public void testPayment() {
      Customer customer = new Customer();
      customer.createBill(new BigDecimal(500));

      Iterator bills = customer.getBills().iterator();

      while (bills.hasNext()) {
         Bill bill = (Bill) bills.next();
         BigDecimal paidAmount = bill.pay();
         assertEquals(&quot;Paid amount not correct.&quot;, new BigDecimal(485).setScale(2), paidAmount);
      }
    }

   public void testPaymentWithoutCustomer() {
      Bill bill = new Bill(new DiscountCalculator() {
         public BigDecimal getDiscountAmount() { return new BigDecimal(0.1); }
         }, new BigDecimal(500));

      BigDecimal paidAmount = bill.pay();
      assertEquals(&quot;Paid amount not correct.&quot;, new BigDecimal(450).setScale(2), paidAmount);
   }
}
</pre>
<p style="text-align: left;">This test case results in the relationships between modules shown in Figure 4, where we can now see the billingtest.jar test module.</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-259" style="border: 0pt none;" title="TestModule" src="http://www.kirkk.com/modularity/wp-content/uploads/2009/12/TestModule.jpg" alt="TestModule" width="600" height="342" /></p>
<p style="text-align: center;"><strong>Figure 4: The PaymentTest Module</strong></p>
<p style="text-align: left;"><img src="file:///Users/kirk/Library/Caches/TemporaryItems/moz-screenshot-13.png" alt="" />The PaymentTest is actually an integration test because it uses the Customer to to test the Bill.</p>
<p style="text-align: left;">
<h2>Wrapping Up</h2>
<p>Like unit testing, where classes are ideally tested in isolation, a test module allows you to test modules in isolation. A test module can help identify undesirable module dependencies. Then, utilizing other patterns, these dependencies can be broken if that&#8217;s the desirable course of action. Unlike unit testing, a test module is a more granular kind of test. Test modules also provide excellent insight on how to configure a module and interact with its Published Interface.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.kirkk.com/modularity/2009/12/test-module-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
