Skip to content

Instantly share code, notes, and snippets.

@malalanayake
Created April 11, 2016 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malalanayake/2572aa7b59f25e410ab1f0c7d18021d3 to your computer and use it in GitHub Desktop.
Save malalanayake/2572aa7b59f25e410ab1f0c7d18021d3 to your computer and use it in GitHub Desktop.
Liskov's Substitution Principle - Good sample code
package sample.design.liskov.substitution.good;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 3:52:15 PM
*
* @blog https://malalanayake.wordpress.com/
*/
public class CarNew extends VehicleNew {
@Override
public void setRimSize(float rimSize) {
super.setRimSize(rimSize);
}
@Override
public void setTireSize(float tireSize) {
super.setTireSize(tireSize);
}
@Override
public void initEngine() {
System.out.println("[START:Car Engine]");
}
}
package sample.design.liskov.substitution.good;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 3:57:49 PM
*
* @blog https://malalanayake.wordpress.com/
*/
public class LSPTestNew {
public static void main(String[] args) {
VehicleNew vehicle = new VehicleNew();
vehicle.setRimSize(10);
vehicle.setTireSize(12);
vehicle.initEngine();
vehicle.print();
VehicleNew car = new CarNew();
car.setRimSize(10);
car.setTireSize(12);
car.initEngine();
car.print();
}
}
package sample.design.liskov.substitution.good;
/**
*
* Distibution under GNU GENERAL PUBLIC LICENSE Version 2, June 1991
*
* @author dmalalan
* @created Apr 11, 2016 3:41:57 PM
*
* @blog https://malalanayake.wordpress.com/
*/
public class VehicleNew {
private float tireSize;
private float rimSize;
public float getTireSize() {
return tireSize;
}
public float getRimSize() {
return rimSize;
}
public void setTireSize(float tireSize) {
this.tireSize = tireSize;
}
public void setRimSize(float rimSize) {
this.rimSize = rimSize;
}
public void initEngine() {
System.out.println("[START:Vehicle Engine]");
}
public void start() {
System.out.println("[ENTER:KEY]");
initEngine();
System.out.println("[INIT:DashBoard Connection]");
}
public void print() {
System.out.println("RIM Size:" + rimSize + " TIRE Size:" + tireSize + "");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment