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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  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.

Efthymios Vafeiadis user avatar by
Efthymios Vafeiadis
CORE ·
Oct. 09, 18 · Tutorial
Like (7)
Save
Tweet
Share
36.45K 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.

Popular on DZone

  • Handling Automatic ID Generation in PostgreSQL With Node.js and Sequelize
  • Handling Virtual Threads
  • Artificial Intelligence in Drug Discovery
  • Revolutionizing Supply Chain Management With AI: Improving Demand Predictions and Optimizing Operations

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: