Note that there are some explanatory texts on larger screens.

plurals
  1. POScala Factory pattern
    text
    copied!<p>In trying to write more testable Java code, I have been using the Model-View-Presenter pattern that Martin Fowler blogged about years ago (<a href="http://martinfowler.com/eaaDev/ModelViewPresenter.html" rel="nofollow noreferrer">http://martinfowler.com/eaaDev/ModelViewPresenter.html</a> -- yeah, I know he deprecated it, but I still like the simplicity).</p> <p>I create a View interface for each JFrame, JDialog, etc. and use a Factory to actually generate them so that I can generate mocks for unit testing.</p> <p>Below is a small set of sample classes and interfaces. Is there a better way in Scala than a straight syntax translation? In other words, how do I use traits, self-type references, etc. to better follow DRY principles and still write type-safe code?</p> <pre><code>import java.awt.Dialog.ModalityType; import java.awt.Window; import java.awt.event.ActionListener; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import javax.swing.JButton; import javax.swing.JDialog; interface View { void okButtonAddActionListener(final ActionListener actionListener); } class Dialog extends JDialog implements View { private final JButton okButton = new JButton("OK"); public Dialog(final Window owner, final ModalityType modalityType) { super(owner, modalityType); } public void okButtonAddActionListener(final ActionListener actionListener) { okButton.addActionListener(actionListener); } } interface ViewFactory&lt;I, C extends I&gt; { I newView(final Window owner, final ModalityType modalityType) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException; } class AbstractViewFactory&lt;I, C extends I&gt; implements ViewFactory&lt;I, C&gt; { private final Class&lt;C&gt; cls; public AbstractViewFactory(Class&lt;C&gt; cls) { this.cls = cls; } public I newView(final Window owner, final ModalityType modalityType) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException { final Constructor&lt;C&gt; constructor = cls.getConstructor(Window.class, ModalityType.class); return constructor.newInstance(owner, modalityType); } } class DialogFactory extends AbstractViewFactory&lt;View, Dialog&gt; { private static final class InstanceHolder { public static ViewFactory&lt;View, Dialog&gt; instance = new DialogFactory(); } public DialogFactory() { super(Dialog.class); } public static ViewFactory&lt;View, Dialog&gt; getInstance() { return InstanceHolder.instance; } public static void setInstance(final ViewFactory&lt;View, Dialog&gt; instance) { InstanceHolder.instance = instance; } } // Here is a typical usage in production class DialogFactoryUser { private void userFactory() { final Window window = new Window(null); try { final View view = DialogFactory.getInstance().newView(window, ModalityType.APPLICATION_MODAL); } catch (final Exception ex) { ex.printStackTrace(); } } } // Here is a typical usage in a unit test class Test { public void test() { ... mockView = createMock(View.class); final Window window = new Window(null); mockViewFactory = createMock(ViewFactory.class); expect(mockViewFactory.newView(window, ModalityType.APPLICATION_MODAL)).andReturn(mockView); ... DialogFactory.setInstance(mockViewFactory); } } </code></pre> <p><strong>UPDATE:</strong>: I realized that I asked a <a href="https://stackoverflow.com/questions/2915041/scala-model-view-presenter-traits">similar question</a> last year and got a different "best" answer. Check out the answer by <a href="https://stackoverflow.com/users/4893/sblundy">sblundy</a> -- very nice.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload