jeudi 25 janvier 2018

Working with subclasses that have dissimilar methods without violating SOLID principles

//interfaces

public interface Singer{
    void sing();
}
public interface SongWriter{
    void writeSong();
}

//Implementations

public class PureSinger implements Singer{
    void sing(){}
}
public class SingerSongWriter implements Singer, SongWriter{
    void sing(){}
    void writeSong(){}
}

//Client code

void methodA(){
    Singer objPureSinger = new PureSinger();
    Singer objSingerSWer = new SingerSongWriter();

    doSomething(objPureSinger);
    doSomething(objSingerSWer);
}

public void doSomething(Singer obj){
    obj.sing();
    obj.writeSong();    //<--- this does not work.
}

In order to acheve this type of code, how should I design the class structure?

Aucun commentaire:

Enregistrer un commentaire