473,396 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How Do Interfaces Reduce Dependencies

How do interfaces help us achieve separation or reduce dependencies between
classes?

Suppose I have a "PersonPresenter" class and want to enable its data to be
editable in a Form. I have read that, in order to separate the UI from the
data, we can implement a view (e.g., IPersonView) in the form (e.g., class
PersonForm : Form, IPersonView) as part of the solution. I just don't see
how implementing an interface causes us to separate anything - especially in
this example - because the PersonForm class would have to implement the view
(i.e, it would have all the code that implements the IPersonView methods and
properties). I see no separation given that the form module physically
contains all the code that implements the view. I don't see that the form is
a black box in this situation.

Am I missing something?

Thanks!

Feb 14 '06 #1
6 1725
Jeff S wrote:
How do interfaces help us achieve separation or reduce dependencies between
classes?

Suppose I have a "PersonPresenter" class and want to enable its data to be
editable in a Form. I have read that, in order to separate the UI from the
data, we can implement a view (e.g., IPersonView) in the form (e.g., class
PersonForm : Form, IPersonView) as part of the solution. I just don't see
how implementing an interface causes us to separate anything - especially in
this example - because the PersonForm class would have to implement the view
(i.e, it would have all the code that implements the IPersonView methods and
properties). I see no separation given that the form module physically
contains all the code that implements the view. I don't see that the form is
a black box in this situation.

Am I missing something?

Thanks!


Maybe I'm being a bit rash, but I think (if I read correctly) it's the
other way around. It is the *Person* class that is the black box. So
to take your example one step further, say you wanted to use one form
(for some reason) as an interface for a class Car and also a class
Bicycle. In order for the Form to not know what object it really has,
both Bicycle and Car could implement an interface IVehicleView.

Others may have better or different ways of doing this, but in my form I
have a field holding the object:

class MyView : Form {
// all the other stuff
private IVehicleView vehicle;
}

Then, depending on what the interface definition is, you could
read/write to the actual object without knowing what it is. The trick
is that the form knows how to "edit" the object, but the object itself
(Car or Bicycle) doesn't have any idea it is being displayed (otherwise
it would have put on make-up).

(Rereading the OP): In your words, the "black box" is not the form but
the object implementing IVehicleView. The Form and (Car or Bicycle) are
completely separated from each other except for the "link" through the
interface.

Scott
Feb 14 '06 #2
Jeff S wrote:
How do interfaces help us achieve separation or reduce dependencies between
classes?

Suppose I have a "PersonPresenter" class and want to enable its data to be
editable in a Form. I have read that, in order to separate the UI from the
data, we can implement a view (e.g., IPersonView) in the form (e.g., class
PersonForm : Form, IPersonView) as part of the solution. I just don't see
how implementing an interface causes us to separate anything - especially in


In this example you could give an instance of your form to your Person
object (via an interface reference) and all the Person object would know
is that it deals with something that can view a person.

Exactly what object it deals with is unknown and unnecessary to know and
thus you gain separation.

The alternative would be for you to write code in your Person object to
handle that specific Form object and thus you have coupled that Person
object to that particular class, and thus no separation.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Feb 14 '06 #3

"Scott C" <sdcoonce@g_m_a_i_l.com> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
Jeff S wrote:
How do interfaces help us achieve separation or reduce dependencies
between classes?

Suppose I have a "PersonPresenter" class and want to enable its data to
be editable in a Form. I have read that, in order to separate the UI from
the data, we can implement a view (e.g., IPersonView) in the form (e.g.,
class PersonForm : Form, IPersonView) as part of the solution. I just
don't see how implementing an interface causes us to separate anything -
especially in this example - because the PersonForm class would have to
implement the view (i.e, it would have all the code that implements the
IPersonView methods and properties). I see no separation given that the
form module physically contains all the code that implements the view. I
don't see that the form is a black box in this situation.

Am I missing something?

Thanks!


Maybe I'm being a bit rash, but I think (if I read correctly) it's the
other way around. It is the *Person* class that is the black box. So to
take your example one step further, say you wanted to use one form (for
some reason) as an interface for a class Car and also a class Bicycle. In
order for the Form to not know what object it really has, both Bicycle and
Car could implement an interface IVehicleView.

Others may have better or different ways of doing this, but in my form I
have a field holding the object:

class MyView : Form {
// all the other stuff
private IVehicleView vehicle;
}

Then, depending on what the interface definition is, you could read/write
to the actual object without knowing what it is. The trick is that the
form knows how to "edit" the object, but the object itself (Car or
Bicycle) doesn't have any idea it is being displayed (otherwise it would
have put on make-up).

(Rereading the OP): In your words, the "black box" is not the form but the
object implementing IVehicleView. The Form and (Car or Bicycle) are
completely separated from each other except for the "link" through the
interface.

Scott


Thanks Scott,

