Default Implementation For an Interface

Jacob Carlborg doob at me.com
Fri Feb 17 09:21:36 PST 2012


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


More information about the Digitalmars-d-learn mailing list