DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Why Kotlin Multiplatform is a Game-Changer for Startup Teams
  • Edge Computing's Infrastructure Problem: What Two Years of Factory Visits Actually Revealed
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Should You Use Azure Data Factory?

Trending

  • YOLOv5 PyTorch Tutorial
  • Java String Format Examples
  • Evolving Spring Boot APIs to an Event-Driven Mesh
  • Agentic AI Design Patterns and Principles: Building Autonomous, Collaborative Systems
  1. DZone
  2. Coding
  3. Languages
  4. Factory Pattern in Kotlin

Factory Pattern in Kotlin

Want to learn more about the factory design pattern in Kotlin? Check out this post where we will explore factory pattern with a simple example.

By 
Efthymios Vafeiadis user avatar
Efthymios Vafeiadis
·
Updated Oct. 09, 18 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
38.9K Views

Join the DZone community and get the full member experience.

Join For Free

Kotlin is one of the most competitive JVM languages. It has been created by JetBrains and it keeps rising. Google recently endorsed Kotlin as an officially supported language for Android development, enhancing Kotlin’s profile, but it also applies to server-side development. For example, Kotlin works smoothly with Spring Boot.

The factory method pattern is one of the creational patterns of the well known “Gang of Four” design patterns. In the factory pattern, we can create objects by calling a factory method rather by calling a constructor. As a result, we don’t need to specify the class of the created object. It provides different approaches to code for interface rather than implementation, and it makes our code less coupled and more extendable.

This scenario is an implementation of creating Book objects and accessing their methods with the factory pattern. 

First, we will create a Book interface and declare three methods that represent actions for the books:

Image title

Next, create a Genre Enum with two instances "SCIENCE" and "LITERATURE:"

Image title

After that, create a BookFactory class for creating objects using the Kotlin companion object. Every class can implement a companion object, which is common to all instances of that class. It is similar to static fields in Java. So, we don’t need to instantiate the BookFactory at all. We implemented a createBook method with Genre as an argument and create interface method implementations for each instance.

Image title

At last, implement the main method, which we call createBook from BookFactory, and we are able to dynamically get Book by passing the type in method, for example, “SCIENCE."

Image title

If we run main, we get the output below at console in IntelliJ:

Image title

I hope this post will help you get started with the factory method pattern in Kotlin. Happy coding!

Kotlin (programming language) Factory (object-oriented programming)

Opinions expressed by DZone contributors are their own.

Related

  • Why Kotlin Multiplatform is a Game-Changer for Startup Teams
  • Edge Computing's Infrastructure Problem: What Two Years of Factory Visits Actually Revealed
  • Kotlin Code Style: Best Practices for Former Java Developers
  • Should You Use Azure Data Factory?

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook