Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

instanceof vs contains

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

How can i simplify Code A to something like Code B? Need your help. Thanks.

Code A


Code B

 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a common superclass or interface for ReqA, ReqB, and ReqC that you can use? If so, you can check for the instanceof the superclass or interface instead.

If not, then alternatively, you can put all the class types (as Class instances) in a list, and then loop through them checking for whether it is an instanceof that type. In other words, use reflection.

Henry
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Henry,

Thanks for your prompt response. Appreciate it. Yes, they are having the same superclass, but :


Sorry Henry, Im still new in Java, not really sure hows should this works, do you mind to give me some example? Thanks in advance

If not, then alternatively, you can put all the class types (as Class instances) in a list, and then loop through them checking for whether it is an instanceof that type. In other words, use reflection.

 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeansonne Pierre wrote:
Sorry Henry, Im still new in Java, not really sure hows should this works, do you mind to give me some example? Thanks in advance

If not, then alternatively, you can put all the class types (as Class instances) in a list, and then loop through them checking for whether it is an instanceof that type. In other words, use reflection.



The link that I provided should have some examples.

On the other hand, I must mention that the reflection API is arguably *not* a beginners API. If you are new to Java, perhaps using reflection is not a good idea.

Henry
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeansonne Pierre wrote:Thanks for your prompt response. Appreciate it. Yes, they are having the same superclass, but :...


OK, well how about:?
However, this sort of "fiddly" checking is often an indication that something is wrong with your design, so you might want to try to describe in English (or your native language) what it is about ReqD that makes it different from your other 'Req' types.

On the other hand, could it be that all these Req things are actually the same type - ie, they all have the same methods and information - but simply differ in some minor detail like colour or size. If that's the case, you could possibly categorise them with an Enum, eg:and then use EnumSets (←click) to group them.

That said, generally speaking, code of the type:is the antithesis of Object-Oriented programming.

It's called "dispatch logic", and experienced programmers work very hard to avoid it.

So basically: if you find yourself needing it, sit back and think about how you could do it another way - because there usually is one.

HIH

Winston
 
Jeansonne Pierre
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Thanks for the help. but I'm still cant find the solution yet. can it code it something like this?




I wanna try something advanced to improve myself if possible, but need your Example. thanks.
 
Greenhorn
Posts: 17
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suggest playing around with generics. From what I understand you are asking, you could do something like this:



I hope this helps

EDIT: I just threw this together real quick. This earlt in the morning not sure if LinkedList is the collection to use. Maybe an ArrayList would be just as good or better?
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeansonne Pierre wrote:



The "classes" reference variable refers to an array of Class objects. So, calling the getClass() method on that object, will return a Class instance that represents the array of Class objects. And calling the getClasses() method on the Class instance, will return an array of Class objects for that class instance.

So, you are looping though the class and interface types supported by the array of Class objects.

Henry
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeansonne Pierre wrote:Thanks for the help. but I'm still cant find the solution yet. can it code it something like this?


No, for the reasons Henry outlined.

I wanna try something advanced to improve myself if possible, but need your Example. thanks.


I already gave you one possible solution based on what you said you wanted.

But the "advanced" way is not to do it at all.

The question you should be asking yourself is not "how do I do this?" but "why do I need to do this?" because, as I already pointed out, "dispatch" code like this is precisely what languages like Java were designed to avoid.

HIH

Winston
 
What a show! What atmosphere! What fun! What a tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic