interfaces...I'm confused!

  • Thread starter Thread starter Andy B.
  • Start date Start date
A

Andy B.

Hi.

I have read tons of blog posts and articles about how and when to use
interfaces. I have seen some people use them to define every object they
create and I have seen some people say to not ever use them. Other people
say not to use them unless you have some actions that multiple objects will
use, an example is like this: You can have an interface that tells all
breeds of cats that they must eat their food, sleep or even meow. Now, you
have different kinds of cats: house cat, farm cat, tiger, lion, panther. All
of these cats do the exact same thing, just in very different ways. This
would be good for the interface because regardless of what kind of cat it
is, the ICat interface will let you define eat(), Meow() and Sleep() without
being stuck to inheriting from a root cat that has this defined. I will go
with this idea, but there has to be more to it than this? An example I deal
with now is this:

I have to create an implement a Headline object that has/does the following
things:

1. It has the properties ModuleID, HeadlineID, Title, BodyText,
CreatedByUser, CreatedDate, ExpirationDate, ActivationDate, Status.
2. It has the events Saving, Saved, Deleting, Deleted, Editing, Edited,
Creating and Created.
3. It has no methods at this point since a headline (news article) really
can't do anything except sit there and look good.

I was told this really wouldn't fit for an interface because it doesn't have
any ability to do anything and that it is just a stateless object. Any ideas
on this matter?
 
Hallo Andy,

An Interface is a contract what a class contains (not more although it can
be used to cast the names in an interface as a kind of translator).

Because of that it is often misused to replace a C Header (by instance for
things like UML)

Java was created to get rid of those headers

The class you describe has in my opinion nothing which would need an
Interface.

Be aware that what you call Events are probably methods which will be
handled by an event.

A class has nothing to do with a stateless object

If a class is not shared it is a type to instance objects for the heap, as
it is shared it is simple a module with residence on the main stack of a
program.

Success

Cor
 
Andy said:
Hi.

I have read tons of blog posts and articles about how and when to use
interfaces. I have seen some people use them to define every object they
create and I have seen some people say to not ever use them. Other people
say not to use them unless you have some actions that multiple objects will
use, an example is like this: You can have an interface that tells all
breeds of cats that they must eat their food, sleep or even meow. Now, you
have different kinds of cats: house cat, farm cat, tiger, lion, panther. All
of these cats do the exact same thing, just in very different ways. This
would be good for the interface because regardless of what kind of cat it
is, the ICat interface will let you define eat(), Meow() and Sleep() without
being stuck to inheriting from a root cat that has this defined. I will go
with this idea, but there has to be more to it than this? An example I deal
with now is this:

I have to create an implement a Headline object that has/does the following
things:

1. It has the properties ModuleID, HeadlineID, Title, BodyText,
CreatedByUser, CreatedDate, ExpirationDate, ActivationDate, Status.
2. It has the events Saving, Saved, Deleting, Deleted, Editing, Edited,
Creating and Created.
3. It has no methods at this point since a headline (news article) really
can't do anything except sit there and look good.

I was told this really wouldn't fit for an interface because it doesn't have
any ability to do anything and that it is just a stateless object. Any ideas
on this matter?

If you neither have a field (variable at class level) nor implemented procedures,
make an interface. Otherwise write a base class.
 
Hi.

I have read tons of blog posts and articles about how and when to use
interfaces. I have seen some people use them to define every object they
create and I have seen some people say to not ever use them. Other people
say not to use them unless you have some actions that multiple objects will
use, an example is like this: You can have an interface that tells all
breeds of cats that they must eat their food, sleep or even meow. Now, you
have different kinds of cats: house cat, farm cat, tiger, lion, panther. All
of these cats do the exact same thing, just in very different ways. This
would be good for the interface because regardless of what kind of cat it
is, the ICat interface will let you define eat(), Meow() and Sleep() without
being stuck to inheriting from a root cat that has this defined. I will go
with this idea, but there has to be more to it than this? An example I deal
with now is this:

I have to create an implement a Headline object that has/does the following
things:

1. It has the properties ModuleID, HeadlineID, Title, BodyText,
CreatedByUser, CreatedDate, ExpirationDate, ActivationDate, Status.
2. It has the events Saving, Saved, Deleting, Deleted, Editing, Edited,
Creating and Created.
3. It has no methods at this point since a headline (news article) really
can't do anything except sit there and look good.

I was told this really wouldn't fit for an interface because it doesn't have
any ability to do anything and that it is just a stateless object. Any ideas
on this matter?

I do not see how using an Interface would have any benefit in your
current situation gven the information you supplied.

You would normally use an Interface when you have different classes
that need to be dealt with by common code that doesn't care about
their differences.

I probably wouldn't use an Interface for the cat example either, but
instead use a base class that all of the others derive from.

Where you might find Interfaces useful is if you have other objects
besides Headline. If some of those other objects share some
properties and/or events with Headline, it might be useful to have all
of those classes implement an Interface so common code can deal with
any of them without having to know what kind of object it is.
 
Armin Zingler said:
If you neither have a field (variable at class level) nor implemented
procedures,
make an interface. Otherwise write a base class.
Can you explain what you mean a little?

1. The class has lots of fields defined.
- private fields and properties
2. Defined methods? It has events linked to it but no methods at all.
 
Can you explain what you mean a little?

1. The class has lots of fields defined.
- private fields and properties
2. Defined methods? It has events linked to it but no methods at all.

How would a Headline object raise an event without having any methods?
It sounds likely you would have some "Save()" method that is called to
save the object somewhere and raises the event.
 
Family Tree Mike said:
How would a Headline object raise an event without having any methods? It
sounds likely you would have some "Save()" method that is called to save
the object somewhere and raises the event.

UH... oops. Totally forgot about that... go figure.

There probably will be some then to save/delete and stuff like that.

Public sub WriteToDB()
'This is stripped down to make it short and simple...
using DB as new Entities(Entities.CreateConnection())
DB.AddHeadline(Me)
RaiseEvent Saved(...)
end using
end sub

'to use the code.
Dim Headline as new Headline()
Headline.ModuleID=ModuleID
Headline.Title="Test headline"
Headline.CreatedByUser=1
Headline.Body="Testing this headline object."
Headline.CreatedDate=Date.Now
Headline.ActivatedDate=Date.Now
Headline.ExpirationDate="12/31/2010 9:55:00AM"

Headline.WriteToDB()

Does this look ok to actually do?
 
UH... oops. Totally forgot about that... go figure.

There probably will be some then to save/delete and stuff like that.

Public sub WriteToDB()
'This is stripped down to make it short and simple...
using DB as new Entities(Entities.CreateConnection())
DB.AddHeadline(Me)
RaiseEvent Saved(...)
end using
end sub

'to use the code.
Dim Headline as new Headline()
Headline.ModuleID=ModuleID
Headline.Title="Test headline"
Headline.CreatedByUser=1
Headline.Body="Testing this headline object."
Headline.CreatedDate=Date.Now
Headline.ActivatedDate=Date.Now
Headline.ExpirationDate="12/31/2010 9:55:00AM"

Headline.WriteToDB()

Does this look ok to actually do?

That looks fine to me, but I would not mix real datetime properties
(CreatedDate, ActivatedDate) and string dates (ExpirationDate). Just
use real dates.
 
Family Tree Mike said:
That looks fine to me, but I would not mix real datetime properties
(CreatedDate, ActivatedDate) and string dates (ExpirationDate). Just use
real dates.

OK. and the interface? is it a do or a no do?
 
OK. and the interface? is it a do or a no do?

Sorry, I guess we did get away from the question... :)

No, I don't see a need for any interface for this class based on the
discussion so far.
 
Andy said:
Hi.

I have read tons of blog posts and articles about how and when to use
interfaces. I have seen some people use them to define every object they
create and I have seen some people say to not ever use them. Other people
say not to use them unless you have some actions that multiple objects will
use, an example is like this: You can have an interface that tells all
breeds of cats that they must eat their food, sleep or even meow. Now, you
have different kinds of cats: house cat, farm cat, tiger, lion, panther. All
of these cats do the exact same thing, just in very different ways. This
would be good for the interface because regardless of what kind of cat it
is, the ICat interface will let you define eat(), Meow() and Sleep() without
being stuck to inheriting from a root cat that has this defined. I will go
with this idea, but there has to be more to it than this? An example I deal
with now is this:

I have to create an implement a Headline object that has/does the following
things:

1. It has the properties ModuleID, HeadlineID, Title, BodyText,
CreatedByUser, CreatedDate, ExpirationDate, ActivationDate, Status.
2. It has the events Saving, Saved, Deleting, Deleted, Editing, Edited,
Creating and Created.
3. It has no methods at this point since a headline (news article) really
can't do anything except sit there and look good.

I was told this really wouldn't fit for an interface because it doesn't have
any ability to do anything and that it is just a stateless object. Any ideas
on this matter?

No, the Headline class itself wouldn't fit an interface unless you had
different classes that could *act like* headlines without necessarily
*being* (or actually, inheriting from) one.

