<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->

<document>
  <header>
    <title>Avalon Framework - Guide - Separation of Interface and Implementation</title>
    <authors>
      <person name="Paul Hammant" email="Paul_Hammant@yahoo.com"/>
      <person name="Peter Donald" email="peter at apache.org"/>
    </authors>
  </header>
  <body>
    <section name="Introduction">
      <p>
        The core concept of interface and implementation separation is built into
        Java itself in that it has interfaces and classes.  Many toolkits have
        been developed along the lines of an API / implementation separation.
        One such toolkit is the SAX API and the multiple XML parsers that implement
        it. Developers are quite happy using Apache's Xerces via the SAX API and
        understand that SAX represents the interface and Xerces an implementation.
        We notice that a lot of developers are happy to use interface/impl
        separated tools, but not to make them.  We will try to justify in this
        document why we think people making applications should define
        interface/impl boundaries early in the design cycle.
      </p>
      <subsection name="Justification">
        <p>
          The main reason we do it is because:
        </p>
          <ol>
              <li>it forces you to decouple different modules/components/objects</li>
              <li>if specified correctly allows you to easily change the implementation of
                  the interface/contract in the future</li>
              <li>makes it possible for a user to read documentation about the interface
                  without having the implementation details clutter up their perception</li>
              <li>increases the possibility of reuse in a larger application</li>
          </ol>
        <p>
          If you are building objects with the aim of reuse then [3] is important but
          most people don't build for reuse (and most XP advocates say you should just
          plan to use not reuse) and thus [1] and [2] are more important. If you feel
          like documenting that and expanding this then feel free to.
        </p>
      </subsection>
      <subsection name="Example">
        <p>
          Let us hope this is not necessary:
        </p>
<source>
package helloworld;
public interface HelloWorld {
    void sayHello(String greeting);
}

package helloworld.impl.default;
public class DefaultHelloWorld implements HelloWorld {
    void sayHello(String greeting) {
        System.out.println("HelloWorld Greeting: " + greeting);
    }
}

package helloworld.impl.remote;
public class RemoteHelloWorld implements HelloWorld {
    private RemoteMessager mRemoteMessager;
    public RemoteHelloWorld(RemoteMessager rm) {
        RemoteMessager = rm;
    }
    void sayHello(String greeting) {
        rm.sendMessage("HelloWorld Greeting: " + greeting);
    }
}
</source>

      </subsection>
      <subsection name="History">
        <p>
          We are referring to this pattern as <strong>interface/impl separation</strong>.
          Wiley's Patterns in Java book refers to it simply as 'Interface', but we feel
          that the word interface is overloaded enough in English and computing.
        </p>
        <p>
          It might be true to say that this is 'API/implementation separation', but
          this too could be confusing as the aforementioned SAX is not quite a
          pure set of interfaces. It has a static factory that thunks in an
          implementation that all subsequent calls to the factory method will be
          forced to use. See Anti-Patterns below.
        </p>
        <p>
          Better might be 'separation of implementation and the interface/contract' as
          that is quite correct, but a tad unwieldy.
        </p>
      </subsection>
    </section>
    <section name="Related Topics">
      <subsection name="Implementation Hiding">
        <p>
          Once a tool is split into interface and impl, it is possible for a container
          to hide the implementation.  Most containers already use dynamic proxys
          (available in the JDK since 1.3), but we are talking about having the classes
          of the implementation hidden from classes using the interface.
        </p>
        <p>
          To do this, it is easiest to mount the impl classes in a separate classloader
          to the classloader that the interface-using classes are mounted in.  The
          interfaces being mounted in a classloader that is visible to both.
        </p>
        <p>
          This is not a new proposition.  Sun defined the servlet spec, and included
          rules about implementation hiding for hosted servlets.  Essentially,
          instantiated servlets are only allowed to 'see' classes from the JDK, their
          own WAR file and those of the Servlet API itself.  Tomcat correctly hides
          the implementation of the Servlet API from the hosted servlets.
        </p>
        <p>
          To actually achieve this separation, many containers (including those from
          the Avalon project) require that the interface and impl are in separate jars.
          Or to put it another way, there is no point separating your interface and impl
          classes if you are going to distribute them in the same jar.
        </p>
      </subsection>
      <subsection name="Kernel, Client API, Hosted Components">
        <p>
          This is building on the previous section, and in short is referred to as K/CAPI/HC.
          Basically the kernel mounts hosted components and satisfies their need for a
          client API. However the kernel wants to hide its implementation from the hosted
          components.
        </p>
        <p>
          An EJB container is another good example of this.  EntityBean, SessionBean etc. is
          the client API.  The hosted components are the beans, and the container has a
          kernel.  It builds a complex tree of classloaders to separate its implementation,
          the client API, the JDK's runtime jar (that always being in the system or
          primordial classloader), and the hosted components.
        </p>
        <p>
          The central message of this is that if you have interface/impl separated your
          tool, and are doing tricky things with more classloaders in the implementation,
          please make sure you do not assume that the parent classloader of any classloader
          is the system classloader.  If your reusable tool has been taken by another team
          and at some non root place in a classloader tree, then the tools will fail if
          you have made such assumptions.
        </p>
      </subsection>
      <subsection name="Anti-Patterns">
        <p>
          SAX, mentioned in multiple contexts in this document, is also an example of
          where the design can go wrong.  The Factory is static (that in itself is an
          anti-pattern to IoC).  Despite giving the appearance of having the ability
          to generate a parser based on the implementation's class name, only the first
          caller of that method will register a parser for the whole environment to use.
        </p>
        <p>
          Given that the SAX API is now in the JDK, the environment we allude to above
          is the whole JVM.  This is a problem because in a very complex application
          with differing concurrent needs for implementation of parsers, not all can be
          met if the SAX API is used for making parsers.
        </p>
      </subsection>
    </section>
  </body>
</document>
