The factory method is used to offer a single interface to instantiate one of the multiple classes. In the Factory pattern, the objective is to make the object in such a way that it does not expose to the creation logic to the Client and can always refer to a newly created object with the help of a common interface.
Let's try to understand it with an example.
Assume that we need to create a library for a Car Purchase Application. The library is supposed to give three choices of cars types.
Scala
Output:
- Standard
- Deluxe
- Luxury
// Scala program of Design factory pattern
// creating abstract class for the car
abstract class Car{
// Creating four abstract methods
def bookingPrice : Double
def Brands : List[String]
def availability : Int
def book(noOfCars:Int)
}
// Creating an object
object Car{
val STANDARD = 0
val DELUXE = 1
val LUXURY = 2
// Creating private class
private class standardCar extends Car{
private var _availability = 100
override def bookingPrice = 200000
override def Brands = List("Maruti", "Tata", "Hyundai")
override def availability = _availability
override def book(noOfCars:Int) = {
_availability = _availability - noOfCars
}
}
// Creating private class
private class DeluxeCar extends Car{
private var _availability = 50
override def bookingPrice = 500000
override def Brands = List("Honda", "Mahindra", "Chevrolet")
override def availability = _availability
override def book(noOfCars:Int) = {
_availability = _availability - noOfCars
}
}
// Creating private class
private class LuxuryCar extends Car{
private var _availability = 5
override def bookingPrice = 900000
override def Brands = List("Audi","BMW", "Mercedes")
override def availability = _availability
override def book(noOfCars:Int) = {
_availability = _availability - noOfCars
}
}
// create the apply method
// single method to create a variety of objects
def apply(carType:Int):Car = {
carType match {
case LUXURY => new LuxuryCar()
case DELUXE => new DeluxeCar()
case STANDARD => new standardCar()
case _ => new standardCar()
}
}
// Main method
def main(args: Array[String])
{
val s = Car(STANDARD)
println(s.bookingPrice)
println(s.availability)
}
}
200000.0 100The factory method thus helps a lot in making programming concepts easier. In this way, we can always save the codespace and the number of classes if declaring multiple objects of same type with some dissimilarities.