3

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.

1
  • This is too abstract to give a good answer. Can you give a concrete example? Commented Jun 9, 2013 at 0:02

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

FYI, there is no "else" after a return - you may remove all such elses without changing the way it executes.
Do you mean to say, I should have used a series of if conditions? Does make sense.
wow.... I have been coding all day and my mind is mush. I cannot believe I didn't realize this! I was actually reading up on many different design patterns just the other day and read through several different factory patterns! I should have known better! Thanks for this! I won't forget it now!
I edited your answer to show you want I meant - ie i removed all the "elde". When execution terminates in an "if",there is no "else"
0

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

6 Comments

The else statements are redundant. And you probable mean that the objects to be returned are implementations of Sign, not subtypes (there are no subtypes in java -- there are implementations and specializations).
Yea your right they are redundant,nevertheless redundancy aint of importance in this thread correct?
What you mean there are no subtypes in java?how can java support subtyping but there isn't a subtype, its just terminology Dollery....."All classes are subtypes of class Object", subclass-subtype-childclass-baseclass-specializations all refer to the same thing...
A subtype has to be a subordinate. Implementing an interface gives you no subordinate relationship. You could argue that in specific cases a subordinate relationship has been created when extending a class, but that's an application specific concept. In java a subclass is not a subordinate, it's a specialization.
And yes, I know that they use this word in the jls, I just happen to think that they were wrong to do so and should have been more explicit with what is really going on.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.