Note that there are some explanatory texts on larger screens.

plurals
  1. POcode reuse in Methods.class vs strategy pattern and dependency injection
    primarykey
    data
    text
    <p><strong><code>Status:</code></strong> Answers of Fendy and Glen Best are <strong>equally acceptable</strong> and honored by me but since one can be accepted and bounty be given, I choose Fendy's answer.</p> <p><strong><code>Scenario:</code></strong></p> <p>If I have <strong>some code</strong> that has <strong>to be reused many times</strong> in <strong>many classes</strong> (rarely with minor parameter changes which is obvious) and concurrent threads, Which approach to go for?</p> <p>The code that has to be reused can be any sane thing (with appropriate care of static and non-static context in mind and method making techniques). It can be an algorithm, A DB method doing connect,operate,close. Anything.</p> <ol> <li><p>Make some class like <code>MyMethods.class</code> and <strong>put all those methods in it</strong>. </p> <p>1.a. Make <strong>methods</strong> <code>static</code> and call (by all classes and concurrent threads) directly as <code>MyMethods.someMethod();</code></p> <p>1.b. Make <strong>methods</strong> <code>non-static</code> and at the time to call them, <strong><code>instantiate</code> the whole class</strong> by <code>MyMethods mm = MyMethods(); mm.someMethod();</code></p></li> <li><p>Use <strong>Strategy pattern</strong> stated at <a href="https://en.wikipedia.org/wiki/Strategy_pattern" rel="nofollow">https://en.wikipedia.org/wiki/Strategy_pattern</a> (code attached here with).</p></li> <li><p>Use <strong>dependency injection</strong> stated at <a href="https://en.wikipedia.org/wiki/Dependency_injection#Java" rel="nofollow">https://en.wikipedia.org/wiki/Dependency_injection#Java</a></p></li> </ol> <p><strong><code>Problems:</code></strong></p> <ol> <li><p>Some people would say <strong>Unit test</strong> <a href="http://en.wikipedia.org/wiki/Unit_testing" rel="nofollow">http://en.wikipedia.org/wiki/Unit_testing</a> <strong>wont be possible</strong> with this approach, will make <strong>trouble in swaping out</strong> latter. if you want to test your class and use a <strong>mock version</strong> of the <strong>dependency</strong> </p> <p>1.a. Will there be any problems with <strong>concurrent</strong> calls or multiple classes? specially in <code>JDBC static methods</code> for just an example?</p> <p>1.b. I think it would make too much <strong>memory load</strong> as a <strong>whole class would</strong> be <code>instanticated</code> many times just to call one or two methods</p></li> <li><p>Thats way over my head, do explain that and or any advantages/disadvantages</p></li> <li><p>I <strong>do Not</strong> want to <strong>use a framework</strong> in context to this question.. Thats way over my head, do explain that and or any advantages/disadvantages</p></li> <li><p>Awaiting any other strategies or recommendations, if any.</p></li> </ol> <p><strong><code>Request:</code> Please only answer if you are experienced and know the implications deeply and can comprehensively, with your answer, help me and the community as a whole!</strong></p> <p><strong><code>Code:</code></strong></p> <pre><code>/** The classes that implement a concrete strategy should implement this. * The Context class uses this to call the concrete strategy. */ interface Strategy { int execute(int a, int b); } /** Implements the algorithm using the strategy interface */ class Add implements Strategy { public int execute(int a, int b) { System.out.println("Called Add's execute()"); return a + b; // Do an addition with a and b } } class Subtract implements Strategy { public int execute(int a, int b) { System.out.println("Called Subtract's execute()"); return a - b; // Do a subtraction with a and b } } class Multiply implements Strategy { public int execute(int a, int b) { System.out.println("Called Multiply's execute()"); return a * b; // Do a multiplication with a and b } } // Configured with a ConcreteStrategy object and maintains // a reference to a Strategy object class Context { private Strategy strategy; public Context(Strategy strategy) { this.strategy = strategy; } public int executeStrategy(int a, int b) { return this.strategy.execute(a, b); } } /** Tests the pattern */ class StrategyExample { public static void main(String[] args) { Context context; // Three contexts following different strategies context = new Context(new Add()); int resultA = context.executeStrategy(3,4); context = new Context(new Subtract()); int resultB = context.executeStrategy(3,4); context = new Context(new Multiply()); int resultC = context.executeStrategy(3,4); System.out.println("Result A : " + resultA ); System.out.println("Result B : " + resultB ); System.out.println("Result C : " + resultC ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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