February 17, 2012 Re: Default Implementation For an Interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kevin | On Wed, 15 Feb 2012 22:01:51 -0500, Kevin <kevincox.ca@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. Note that template methods are supposed to work (And also are implicitly final), but this doesn't currently work. http://d.puremagic.com/issues/show_bug.cgi?id=4174 -Steve |
February 17, 2012 Re: Default Implementation For an Interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On 2012-02-17 18:04, Steven Schveighoffer wrote: > On Wed, 15 Feb 2012 22:01:51 -0500, Kevin <kevincox.ca@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 |
February 17, 2012 Re: Default Implementation For an Interface | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg Attachments:
| 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@me.com> wrote:
> On 2012-02-17 18:04, Steven Schveighoffer wrote:
>
>> On Wed, 15 Feb 2012 22:01:51 -0500, Kevin <kevincox.ca@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
>
|
Copyright © 1999-2021 by the D Language Foundation