Ejemplo Liskov con cuentas corrientes

package com.trifulcas.liskov;

public abstract class BankAcount {
	protected double balance;
	 
    public BankAcount(double balance) {
		super();
		this.balance = balance;
	}

	public void Deposit(double amount) {
        balance += amount;
        System.out.printf("Deposit: %.2f, Total Amount: %.2f\n", amount, balance);
    }
 
    public abstract void Withdraw(double amount);
 
    public double GetBalance() {
        return balance;
    }

	@Override
	public String toString() {
		return "BankAcount [balance=" + balance + "]";
	}
    
}

package com.trifulcas.liskov;

public class RegularAccount extends BankAcount {

	public RegularAccount(double balance) {
		super(balance);
		// TODO Auto-generated constructor stub
	}

	@Override
	 public void Withdraw(double amount) {
        if (balance >= amount) {
            balance -= amount;
            System.out.printf("Withdraw: %.2f, Balance: %.2f\n", amount, balance);
        } else {
            System.out.printf("Trying to Withdraw: %.2f, Insufficient Funds, Available Funds: %.2f\n", amount, balance);
        }
    }

}

package com.trifulcas.liskov;

public class FixedAccount extends BankAcount {
	public FixedAccount(double balance) {
		super(balance);
		// TODO Auto-generated constructor stub
	}

	private boolean termEnded = false; // simplification for the example

	@Override
	public void Withdraw(double amount) {
		if (!termEnded) {
			System.out.println("Cannot withdraw from a fixed term deposit account until term ends");
		} else if (balance >= amount) {
			balance -= amount;
			System.out.printf("Withdraw: %.2f, Balance: %.2f\n", amount, balance);
		} else {
			System.out.printf("Trying to Withdraw: %.2f, Insufficient Funds, Available Funds: %.2f\n", amount, balance);
		}
	}

}

package com.trifulcas.liskov;

import java.util.ArrayList;

public class Customer {
	private String name;
	private ArrayList<BankAcount> counts;

	public Customer(String name) {
		super();
		this.name = name;
		counts = new ArrayList<>();
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	public void addRegular(int amount) {
		counts.add(new RegularAccount(amount));
	}

	public void addFixed(int amount) {
		counts.add(new FixedAccount(amount));
	}

	public void addAll(int amount) {
		// Usando Liskov. Todas las subclases se tratan como la superclase
		for (BankAcount ba : counts) {
			ba.Deposit(amount);
		}
	}

	@Override
	public String toString() {
		return "Customer [name=" + name + ", counts=" + counts + "]";
	}

}

package com.trifulcas.liskov;

public class TestLiskov {
	public static void main(String[] args) {
		Customer ana=new Customer("Ana Pi");
		
		ana.addFixed(400);
		ana.addRegular(300);
		ana.addFixed(300);
		
		System.out.println(ana);
		
		ana.addAll(1000);
		System.out.println(ana);
	}
}

Publicado por

Juan Pablo Fuentes

Formador de programación y bases de datos