Skip to content

Instantly share code, notes, and snippets.

@YuanLiou
Created January 18, 2018 10:39
Show Gist options
  • Save YuanLiou/81a15ca156f37c147ffd48f98f706d55 to your computer and use it in GitHub Desktop.
Save YuanLiou/81a15ca156f37c147ffd48f98f706d55 to your computer and use it in GitHub Desktop.
Liskov Substitution Principle
public class somebody {
public static void main(String[] argv) {
Store store = new Store();
List<Cellphone> cellphones = store.getCellphones();
for (int i = 0; i < cellphones; i++) {
cellphones[i].power(false);
}
iPhone iphoneX = new iPhone();
cellphones.add(iphoneX);
}
public void callMom(Cellphone cellphone) {
cellphone.call(mom)
}
}
// Cellphone
public interface Cellphone {
void call(People people);
void textMessage(String text);
void power(boolean powerOn);
}
public abstract class Cellphone {}
// Entity
public class Nokia3310 implement Cellphone {
public void strong() { /* WOWOWOWOWOW */ }
@Override
public void call(People people) { /*do something */ }
@Override
public void textMessage(String text) { /* do something */ }
@Override
public void power(boolean powerOn) { /* do something */ }
}
public class iPhone implement Cellphone {
public void surfingInternet() { /* I's so cool!!!! */ }
public void installApp() { /* I's so cool!!!!! Again */ }
@Override
public void call(People people) { /*do something */ }
@Override
public void textMessage(String text) { /* do something */ }
@Override
public void power(boolean powerOn) { /* do something */ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment