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
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Selective Deployment in Azure Data Factory: A Practical Blueprint for Safer CI/CD

Trending

  • Batching Strategies: Throughput vs Latency in Practice
  • To Switch, or Not to Switch the AI Model
  • OpenCode: Setting Up an Open-Source AI Coding Assistant
  • AI Agents and Frontier Models: Insights From The Neuron

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Updated Oct. 09, 18 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
39.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

  • The Window Closes: Why I Switched to Kotlin Before the Codebase Got Too Big
  • Testing Java and Spring Applications With Kotlin: Low-Risk to Adoption
  • Why Kotlin or Java Is the Wrong Question For Enterprises
  • Selective Deployment in Azure Data Factory: A Practical Blueprint for Safer CI/CD

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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