As you may know, interfaces are a way to indicate a given behaviour
without providing an inplementation. They are, mainly, an OOP device
for languages that don't have multiple inheritance.

With a language that provides for multiple inheritance, an object may
have more than one ancestor type. For example, a given class may
inherit from both the classes Animal and Food, while other classes
inherit from, say, Animal and Pet, or Food and Vegetable or Animal,
Pet *and* Food (ouch). Each of these base classes contribute to the
derived class "identity". Not only that, those base classes cntribute
with code that the derived class may not need to re-implement.

This is not possible in single inheritance languages (VB.net, C#,
Java...). In such languages a class can only have a single ancestor.
To provide extra functionality, Interfaces were invented: an interface
is like a class without any code in it, just a signature (methods and
properties). A given class may *implement* (that is, provide code for)
any number of interfaces, even though it can only inherit from a
single type.

Moving back to your headline object. Let's assume it's part of a
framework where a Page would contain Headlines, but also Pictures,
Captions, Advertisements, Gadgets, etc. Maybe it would be wise to
provide a base class for all of these items, say, PageElement.
Alternatively, Headlines could also be used in many other contexts: as
part of a NewsArticle, or in a Summary, or as something that could be
listed and edited in a grid, etc, etc. Being just a PageElement
suddenly becomes too restrictive for a HeadLine.

Enter interfaces. For each of these scenarios, you may have generic
behavior that would be required from the participating classes. So,
the elements in a page would be implementations of the IPageElement
interface, with properties such as Page, Position, ZLevel, Hierarchy,
or methods such as Render and Refresh. The elements that could be
edited in a grid would implement the INotifyPropertyChanged interface,
which would raise events whenever a given property had changed. You
could also have your classes implement the ISerializable interface, so
instances could be saved in a arbitrary media.

Implementing those interfaces add "alternate identities" to your base
class, so your Headline class is *also* an IPageElement, an
INotifyPropertyChanged and an ISerializable.

What you don't get by implementing interfaces is a base implementation
-- It's your job to write that code. So, if Headline implements
ISerializable, you must write the code that actually serializes the
object. Compare this with inheritance: when you inherit a class you
usually have some base behavior already implemented.

Just to illustrate, consider when you add a new form to your
application. Your form is a new class which inherits from Form, which
inherits from ContainerControl which inherits from ScrollableControl
which inherits from Control, etc, etc, all way up (or down) to Object:

YourForm >> Form >> ContainerControl >> ScrollableControl >> Control
As you can see, there's a hierarchy, and each hierarchic level
contributes to your form's identity (e.g. an instance of your form is
also a control and a component), but also with implementation (so you
don't need to write the code that manages the form drawing or handles
control intialization, etc, etc). Now, you can decide to change some
of the inherited behavior. To that end, you need only to override the
methods that you intend to change. You don't need, for instance, to
rewrite all the form class from scratch just because you need to
change the way your form handles doubleclicks (fortunately).

Appart from that, your form also implicitly implements a number of
interfaces -- most of which are implemented by those base ancestors:

IComponent, IDisposable, ISynchronizedInvoke, IBindableObject,
IDropTarget, among many others.

These interfaces have no relation with each other or to the classes
that implement then (i.e. no hierarchy whatsoever). In case you decide
to *reimplement* any of then you must completely override the
implementation of a base class, i.e., you must reimplement the
complete interface, not the bits and pieces that you want to add new
behavior. Also, you may implement new interfaces, which will add new
"identities" to your form class, allowing it to be used in other
contexts.

So, as you can (hopefully) see, while inheritance is "atavic" (you and
your brother have the same inheritance, even though you are different
classes), interfaces are "acquired" (you are a doctor, your brother is
a race car lover as well as your neighbor, whom is *also* a doctor).
When to define a new interface? When you need to specify a behavior
that would be provided by classes of different types. When to
implement an interface? When you want your class to participate in
contexts that won't accept its base (or inherited) class.

Hope that helped a little.

Regards,

B.
 
I don't know what others are talking about, they are actually getting away
from the question. This topic is so broad that cannot be described here. I'm
just telling you some facts, you should google about it. Actually Interfaces
are part of a pattern, The pattern .NET framework is based upon, used by
many, and described by GOF (Gang of Four). You should take look on following
architectures:
1. Architecture of KIGG (a clone of DIGG) built using ASP.NET MVC,
2. Event Aggregation
3. implementation of IEnumerable, IEnumerable<T>, IQueriable<T>, IEquatable,
IEditableObject etc......
4. IoC (Inversion of Control), eg. Unity, StructureMap etc.
 
Back
Top