• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to design - Interface Usage  RSS feed

 
Anke Mueller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class as shown below.

This class has a method addFilter with 2 parameters (type String and type Command1 )


I have another class Command2. Here I need a similar method. The only difference is in the type of parameters passed. (type String and type Command).



I have started by using Interface


I would like the classes Command1 and Command2 to implement the interface Helper and override it these two public classes.
However my problem is in dealing with the 2nd parameter. Can anybody tell me how do I handle this issue?
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I see 2 ways of doing this.

Approach 1 = use "Helper" as the second parm in the addFilter() method in the interface Helper.

Approach 2 = Have an abstract class say AbstractCommand then have the concrete class Command1 and Command2 extends this. Then in the Helper interface the second parm will be AbstractCommand.
 
Anke Mueller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please elaborate ?

Should I do the following?





public interface Helper {
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you got the idea.

For the concrete class, you need down-cast the Helper to Command1 or Command2 accordingly because you can use Command1 or Command2 specific methods/attributes.



The thing to watch out for is if "param" is not a the class you expect, you may want to return null (instead of query) or throw an exception.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I am afraid there is something wrong if you start using casts like that. If you want Helpers, and the Command classes implement Helper, why can't you use polymorphism? If you need methods, why are they not in the Helper interface?
Why are you returning StringBuffers? You are probably better off using StringBuilders, but why return objects which are designed for changeability?

I have added code tags to your posts; always use them. Doesn't it make it look better
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You appear to be returning Strings and declaring a return type of StringBuffer. How did you get that to compile?
 
Anke Mueller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your response. How to make StringBuffer also implement an interface?







 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anke Mueller wrote:Thank you for your response. How to make StringBuffer also implement an interface?
. . .
It already does.

Unless you have managed to create your own class with the same name as a class in the java.lang. package.

This looks to me as if you were simply throwing bits of code together without having thought what it is supposed to do.
 
Anke Mueller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually not. I tested and Eclipse IDE is giving me "Incompatible conditional operand types MyReturnType and StringBuffer"!!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's to be expected as StringBuffer does not implement MyReturnType - only SomeClass does. So you can't use a StringBuffer where a MyReturnType is expected.
 
Anke Mueller
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please explain how to solve my problem.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the first question you need to ask yourself is: why do you need such very different types as parameters? What is this "SomeClass" that it can fulfill a similar role to a StringBuffer? Can you just use one, or just use the other? If not, maybe making them both part of the same interface isn't the right approach.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!