Kotlin Lazy Initialization Example
Kotlin
87
Male avatar

loveprogramming viết ngày 16/04/2021

https://grokonez.com/kotlin/kotlin-lazy-initialization-example

Kotlin Lazy Initialization Example

Lazy Initialization is a common pattern for delaying creation of an object, calculation of a value, or an expensive process until it’s accessed for the first time. It is helpful when the initialization process consumes significant resources and the data isn’t always required when the object is used. Kotlin provides a good solution for that with lazy function. In this tutorial, we're gonna look at a Kotlin Lazy Initialization example.

I. Technology

  • Java 1.8
  • Kotlin 1.1.2

    II. Overview

    Assume that we have a Person(name,books) class that lets us access a list of the books own by a person. Books list is stored in a database that we need time to access.

We want to load books on first access to the books property and do it only once.
It can be done with a delegated property:


data class Person(val name: String) {

    val books by lazy { loadBooks(this) }
}
  • lazy function (thread-safe by default) returns an object that has getValuemethod called.
  • parameter of lazy is a lambda to initialize the value.

    III. Practice

    1. Helper Class

    BookManager class handles processing data:

More at:

https://grokonez.com/kotlin/kotlin-lazy-initialization-example

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

447 bài viết.
78 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 5 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/kotlin/kotlinreadwritecsvfileexample How to read/write CSV file in Kotlin In this tutorial, we're gonna look at examples tha...
loveprogramming viết 7 ngày trước
0 0
Male avatar
0 0
https://grokonez.com/frontend/angular/angular6/kotlinspringbootangular6crudhttpclientmysqlexamplespringdatajparestapisexample Kotlin Spring Boot +...
loveprogramming viết 1 tháng trước
0 0
Male avatar
0 0
https://grokonez.com/kotlin/kotlinmutableimmutablevariablevalvsvar Kotlin mutable & immutable variable – Val vs Var In the tutorial, JavaSampleAp...
loveprogramming viết 28 ngày trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
447 bài viết.
78 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!