473,396 Members | 2,154 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.

Implementation of interface method is the same in all of the interface's implementations

I'm somewhat of a newbie.

This question has come up for me a couple of times, and it came up
again today: I have an interface IProviderFileVersionNumber that has
this method called GetOrderable. The implementation of GetOrderable
is _always_ exactly the same in each class that implements
IProviderFileVersionNumber, and always will be. It doesn't depend on
any particular details of the implementation. In a sense, I'd like to
put the code for GetOrderable in the interface, but of course I
cannot.

I could make the interface a public abstract class, except that I
think I need multiple inheritance. I have another interface
inheriting from IProviderFileVersionNumber called
IProviderFileVersionNumberOrderable (yes, I know these names are
getting annoyingly long), which needs to be fairly general. On the
other hand, I have specific implementations of these called
PerforceVersionNumber and PerforceVersionNumberOrderable. There is
lots of Perforce-specific stuff in PerforceVersionNumber that
PerforceVersionNumberOrderable needs to inherit, but as you can tell
from the names, PerforceVersionNumberOrderable also needs to be an
IProviderFileVersionNumberOrderable.

If I could multiply inherit classes, the answer would be obvious, but
I can't. Is there any way to avoid duplicating the same code for
GetOrderable across 10 different classes?

Thanks!
Steve Brown

Mar 15 '07 #1
6 1274
<St***********@gmail.comwrote:

<snip>
If I could multiply inherit classes, the answer would be obvious, but
I can't. Is there any way to avoid duplicating the same code for
GetOrderable across 10 different classes?
Create a class which has the appropriate code in and nothing else, then
create an instance of that class in each of your implementations of
IProviderFileVersionNumber. Proxy the call to GetOrderable to that
instance.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 16 '07 #2
Jon Skeet [C# MVP] <sk***@pobox.comwrote in
news:MP************************@msnews.microsoft.c om:
<St***********@gmail.comwrote:

<snip>
>If I could multiply inherit classes, the answer would be obvious, but
I can't. Is there any way to avoid duplicating the same code for
GetOrderable across 10 different classes?

Create a class which has the appropriate code in and nothing else, then
create an instance of that class in each of your implementations of
IProviderFileVersionNumber. Proxy the call to GetOrderable to that
instance.
Is it at all possible to do this in a smarter way? I have also reasonably
often come across this type of problem, and end up making a
"helper/utility" class. You still have to write the methods in the
implementations and redirect the call to the helper class - it would be
nicer if there was some sort of inheritance possibility.

/Peter
Mar 16 '07 #3
On Fri, 16 Mar 2007 02:33:06 -0700, Peter K <xd****@hotmail.com>
wrote:
>Is it at all possible to do this in a smarter way?
Not in C# as it stands, but Jon Skeet himself has thought of one:
http://msmvps.com/blogs/jon.skeet/ar...e-mix-ins.aspx

Here he also recommends abolishing inheritance altogether which IMO is
going too far. But an automatic proxy mechanism that generates simple
forwarder methods for interfaces would be very useful regardless.
--
http://www.kynosarges.de
Mar 16 '07 #4
I must brush up on my Reflection.Emit... there must be some
possibilities here... (hums merrily)

Marc
Mar 16 '07 #5
Chris Nahr <di******@kynosarges.dewrote:
On Fri, 16 Mar 2007 02:33:06 -0700, Peter K <xd****@hotmail.com>
wrote:
Is it at all possible to do this in a smarter way?

Not in C# as it stands, but Jon Skeet himself has thought of one:
http://msmvps.com/blogs/jon.skeet/ar...cky-ideas-1-in
heritance-is-dead-long-live-mix-ins.aspx
Gosh, and here I was thinking no-one had actually read it :)
Here he also recommends abolishing inheritance altogether which IMO is
going too far. But an automatic proxy mechanism that generates simple
forwarder methods for interfaces would be very useful regardless.
I think "recommends" is a bit stronger than I'd put it. It was a wacky
idea that I pushed a little bit beyond sanity :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Mar 16 '07 #6
Looks interesting... I'm not sure I'd jump on the "ditch inheritance"
wagon quite so quickly, but I see where you are coming from. What
would be interesting, however, is the provision of "proxies" as a
language extension, i.e. another tool at our disposal. I don't see how
this would be much different to the language support of closures /
captured variables etc - i.e. the compiler doing some tedious plumbing
for us at a stroke while still leaving the other tools intact.

Marc

Mar 16 '07 #7

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

Similar topics

3
by: masood.iqbal | last post by:
In this day and age, you never say no to any work that is thrown at you ---- so when I was offered this short-term contract to convert legacy C code to C++, I did not say no. Personally I believed...
5
by: Mark | last post by:
Below I've created an interface ... why do all implementations of the methods have to be public? What if I want them to be private or protected? public interface IOisWebPageStandard { void...
5
by: Keith Patrick | last post by:
Could someone tell me if it's possible (and if so, how) to call an explicitly-implemented interface method from a subclass? I have a class in which I have to explicity implement some methods, but...
1
by: Mitan | last post by:
Hello, I'm a beginner with what appears to be a simple question for which I haven't been able to get an answer. Can someone explain what "implementation code" is in relation to VB.NET? I want to be...
6
by: Zach | last post by:
Hi everyone, I'm a bit of a newbie to C# programming so forgive this innocent question, but coming from a C++ background this seems very odd to me, and I'm hoping someone can shed some light...
2
by: Kevin Frey | last post by:
Consider this simple example: interface IReader { bool Read( ); }; class MyReader : IReader { bool Read( ); // or should it be bool IReader.Read( ) ?
20
by: Luc Kumps | last post by:
(Sorry about the previous post, it got transmitted before it was complete) We try to separate implementation and interface defintions, but we run into a problem. I hope the guru's can solve this,...
7
by: Mukesh | last post by:
Hi, Consider a situation where a class (Say C1) is implementing two interfaces (I1, I2). Both the interface contains a method (M1) with the same signature. class C1:I1, I2 { //implementation...
3
by: Armin Zingler | last post by:
Hi, sorry for this C++ noob question... I'm coming from VB.Net actually and I am trying to do the same in C++ (/CLR) as I do here (pseudo code partial): class C implements I1 implements I2...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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...

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.