BAEL-4006 | Liskov Substitution Principle in Java (#9431)

* BAEL-4006 | Liskov Substitution Principle in Java

* BAEL-4006 | UNDO unintentional commit to pom.xml

* BAEL-4006 | Code review feedback implementation

* BAEL-4006 | Changes made to support the article review feedback implementation

* BAEL-4006 | Minor change to a comment
This commit is contained in:
rdevarakonda 2020-07-25 10:43:52 +01:00 committed by GitHub
parent 1398093dcc
commit 41c8fa83ef
21 changed files with 369 additions and 0 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public abstract class Account {
protected abstract void deposit(BigDecimal amount);
/**
* Reduces the account balance by the specified amount
* provided given amount > 0 and account meets minimum available
* balance criteria.
*
* @param amount
*/
protected abstract void withdraw(BigDecimal amount);
}

View File

@ -0,0 +1,15 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public class BankingAppWithdrawalService {
private Account account;
public BankingAppWithdrawalService(Account account) {
this.account = account;
}
public void withdraw(BigDecimal amount) {
account.withdraw(amount);
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.l.advanced;
public class Bar extends Foo {
@Override
// precondition: 0 < num <= 10
public void doStuff(int num) {
if (num <= 0 || num > 10) {
throw new IllegalArgumentException("Input out of range 1-10");
}
// some logic here...
}
@Override
// precondition: 0 < num <= 3
public void doOtherStuff(int num) {
if (num <= 0 || num > 3) {
throw new IllegalArgumentException("Input out of range 1-3");
}
// some logic here...
}
@Override
public Integer generateNumber() {
return new Integer(10);
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.l.advanced;
public abstract class Car {
protected int limit;
// invariant: speed < limit;
protected int speed;
// Allowed to be set once at the time of creation.
// Value can only increment thereafter.
// Value cannot be reset.
protected int mileage;
public Car(int mileage) {
this.mileage = mileage;
}
protected abstract void turnOnEngine();
// postcondition: speed < limit
protected abstract void accelerate();
// postcondition: speed must reduce
protected abstract void brake();
}

View File

@ -0,0 +1,15 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public class CurrentAccount extends Account {
@Override
protected void deposit(BigDecimal amount) {
// Deposit into CurrentAccount
}
@Override
protected void withdraw(BigDecimal amount) {
// Withdraw from CurrentAccount
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.l.advanced;
public class ElectricCar extends Car {
public ElectricCar(int mileage) {
super(mileage);
}
@Override
protected void turnOnEngine() {
throw new AssertionError("I am an Electric Car. I don't have an engine!");
}
@Override
protected void accelerate() {
// this acceleration is crazy!
}
@Override
protected void brake() {
// Apply ElectricCar brake
}
}

View File

@ -0,0 +1,18 @@
package com.baeldung.l.advanced;
import java.io.IOException;
public class FilePurgingJob {
private FileSystem fileSystem;
public FilePurgingJob(FileSystem fileSystem) {
this.fileSystem = fileSystem;
}
public void purgeOldestFile(String path) throws IOException {
if (!(fileSystem instanceof ReadOnlyFileSystem)) {
// code to detect oldest file
fileSystem.deleteFile(path);
}
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.l.advanced;
import java.io.File;
import java.io.IOException;
public interface FileSystem {
File[] listFiles(String path);
void deleteFile(String path) throws IOException;
}

View File

@ -0,0 +1,15 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public class FixedTermDepositAccount extends Account {
@Override
protected void deposit(BigDecimal amount) {
// Deposit into this account
}
@Override
protected void withdraw(BigDecimal amount) {
throw new UnsupportedOperationException("Withdrawals are not supported by FixedTermDepositAccount!!");
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.l.advanced;
public abstract class Foo {
// precondition: 0 < num <=5
public void doStuff(int num) {
if (num <= 0 || num > 5) {
throw new IllegalArgumentException("Input out of range 1-5");
}
// some logic here...
}
// precondition: 0 < num <=5
public void doOtherStuff(int num) {
if (num <= 0 || num > 5) {
throw new IllegalArgumentException("Input out of range 1-5");
}
// some logic here...
}
public abstract Number generateNumber();
}

View File

@ -0,0 +1,28 @@
package com.baeldung.l.advanced;
public class HybridCar extends Car {
// invariant: charge >= 0;
private int charge;
public HybridCar(int mileage) {
super(mileage);
}
@Override
protected void turnOnEngine() {
// Start HybridCar
}
@Override
// postcondition: speed < limit
protected void accelerate() {
// Accelerate HybridCar speed < limit
}
@Override
// postcondition: speed must reduce
// postcondition: charge must increase
protected void brake() {
// Apply HybridCar brake
}
}

View File

@ -0,0 +1,25 @@
package com.baeldung.l.advanced;
public class MotorCar extends Car {
public MotorCar(int mileage) {
super(mileage);
}
@Override
protected void turnOnEngine() {
// Start MotorCar
}
@Override
// postcondition: speed < limit
protected void accelerate() {
// Accelerate MotorCar
}
@Override
// postcondition: speed must reduce
protected void brake() {
// Apply MotorCar brake
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.l.advanced;
import java.io.File;
import java.io.IOException;
public class ReadOnlyFileSystem implements FileSystem {
public File[] listFiles(String path) {
// code to list files
return new File[0];
}
public void deleteFile(String path) throws IOException {
// Do nothing.
// deleteFile operation is not supported on a read-only file system
}
}

View File

@ -0,0 +1,15 @@
package com.baeldung.l.advanced;
import java.math.BigDecimal;
public class SavingsAccount extends Account {
@Override
protected void deposit(BigDecimal amount) {
// Deposit into SavingsAccount
}
@Override
protected void withdraw(BigDecimal amount) {
// Withdraw from SavingsAccount
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.l.advanced;
public class ToyCar extends Car {
public ToyCar(int mileage) {
super(mileage);
}
protected void turnOnEngine() {
}
protected void accelerate() {
}
protected void brake() {
}
public void reset() {
mileage = 0;
}
}

View File

@ -0,0 +1,7 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public abstract class Account {
protected abstract void deposit(BigDecimal amount);
}

View File

@ -0,0 +1,17 @@
package com.baeldung.l.advanced.refactored;
import com.baeldung.l.advanced.Account;
import java.math.BigDecimal;
public class BankingAppWithdrawalService {
private WithdrawableAccount withdrawableAccount;
public BankingAppWithdrawalService(WithdrawableAccount withdrawableAccount) {
this.withdrawableAccount = withdrawableAccount;
}
public void withdraw(BigDecimal amount) {
withdrawableAccount.withdraw(amount);
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public class CurrentAccount extends WithdrawableAccount {
protected void deposit(BigDecimal amount) {
// Deposit into CurrentAccount
}
protected void withdraw(BigDecimal amount) {
// Withdraw from CurrentAccount
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public class FixedTermDepositAccount extends Account {
protected void deposit(BigDecimal amount) {
// Deposit into this account
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public class SavingsAccount extends WithdrawableAccount {
protected void deposit(BigDecimal amount) {
// Deposit into SavingsAccount
}
protected void withdraw(BigDecimal amount) {
// Withdraw from SavingsAccount
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.l.advanced.refactored;
import java.math.BigDecimal;
public abstract class WithdrawableAccount extends Account {
/**
* Reduces the account balance by the specified amount
* provided given amount > 0 and account meets minimum available
* balance criteria.
*
* @param amount
*/
protected abstract void withdraw(BigDecimal amount);
}