Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Building stable and flexible libraries
Search
Keishin Yokomaku
December 17, 2014
Technology
1
3.1k
Building stable and flexible libraries
Keishin Yokomaku
December 17, 2014
Tweet
Share
More Decks by Keishin Yokomaku
See All by Keishin Yokomaku
Base64 in Android
keithyokoma
0
35
One screen, many BottomSheets
keithyokoma
0
430
LazyColumnのitemがViewPortの中で占める領域の割合を知りたい
keithyokoma
0
700
Build apps for Cars
keithyokoma
0
550
Save the state
keithyokoma
0
590
Either in Kotlin
keithyokoma
0
610
持続的なアプリ開発のためのDXを支える技術
keithyokoma
2
5.4k
Make the objects serializable with kotlinx.serialization
keithyokoma
0
5.2k
Kotlin で書く Gradle Custom Tasks
keithyokoma
0
560
Other Decks in Technology
See All in Technology
2025年の医用画像AI/AI×medical_imaging_in_2025_generated_by_AI
tdys13
0
260
ペアーズにおけるAIエージェント 基盤とText to SQLツールの紹介
hisamouna
2
2k
Directions Asia 2025 _ Let’s build my own secretary (AI Agent) Part 1 & 2
ryoheig0405
0
110
ハッカソンから社内プロダクトへ AIエージェント「ko☆shi」開発で学んだ4つの重要要素
sonoda_mj
6
2k
AWSの新機能をフル活用した「re:Inventエージェント」開発秘話
minorun365
2
530
Redshift認可、アップデートでどう変わった?
handy
1
120
AI時代のアジャイルチームを目指して ー スクラムというコンフォートゾーンからの脱却 ー / Toward Agile Teams in the Age of AI
takaking22
0
190
フィッシュボウルのやり方 / How to do a fishbowl
pauli
2
430
マーケットプレイス版Oracle WebCenter Content For OCI
oracle4engineer
PRO
5
1.5k
Microsoft Agent Frameworkの可観測性
tomokusaba
1
120
日本Rubyの会: これまでとこれから
snoozer05
PRO
6
250
AIBuildersDay_track_A_iidaxs
iidaxs
4
1.7k
Featured
See All Featured
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
58
41k
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
34
It's Worth the Effort
3n
187
29k
svc-hook: hooking system calls on ARM64 by binary rewriting
retrage
1
42
Making Projects Easy
brettharned
120
6.5k
Odyssey Design
rkendrick25
PRO
0
450
AI: The stuff that nobody shows you
jnunemaker
PRO
1
39
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
130
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
70k
Paper Plane (Part 1)
katiecoart
PRO
0
2.5k
Making the Leap to Tech Lead
cromwellryan
135
9.7k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.1k
Transcript
Building stable and flexible libraries @KeithYokoma - Drivemode, Inc. potatotips
#12
KeithYokoma Keishin Yokomaku Drivemode, Inc. Android Engineer GitHub: https://github.com/KeithYokoma e-Book:
http://amzn.to/1mZNydv
Agenda • Stability • Flexibility
Make libraries STABLE
Make libraries STABLE • Entity class declaration • Multi-thread compatibility
• Lifecycle management
Make libraries STABLE • Entity class declaration • Don’t •
Do void setToken(String token, String type, String refresh, long by); void setToken(AccessToken token);
Make libraries STABLE • Entity class declaration • Hard to
remember the type of args • Not Type-Safe(ref. Effective Java) void setToken(String token, String type, String refresh, long by);
• Entity class declaration • Easy to remember the type
of args • Type-Safe void setToken(AccessToken token); Make libraries STABLE
Make libraries STABLE • Multi-thread compatibility • Synchronization • Immutable
entity • Thread pool and callback lifecycle • Singleton implementation
Make libraries STABLE • Multi-thread compatibility • Synchronization • “synchronized”
block • Synchronization utils(CyclicBarrier, …) • Atomicity(AtomicInteger, …) • “volatile” field
Make libraries STABLE • Multi-thread compatibility • Immutable entity •
Immutable entity is thread safe
Make libraries STABLE • Multi-thread compatibility • Thread pool and
callback lifecycle • Reduce thread initialization cost • Align callback lifetime with “Context” • Do NOT callback to dead object
Make libraries STABLE • Multi-thread compatibility • Singleton implementation •
Be aware of “Lazy Initialization”
// NOT thread safe!! public class Singleton { private static
Singleton sInstance; public static Singleton getInstance() { if (sInstance == null) { sInstance = new Singleton(); } return sInstance; } } Case Study Multi-thread compatibility
Make libraries STABLE • Multi-thread compatibility • Singleton implementation •
“synchronized” block • Double checked locking • Initialization on demand holder
private static Singleton sInstance; public static synchronized Singleton getInstance() {
if (sInstance == null) { sInstance = new Singleton(); } return sInstance; } Case Study Multi-thread compatibility
private static volatile Singleton sInstance; public static Singleton getInstance() {
if (sInstance == null) { synchronized (Singleton.class) { if (sInstance == null) { sInstance = new Singleton(); } } } return sInstance; } Case Study Multi-thread compatibility
static class Holder { public static final Singleton SINGLETON =
new Singleton(); } public static getInstance() { return Holder.SINGLETON; } Case Study Multi-thread compatibility
Make libraries STABLE • Lifecycle management • Object lifetime alignment
Make libraries STABLE • Lifecycle management • Object lifetime alignment
• Lifecycle methods of various “Context” • onCreate/onDestroy • onStart/onStop, onResume/onPause
Make libraries STABLE • Lifecycle management • Object lifetime alignment
• Naming convention • add/remove, register/unregister • start/finish, initialize/destroy
Make libraries FLEXIBLE
Make libraries FLEXIBLE • Annotations vs Listeners • Customizable resources
• Split package by domain
Make libraries FLEXIBLE • Annotations ✓ Fast and easy development
for client ✓ Automatic code generation(with apt) ✗ Slow(both runtime and apt takes time) ✗ Hard to dig into library itself
Make libraries FLEXIBLE • Listeners ✓ Faster than annotations(runtime) ✓
Simple architecture ✗ Client should maintain the lifetime
Make libraries FLEXIBLE • Annotations and Listeners • Do NOT
call methods of dead object
• Customizable resources • If the library has UI resources…
• Theme should be customizable • What about layout resources? Make libraries FLEXIBLE
• Customizable resources • At least you need to… •
Define ID resources that the library uses • Otherwise layout may not be customized Make libraries FLEXIBLE
Make libraries FLEXIBLE • Split package by domain • Avoid
exceeding 65k method limit • Less effort to strip out codes not used
Make libraries FLEXIBLE • Split package by domain • e.g.
Guava • guava, guava-gwt, guava-annotations, … • e.g. Google Play Services 6.5 • play-services, play-services-wearable, …
–Joshua Bloch “Never make the client do anything the library
can do for the client.”
Building stable and flexible libraries @KeithYokoma - Drivemode, Inc. potatotips
#12