Perhaps that's why things didn't make sense to me. Maybe I just got it
backwards, but I'm not sure yet. I was reading an article whose whole point
was to demonstrate how to pull out all data and associated logic from the
view (form). Part of the solution was to implement an interface in the form
(FWIW; here's what I was reading:
http://www.martinfowler.com/eaaDev/M...resenter.html). It just seems
like a whole lot of work and you don't really have the claimed independence
at the end of the day because the form has to "know about" the underlying
data/class at least to the extent that the form must implement an interface.

-J
Feb 14 '06 #4
Jeff S wrote:
How do interfaces help us achieve separation or reduce dependencies between
classes?


By specifying that the only things needed by (and available to) one
object are the members of the interface. Anything else which may happen
to be available in the implementing class is effectively invisible.
There may be many implementations of the interface - for instance, one
web UI and one Windows Forms UI - but so long as they both implement
the interface, then whatever is using that interface needn't know what
it's actually using behind the scenes.

(As an aside, using interfaces makes life a lot simpler at the unit
test level, as mocking frameworks generally work much better with
interfaces than classes - you can "expect" certain calls and respond to
them in appropriate ways.)

Jon

Feb 14 '06 #5
I have edited this quite a bit in view of your reference to teh fowler
article:

"Jeff S" <A@B.COM> wrote in message
news:eq**************@TK2MSFTNGP15.phx.gbl...

"Scott C" <sdcoonce@g_m_a_i_l.com> wrote in message
news:OH**************@TK2MSFTNGP10.phx.gbl...
Jeff S wrote:
How do interfaces help us achieve separation or reduce dependencies
between classes?

Suppose I have a "PersonPresenter" class and want to enable its data to
be editable in a Form. I have read that, in order to separate the UI
You haven't read the article properly - The PersonPresenter has no data - It
is an intermediary between the Person(data) and PersonForm(view).
the data, we can implement a view (e.g., IPersonView) in the form (e.g.,
class PersonForm : Form, IPersonView) as part of the solution. I just
don't see how implementing an interface causes us to separate anything -
especially in this example - because the PersonForm class would have to
implement the view (i.e, it would have all the code that implements the
IPersonView methods and properties). I see no separation given that the
form module physically contains all the code that implements the view. I
don't see that the form is a black box in this situation.

Am I missing something?

Consider:

class PersonForm : Form, IPersonView.....

class PersonFormForTheVisualyImpaired : Form, IPersonView....

Switching to the new form (possibly by config file) Requires no code change
other than passing a PersonFormForTheVisualyImpaired rather than a
PersonForm to the PersonPresenter in its constructor

PersonPresenter(IPersonView view,Person person)....
Thanks!

Maybe I'm being a bit rash, but I think (if I read correctly) it's the
other way around. It is the *Person* class that is the black box. So to
take your example one step further, say you wanted to use one form (for
some reason) as an interface for a class Car and also a class Bicycle.
In order for the Form to not know what object it really has, both Bicycle
and Car could implement an interface IVehicleView.
This is a completely different problem:

Your "solution" is reasonable but the interface would be called IVehicle and
not IVehicleView. Furthermore it would be better and simpler to simply move
the common vehicle stuff into a common base class Vehicle of both Car and
Bicycle firstly because it is less code a secondly because base classes are
more robust under versioning than interfaces (Clearly this is not an option
with the views). The form would also necessarily be called VehicleForm.

Others may have better or different ways of doing this, but in my form I
have a field holding the object:

class MyView : Form {
// all the other stuff
private IVehicleView vehicle;
}

Then, depending on what the interface definition is, you could read/write
to the actual object without knowing what it is. The trick is that the
form knows how to "edit" the object, but the object itself (Car or
Bicycle) doesn't have any idea it is being displayed (otherwise it would
have put on make-up).

(Rereading the OP): In your words, the "black box" is not the form but
the object implementing IVehicleView. The Form and (Car or Bicycle) are
completely separated from each other except for the "link" through the
interface.

Scott


As I said this is a different problem entirely.

Thanks Scott,

Perhaps that's why things didn't make sense to me. Maybe I just got it
backwards, but I'm not sure yet. I was reading an article whose whole
point was to demonstrate how to pull out all data and associated logic
from the view (form). Part of the solution was to implement an interface
in the form (FWIW; here's what I was reading:
http://www.martinfowler.com/eaaDev/M...resenter.html). It just seems
like a whole lot of work and you don't really have the claimed
independence at the end of the day because the form has to "know about"
the underlying data/class at least to the extent that the form must
implement an interface.

-J


It's a good article - Read it again whilst thinking about my earlier
example.
Feb 14 '06 #6
Thank you very much Nick (and all others).

-Jeff
Feb 15 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Voltaic | last post by:
i have been trying to use the JINI tool ClassDep to create a list of the dependencies of a program i have written, but have been unsuccessful. when i run it i output a list of classes that...
3
by: Sam Loveridge | last post by:
Hi all. I have a Web Service that I'm writing that needs to reference a VB.NET dll. The catch is that the VB.NET dll has a dependency on a COBOL dll which I can't load in my development...
1
by: vimal | last post by:
Hi , I am currently learning COM using c++. Just when I completed the chapter about Interfaces in Dale Rogerson's Inside COM, i got a basic question in my mind. Why do we need Interfaces??...
11
by: Steven T. Hatton | last post by:
In the past there have been lengthy discussiions regarding the role of header files in C++. People have been very adamat about header files serving as in interface to the implementation. I do...
7
by: Ant | last post by:
Hi, I’m wondering what practical use is there for creating interfaces. I can fully appreciate creating an abstract class for the purpose of inheriting, but why an interface? Is it just to...
3
by: pratham | last post by:
Hi, I was seeing code of PetShop 2.0 and find is quite .. differnet design and architecture .. too much of clutter in classes .. order class using and Iorder and Iorder using an orderInfo and...
7
by: barias | last post by:
Although circular dependencies are something developers should normally avoid, unfortunately they are very easy to create accidentally between classes in a VS project (i.e. circular compile-time...
9
by: xxbabysue123xx | last post by:
THe question is: Modify the RationalNumber Class so that it implements the Comparable interface. To perform the comparison, compute an equivalent floating point value from the numerator and...
1
by: bantunks | last post by:
Hello, I am trying to figure out the advantages and disadvantages of exposing interfaces through Opaque data types in C. I have figured/found out the following two advantages 1. Higher level of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.