Demonstration of Object Pool Design Pattern using Kotlin language and Coroutine

Last update: Apr 12, 2022

Object Pool Design Pattern with Kotlin

Demonstration of Thread Safe Object Pool Design Pattern using Kotlin language and Coroutine.

Abstract

The object pool pattern is a design pattern that can improve performance when working with classes that are slow to instantiate. Rather than constructing new objects, reusable objects are retrieved from the pool as required. When no longer needed, objects are returned to the pool rather than being destroyed.

Singleton: Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code. Singleton has almost the same pros and cons as global variables. Although they’re very useful for storing shared data where you need it, they break the modularity of your code and unit testing becomes harder -

Pool: The most important class. It works as a controller to handle clients’ requests for reusable objects.

Objects: The reusable objects that are expensive or slow to instantiate.

Client: The class that requests to use object(s) in the pool.

Scenarios

Scenario 1: 10 pre-prepared objects are kept in the Object Pool. But no more than 10 objects are allowed. If 10 objects are in use, it is ensured that the requesters are waited. A request was made by starting 15 separate threads. Only 10 of these 15 threads were able to retrieve objects from the object pool and left the object pool back after use. Out of 15 threads, 5 of which could not receive an object were made to wait and continued to run when the object pool responded to the object request.

Scenario 2: There is a reference to the object pool object defined as Singleton in each thread. All threads can change the object pool size within themselves. Since the Object pool is singleton, other instances are also affected by this situation. This process was carried out using 4 different threads.

Scenario 3: Object Pool, which has 3 pre-prepared objects, works in 4 different threads. The Object Pool object defined outside the threads receives or leaves objects with 50% probability for each thread.

UML Diagram

UML Diagram for This Example of Object Pool Design Pattern

GitHub

https://github.com/Enes-Kayiklik/Kotlin-Object-Pool-Design-Pattern
You might also like...

A showcase music app for Android entirely written using Kotlin language

Bandhook Kotlin This project is a small replica of the app I developed some time ago. Bandhook can still be found on Play Store At the moment it will

May 13, 2022

Saga pattern implementation in Kotlin build in top of Kotlin's Coroutines.

