title: How not to expose a public interface in Java
body: <p>In my project <a href="http://www.jooq.org" rel="nofollow">jOOQ</a>, I model SQL queries with a complex data structure. All components of a query implement</p>
  
  <pre><code>public interface QueryPart {
    int bind(java.sql.PreparedStatement stmt);
    int bind(java.sql.PreparedStatement stmt, int initialIndex);
    SQLDialect getDialect();
    String toSQLDeclaration();
    String toSQLDeclaration(boolean inlineParameters);
    String toSQLReference();
    String toSQLReference(boolean inlineParameters);
  }
  </code></pre>
  
  <p>This interface's methods are used internally by all packages of the library to construct and execute SQL. They should not be invoked directly from client code. For that purpose, I have added</p>
  
  <pre><code>public interface QueryPartProvider {
    QueryPart getQueryPart();
  }
  </code></pre>
  
  <p>Which is the only publicly exposed interface. An example of an actual query part is:</p>
  
  <pre><code>public interface Table extends QueryPartProvider {}
  class TableImpl implements QueryPart, Table {}
  </code></pre>
  
  <p>As you can see, the QueryPart methods can only be accessed via <code>Table.getQueryPart().toSQLDeclaration()</code>, etc.</p>
  
  <p>My design helps discouraging direct access to QueryPart methods, but cannot completely hide it. My question is: <strong>Can anyone tell me a good design pattern to achieve this goal?</strong></p>
  
  <p>Note: The simplest but not very nice solution would be to cast all objects to QueryPart, e.g. <code>((QueryPart) table).toSQLDeclaration()</code></p>