From 7e56fd39a24407cc73300b2654fc76831cb49b31 Mon Sep 17 00:00:00 2001 From: Dmitry Popov Date: Sun, 1 Jun 2014 00:28:17 +0400 Subject: [PATCH] add factory pattern SimpleAtomicLongMultithreadedTest Create Runnnable intarfaces for RunTest class using factory method --- .../SimpleAtomicLongMultithreadedTest.java | 84 +++++++++++-------- 1 file changed, 48 insertions(+), 36 deletions(-) diff --git a/assignments/week-2-assignment-1/src/edu/vuum/mocca/SimpleAtomicLongMultithreadedTest.java b/assignments/week-2-assignment-1/src/edu/vuum/mocca/SimpleAtomicLongMultithreadedTest.java index 401bcd63f..14414727c 100644 --- a/assignments/week-2-assignment-1/src/edu/vuum/mocca/SimpleAtomicLongMultithreadedTest.java +++ b/assignments/week-2-assignment-1/src/edu/vuum/mocca/SimpleAtomicLongMultithreadedTest.java @@ -45,20 +45,6 @@ public class SimpleAtomicLongMultithreadedTest { */ static SimpleAtomicLong mCounter; - /** - * Runnable commands that use the mCounter methods - * get() - * incrementAndGet() - * getAndIncrement() - * decrementAndGet() - * getAndDecrement() - */ - static Runnable getCommand; - static Runnable incrementGetCommand; - static Runnable getIncrementCommand; - static Runnable decrementGetCommand; - static Runnable getDecrementCommand; - /** * The value of mCounter prior to any changes made by testing. */ @@ -128,7 +114,43 @@ public void run() { public long getIterations() { return iterations; } - } + } + + /** + * @class RunnableFactory + * + * @brief This class provide method for create Runnable interface. + * Class demonstrates Factory pattern. + */ + static class RunnableFactory { + + enum ETestCommand{ + GET, + INCREMENT_AND_GET, + GET_AND_INCREMENT, + DECREMENT_AND_GET, + GET_AND_DECREMENT + } + + static Runnable make(ETestCommand command){ + + switch (command){ + case INCREMENT_AND_GET: + return new Runnable() { public void run() { mCounter.incrementAndGet(); } }; + case GET_AND_INCREMENT: + return new Runnable() { public void run() { mCounter.getAndIncrement(); } }; + case DECREMENT_AND_GET: + return new Runnable() { public void run() { mCounter.decrementAndGet(); } }; + case GET_AND_DECREMENT: + return new Runnable() { public void run() { mCounter.getAndDecrement(); } }; + case GET: + default: + return new Runnable() { public void run() { mCounter.get(); } }; + } + + } + + } /** * Runs prior to all tests. Creates a static instance of @@ -140,17 +162,7 @@ public static void setUpBeforeClass() throws Exception { * Instance of SimpleAtomicLong class */ mCounter = new SimpleAtomicLong(INITIAL_VALUE); - - /** - * Runnable commands that execute get(), incrementAndGet(), - * getAndIncrement(), decrementAndGet(), getAndDecrement(), - * respectively, on the SimpleAtomicLong instance - */ - getCommand = new Runnable() { public void run() { mCounter.get(); } }; - incrementGetCommand = new Runnable() { public void run() { mCounter.incrementAndGet(); } }; - getIncrementCommand = new Runnable() { public void run() { mCounter.getAndIncrement(); } }; - decrementGetCommand = new Runnable() { public void run() { mCounter.decrementAndGet(); } }; - getDecrementCommand = new Runnable() { public void run() { mCounter.getAndDecrement(); } }; + } /** @@ -169,7 +181,7 @@ public void multiGetTest() { /** * run multiple threads calling mCounter.get(). */ - runThreads(getCommand); + runThreads(RunnableFactory.make(RunnableFactory.ETestCommand.GET)); /** * The expected post-test value is no change in the pre-test * value. @@ -184,7 +196,7 @@ public void multiGetTest() { */ @Test public void multiIncrementAndGetTest() { - runThreads(incrementGetCommand); + runThreads(RunnableFactory.make(RunnableFactory.ETestCommand.INCREMENT_AND_GET)); /** * expected value after threads are run should be the number * of maximum iterations times the number of threads plus the @@ -200,7 +212,7 @@ public void multiIncrementAndGetTest() { */ @Test public void multiGetAndIncrementTest() { - runThreads(getIncrementCommand); + runThreads(RunnableFactory.make(RunnableFactory.ETestCommand.GET_AND_INCREMENT)); assertEquals(preTestValue + mMaxIterations*numThreads, mCounter.get()); @@ -212,7 +224,7 @@ public void multiGetAndIncrementTest() { */ @Test public void multiDecrementAndGetTest() { - runThreads(decrementGetCommand); + runThreads(RunnableFactory.make(RunnableFactory.ETestCommand.DECREMENT_AND_GET)); /** * Expected value of mCounter after threads have completed * running is the pre-test value minus the maximum iterations @@ -228,7 +240,7 @@ public void multiDecrementAndGetTest() { */ @Test public void multiGetAndDecrementTest() { - runThreads(getDecrementCommand); + runThreads(RunnableFactory.make(RunnableFactory.ETestCommand.GET_AND_DECREMENT)); /** * expected value of mCounter after threads have completed running * is the pre-test value minus the maximum iterations times the number @@ -280,11 +292,11 @@ private void runThreads(Runnable command) { RunTest[] runTests; if(command == null) { runTests = new RunTest[5]; - runTests[0] = new RunTest(getCommand); - runTests[1] = new RunTest(incrementGetCommand); - runTests[2] = new RunTest(decrementGetCommand); - runTests[3] = new RunTest(getDecrementCommand); - runTests[4] = new RunTest(getIncrementCommand); + runTests[0] = new RunTest(RunnableFactory.make(RunnableFactory.ETestCommand.GET)); + runTests[1] = new RunTest(RunnableFactory.make(RunnableFactory.ETestCommand.INCREMENT_AND_GET)); + runTests[2] = new RunTest(RunnableFactory.make(RunnableFactory.ETestCommand.DECREMENT_AND_GET)); + runTests[3] = new RunTest(RunnableFactory.make(RunnableFactory.ETestCommand.GET_AND_DECREMENT)); + runTests[4] = new RunTest(RunnableFactory.make(RunnableFactory.ETestCommand.GET_AND_INCREMENT)); } else { runTests = new RunTest[numThreads];