Top Banner
Building stable and flexible libraries @KeithYokoma - Drivemode, Inc. potatotips #12
32

Building stable and flexible libraries

Aug 06, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Building stable and flexible libraries

Building stable and flexible libraries

@KeithYokoma - Drivemode, Inc. potatotips #12

Page 2: Building stable and flexible libraries

KeithYokoma

Keishin Yokomaku Drivemode, Inc. Android Engineer GitHub: https://github.com/KeithYokoma e-Book: http://amzn.to/1mZNydv

Page 3: Building stable and flexible libraries

Agenda

• Stability

• Flexibility

Page 4: Building stable and flexible libraries

Make libraries STABLE

Page 5: Building stable and flexible libraries

Make libraries STABLE

• Entity class declaration

• Multi-thread compatibility

• Lifecycle management

Page 6: Building stable and flexible libraries

Make libraries STABLE

• Entity class declaration

• Don’t

• Do

void setToken(String token, String type, String refresh, long by);

void setToken(AccessToken token);

Page 7: Building stable and flexible libraries

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);

Page 8: Building stable and flexible libraries

• Entity class declaration

• Easy to remember the type of args

• Type-Safe

void setToken(AccessToken token);

Make libraries STABLE

Page 9: Building stable and flexible libraries

Make libraries STABLE

• Multi-thread compatibility

• Synchronization

• Immutable entity

• Thread pool and callback lifecycle

• Singleton implementation

Page 10: Building stable and flexible libraries

Make libraries STABLE

• Multi-thread compatibility

• Synchronization

• “synchronized” block

• Synchronization utils(CyclicBarrier, …)

• Atomicity(AtomicInteger, …)

• “volatile” field

Page 11: Building stable and flexible libraries

Make libraries STABLE

• Multi-thread compatibility

• Immutable entity

• Immutable entity is thread safe

Page 12: Building stable and flexible libraries

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

Page 13: Building stable and flexible libraries

Make libraries STABLE

• Multi-thread compatibility

• Singleton implementation

• Be aware of “Lazy Initialization”

Page 14: Building stable and flexible libraries

// 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

Page 15: Building stable and flexible libraries

Make libraries STABLE

• Multi-thread compatibility

• Singleton implementation

• “synchronized” block

• Double checked locking

• Initialization on demand holder

Page 16: Building stable and flexible libraries

private static Singleton sInstance;

public static synchronized Singleton getInstance() { if (sInstance == null) { sInstance = new Singleton(); } return sInstance;}

Case Study Multi-thread compatibility

Page 17: Building stable and flexible libraries

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

Page 18: Building stable and flexible libraries

static class Holder { public static final Singleton SINGLETON = new Singleton();}

public static getInstance() { return Holder.SINGLETON;}

Case Study Multi-thread compatibility

Page 19: Building stable and flexible libraries

Make libraries STABLE

• Lifecycle management

• Object lifetime alignment

Page 20: Building stable and flexible libraries

Make libraries STABLE

• Lifecycle management

• Object lifetime alignment

• Lifecycle methods of various “Context”

• onCreate/onDestroy

• onStart/onStop, onResume/onPause

Page 21: Building stable and flexible libraries

Make libraries STABLE

• Lifecycle management

• Object lifetime alignment

• Naming convention

• add/remove, register/unregister

• start/finish, initialize/destroy

Page 22: Building stable and flexible libraries

Make libraries FLEXIBLE

Page 23: Building stable and flexible libraries

Make libraries FLEXIBLE

• Annotations vs Listeners

• Customizable resources

• Split package by domain

Page 24: Building stable and flexible libraries

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

Page 25: Building stable and flexible libraries

Make libraries FLEXIBLE

• Listeners

✓ Faster than annotations(runtime)

✓ Simple architecture

✗ Client should maintain the lifetime

Page 26: Building stable and flexible libraries

Make libraries FLEXIBLE

• Annotations and Listeners

• Do NOT call methods of dead object

Page 27: Building stable and flexible libraries

• Customizable resources

• If the library has UI resources…

• Theme should be customizable

• What about layout resources?

Make libraries FLEXIBLE

Page 28: Building stable and flexible libraries

• Customizable resources

• At least you need to…

• Define ID resources that the library uses

• Otherwise layout may not be customized

Make libraries FLEXIBLE

Page 29: Building stable and flexible libraries

Make libraries FLEXIBLE

• Split package by domain

• Avoid exceeding 65k method limit

• Less effort to strip out codes not used

Page 30: Building stable and flexible libraries

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, …

Page 31: Building stable and flexible libraries

–Joshua Bloch

“Never make the client do anything the library can do for the client.”

Page 32: Building stable and flexible libraries

Building stable and flexible libraries

@KeithYokoma - Drivemode, Inc. potatotips #12