Created
July 5, 2023 09:25
-
-
Save senamit2708/b6463e65d1b0cbd663627ebf353c7516 to your computer and use it in GitHub Desktop.
By - delegates basic learning
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.example.coroutinelearning.fragments | |
| import android.os.Bundle | |
| import android.util.Log | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import androidx.fragment.app.Fragment | |
| import com.example.coroutinelearning.databinding.ByDelegateLearningBinding | |
| class ByDelegateFragment : Fragment() { | |
| private val TAG = ByDelegateFragment::class.simpleName | |
| private lateinit var binding: ByDelegateLearningBinding | |
| private val taxCollector = TaxCollector() | |
| private val accountant: Accountant by lazy { //one way | |
| Accountant() | |
| } | |
| //another way-> if you initialize delegate in such way, surchargeTaxDelegate.isInitialized() will be available | |
| private val surchargeTaxDelegate = lazy { | |
| SurchargeTax() | |
| } | |
| private val surchargeTax by surchargeTaxDelegate | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| } | |
| override fun onCreateView( | |
| inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? | |
| ): View? { | |
| binding = ByDelegateLearningBinding.inflate(inflater, container, false) | |
| return binding.root | |
| } | |
| override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
| super.onViewCreated(view, savedInstanceState) | |
| salaryTaxExample() | |
| } | |
| private fun salaryTaxExample() { | |
| Log.i(TAG, "Is Surcharge initialized ${surchargeTaxDelegate.isInitialized()}") | |
| val THRESHOLD = 80 | |
| val salaries = listOf(50, 100, 70, 120) //salary in k -> 1000 | |
| for (salary in salaries) { | |
| taxCollector.taxCalculation(salary) | |
| if (salary > THRESHOLD) { | |
| accountant.accountantDetail() | |
| surchargeTax.surchargeDeduction() | |
| Log.i(TAG, "Is Surcharge initialized ${surchargeTaxDelegate.isInitialized()}") | |
| } | |
| } | |
| } | |
| } | |
| class SurchargeTax { | |
| private val TAG = "ByDelegateFragment" | |
| init { | |
| Log.i(TAG, "SurchargeTax class is initialized") | |
| } | |
| fun surchargeDeduction() { | |
| Log.i(TAG, "Surcharge deduction function") | |
| } | |
| } | |
| class Accountant { | |
| private val TAG = "ByDelegateFragment" | |
| init { | |
| Log.i(TAG, "Accountant class is initialized") | |
| } | |
| fun accountantDetail() { | |
| Log.i(TAG, "accountantDetail function") | |
| } | |
| } | |
| class TaxCollector { | |
| private val TAG = "ByDelegateFragment" | |
| init { | |
| Log.i(TAG, "TaxCollector class is initialized") | |
| } | |
| fun taxCalculation(salary: Int = 0) { | |
| Log.i(TAG, "taxcalculation method, salary: $salary") | |
| } | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 2023-07-05 14:45:41.329 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: TaxCollector class is initialized | |
| 2023-07-05 14:45:41.360 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: Is Surcharge initialized false | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: taxcalculation method, salary: 50 | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: taxcalculation method, salary: 100 | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: Accountant class is initialized | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: accountantDetail function | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: SurchargeTax class is initialized | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: Surcharge deduction function | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: Is Surcharge initialized true | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: taxcalculation method, salary: 70 | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: taxcalculation method, salary: 120 | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: accountantDetail function | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: Surcharge deduction function | |
| 2023-07-05 14:45:41.361 5548-5548/com.example.coroutinelearning I/ByDelegateFragment: Is Surcharge initialized true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| https://www.youtube.com/watch?v=tT7pQjSv-kc | |
| The most common usage of delegated property in my opinion is by lazy. | |
| I personally prefer val over var. So I use by lazy a lot whenever is possible instead of using lateinit var | |
| use by lazy if creating any class or anything is costly or might be raerly used. so keep it with by delegate. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment