473,702 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Interface question

I am puzzled, what is the purpose of an interface? How does it work,
what i mean is how does the compiler treats this? Why when we talk
about separating user interface from business logic, an interface is
declared, what is it's purpose?
thanks,
BRAMOIN

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
12 2831
In the simplest terms, an interface defines _what_ something in your
system does without defining _how_ it does it.

For example, if you define an interface in your business layer that
defines the operations of a data layer, what your business layer is
saying is, "I will work with any data layer that provides the following
properties, events, and methods." The business layer doesn't care which
data layer it's talking to: it may be an object that stores information
in a database, a different object that stores information in a
collection of XML files, or something else.

You could even define a "pluggable" data layer, in which you could
build your application with multiple data layers and then switch
between them at run time depending upon your needs.

Since all the business layer cares about is that the object it's
calling implements the interface defined by the business layer, it
doesn't matter which object it is, exactly.

Interfaces come in very handy in large organizations: the developer who
builds the business layer can also build the data layer interface and
hand it to the data layer developer, saying, "Build this!" If the
interface is clear enough, the data layer developer can "build to
contract". Meanwhile, the business layer developer can use a quickie,
"fake" data layer to develop and start testing his business layer
before the data layer is even finished. So long as the "fake" data
layer and the real data layer implement the same interface, all is
well. (Of course, it's not quite that simple, but you get the idea.)

Nov 17 '05 #2
Bruce,
Can you give me an example of this? Looking for a simple,simple class in
the bussiness layer and an another one in the data layer with code that
uses these. I believe you, but i am having problem invisioning this.
thanks,
BRAMOIN

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
"Meya-awe" <br*****@yahoo. com> a écrit dans le message de news:
en************* *@TK2MSFTNGP14. phx.gbl...
Can you give me an example of this? Looking for a simple,simple class in
the bussiness layer and an another one in the data layer with code that
uses these. I believe you, but i am having problem invisioning this.
thanks,


I have a framework that allows me to store objects in a database, but I
don't want the people who develop the business objects to have to know
anything about SQL or any other database stuff.

So I declare an interface or "contract" that I can talk to in a totally
database independent way.

Here is a simplified example :

public interface IObjectSpace
{
bool StoreObject(obj ect obj);
bool DeleteObject(ob ject obj);
object RetrieveObject( int id);
IList RetrieveCollect ion(Type objectType);
...
}

Now I can call this code like this :

{
IObjectSpace os = new SQLServerObject Space();
IList list = os.RetrieveColl ection(typeof(C ustomer));
int custId = ((Customer) list[0]).Id;
Customer cust = (Customer) os.RetrieveObje ct(custId);
cust.Address = "123 This Street";
os.StoreObject( cust);
}

This code assumes that we are creating an instance of a class that knows how
to manage converting objects to/from SQLServer tables.

However, by just changing the first line of that code example, and assuming
I have another class that represents a mechanism for converting objects
t/from XML, I can work with a test "database" which is a simple XML storage
mechanism.

{
IObjectSpace os = new TestXMLObjectSp ace();
IList list = os.RetrieveColl ection(typeof(C ustomer));
int custId = ((Customer) list[0]).Id;
Customer cust = (Customer) os.RetrieveObje ct(custId);
cust.Address = "123 This Street";
os.StoreObject( cust);
}

Notice that it is only the first line of the example that has changed; all
the rest of the code remains identical. This is because I have defined a
contract that I expect any class that implements IObjectSpace to fulfil.

Now I can design my business classes and test them against my own XML test
object space, whilst someone else goes off and writes the SQLServer version.
When they have finished all I have to change is that one line of code that
creates the actual object space instance.

Does that help ?

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #4
Joanna,
Yes, that helped a lot to understand it. Since the methods in an
interface have to be defined by the class(es) implementing the
interface, there would be two implementations one for SQL and one for
XML. So, an interface forces the higher level logic using an object,
implementing the interface to adhere to a contract defined by the
interface.
Ever since i posted this, i have been reading about this subject. And
now the question is, when would i use "Interface Polymorphism" or
"Inheritanc e polymorphism" or "Polymorphi sm through abstract classes"?
thanks,
BRAMOIN

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5
"Meya-awe" <br*****@yahoo. com> a écrit dans le message de news:
eN************* *@tk2msftngp13. phx.gbl...
Ever since i posted this, i have been reading about this subject. And
now the question is, when would i use "Interface Polymorphism" or
"Inheritanc e polymorphism" or "Polymorphi sm through abstract classes"?


If I remember correctly, interface polymorphism relies on the implementing
class to execute code that may vary depending on the class used to implement
the interface, this kind of polymorphism can cross class hierarchy
boundaries; inheritance polymorphism is simply using virtual/overridden
methods within a single class hierarchy; polymorhism through abstract
classes also can only occur within a single hierarchy, but a pure abstract
class is the equivalent of an interface.

Does that help or hinder ? :-)

