The Artima Developer Community
Sponsored Link

Design Forum
Should I create an interface if I have only one implementation?

9 replies on 1 page. Most recent reply: Nov 4, 2005 11:13 AM by Shashank D. Jha

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
Robert Johnson

Posts: 1
Nickname: robjkc
Registered: Mar, 2005

Should I create an interface if I have only one implementation? Posted: Mar 16, 2005 9:51 AM
Reply to this message Reply
Advertisement
I'm having a discussion with a co-worker about when to create an interface. I'm from the school that one should always code to an interface. I'll usually like to have interfaces as parameters to methods and as return parameters (http://www.artima.com/interfacedesign/WhatCanDo.html).

My co-worker believes that if you are probably only going to have one implementation then it is probably too much overhead to create an interface.

Any thoughts?


Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: Should I create an interface if I have only one implementation? Posted: Jun 8, 2005 7:44 AM
Reply to this message Reply
responding to Robert
> I'm from the school that one should
> always code to an interface. I'll usually like to have
> interfaces as parameters to methods and as return
> parameters
> (http://www.artima.com/interfacedesign/WhatCanDo.html).

I will go with you.


> My co-worker believes that if you are probably only going
> to have one implementation then it is probably too much
> overhead to create an interface.
< any thoughts?

Software lifecycle is nt limited to when you start design/ coding it first time. 90% of time is spend on maintainence, and adg features. In that regard, we cannot be sure that in future as well, we gonna have just one implementaion??

regards,

------------
Shashank D. Jha
iCMG
e-mail : shashank@icmgworld.com
Phone : +91-80-98451 87302

Vijay Rao

Posts: 2
Nickname: babylisper
Registered: Nov, 2004

Re: Should I create an interface if I have only one implementation? Posted: Jun 9, 2005 11:23 AM
Reply to this message Reply
We are considering two related things here.

Interfaces are your presentation of the object to the outside world. They are expected to be more stable, when compared to the implementation.

The reason we have interfaces , is because implementation is inherently more susceptible to change.

When an interface is not implemented with the right abstractions, then the interface is also more susceptible to change. Sometimes this happens when you have say, two customers (or users or clients or consumers) of an interface. One fine day, 3 other people jump onto the bandwagon, now you have 5 consumers of that interface... Also, lets assume the implementation is perfect , but the interface isn't generic enough .. what happens is that the interface is forced to change, the 2 original consumers are also forced to swallow the change...


The whole problem being addressed here is change ( the only "constant" as some people would say)... If your situation will have no change ( one of those *rare* things - or are we really sure?) then why separate the interface from the implementation at all? Go with a monolithic implementation , there is nothing wrong with it.

The reason one creates an interface is to separate,... and to uncouple ... without separation there is no need for an interface.

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: Should I create an interface if I have only one implementation? Posted: Jun 9, 2005 12:18 PM
Reply to this message Reply
responding to Vijay

> The reason we have interfaces , is because implementation
> is inherently more susceptible to change.

When you say implementation changes you are refering to change in operation signature?

If it is required to change the signature then probably abstraction was not proper. As you have correctly mentioned this among your points.

But if you refereing to change in implementation logic, this anyway doesnt affects the user of the class/ object.


In either case, how do you define this decoupling? that you have mentioned.

regards,
Shashank
e-mail shashank@icmgworld.com

Ahmet Emre

Posts: 2
Nickname: ketpah
Registered: Jun, 2005

Re: Should I create an interface if I have only one implementation? Posted: Jun 13, 2005 3:48 PM
Reply to this message Reply
I agree with Vijay.

There is nothing wrong with concrete classes being sent as parameters if they have only one implementation.

If you have only one implementation, here is the question to ask: Is it realistic to expect more implementation classes?
If yes, go with the interface.
Otherwise -you do not expect more implementation classes-, do not complicate the matters by creating an immature interface.

One can argue that how will you know whether you will need more than one implementation of a certain class. My answer is that, if you cannot think of any possible scenarios for multiple implementations, then it is also not realistic to come up with a generic interface. With the next requirements change, you will most likely change your interface as well.

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: Should I create an interface if I have only one implementation? Posted: Jun 13, 2005 9:45 PM
Reply to this message Reply
I think we need to separate out the design concerns from implementtion concerns.

System design is expressed through interfaces. While Class offers implmentation, srategy (hierarchy of classes, etc.)

So for decoupling of design and implementation we need to always separate these two conerns.

For any system of considerable size, it is always better to implement this way.

If we look into any currernt technology .NET, J2EE or CCM, everywhere the system development process, includes as first step defining the system interfaces.

So whether or not we need more then one implementation with time, we should always go by designing interfaces.

nes

Posts: 137
Nickname: nn
Registered: Jul, 2004

Re: Should I create an interface if I have only one implementation? Posted: Jun 14, 2005 4:17 PM
Reply to this message Reply
> I'm having a discussion with a co-worker about when to
> create an interface. I'm from the school that one should
> always code to an interface. I'll usually like to have
> interfaces as parameters to methods and as return
> parameters
> (http://www.artima.com/interfacedesign/WhatCanDo.html).
>
> My co-worker believes that if you are probably only going
> to have one implementation then it is probably too much
> overhead to create an interface.
>
> Any thoughts?

I will side with YAGNI (You are not gonna need it). If you ever decide to add a method and you have an interface you will have to change the class AND the interface instead of just the class. After you have two different classes with the same methods you should refactor to an interface though.
But then I am a bottom up programmer, I like to determine categories by looking at already existing species, otherwise I usually get the categories wrong.

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: Should I create an interface if I have only one implementation? Posted: Jun 15, 2005 8:21 AM
Reply to this message Reply
> I will side with YAGNI (You are not gonna need it). If you
> ever decide to add a method and you have an interface you
> will have to change the class AND the interface instead of
> just the class. After you have two different classes with
> the same methods you should refactor to an interface
> though.

I think we need to write interfaces as well for possibly three main reasons they are
1. Interface is a mechanism used to design system specification. This should be separated from implementation concerns, like class, (single or hierarchy).

2. Unless system if very trivial, writing interfaces considerably improves abstraction, for obvious reasons.

3. Writing interfaces makes it easy to modify the application to remove dependeny on actual implementation class thereby making it easier to implement Inversion of control design.

4. Considering any future extension, its always better design to keep things open for extension, read here as another improved implementation.

there could be more that could be listed here. As we get more arguments agains this.

Ahmet Emre

Posts: 2
Nickname: ketpah
Registered: Jun, 2005

Re: Should I create an interface if I have only one implementation? Posted: Jul 14, 2005 7:12 PM
Reply to this message Reply
I want to approach from a different perspective. I am in favor of not bringing any complexity until you need it. The staffing and the nature of the software that you are designing are crucially important when making decisions about interfaces-all-the-way, or interfaces-when-you-need kind of approaches.

If you are developing a product to sell (which will hopefully have lots of customers and hence implementations), or developing a framework; typically, product/framework development teams are normally more skilled Java/OO engineers. In this case it is better to make use of more interfaces, complexity is not a concern, it is an advantage in favor of flexibility and extensibility.

But if you are developing a custom tailored software for one and only one company to use, and the staff in the company are not very experienced in OO/Java, you have to think twice before making the architecture complicated by introducing overly used interfaces and factory/locators to find the only instance ever of a class in a system. Plus imaging the developers will have to understand and make modifications to the system down the road.

I also believe that a class carries an interface with its public methods. And today's IDEs make it very easy to refactor when you want to change an existing class. And having interfaces does not mean that you won't need to change your system all over the place. You won't need to change your system ONLY IF you can come up with a generic interface, otherwise the effort will be the same with or without the interfaces.

Here is an example: I am building custom software for a company that is doing business only in US, and I need to deal with Addresses. Should I create only one Address class that has street, city, state and zip codes? Or alternatively should I careate an Address interface, Address class, and a factory/locator. if you choose interfaces, your interface will most likely have some methods to deal with state and zip codes.
Now, say that the company got bigger, and they now want to support Canada and Mexico addresses. Can u tell me how helpful it was that you chose interfaces? I can see that your whole interface is changing dramatically, because state and zip code stuff are different in other countries. So I believe choosing interface would be 'immature', and the complexity introduced would not bring any benefit down the road based on the scenario above.

Shashank D. Jha

Posts: 68
Nickname: shashankd
Registered: May, 2004

Re: Should I create an interface if I have only one implementation? Posted: Nov 4, 2005 11:13 AM
Reply to this message Reply
responding->
> I want to approach from a different perspective. I am in
> favor of not bringing any complexity until you need it.
> The staffing and the nature of the software that you are
> designing are crucially important when making decisions
> about interfaces-all-the-way, or interfaces-when-you-need
> kind of approaches.

I disagree. I think this is just related to "better practices" in software development.

> But if you are developing a custom tailored software for
> one and only one company to use, and the staff in the
> company are not very experienced in OO/Java, you have to
> think twice before making the architecture complicated by
> introducing overly used interfaces and factory/locators to
> find the only instance ever of a class in a system. Plus
> imaging the developers will have to understand and make
> modifications to the system down the road.

You must not forgt that we dont make softwares for "our own" consumption. Every enterprise follows some standard in developing any software , to be or not to be used, in-house. You just cant go about start coding and expect user or developer to know the things.


> I also believe that a class carries an interface with its
> public methods. And today's IDEs make it very easy to
> refactor when you want to change an existing class. And
> having interfaces does not mean that you won't need to
> change your system all over the place. You won't need to
> change your system ONLY IF you can come up with a generic
> interface, otherwise the effort will be the same with or
> without the interfaces.

Why do you forget, that interface is design while classes provides implementation of design !!

> Here is an example: I am building custom software for a
> company that is doing business only in US, and I need to
> deal with Addresses. Should I create only one Address
> class that has street, city, state and zip codes? Or
> alternatively should I careate an Address interface,
> Address class, and a factory/locator. if you choose
> interfaces, your interface will most likely have some
> methods to deal with state and zip codes.
> Now, say that the company got bigger, and they now want to
> support Canada and Mexico addresses. Can u tell me how
> helpful it was that you chose interfaces? I can see that
> your whole interface is changing dramatically, because
> state and zip code stuff are different in other countries.
> So I believe choosing interface would be 'immature', and
> the complexity introduced would not bring any benefit down
> the road based on the scenario above.

You are missing my point. Design and implementation are concerns of different people. Designer needs to work in his place to work out future/ current strategies. While developers work gets easy because of better IDE, designers not, because of changing senarios as mentioned by you.

regards,
Shashank

Flat View: This topic has 9 replies on 1 page
Topic: Architectural Design change Previous Topic   Next Topic Topic: Designing CRUD operations

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use