GithubHelp home page GithubHelp logo

kotlin-object-pool-design-pattern's Introduction

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

kotlin-object-pool-design-pattern's People

Contributors

enes-kayiklik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.