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
2.8k
Building stable and flexible libraries
Keishin Yokomaku
December 17, 2014
Tweet
Share
More Decks by Keishin Yokomaku
See All by Keishin Yokomaku
LazyColumnのitemがViewPortの中で占める領域の割合を知りたい
keithyokoma
0
410
Build apps for Cars
keithyokoma
0
370
Save the state
keithyokoma
0
430
Either in Kotlin
keithyokoma
0
410
持続的なアプリ開発のためのDXを支える技術
keithyokoma
2
4.6k
Make the objects serializable with kotlinx.serialization
keithyokoma
0
4.5k
Kotlin で書く Gradle Custom Tasks
keithyokoma
0
460
DX Improvements
keithyokoma
3
360
Get Things Done with Gradle Custom Tasks
keithyokoma
1
93
Other Decks in Technology
See All in Technology
私が trocco を推す理由
__allllllllez__
1
210
Tellus の衛星データを見てみよう #mf_fukuoka
kongmingstrap
0
180
反実仮想機械学習とは何か
usaito
PRO
11
4.3k
20分で完全に理解するGrafanaダッシュボード
hamadakoji
3
420
[新卒向け研修資料] テスト文字列に「うんこ」と入れるな(2024年版)
infiniteloop_inc
3
13k
アクセス制御にまつわる改善 / Improving access control
itkq
0
530
開発パフォーマンスを最大化するための開発体制
ham0215
2
320
DevOpsメトリクスとアウトカムの接続にトライ!開発プロセスを通して計測できるメトリクスの活用方法
ham0215
2
230
MLOpsの「壁」を乗り越える、LINEヤフーの Data Quality as Code
lycorptech_jp
PRO
5
490
DevOpsDays History and my DevOps story
kawaguti
PRO
9
2.5k
家族アルバム みてねにおけるGrafana活用術 / Grafana Meetup Japan Vol.1 LT
isaoshimizu
1
560
Next'24 事例セッションの紹介とクラウド資格を活用したキャリア形成について語りMuscle
yasumuusan
1
440
Featured
See All Featured
Product Roadmaps are Hard
iamctodd
44
9.7k
Visualization
eitanlees
136
14k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
357
22k
It's Worth the Effort
3n
180
27k
Fireside Chat
paigeccino
21
2.6k
YesSQL, Process and Tooling at Scale
rocio
164
13k
The Brand Is Dead. Long Live the Brand.
mthomps
49
28k
Reflections from 52 weeks, 52 projects
jeffersonlam
345
19k
A better future with KSS
kneath
231
16k
RailsConf 2023
tenderlove
4
540
Art, The Web, and Tiny UX
lynnandtonic
289
19k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
2
1.3k
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