Question

I have this problem where there are several parts in my code where I check if these certain conditions are met so that I can understand if what I am checking is of one type or the other. this ends up becoming large if else trees because I am making lots of checks, the same checks in each method, and there are several different types the thing I am checking can be. This I know can be solved using objects! Specifically, the things I am checking are 4 string values from a file. based on these string values, the 4 strings together can make one of 3 types. Rather than making these same checks every time I need to get the type the 4 strings make up, I am wondering if I can create a general object given these 4 strings and then determine if that object is an instanceof either specific class 1, 2, or 3. Then I would be able to cast that general object to the specific object.

Say I name the general object that the 4 strings create called Sign. I would take those 4 strings and create a new Sign object:

Sign unkownType = new Sign(string1, string2, string3, string4);

I need to check which specific type of sign this sign is.

EDIT: for more detail, the Signs I am checking are not symbols like "+" or "-", they are signs with text like you would see on the road. there are 4 lines on each sign and they need to be checked to see if each line evaluates to match a specific type of sign. The first line of SignType1 will be different of the first line of SignType2, and I want to take those 4 lines (Strings) and pass it onto an object and use that object throughout my code to get the values from it rather than making the same checks in each method. If you want me to show some code, I can, but it won't make much sense.

Was it helpful?

Solution

What you seem to asking for is a factory pattern

public interface ISign {
    public void operation1();
    public void operation2();

}

and a Factory class to generate classes based on input

public class SignGenerator {
    public static ISign getSignObject(String str1,String str2, String str3, String str4) {
        if(str1.equals("blah blah"))
           return new FirstType();
        if(str1.equals("blah blah2") && str2.equals("lorem ipsum"))
           return new SecondType();
        return new ThirdType();
    }
}

public class FirstType implements ISign {
}

public class SecondType implements ISign {
}

public class ThirdType implements ISign {
}

Implement all Type specific logic in these classes so you can call them without checking with tons of if..else clauses first

OTHER TIPS

From what I gathered from your statement. Say: create the method that returns a certain object provided the given string is equal to whateva value you specify

    //provided the objects to be returned are subtypes of Sign
    public Sign getInstance(String first, String second, String third, String fourth)
    {

        if(first==null || second==null || third==null || fourth===null )
            return null;

      if(compare1.equals(first))
       return new SignType1();
      else
      if(compare2.equals(second))
       return new SignType2();
      else  
      if(compare3.equals(third))
       return new SignType3();
      else
      if(compare4.equals(fourth))
       return new SignType4();

    }

Above code checks and returns thee appropriet instance corresponding to the string passed Hope that's what was your concern

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top