The Factory Pattern in Scala Last Updated : 12 Jul, 2025 Comments Improve Suggest changes Like Article Like Report 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. Standard Deluxe 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. Scala // 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) } } 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. Comment More infoAdvertise with us Next Article Using Extractors with Pattern Matching In Scala S ShikharMathur1 Follow Improve Article Tags : Scala Scala Scala-OOPS Similar Reads Scala | Pattern Matching Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. It is similar to the switch statement of Java and C. Here, "match" keyword is used inste 3 min read Scala | Pattern Matching Pattern matching is a way of checking the given sequence of tokens for the presence of the specific pattern. It is the most widely used feature in Scala. It is a technique for checking a value against a pattern. It is similar to the switch statement of Java and C. Here, "match" keyword is used inste 3 min read Using Extractors with Pattern Matching In Scala Scala Extractor is defined as an object which has a method named unapply as one of its part. Extractors can be utilized in Pattern Matching. The unapply method will be executed spontaneously, While comparing the Object of an Extractor in Pattern Matching. Below is the example of Extractor with patte 2 min read Using Extractors with Pattern Matching In Scala Scala Extractor is defined as an object which has a method named unapply as one of its part. Extractors can be utilized in Pattern Matching. The unapply method will be executed spontaneously, While comparing the Object of an Extractor in Pattern Matching. Below is the example of Extractor with patte 2 min read Interesting fact about Scala Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source 3 min read Scala Parser Combinators When a parser generator is required, some famous parsers that cross our mind are: Yacc and Bison for parsers written in C and ANTLR for parsers written in Java but they are designed to run for specific programming languages. This shortens the scope of use of parsers. However, Scala provides a unique 3 min read Like