objectfactory.html

来自「基于mondrian 开源框架进行OLAP多维分析」· HTML 代码 · 共 888 行 · 第 1/3 页

HTML
888 行
字号
          static class BooTest2 implements                      java.lang.reflect.InvocationHandler {              private final Boo boo;              public BooTest2() {                  // remove test class name                  BooFactory.clearThreadLocalClassName();                  // get default Boo implementation                  this.boo = BooFactory.instance().getObject();              }              public Object invoke(Object proxy, Method method, Object[] args)                  throws Throwable {                  if (method.getName().equals("getValue")) [                      return new Boolean(MyTest.testValue);                  } else {                      return method.invoke(this.boo, args);                  }              }          }          public void test1() {              try {                  // Factory will creates test class                  BooFactory.setThreadLocalClassName("MyTest.BooTest1");                  MyTest.testValue = true;                  MyCode myCode = new MyCode();                  int value = myCode.getValue();                  assertTrue("Value not 1", (value == 1));                  MyTest.testValue = false;                  myCode = new MyCode();                  value = myCode.getValue();                  assertTrue("Value not 0", (value == 0));              } finally {                  BooFactory.clearThreadLocalClassName();              }          }          public void test2() {              try {                  // Use InvocationHandler and Factory Proxy capability                  BooFactory.setThreadLocalClassName("MyTest.BooTest2");                  MyTest.testValue = true;                  MyCode myCode = new MyCode();                  int value = myCode.getValue();                  assertTrue("Value not 1", (value == 1));                  MyTest.testValue = false;                  myCode = new MyCode();                  value = myCode.getValue();                  assertTrue("Value not 0", (value == 0));              } finally {                  BooFactory.clearThreadLocalClassName();              }          }      } </pre> <p> While this is a very simple example, it shows how using such factories can aid in creating testable code. The MyCode method is a client of the Boo implementation. How to test the two different code branches the method can take? Because the Boo object is generated by a factory, one can override what object the factory returns.<P><P><DL><DT><B>Since:</B></DT>  <DD>Feb 01 2007</DD><DT><B>Version:</B></DT>  <DD>$Id: //open/mondrian-release/3.0/src/main/mondrian/util/ObjectFactory.java#2 $</DD><DT><B>Author:</B></DT>  <DD>Richard M. Emberson</DD></DL><HR><P><!-- ======== NESTED CLASS SUMMARY ======== --><A NAME="nested_class_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2"><B>Nested Class Summary</B></FONT></TH></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>static&nbsp;interface</CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.Context.html" title="interface in mondrian.util">ObjectFactory.Context</A></B></CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This is for testing only.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>static&nbsp;class</CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.Singleton.html" title="class in mondrian.util">ObjectFactory.Singleton</A>&lt;<A HREF="../../mondrian/util/ObjectFactory.Singleton.html" title="type parameter in ObjectFactory.Singleton">T</A>&gt;</B></CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Implementation of ObjectFactory that returns only a single instance of the Object.</TD></TR></TABLE>&nbsp;<!-- ======== CONSTRUCTOR SUMMARY ======== --><A NAME="constructor_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2"><B>Constructor Summary</B></FONT></TH></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected </CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#ObjectFactory(java.lang.Class)">ObjectFactory</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Class.html" title="class or interface in java.lang">Class</A>&lt;<A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A>&gt;&nbsp;interfaceClass)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates a new factory object.</TD></TR></TABLE>&nbsp;<!-- ========== METHOD SUMMARY =========== --><A NAME="method_summary"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="2"><FONT SIZE="+2"><B>Method Summary</B></FONT></TH></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected &nbsp;<A HREF="../../mondrian/util/CreationException.html" title="class in mondrian.util">CreationException</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#defaultCreationException()">defaultCreationException</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Factory method which creates an exception to be thrown if an object can not be created.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected &nbsp;<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/String.html" title="class or interface in java.lang">String</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getClassName()">getClassName</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the name of a class to use to create an object.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected &nbsp;<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/String.html" title="class or interface in java.lang">String</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getClassName(java.util.Properties)">getClassName</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/util/Properties.html" title="class or interface in java.util">Properties</A>&nbsp;props)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the name of a class to use to create an object.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected abstract &nbsp;<A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getDefault(java.lang.Class[], java.lang.Object[])">getDefault</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Class.html" title="class or interface in java.lang">Class</A>[]&nbsp;parameterTypes,           <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" title="class or interface in java.lang">Object</A>[]&nbsp;parameterValues)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For most uses (other than testing) this is the method that derived classes implement that return the desired object.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected &nbsp;<A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getObject()">getObject</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructs an object where the System Properties can be used to look up a class name.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected &nbsp;<A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getObject(java.lang.Class[], java.lang.Object[])">getObject</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Class.html" title="class or interface in java.lang">Class</A>[]&nbsp;parameterTypes,          <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" title="class or interface in java.lang">Object</A>[]&nbsp;parameterValues)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructs an object where the <code>parameterTypes</code> and <code>parameterValues</code> are constructor parameters and System Properties are used to look up a class name.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected &nbsp;<A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getObject(java.util.Properties)">getObject</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/util/Properties.html" title="class or interface in java.util">Properties</A>&nbsp;props)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructs an object where the <code>Properties</code> parameter can be used to look up a class name.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected &nbsp;<A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getObject(java.util.Properties, java.lang.Class[], java.lang.Object[])">getObject</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/util/Properties.html" title="class or interface in java.util">Properties</A>&nbsp;props,          <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Class.html" title="class or interface in java.lang">Class</A>[]&nbsp;parameterTypes,          <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" title="class or interface in java.lang">Object</A>[]&nbsp;parameterValues)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Constructs an object where the <code>parameterTypes</code> and <code>parameterValues</code> are constructor parameters and Properties parameter is used to look up a class name.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected &nbsp;<A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getObject(java.lang.String, java.lang.Class[], java.lang.Object[])">getObject</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/String.html" title="class or interface in java.lang">String</A>&nbsp;className,          <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Class.html" title="class or interface in java.lang">Class</A>[]&nbsp;parameterTypes,          <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" title="class or interface in java.lang">Object</A>[]&nbsp;parameterValues)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Creates an instance with the given <code>className</code>, <code>parameterTypes</code> and <code>parameterValues</code> or throw a <code>CreationException</code>.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>protected abstract &nbsp;<A HREF="http://farrago.sourceforge.net/api/org/eigenbase/util/property/StringProperty.html" title="class or interface in org.eigenbase.util.property">StringProperty</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#getStringProperty()">getStringProperty</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Return the <code>StringProperty</code> associated with this factory.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" title="class or interface in java.lang">Object</A></CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#removeContext()">removeContext</A></B>()</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Get the current override values in the opaque context object and clear those values within the Factory.</TD></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1"><CODE>&nbsp;void</CODE></FONT></TD><TD><CODE><B><A HREF="../../mondrian/util/ObjectFactory.html#restoreContext(java.lang.Object)">restoreContext</A></B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" title="class or interface in java.lang">Object</A>&nbsp;context)</CODE><BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Restore the context object resetting override values.</TD></TR></TABLE>&nbsp;<A NAME="methods_inherited_from_class_java.lang.Object"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#EEEEFF" CLASS="TableSubHeadingColor"><TH ALIGN="left"><B>Methods inherited from class java.lang.<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html" title="class or interface in java.lang">Object</A></B></TH></TR><TR BGCOLOR="white" CLASS="TableRowColor"><TD><CODE><A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#clone()" title="class or interface in java.lang">clone</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#equals(java.lang.Object)" title="class or interface in java.lang">equals</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#finalize()" title="class or interface in java.lang">finalize</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#getClass()" title="class or interface in java.lang">getClass</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode()" title="class or interface in java.lang">hashCode</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#notify()" title="class or interface in java.lang">notify</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#notifyAll()" title="class or interface in java.lang">notifyAll</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString()" title="class or interface in java.lang">toString</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#wait()" title="class or interface in java.lang">wait</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#wait(long)" title="class or interface in java.lang">wait</A>, <A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Object.html#wait(long, int)" title="class or interface in java.lang">wait</A></CODE></TD></TR></TABLE>&nbsp;<P><!-- ========= CONSTRUCTOR DETAIL ======== --><A NAME="constructor_detail"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2"><B>Constructor Detail</B></FONT></TH></TR></TABLE><A NAME="ObjectFactory(java.lang.Class)"><!-- --></A><H3>ObjectFactory</H3><PRE>protected <B>ObjectFactory</B>(<A HREF="http://java.sun.com/javase/6/docs/api/java/lang/Class.html" title="class or interface in java.lang">Class</A>&lt;<A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A>&gt;&nbsp;interfaceClass)</PRE><DL><DD>Creates a new factory object. The <code>interfaceClass</code> parameter is used to cast the object generated to type right type.<P><DL><DT><B>Parameters:</B><DD><CODE>interfaceClass</CODE> - the class object for the interface implemented by the objects returned by this factory</DL></DL><!-- ============ METHOD DETAIL ========== --><A NAME="method_detail"><!-- --></A><TABLE BORDER="1" WIDTH="100%" CELLPADDING="3" CELLSPACING="0" SUMMARY=""><TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor"><TH ALIGN="left" COLSPAN="1"><FONT SIZE="+2"><B>Method Detail</B></FONT></TH></TR></TABLE><A NAME="getObject()"><!-- --></A><H3>getObject</H3><PRE>protected final <A HREF="../../mondrian/util/ObjectFactory.html" title="type parameter in ObjectFactory">V</A> <B>getObject</B>()                     throws <A HREF="../../mondrian/util/CreationException.html" title="class in mondrian.util">CreationException</A></PRE><DL><DD>Constructs an object where the System Properties can be used

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?