Default Implementation For an Interface

Kevin Cox kevincox.ca at gmail.com
Fri Feb 17 13:42:50 PST 2012


I wasn't looking to implement meathods in the interface, I was looking to
have a default class that implements the interface that would be created if
you called `new Interface();`  I don't think this is possible. and now that
I think about it I think that it is for a good reason.

On Fri, Feb 17, 2012 at 12:21 PM, Jacob Carlborg <doob at me.com> wrote:

> On 2012-02-17 18:04, Steven Schveighoffer wrote:
>
>> On Wed, 15 Feb 2012 22:01:51 -0500, Kevin <kevincox.ca at gmail.com> wrote:
>>
>>  I was implementing a framework and I found that I wanted two things.
>>> - A strong set of interfaces so that I can get what I want from a
>>> variety of sources.
>>> - Some basic implementations of these interfaces.
>>>
>>> For example, say I was writing a database class. I could either name
>>> the interface Database and call the class DatabaseImplementation or
>>> something but that is ugly. If I call the interface IDatabase, the
>>> Database class looks nice but I need to convince users to write
>>> functions that take IDatabases not Databases.
>>>
>>> I was wondering if there was any way to implement a default
>>> implementation. This way, I could create my Database interface and
>>> classes could implement that but if you called `new Database()` you
>>> would still get a basic database.
>>>
>>
>> Aside from what has been said already, if you wish to have methods that
>> are not static defined in the interface, final methods currently work:
>>
>> interface I
>> {
>> void foo();
>> final void callFoo() {writeln("about to call foo"); foo(); writeln("ok,
>> I called foo");}
>> }
>>
>> This isn't exactly a "default implementation", since you can't override
>> it.
>>
>
> But you could have one final method, the implementation and one virtual,
> the one you would override. It's an idea, I don't know if it's a good one.
>
>
> --
> /Jacob Carlborg
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120217/bce8d072/attachment.html>


More information about the Digitalmars-d-learn mailing list