<...>
NNTP-Downloader: 2408 messages in group digitalmars.D.ldc.
NNTP-Downloader: < ARTICLE 1133
NNTP-Downloader: > 211 29502 1 29714 D
NNTP-Downloader: < ARTICLE 1134
NNTP-Downloader: > 220 1071 <[email protected]> article
NNTP-Downloader: > Path: digitalmars.com!elf.comlab!yqz
NNTP-Downloader: > From: Yu Qian Zhou <[email protected]>
NNTP-Downloader: > Newsgroups: D
NNTP-Downloader: > Subject: How to avoid code duplication in D? MI/CI/delegation/C&P?
NNTP-Downloader: > Date: Thu, 30 Aug 2001 06:06:50 +0100
NNTP-Downloader: > Organization: Digital Mars
NNTP-Downloader: > Lines: 125
NNTP-Downloader: > Sender: [email protected]
NNTP-Downloader: > Message-ID: <[email protected]>
NNTP-Downloader: > Mime-Version: 1.0
NNTP-Downloader: > Content-Type: TEXT/PLAIN; charset=X-UNKNOWN
NNTP-Downloader: > Content-Transfer-Encoding: QUOTED-PRINTABLE
NNTP-Downloader: > X-Trace: digitaldaemon.com 999147976 26341 163.1.27.82 (30 Aug 2001 05:06:16 GMT)
NNTP-Downloader: > X-Complaints-To: [email protected]
NNTP-Downloader: > NNTP-Posting-Date: Thu, 30 Aug 2001 05:06:16 +0000 (UTC)
NNTP-Downloader: > In-Reply-To: <[email protected]>
NNTP-Downloader: > Xref: digitalmars.com D:1071
NNTP-Downloader: >
NNTP-Downloader: >
NNTP-Downloader: > Admittedly Java's single inheritance is easy to implement, and simple to
NNTP-Downloader: > use. But it does have some defect which sometimes force a programmer to
NNTP-Downloader: > duplicate code by 'copy & paste':
NNTP-Downloader: >
NNTP-Downloader: > Suppose I have two classes 'Car' and 'Dog', which *have already inherited*
NNTP-Downloader: > from some more important classes 'Vehicle' and 'Animal' respectively
NNTP-Downloader: > (let's assume that 'Vehicle' and 'Dog' do not have a common modifiable
NNTP-Downloader: > ancestor); now I want both these two classes to implement another
NNTP-Downloader: > interface 'Idable', how can I do it?
NNTP-Downloader: >
NNTP-Downloader: > Answer:
NNTP-Downloader: > =09 Java: Copy & Paste, code duplication !!!
NNTP-Downloader: > =09 C++: Multiple inheritance, no code duplication
NNTP-Downloader: > =09 C++: Textual code inclusion, no code duplication
NNTP-Downloader: > =09Eiffel: Multiple inheritance, no code duplication
NNTP-Downloader: > =09Sather: Semantic code inclusion, no code duplication
NNTP-Downloader: > Kiev: MI by delegation, no code duplication
NNTP-Downloader: >
NNTP-Downloader: > so what's the solution D can provide?
NNTP-Downloader: >
NNTP-Downloader: > =09 D: ??? code duplication ???
NNTP-Downloader: >
NNTP-Downloader: > D borrowed DbC from Eiffel, perhaps it also worth to check the multiple
NNTP-Downloader: > inheritance mechanism of Eiffel, which is much cleaner than C++ (by
NNTP-Downloader: > renaming and redefinition).
NNTP-Downloader: >
NNTP-Downloader: > Or if you still feel strongly against MI, maybe you can also consider
NNTP-Downloader: > Sather's semantic code inclusion, or Kiev's forward-able delegation?
NNTP-Downloader: >
NNTP-Downloader: > Let me know your thoughts on this.
NNTP-Downloader: >
NNTP-Downloader: > YuQian
NNTP-Downloader: >
NNTP-Downloader: >
NNTP-Downloader: > Sample code:
NNTP-Downloader: > /***********************************************************************
NNTP-Downloader: > Java: Copy & Paste, code duplication
NNTP-Downloader: > ***********************************************************************/
NNTP-Downloader: > interface Idable
NNTP-Downloader: > {
NNTP-Downloader: > public void setId(int i);
NNTP-Downloader: > public void getId();
NNTP-Downloader: > }
NNTP-Downloader: >
NNTP-Downloader: > class Car
NNTP-Downloader: > extends Vehicle
NNTP-Downloader: > implements Idable
NNTP-Downloader: > {
NNTP-Downloader: > private int id;
NNTP-Downloader: > public void setId(int i) { id =3D i; } =20
NNTP-Downloader: > public void getId() { return i; }
NNTP-Downloader: > }
NNTP-Downloader: >
NNTP-Downloader: >
NNTP-Downloader: > class Dog=20
NNTP-Downloader: > extends Animal
NNTP-Downloader: > implements Idable
NNTP-Downloader: > {
NNTP-Downloader: > private int id;=09=09=09=09// copy
NNTP-Downloader: > public void setId(int i) { id =3D i; } =09// &
NNTP-Downloader: > public void getId() { return i; } =09// paste!
NNTP-Downloader: > }
NNTP-Downloader: >
NNTP-Downloader: > /***********************************************************************
NNTP-Downloader: > Kiev: Multiple inheritance by delegation, no code duplication
NNTP-Downloader: > ***********************************************************************/
NNTP-Downloader: > class Id
NNTP-Downloader: > {
NNTP-Downloader: > private int id;
NNTP-Downloader: > public void setId(int i) { id =3D i; } =20
NNTP-Downloader: > public void getId() { return i; }
NNTP-Downloader: > }
NNTP-Downloader: >
NNTP-Downloader: > class =A0Car=A0
NNTP-Downloader: > extends Vehicle
NNTP-Downloader: > {
NNTP-Downloader: > =A0=A0=A0=A0forward=A0public=A0Id myId; // forward car.getId() to car.myI=
NNTP-Downloader: > d.getId()
NNTP-Downloader: > }
NNTP-Downloader: >
NNTP-Downloader: > class =A0Dog
NNTP-Downloader: > extends Animal
NNTP-Downloader: > {
NNTP-Downloader: > =A0=A0=A0=A0forward=A0public=A0Id myId; // forward dog.getId() to dog.myI=
NNTP-Downloader: > d.getId()
NNTP-Downloader: > }
NNTP-Downloader: >
NNTP-Downloader: > /***********************************************************************
NNTP-Downloader: > C++: Multiple inheritance, no code duplication
NNTP-Downloader: > ***********************************************************************/
NNTP-Downloader: > class Idable
NNTP-Downloader: > {
NNTP-Downloader: > private: int id;
NNTP-Downloader: > public:
NNTP-Downloader: > void setId(int i) { id =3D i; } =20
NNTP-Downloader: > void getId() { return i; }
NNTP-Downloader: > };
NNTP-Downloader: >
NNTP-Downloader: > class Car: public Vehicle, Idable {};
NNTP-Downloader: > class Dog: public Animal, Idable {};
NNTP-Downloader: >
NNTP-Downloader: > /***********************************************************************
NNTP-Downloader: > C++: Textual code inclusion, no code duplication
NNTP-Downloader: > ***********************************************************************/
NNTP-Downloader: >
NNTP-Downloader: > //--- begin file: id.ci ---
NNTP-Downloader: > private: int id;
NNTP-Downloader: > public:
NNTP-Downloader: > void setId(int i) { id =3D i; } =20
NNTP-Downloader: > void getId() { return i; }
NNTP-Downloader: > //--- end file: id.ci ---
NNTP-Downloader: >
NNTP-Downloader: >
NNTP-Downloader: > class Car: public Vehicle
NNTP-Downloader: > {
NNTP-Downloader: > #include "id.ci"
NNTP-Downloader: > };
NNTP-Downloader: >
NNTP-Downloader: > class Dog: public Animal
NNTP-Downloader: > {
NNTP-Downloader: > #include "id.ci"
NNTP-Downloader: > };
NNTP-Downloader: >
NNTP-Downloader: >
NNTP-Downloader: > .
NNTP-Downloader: Got message 1071 (<[email protected]>)
/usr/bin/iconv: conversion from X-UNKNOWN unsupported
/usr/bin/iconv: try '/usr/bin/iconv -l' to get the list of supported encodings