Module Saga Website can be found here Add in build.gradle.kts repositories { mavenCentral() } dependencies { implementation("io.github.nomisr

Apr 4, 2022

An Android app built with Kotlin, consuming StarWars API to display characters of the popular StarWars Movie. It is built with the MVVM pattern and the latest Jetpack components.

An Android app built with Kotlin, consuming StarWars API to display characters of the popular StarWars Movie. It is built with the MVVM pattern and the latest Jetpack components.

StarWars An Android app built with Kotlin, consuming StarWars API to display characters of the popular StarWars Movie. It is built with the MVVM patte

Apr 1, 2022

It is a project that contains lessons and examples about Kotlin programming language. 🇰

It is a project that contains lessons and examples about Kotlin programming language. 🇰

Kotlin Tutorials What is Kotlin? I added the platforms it supports and great resources. You can access the article from the link below: https://medium

Mar 24, 2022

The Okila server project uses the Spring boot framework and uses the Kotlin language

Okila-Server The Okila server project uses the Spring boot framework and uses the Kotlin language Format Response //The response successfully format

Oct 25, 2021

An Android app consuming Pixabay API to search and display list of images, built with MVVM pattern

An Android app consuming Pixabay API to search and display list of images, built with MVVM pattern

Images An Android app consuming Pixabay API to search and display list of images, built with MVVM pattern as well as Architecture Components. Min Api

Apr 6, 2022

101 examples for Kotlin Programming language.

This is a collection of runnable console applications that highlights the features of Kotlin programming language. The use of console application enab

Feb 17, 2022

Practising kotlin language

Practising kotlin language

Kotlin Learning About this project : 1.Starting with kotlin. 2.Creating a birthday wishing program. 📌 Tech Stack: [] [] 📌 Main Page: HELLO WORLD BIR

Dec 4, 2021

Kotrlin Programming Language Cross-Platform Development which includes Android, iOS and Backend. Pretty much everwhere.

Kotrlin Programming Language Cross-Platform Development which includes Android, iOS and Backend. Pretty much everwhere.

Kotlin-Everywhere: Kotlin Programming Language Cross-Platform Development This is still a WIP but the idea is to create a tiny KOTLIN project that cou

Jan 14, 2022
Android Multi Theme Switch Library ,use kotlin language ,coroutine ,and so on ...
Android Multi Theme Switch Library ,use  kotlin language ,coroutine ,and so on ...

Magic Mistletoe Android多主题(换肤)切换框架 背景 时隔四年,在网易换肤之前的思路下,做了几点改进,现在完全通过反射创建View,并且在SkinLoadManager中提供一个configCustomAttrs以支持自定义View的属性插队替换 摈弃了之前的AsyncTask

May 9, 2022
An Interpreter/Transpiler for the Folders esoteric programming language, a language with no code and just folders
An Interpreter/Transpiler for the Folders esoteric programming language, a language with no code and just folders

Folders2kt ?? An Interpreter/Transpiler of the Folders esoteric programming language, a language with no code and just folders, written in Kotlin Show

Feb 13, 2022
Kotlin extension function provides a facility to "add" methods to class without inheriting a class or using any type of design pattern

What is Kotlin Extension Function ? Kotlin extension function provides a facility to "add" methods to class without inheriting a class or using any ty

Apr 19, 2022
AbstractFactoryDesignPatternWithKotlin - Abstract Factory Design Pattern With Kotlin
AbstractFactoryDesignPatternWithKotlin - Abstract Factory Design Pattern With Kotlin

AbstractFactoryDesignPatternWithKotlin Abstract Factory Design Pattern With Kotl

Jan 2, 2022
AbstractFactoryDesignPatternWithKotlin - Abstract Factory Design Pattern With Kotlin
AbstractFactoryDesignPatternWithKotlin - Abstract Factory Design Pattern With Kotlin

AbstractFactoryDesignPatternWithKotlin Abstract Factory Design Pattern With Kotl

Jan 2, 2022
Kotlin microservices with REST, and gRPC using BFF pattern. This repository contains backend services. Everything is dockerized and ready to "Go" actually "Kotlin" :-)
Kotlin microservices with REST, and gRPC using BFF pattern. This repository contains backend services. Everything is dockerized and ready to

Microservices Kotlin gRPC Deployed in EC2, Check it out! This repo contains microservices written in Kotlin with BFF pattern for performing CRUD opera

Apr 21, 2022
FirestoreCleanArchitectureApp is an app built with Kotlin and Firestore that displays data in real-time using the MVVM Architecture Pattern. For the UI it uses Jetpack Compose, Android's modern toolkit for building native UI.
FirestoreCleanArchitectureApp is an app built with Kotlin and Firestore that displays data in real-time using the MVVM Architecture Pattern. For the UI it uses Jetpack Compose,  Android's modern toolkit for building native UI.

FirestoreCleanArchitectureApp FirestoreCleanArchitectureApp is an app built with Kotlin and Cloud Firestore that displays data in real-time using Andr

May 11, 2022
R2DBC Sharding Example with Kotlin Coroutine

R2DBC Sharding Example with Kotlin Coroutine A minimal sharding example with R2DBC and coroutine, where user table is divided into 2 different shards.

Oct 4, 2021
Kreds - a thread-safe, idiomatic, coroutine based Redis client written in 100% Kotlin

Kreds Kreds is a thread-safe, idiomatic, coroutine based Redis client written in 100% Kotlin. Why Kreds? Kreds is designed to be EASY to use. Kreds ha

May 22, 2022
A program written entirely in Kotlin using the principles of Clean Architecture according to the MVVM pattern
 A program written entirely in Kotlin using the principles of Clean Architecture according to the MVVM pattern

A program written entirely in Kotlin using the principles of Clean Architecture according to the MVVM pattern. An application that shows a list of ani

Apr 26, 2022