Making the example with MixedClass vs. NonMixed solution more real, so it can be unit tested
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/samples/sidemeanings/src/org/apidesign/sidemeanings/MixedClass.java Sat Jun 14 10:04:50 2008 +0200
1.3 @@ -0,0 +1,24 @@
1.4 +package org.apidesign.sidemeanings;
1.5 +
1.6 +// BEGIN: sidemeanings.Mixed.Dirty
1.7 +public abstract class MixedClass {
1.8 + private int counter;
1.9 + private int sum;
1.10 +
1.11 + protected MixedClass() {
1.12 + super();
1.13 + }
1.14 +
1.15 + public final int apiForClients() {
1.16 + int subclass = toBeImplementedBySubclass();
1.17 + sum += subclass;
1.18 + return sum / counter;
1.19 + }
1.20 +
1.21 + protected abstract int toBeImplementedBySubclass();
1.22 +
1.23 + protected final void toBeCalledBySubclass() {
1.24 + counter++;
1.25 + }
1.26 +}
1.27 +// END: sidemeanings.Mixed.Dirty
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/samples/sidemeanings/src/org/apidesign/sidemeanings/NonMixed.java Sat Jun 14 10:04:50 2008 +0200
2.3 @@ -0,0 +1,43 @@
2.4 +package org.apidesign.sidemeanings;
2.5 +
2.6 +// BEGIN: sidemeanings.Mixed.Clean
2.7 +public final class NonMixed {
2.8 + private int counter;
2.9 + private int sum;
2.10 + private final Provider impl;
2.11 +
2.12 + private NonMixed(Provider impl) {
2.13 + this.impl = impl;
2.14 + }
2.15 + public static NonMixed create(Provider impl) {
2.16 + NonMixed api = new NonMixed(impl);
2.17 + Callback callback = new Callback(api);
2.18 + impl.initialize(callback);
2.19 + return api;
2.20 + }
2.21 +
2.22 + public final int apiForClients() {
2.23 + int subclass = impl.toBeImplementedBySubclass();
2.24 + sum += subclass;
2.25 + return sum / counter;
2.26 + }
2.27 +
2.28 + public interface Provider {
2.29 + public void initialize(Callback c);
2.30 + public int toBeImplementedBySubclass();
2.31 + }
2.32 +
2.33 + public static final class Callback {
2.34 + NonMixed api;
2.35 +
2.36 + Callback(NonMixed api) {
2.37 + this.api = api;
2.38 + }
2.39 + public final void toBeCalledBySubclass() {
2.40 + api.counter++;
2.41 + }
2.42 + }
2.43 +}
2.44 +// END: sidemeanings.Mixed.Clean
2.45 +
2.46 +
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/samples/sidemeanings/test/org/apidesign/sidemeanings/test/MixedTest.java Sat Jun 14 10:04:50 2008 +0200
3.3 @@ -0,0 +1,63 @@
3.4 +package org.apidesign.sidemeanings.test;
3.5 +
3.6 +
3.7 +import org.apidesign.sidemeanings.Mixed;
3.8 +import org.apidesign.sidemeanings.MixedClass;
3.9 +import org.apidesign.sidemeanings.NonMixed;
3.10 +import org.apidesign.sidemeanings.NonMixed.Callback;
3.11 +import org.junit.After;
3.12 +import org.junit.Before;
3.13 +import org.junit.Test;
3.14 +import static org.junit.Assert.*;
3.15 +
3.16 +public class MixedTest {
3.17 +
3.18 + public MixedTest() {
3.19 + }
3.20 +
3.21 + @Before
3.22 + public void setUp() {
3.23 + }
3.24 +
3.25 + @After
3.26 + public void tearDown() {
3.27 + }
3.28 +
3.29 + // BEGIN: sidemeanings.Mixed.Use
3.30 + @Test public void useOfClassWithMixedMeanings() {
3.31 + class AddFiveMixedCounter extends MixedClass {
3.32 + @Override
3.33 + protected int toBeImplementedBySubclass() {
3.34 + toBeCalledBySubclass();
3.35 + return 5;
3.36 + }
3.37 + }
3.38 + AddFiveMixedCounter add5 = new AddFiveMixedCounter();
3.39 + assertEquals("5/1 = 5", 5, add5.apiForClients());
3.40 + assertEquals("10/2 = 5", 5, add5.apiForClients());
3.41 + assertEquals("15/3 = 5", 5, add5.apiForClients());
3.42 + }
3.43 + // END: sidemeanings.Mixed.Use
3.44 +
3.45 + // BEGIN: sidemeanings.Mixed.Clean.Use
3.46 + @Test public void useWithoutMixedMeanings() {
3.47 + class AddFiveMixedCounter implements NonMixed.Provider {
3.48 + private Callback callback;
3.49 +
3.50 + public int toBeImplementedBySubclass() {
3.51 + callback.toBeCalledBySubclass();
3.52 + return 5;
3.53 + }
3.54 +
3.55 + public void initialize(Callback c) {
3.56 + callback = c;
3.57 + }
3.58 + }
3.59 + NonMixed add5 = NonMixed.create(new AddFiveMixedCounter());
3.60 + assertEquals("5/1 = 5", 5, add5.apiForClients());
3.61 + assertEquals("10/2 = 5", 5, add5.apiForClients());
3.62 + assertEquals("15/3 = 5", 5, add5.apiForClients());
3.63 + }
3.64 + // END: sidemeanings.Mixed.Clean.Use
3.65 +}
3.66 +