Joanna

--
Joanna Carter (TeamB)

Consultant Software Engineer
TeamBUG support for UK-BUG
TeamMM support for ModelMaker
Nov 17 '05 #6
> Notice that it is only the first line of the example that has changed; all
the rest of the code remains identical.

I am newbie too. You don`t convince me of use interfaces yet .
I think, we obtain the same results using simply Polimorphism and
we can do without interfaces.
Nov 17 '05 #7
Slawek Weclewski <we*****@NOSPAM pocta.onet.bola nda> wrote:
Notice that it is only the first line of the example that has changed; all
the rest of the code remains identical.

I am newbie too. You don`t convince me of use interfaces yet .
I think, we obtain the same results using simply Polimorphism and
we can do without interfaces.


No, you can't. Without interfaces or multiple inheritance, how would
write the equivalent of a class which implements (say) IDisposable and
IComparable?

Interfaces are incredibly useful at completely separating interface
from implementation. Why should two classes have to have anything in
common in terms of an inheritance hierarchy just to expose the same
interface?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #8
The classic example of interfaces comes from Java, which has, I
believe, IPrintable, or something like that.

This clearly indicates the division between interfaces and the class
hierarchy: two objects that have nothing to do with each other might
both want to be Printable. So, they both implement IPrintable, and you
can write other code that does things with "Printable" objects with no
regard as to where in the class hierarchy they might fall.

For example, both a Document and an Image might be Printable. Do you
really want to _have_ to inherit both of them from a Printable parent
class?

You can argue that a language with multiple inheritance has no need for
interfaces, but single-inheritance languages like Java and C#
definitely need them, otherwise you end up with a tortured class
hierarchy just to get completely dissimilar things to "look the same"
for certain purposes.

