0

Wanted to know inputs for possible design pattern approach in java:

Scenario: Trying to create an arraylist for a combination of object and value pair. If Objects has A, B, C...etc If value has X1, X2, X3...etc

there could be an arraylist of Y1 for a combination of A & X1 there could be an arraylist of Y2 for a combination of A & X2 ..... etc, so on and so forth.

I tried with Factory Creational Design Pattern, however did not match quite good.Can anyone suggest a possible design approach for this issue ?

I cannot use Apache Pair in my implementation. ok I have added my problem Java class code:

import java.util.ArrayList;
import java.util.List;

public class FieldsProvider2 {
private MyObject object;
private MyAction action;

public FieldsProvider2(MyObject object, MyAction action) {
    this.object = object;
    this.action = action;
}

public List<Object> getList() {
    if ((action != null && object != null)) {
        if (Action.A == action) {
            List<Object> fields = new ArrayList<>();
            if (object.getTypeKey().equals(MYObject.X1)) {
                // fields.add(Y1);
                // fields.add(Y2);

                // fields.add(Y3);
                // fields.add(Y4);

                // fields.add(Y5);

                // fields.add(Y5);

                // fields.add(Y6);
                // fields.add(Y7);

                return fields;
            }

            if (object.getTypeKey().equals(MYObject.X2)) {
                // nothing
            }

            if (object.getTypeKey().equals(MYObject.X3)) {
                // fields.add(Y1);
            }

            if (object.getTypeKey().equals(MYObject.X4)) {
                // fields.add(Y1);
                // fields.add(Y8);
                // fields.add(Y9);
            }

            if (object.getTypeKey().equals(MYObject.X5)) {
                // fields.add(Y1);
            }

            if (object.getTypeKey().equals(MYObject.X6)) {
                // fields.add(Y1);
            }

            if (object.getTypeKey().equals(MYObject.X7)) {
                // fields.add(Y1);
            }

            if (object.getTypeKey().equals(MYObject.X8)) {
                // fields.add(Y1);
            }

            if (object.getTypeKey().equals(MYObject.X9)) {
                // fields.add(Y9);
            }

            if (object.getTypeKey().equals(MYObject.X10)) {
                // fields.add(Y10);
            }

            // Common items

            // fields.add(Y11);
            // fields.add(Y12);

            // fields.add(Y13);
            // fields.add(Y14);
            // or
            // fields.add(Y15);

            return fields;
        }

        if (Action.B == action) {
            List<Object> fields = new ArrayList<>();
            if (object.getTypeKey().equals(MYObject.X1)) {
                // fields.add(Y1);
                // fields.add(Y2);

                // fields.add(Y3);
                // fields.add(Y4);

                // fields.add(Y5);

                // fields.add(Y5);

                // fields.add(Y6);
                // fields.add(Y7);

                return fields;
            }

            if (object.getTypeKey().equals(MYObject.X2)) {
                // fields.add(Y7);
            }
        }
        //Action.C will start
        //Action.D will start and so on...
    return null;
}
}
user1328572
  • 599
  • 1
  • 7
  • 12
  • Please explain more or write down the code you wrote during your attempt to achieve this. I can't understand your problem. – Muhammad Gelbana Jul 20 '16 at 07:29
  • 4
    Design patterns are solutions for certain work flows, not for data structures (though the solution may employ certain data structures). So I think you're on the wrong track here. – RealSkeptic Jul 20 '16 at 07:30
  • Simply create an API of all the desired functions. Like an algebra with the full set of operations required. Then the best data structures fit in automatically. – Joop Eggen Jul 20 '16 at 07:54
  • 1
    Your question makes sense and it's a completely valid request for some help, but it seems to be more about design and not so much about coding. It may be a better fit on: [Programmers](http://programmers.stackexchange.com/). – Sean Mickey Jul 20 '16 at 08:06
  • Design patterns are just *"good practice"*. It's not something you have to include in your class design come hell or high water. If you have a problem that you want to solve with a design pattern, please describe it. (e.g. *"I want my class not to depend on a specific way of storing the data..."*) – fabian Jul 20 '16 at 08:08
  • @SeanMickey when referring other sites, it is often helpful to point that [cross-posting is frowned upon](http://meta.stackexchange.com/tags/cross-posting/info) – gnat Jul 20 '16 at 08:10
  • If you agree that this question will fit better on Programmers, you have the option of asking a moderator to migrate your question ([What is migration and how does it work?](http://meta.stackexchange.com/questions/10249/what-is-migration-and-how-does-it-work)), which will avoid the cross-posting issue that @gnat mentioned. – Sean Mickey Jul 20 '16 at 08:35
  • ok , now I think mine is more of a data structure issue than design problem. and I have posted it on programmers already. thanks sean. Shall I delete this post here ? sorry before seeing this comment, I had posted on programmers. – user1328572 Jul 20 '16 at 12:36

2 Answers2

0

If I get that right you have a structure like that:

Map<Pair<A,X>, List<Y>>

There is no design pattern for filling this Map, but there are some important things to consider. If you use a Pair as key, you must overwrite hashcode() and equals() methods in Pair to make the Map work correctly. Additionaly you should provide an constructor in Pair to create a key for searches etc. easily in one step.

If you are using Java8 you can use streams/lambdas for filtering/searching.

Otis Ottington
  • 425
  • 5
  • 17
0

In my project I implement my own Pair class, similar as Scala does.

Here the code base

      import java.util.Objects;

public class Pair<A, B> {
   public final A _1;
   public final B _2;

  private Pair(A _1, B b) {
    this._1 = _1;
    this._2 = b;
  }

  public A _1() {
    return _1;
  }

  public B _2() {
    return _2;
  }


 @Override
 public String toString() {
    return "(" + _1 + "," + _2 + ")";
}

public static <X, Y> Pair<X, Y> of(X a, Y b) {
    return new Pair<>(a, b);
}


public static <X, Y> Pair<X, Y> pair(X a, Y b) {
    return of(a, b);
}


public boolean equals(Object var1) {
    return var1 instanceof Pair && Objects.equals(this._1, ((Pair) var1)._1) && Objects.equals(this._2, ((Pair) var1)._2);
}

public int hashCode() {
    return this._1 == null ? (this._2 == null ? 0 : this._2.hashCode() + 1) : (this._2 == null ? this._1.hashCode() + 2 : this._1.hashCode() * 17 + this
            ._2.hashCode());
}
}
paul
  • 12,873
  • 23
  • 91
  • 153