SlideShare a Scribd company logo
1 of 75
Kotlin黑魔法:Delegated Properties
En-Ping Hsieh
大綱
●什麼是 Delegated Properties?
●Built-in Delegated Properties
●自己的Delegated Properties自己做!
什麼是 Delegated Properties?
Property是什麼?
定義一個叫name的Property
所以Property就是變數?
Field
類別的資料成員,用來保存狀態的。
Property Field不是
定義一個叫bulbBrightness的Property
Property
物件的屬性,通過存取器(Getter/Setter)來表現資料
當你定義一個 property X,編譯器會為你產生
●一組存取器(accessors):getX(), setX(...)
●Backing field
當你去讀取 property X 時
val y = this.x
實則是隱式調用getX()
val y = this.getX()
同理,當你對 property X 賦值時
this.x = y
其實是呼叫setX(...)
this.setX(y)
Properties represent
accessors(getter/setter), not field.
因為Property是由函式組成,所以代表著可以有更
多的使用可能。
例如有一個代表面積的property
又或者是滿足序列化和易用性...
但也有些需要小心的地方...
這樣會造成重複定義...
或是不小心就遞迴了...
或是不小心就遞迴了...
回顧一下Property...
Setter
Getter
Property
Field
Other
valvar
A little more...
什麼是 Delegate?
假設我定義一個 CountableList 介面,除了取值和
插入外,還要能夠記錄取值的次數。
繼承 ArrayList 並實作 CountableList 介面?
讓我們想想這有沒有什麼缺點?
●函數名稱容易衝突,被父類別限制。
●由於是繼承,無法在執行時期變更實作。
如果把ArrayList放在MyList裡面,再把任務都交給他?
這樣做改變了什麼?
●函數名稱容易衝突,被父類別限制。
●由於是繼承因此無法在執行時期變更實作。
✔
✔
委派模式是一種物件導向的設計模式,旨在希望透過組合而
非繼承的方式達到程式碼重用。
什麼是Delegated Properties?
開發中發現行為相似的 properties,而這時候可
以將這些行為提取成類別並使用委派來取代相似
的程式碼。
var/val by DelegateObject()prop: Type
Delegated Property的使用方式
Built-in Delegated Properties
notNull, map, lazy and observable
NotNull
lateinit不適用於基本型態,可以使用notNull。
Map
property 會被委派給 map 中同名 key 的值
Lazy
●Lazy initialization
●Thread-safe
● LazyThreadSafetyMode.SYNCHRONIZED (預設)
只初始化一次且保證執行緒安全。
● LazyThreadSafetyMode.PUBLICATION
可能會初始化多次,但只有首次的結果會被使用。
● LazyThreadSafetyMode.NONE
只有延遲初始化,適合在單一執行緒下的元件如View widget。
Lazy Delegation的執行緒安全模式
Observable(不是Rx那個)
自己的Delegated Properties自己做!
Show, don’t tell.
Delegated Properties 可以做到哪些功能?
●View and Resource Binding
取代 ButterKnife 或是不想使用 Kotlin Android Extension
●Data Binding
SharedPreference或是Configuration
●Dependency Injection
Koin中大量使用
Let’s get our hands dirty!
這看起來是相似的模式,都是記錄property
的改變和讀取,且可能有許多類似的需求。
回顧一下Delegated Properties
Setter
Getter
Property
valvar
by
Delegated Object
getValue()
setValue()
field(optional)
Class Owner {
}
Delegated classProperty
定義Delegated Object的關係
var/val by DelegatedClass()prop: Type
Property type
Property owner
var by
class LogProperty
operator fun getValue()
operator fun setValue()
userId: String
profile: Profile?
var value : Generic type
設計Delegated Object
再看一次原本的程式碼...
希望簡化後...
var value
Generic type
fun getValue
log value
fun setValue
log value changed
仔細瞧瞧setValue()
●thisRef: Any?
●prop: KProperty<*>
●newValue: T
仔細瞧瞧setValue()
●thisRef: Any?
參數的型態限制delegated class可以在哪些property owner下使用。
其型態必須是property owner的型態或其父類別型態。
若property 位在top-level時會是Null
每次property被賦值時,該property owner的實例也會被當作參數傳入。
Class PropertyOwner {
}
var/val by DelegatedClass()prop: Type
仔細瞧瞧setValue()
●prop: KProperty<*>
其型態必須是KProperty<*>的型態或其父類別型態。
每次property被賦值時,該property的名稱(ex. “userId”, “profile”)和property
type(ex. String, Profile?)以及各種資訊會被封裝成KProperty型態的參數傳入
。
Class PropertyOwner {
}
var/val by DelegatedClass()prop: Type
仔細瞧瞧setValue()
●newValue: T
參數的型態限制哪些property type可以使用該delegated class。其型態必須
是該property的型態或其父類別型態。
每次property被賦值時,新的值會被作為參數傳遞。
Class PropertyOwner {
}
var/val by DelegatedClass()prop: Type
仔細瞧瞧setValue()
●函數主體
Log.d(tag, "${prop.name}: $value -> $newValue")
value = newValue
Class PropertyOwner {
}
var/val by DelegatedClass()prop: Type
但這些這些函數簽名很難記得...
善用Kotlin提供的Delegated Properties 介面
●val → 實作ReadOnlyProperty<R, T>
var → 實作ReadWriteProperty<R, T>
●R 代表 property owner 而 T 代表 property type
Class PropertyOwner {
}
var/val by DelegatedClass()prop: Type
挑戰時間!
Summary
●Properties 代表存取函式而非欄位
●委派模式允許物件通過組合來達成程式碼重用
●DRY! 多用lazy, notNull
●觀察相似的部分並抽取出來成為Delegated Property
●Delegated Property可以藉由泛型功能取代Annotation processing
●預先定義的介面幫助我們更容易實作Delegated Properties
參考資料
●Effective Kotlin - Item 21
●https://hackernoon.com/kotlin-delegates-in-android-development-part-1-50346cf4aed7
●https://medium.com/techshots/delegate-properties-in-kotlin-39524fc4ea13
●https://americanexpress.io/advanced-kotlin-delegates/
●https://proandroiddev.com/the-magic-in-kotlin-delegates-377d27a7b531
●https://medium.com/rocket-fuel/kotlin-by-property-delegation-create-reusable-code-
f2bc2253e227
The End

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Kotlin黑魔法:Delegated Properties

Editor's Notes

  1. 定義 name的屬性 這不就是一個變數?
  2. 這邊說的變數其實應該是是Field, 用來保存資料
  3. Getter/setter 統稱Accessors
  4. Property 是由functions構成的
  5. 面積>=0, 每次檢查很煩 通過override setter 來正規化
  6. epochDate其實並不存在存放變數 field,因此 當序列化時只有epochTimestamp會被序列化
  7. 如果不小心在getter呼叫自己 ..you might..
  8. Recap Property 看起來像變數但實際上代表getter/setter
  9. Extension Property
  10. 名字不好,原因待會說(父類別衝突)
  11. 在物件導向中,委託模式是一種很常見的模式,它使得我們可以用聚合來替代繼承,既保留彈性又可以達成程式碼的復用
  12. delegated object -> 具有一些特殊的operator fun
  13. 怕空值可以使用withDefault
  14. Bind view or resource , replace butter knife
  15. Bind fragment getArguments()
  16. Annotation processing Replace by “delegate prop” easy, type-safe property delegation.
  17. 但不論有沒有implement interface都沒差 operator 的調用是在compile-time時期決定