Nov 17 '05 #9
The choice between interfaces (which automatically imply
polymorphism... if you aren't using them in only one class then they
aren't terribly useful), inheritance polymorphism, and abstract classes
really has to do with the design of your class hierarchy.

This is the beauty of interfaces: you don't inherit from something
because "I need that functionality in my derived class". Well,
sometimes you do, just to be lazy (guilty). What you're supposed to do,
however, is inherit from a parent class because, in real-world terms,
what you're making "is a" specialized case of the parent class.

So, a Dog class would inherit from the Mammal class, because a Dog "is
a" Mammal. Or, you might have two types of PurchaseOrder: an
InternalPurchas eOrder and an ExternalPurchas eOrder. You arrange these
in an inheritance hierarchy because they are logically related.

So, in the case of the purchase orders, you can write code that deals
with PurchaseOrder in general, without worrying about whether it is an
InternalPurchas eOrder or an ExternalPurchas eOrder. Some of your code
will be that way: it won't matter which one you're dealing with. That's
inheritance polymorphism.

However, when you design these three classes, you have to ask yourself
if a PurchaseOrder is a "real" thing in your business: can you make
one, without it being internal or external? In some businesses, there
may be a third type of purchase order that is sort of a basic purchase
order, but in most businesses, the answer will be "no". So, you make
PurchaseOrder abstract: it's an artifact of your class hierarchy... it
doesn't represent a real business object. You need it, because an
InternalPurchas eOrder "is _not_ a" ExternalPurchas eOrder, and an
ExternalPurchas eOrder "is _not_ a" InternalPurchas eOrder, but you still
want them to be related, so you make this fake ("abstract") class that
is the "parent" of them both, so you can, in some of your code, treat
them as equivalent. That's polymorphism through abstract classes.

So, the difference between those two is whether the parent class is
something that logically exists in its own right, or is just a
side-effect of the way you want to arrange your class hierarchy.

Interfaces come into play when the classes you want to treat as
equivalent for some purposes have _nothing_ in common as far as the
class hierarchy is concerned. The only thing they have in common is
that they all offer the same functionality. So, rather than building a
tortured class hierarchy to try to make all of these classes have a
common parent where you can put the functionality, you declare an
interface, and make all of the classes implement it. Then you can write
code that treats the classes as equivalent.

For example, I just built a special kind of ListView, and a special
kind of Panel in WinForms. They are both backed by something called a
ListViewModel, which is the brains behind managing item selection,
sorting, etc. However, I can't have both of these classes inherit from
a common ancestor: they have to inherit from ListView and Panel,
respectively.

That wasn't a problem up until this week, when I decided that I wanted
to make another control that could interact with any control that was
backed by a ListViewModel. Now, suddenly, I have a class that wants to
treat these other two classes (and any others I may build) as
equivalent, but they have no common ancestor (that I can modify). So, I
created an interface called IHasListViewMod el, with one property:
Model. Now I can write my new control to search for and interact with
any control that implements the IHasListViewMod el interface, and it
will know that it can get the model and talk to it, because any control
implementing that interface has to have a ListViewModel behind it.

Notice that, in a single-inheritance language like C#, that was the
_only_ way to solve the problem, other than the totally gross solution
of having my new control know, explicitly, about every class I make
that has ListViewModel, like this:

if (aControl is ListViewForSelf KeyedCollection )
{
ListViewForSelf KeyedCollection lv =
(ListViewForSel fKeyedCollectio n)aControl;
}
if (aControl is ProfileImageGal lery)
{
ProfileImageGal lery pig = (ProfileImageGa llery)aControl;
}
if (aControl is LabelForSelfKey edCollection)
{
... etc.

Ewwww... yuck. Now I have to modify my little helper control every time
I add a new one of these. Gross. With the interface, I just say:

if (aControl is IHasListViewMod el)
{
IHasListViewMod el lvm = (IHasListViewMo del)aControl;
ListViewModel model = lvm.Model;
... off to the races ...
}

Much better, no?

Nov 17 '05 #10

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

Similar topics

65
5371
by: perseus | last post by:
I think that everyone who told me that my question is irrelevant, in particular Mr. David White, is being absolutely ridiculous. Obviously, most of you up here behave like the owners of the C++ language. A C++ interface installation IS ABOUT THE C++ LANGUAGE! The language does not possess the ability to handle even simple file directory manipulation. Those wise people that created it did not take care of it. So, BOOST is a portable...
26
2099
by: Marius Horak | last post by:
As in subject. Thanks MH
4
8578
by: jm | last post by:
Consider: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconwhenshouldiimplementinterfacesinmycomponent.asp // Code for the IAccount interface module. public interface IAccount { void PostInterest(); void DeductFees(IFeeSchedule feeSchedule); }
20
4256
by: Ole Hanson | last post by:
I am accessing my database through an interface, to allow future substitution of the physical datastore - hence I would like to declare in my Interface that my DAL-objects implementing the interface and accessing the datastore MUST pass in a UserToken in the constructor of the object. Is this not possible? Am I forced to add the UserToken as a property on the object instead? /Ole
13
3255
by: John Salerno | last post by:
Hi all. I have a question about interfaces now. According to the book I'm reading, when you implement an interface, the class or structure has to declare all the methods that the interface includes. My question is, if you must declare each method (as opposed to simply 'using' those methods like an #included file), then why not just declare the methods on your own as if you created them yourself? What advantage does using an interface...
6
6078
by: John Salerno | last post by:
I understand how they work (basically), but I think maybe the examples I'm reading are too elementary to really show their value. Here's one from Programming C#: #region Using directives using System; using System.Collections.Generic; using System.Text;
10
667
by: Joe | last post by:
My question is more an OOD question. I know *how* to implement both abstract classes and interfaces. Here's my question - under what circumstacnes does one use an abstract class and under what circumstacnes does one implement an interface? TIA, -- Joe VBA Automation/VB/C++/Web and DB development
4
2849
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. '**************************************************************************** ' Issues '****************************************************************************
5
1667
by: Colin McGuire | last post by:
Hi all, when I write the class below Private Class employee End Class and then add the line "Implements IVF" which is an interface I have written, the IDE modifies my code to display
52
20891
by: Ben Voigt [C++ MVP] | last post by:
I get C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member 'UselessJunkForDissassembly.IInvocableInternals.OperationValidate(string)' C:\Programming\LTM\devtools\UselessJunkForDissassembly\Class1.cs(360,27): error CS0535: 'UselessJunkForDissassembly.InvocableInternals' does not implement interface member...
0
8739
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8652
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8983
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8940
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7831
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6575
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5907
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2036
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.