Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Let's say I have ThingImpl and IThing. The former is an implementation of the latter, which is an interface.

IThing has 1 method: do(arg1)

Is it considered a hack/bad practice to add a method in addition to do()?

Example: do(arg1, arg2)

share|improve this question
add comment

3 Answers

I think you may not be fully understanding what the purpose is behind programming to Interfaces. The reason to do it is so that any client code can get an object of type IThing and know what to do with it. So there would be no way for the overloaded method to ever be called by a client whose entire knowledge of the ThingImpl is that it is an IThing.

That said, I frequently consider my Application as a system similar to ordering something from Amazon.com. I order something and an IThing is made in the Factory. The wiring code in the application is quite happy with an IThing, as the UPS truck is with a box. However, when the box get to its destination, I care again that what's in my box is exactly what I ordered (so I'll check the inventory to make sure the content of the box is specifically what I'm expecting). More literally, the endpoint code will check the implementation type of the IThing before attempting to use it.

It sounds like your code actually is the Factory. In cases like this, I'll actually just create two Factories, one that knows how to set the parameter to true, and the other which knows how to set it to false. The Factories will be stored in some sort of hash and called up based on the specifics of whatever I'm creating (for example, I might have a registry of Factories that create Question data objects based on information I get from an XML file, and I'll pull a particular Factory out of the hash based on the question type).

share|improve this answer
    
how do I reconcile the other two posters' comments as "it is a common pattern" with your post? Thanks –  Kevin Meredith Dec 20 '12 at 15:29
    
Not everything that is common is in accordance with what is good practice or in-depth understanding of the subject. For example, in the language I code in most, it's considered bad practice to use anonymous functions. However, I went in and dug a bit deeper myself and found that the truth is a bit more nuanced, and there are some occasions where an anonymous function will solve the problems we're all told they cause better than other types of function. So you need to make sure you have a full understanding of the principle and judge advice in that light. –  Amy Blankenship Dec 23 '12 at 19:55
add comment

As Oleksi mentioned, it is a common pattern. But make sure it is not public. If you want to make it public, better add to Interface itself.

share|improve this answer
    
But what if 'arg2' does not apply to every implementation of the interface? –  Kevin Meredith Dec 20 '12 at 3:59
2  
@Kevin: Then you have DIP issues. You're making your code dependent on a particular concrete type. You might as well toss out the interface at that point, cause it doesn't mean anything. :P –  cHao Dec 20 '12 at 4:07
2  
If it can have variable number of parameters, use params instead in interface. –  Tilak Dec 20 '12 at 4:15
    
@Tilak - how do I handle this issue in Java? Method chaining seems hacky. –  Kevin Meredith Dec 20 '12 at 15:28
    
I dont know java equivalent, But Object array should suffice. Otherwise only option left is overloaded methods. –  Tilak Dec 20 '12 at 15:38
show 2 more comments

Assuming that do is still an accurate name (and consistent with the other do method), I don't see any problem with doing this. In fact, it's a pretty common pattern.

share|improve this answer
1  
And assuming that arg2 actually means something (and isn't a dummy value that's there just to force the two-arg version to be called). –  cHao Dec 20 '12 at 3:47
    
The reason for the overloaded do(arg1, arg2) is because the do(arg1) creates a new object. When it does so, it automatically sets an argument to 'true'. With do(arg1, arg2), arg2 gets passed to the new object call. –  Kevin Meredith Dec 20 '12 at 3:49
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.