If S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program.
Here subtyping means Inheritance
- When a child Class cannot perform the same actions as its parent Class, this can cause bugs.
- If you have a Class and create another Class from it, it becomes a parent and the new Class becomes a child. The child Class should be able to do everything the parent Class can do. This process is called Inheritance.
- The child Class should be able to process the same requests and deliver the same result as the parent Class or it could deliver a result that is of the same type.
The picture shows that the parent Class delivers Coffee(it could be any type of coffee). It is acceptable for the child Class to deliver Cappucino because it is a specific type of Coffee, but it is NOT acceptable to deliver Water.
NOTE:- If the child Class doesn’t meet these requirements, it means the child Class is changed completely and violates this principle.
Make a trait so that we can overide is methods
trait CoffeeMachine {
def serveCoffee(): Unit
}
- BasicCoffeeMachine class extended with trait and defines it own method and override serveCoffee()
- In this way our subtype is replaced without disturbing desirable properties of program.
class BasicCoffeeMachine extends CoffeeMachine {
val coffee="Powdered"
override def serveCoffee(): Unit = {
val cup = brewFilterCoffee(coffee)
println("Here is your " + cup)
}
def brewFilterCoffee(coffee:String): String ={
println("coffee is in " +coffee+" form")
println("Brewing...")
"Freshly brewed coffee"
}
}
- PremiumCoffeeMachine class extended with trait and defines it own method and override serveCoffee()
class PremiumCoffeeMachine extends CoffeeMachine {
val coffee = "Beans"
override def serveCoffee(): Unit = {
val cup = brewFilterCoffee(coffee)
println("Here is your "+cup)
}
def brewFilterCoffee(coffee:String): String ={
println("coffee is in " +coffee+" form")
println("Grinding beans")
println("Brewing... Filtering...")
"Freshly brewed premium Latte` coffee"
}
}
object Main extends App{
val basic = new BasicCoffeeMachine()
val premium = new PremiumCoffeeMachine
premium.serveCoffee()
basic.serveCoffee()
}
coffee is in Beans form
Grinding beans
Brewing... Filtering...
Here is your Freshly brewed premium Latte` coffee
coffee is in Powdered form
Brewing...
Here is your Freshly brewed coffee
This principle aims to enforce consistency so that the parent Class or its child Class can be used in the same way without any errors.
Clients should not be forced to depend on methods that they do not use.
-
When a Class is required to perform actions that are not useful, it is wasteful and may produce unexpected bugs if the Class does not have the ability to perform those actions.
-
A Class should perform only actions that are needed to fulfil its role. Any other action should be removed completely or moved somewhere else if it might be used by another Class in the future.
Cashier.scala is trait
trait Cashier {
def makeBill(): Int
}
CoffeeMaker is a trait
trait CoffeeMaker {
def makeCoffee(): Unit
}
CoffeeServer.scala is a trait
trait CoffeeServer {
def serveCoffee(): Unit
}
DrinkCoffee.scala is a trait
trait DrinkCoffee {
def drinkCoffee(): Unit
}
In this we made 3 classes to perform task related to coffee shop only that is make coffee, Serve the coffee, Billing
Extends those classes with an respective interface
for example: class Server extends CoffeeServer
class CoffeeShop extends CoffeeMaker {
override def makeCoffee(): Unit = {
println("Making coffee... ")
}
}
class Server extends CoffeeServer{
override def serveCoffee(): Unit = {
println("Serve coffee")
}
}
class Bill(amount:Int) extends Cashier{
override def makeBill(): Int = {
println("Making Bill...")
var price = amount
price
}
}
In this class we defined methods only related to customer We extends this class with interface DrinkCoffee and a method is defined payBill()
class Customer extends DrinkCoffee {
override def drinkCoffee(): Unit = {
println("drinking coffee")
}
def payBill(bill:Int): Unit ={
println("Bill paid of "+bill)
}
}
object Main extends App{
val rahul = new Customer
val cup = new CoffeeShop
cup.makeCoffee()
val server = new Server()
server.serveCoffee()
rahul.drinkCoffee()
val bill = new Bill(100)
rahul.payBill(bill.makeBill())
}
Making coffee...
Serve coffee
drinking coffee
Making Bill...
Bill paid of 100
This principle aims at splitting a set of actions into smaller sets so that a Class executes ONLY the set of actions it requires.
- High-level modules should not depend on low-level modules. Both should depend on the abstraction.
- Abstractions should not depend on details. Details should depend on abstractions.
- High-level Module(or Class): Class that executes an action with a tool.
- Low-level Module (or Class): The tool that is needed to execute the action
- Abstraction: Represents an interface that connects the two Classes
This principle says a Class should not be fused with the tool it uses to execute an action. Rather, it should be fused to the interface that will allow the tool to connect to the Class.
It also says that both the Class and the interface should not know how the tool works. However, the tool needs to meet the specification of the interface.
Coffeemaker.scala is a trait
trait CoffeeMaker {
def makeCoffee(): String
}
This class extends CoffeeMaker trait to make cappuccino coffee
class Cappuccino extends CoffeeMaker {
override def makeCoffee(): String = {
"Makes a Cappuccino"
}
}
This class extends CoffeeMaker trait to make latte coffee
class Latte extends CoffeeMaker {
override def makeCoffee(): String = {
"Makes a Latte"
}
}
class CoffeeServer {
def serveCoffee(coffee:CoffeeMaker): Unit ={
val cup = coffee.makeCoffee()
println(cup)
}
}
object Main extends App{
val serve = new CoffeeServer
serve.serveCoffee(new Cappuccino)
serve.serveCoffee(new Latte)
}
Makes a Cappuccino
Makes a Latte
This principle aims at reducing the dependency of a high-level Class on the low-level Class by introducing an interface.