java - Confusion when using the strategy method -


i trying implement strategy pattern find middle point of arraylists hold different types of numerical data including paired numerical data(coordinates). having difficulty

  1. with pairs and
  2. my strategy method 'findmiddle' not implementing.

how implement strategy pattern , how work in java using pairs?

when comes strategy pattern, 'cannot find symbol' error lines say

    intdata.findmiddle(new midint());     floatdata.findmiddle(new midflo()); 

and when comes pairs, main method outputs this...

    30 added integer arraylist     57 added integer arraylist     22 added integer arraylist     57 removed integer arraylist     5.55 added float arraylist     6.14 added float arraylist     5.42 not in float arraylist     strat.coordinates$pair@2a139a55 removed coordinate arraylist 

what going on in last line of output?

your design isn't 1 begging strategy pattern. pattern best fit class change strategy performing action time time. example player in game attack strategy depend on weapon being used or gui container change layout strategy. have number of "data" classes accept 1 type of data (so seems) 1 strategy apply ever.

in addition, strategies same , in no way need different. therefore, can avoid strategy pattern making single data container class non-specific methods inserting , removing. example:

public class container<t> {     private list<t> list = new arraylist<>();      public void insert(t t) { ... }     public void remove(t t) { ... }     public t findmiddle() { ... }      public static void main(string[] args) {         container<integer> ints = new container<>();         container<double> doubles = new container<>();         container<pair<double, double>> pairs = new container<>();          // insert elements containers         system.out.println(ints.findmiddle() + " middle integer.");         system.out.println(doubles.findmiddle() + " middle double.");         system.out.println(pairs.findmiddle() + " middle pair.");     } } 

the benefit design ability use container class in polymorphic way allowing not need know type you're inserting, removing, or finding middle of (especially since finding middle in no way tied type contained). way use containers cleverly; example:

list<container> containers = new arraylist<>(); containers.add(ints); containers.add(doubles); containers.add(pairs); containers.foreach(container::findmiddle); 

you use strategy patter if weren't willing go without wouldn't recommend it. if do, need add middle instance data classes , apply strategy in findmiddle method. note code in strategies (as written) accomplishes nothing. list in them not list in respective classes. so, requires put more thought findmiddle method(s) in end think you'll find it's misuse of strategy pattern.

given middle strategies, try these changes:

public class midint implements middle {     @override     public void findmiddle(list list){         // find , print middle element!     } }  public class integerdata {     private final list<integer> integers;     private middle middle = new midint();     public void findmiddle() {         middle.findmiddle(integers);     }     public void setmiddlestrategy(middle middle) {         this.middle = middle;     }     ... } 

now, (optionally) start midint strategy. if reason definition of the"middle" integer changed need set new middle strategy , automatically applied next time findmiddle(). of course, you'll need alter middle interface reflect what's shown. changed of names since you're not following java's naming conventions (which should do).


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -