The Factory Pattern in Scala

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.



  1. Standard
  2. Deluxe
  3. Luxury

We are following an object-oriented design, and hence we decided to create three different classes. One for each car type. We also want to provide methods to get the booking price, Brands, and availability. All three classes will implement their methods individually but we have to decide on a simple method to create a Car object. So that all the cars can be purchased using the same method. Here is an example of how we want to book a car. the first statement returns an instance of a Standard car. the second statement returns a Deluxe Car, and the third one gives a Luxury Car. we are in this case offering one single method to create a variety of objects. the method Car here is a factory of Cars. we can book whatever type of car we need and as many as we want. this type of object creation makes programming very easy and the codes compact. one need not worry about different classes for different kinds of cars. That’s what a factory method is all about.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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) 
        }        
    }

chevron_right


Output:

200000.0
100

The 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.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.