开发者

java: inheritance

What are some alternati开发者_如何转开发ves to inheritance?


Effective Java: Favor composition over inheritance. (This actually comes from Gang of Four, as well).

The case he makes is that inheritance can cause a lot of unseemly side effects, if the extended class was not explicitly designed to be inherited. For example, any calls to super.someMethod() can lead you through unexpected paths of unknown code.

Instead, hold a reference to the class you would have otherwise extended, and delegate to it.

Here is a link to an interview with Erich Gamma where he talks about the concept a bit.


Some alternatives:

  • Delegation, also known as Composition
  • Wikipedia article on limitations and alternatives to inheritance
  • Q:Inheritance seems error-prone. How can I guard against these errors? And, what are the alternatives?


I'm assuming you're playing with Java which has some rules about inheritance.

Implementing interfaces is a common and oft used option. So, for example, if you have a data-source class that talks to a RDBMS, rather than inheriting that class and using it to implement a NoSQL datasource, the both can implement the same interface.

Rather than..

public class RDBMSDataSource {

  ...

  public String loadSomeDataFromDataSource() {
    ...Do some stuff...
  }
}

public class NoSQLDataSource extends RDBMSDataSource {

   ...

   @Override
   public String loadSomeDataFromDataSource() {
      ...Do some other stuff...
   }
}

public class DataSourceClient {

   public void foo() {
      RDBMSDataSource ds = new NoSQLDataSource();
      ds.loadSomeDataFromDataSource();
   }
}

Which works but is hard to read, you could use this...

public interface DataSource {
   public String loadSomeDataFromDataSource();
}

public class RDBMSDataSource implements DataSource {

  ...

  public String loadSomeDataFromDataSource() {
    ...Do some stuff...
  }
}

public class NoSQLDataSource implements DataSource {

   ...

   @Override
   public String loadSomeDataFromDataSource() {
      ...Do some other stuff...
   }
}

public class DataSourceClient {

   public void foo() {
      DataSource ds = new NoSQLDataSource();
      ds.loadSomeDataFromDataSource();
   }
}

Another option would be composition. Say you have employees and customers. Your two options would be...

public class Person {

   protected String name;
   protected String address;
   ...More stuff...
}

public class Employee extends Person {

   protected String jobCode;
   protected String department;
   ...More stuff...
}

public class Customer extends Person {

   protected String salesPerson;
   protected Date registrationDate;
   ...More stuff...
}

...or...

public class ContactInfo {
   private String name;
   private String address;
   ...More stuff...
}

public class Employee {

   private ContactInfo contactInfo;
   private String jobCode;
   private String department;
   ...More stuff...
}

public class Customer {

   private ContactInfo contactInfo;
   private String salesPerson;
   private Date registrationDate;
   ...More stuff...
}

Since Java does not have multiple inheritance and you can implement multiple interfaces, you sometimes need to do the above to make sure your development is clean and readable.


I realise this is not Java per se, but Scala (a language running on the Java Virtual Machine) permits mixins (known as traits in Scala).

Mixins allow you to slot some functionality alongside an existing class, rather than within the inheritance tree.

When a class includes a mixin, the class implements the interface and includes, rather than inherits, all the mixin's attributes and methods. They become part of the class during compilation


Delegation is an alternative to inheritance.


I think you should try delegation, delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable. and it plays a beneficial role that it doesn't force you to accept all the methods of the super class.


Try Delegation which is also know as Composition

Also found the new alternative as mixin

The mixins are kind of composable abstract classes. They are used in a multi-inheritance context to add services to a class. The multi-inheritance is used to compose your class with as many mixins as you want. For example, if you have a class to represent houses, you can create your house from this class and extend it by inheriting from classes like Garage and Garden. Here is this example written in Scala:

val myHouse = new House with Garage with Garden

You will get more info about mixin https://kerflyn.wordpress.com/2012/07/09/java-8-now-you-have-mixins/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