

Factory Pattern in Kotlin
source link: https://www.tuicool.com/articles/hit/UJ7riuU
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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:
Next, create a Genre Enum with two instances "SCIENCE" and "LITERATURE:"
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.
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."
If we run main, we get the output below at console in IntelliJ:
I hope this post will help you get started with the factory method pattern in Kotlin. Happy coding!
Recommend
-
56
Creational Design Patterns: Factory Pattern DZone's Guide to Creational Design Patterns: Factory Pattern As we continue to explore creational design patterns...
-
50
This article explores the Factory Method design pattern and its implementation in Python. Design patterns became a popular topic in late 90s after the so-called Gang of Four (GoF: Gamma, Helm, Johson, and Vlissides) publ...
-
59
The factory method design pattern is one of the well-known “Gang of Four” (GoF) design patterns. It is a creational design pattern that uses factory methods to deal with creating instances without specifying the exact cla...
-
11
Design Pattern: factory patterns by Christophe | updated: March 31, 2016 | posted: June 11, 2015
-
5
Abstract Factory Design Pattern in Java - Introduction, Example, and Key points This video tutorial takes a closer look at Abstr...
-
6
Factory Design Pattern - Introduction, Example, and Key points | Factory Design Pattern in Java
-
9
Understanding Factory Method with a Factory Design Pattern Case Study In our previous blog post, we looked at software design patterns and the various benefits of using them. In this blog post, we delve deeper into the factory design...
-
7
除了使用new操作符之外,还有更多制造对象的方法。你将了解到实例化这个活动不应该总是公开的进行,也会意识到初始化会造成“耦合”的问题。工厂模式将会从复杂的依赖中帮你脱困。 1. 简单的工厂 当看到“new”,就会想到“具体”,的确也是在...
-
10
#Kotlin #Factory
-
3
Some background – The Factory Incentive continuing our series of design patterns. When switching from plain Javascript t...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK