Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Agenda • Stability • Flexibility

Slide 4

Slide 4 text

Make libraries STABLE

Slide 5

Slide 5 text

Make libraries STABLE • Entity class declaration • Multi-thread compatibility • Lifecycle management

Slide 6

Slide 6 text

Make libraries STABLE • Entity class declaration • Don’t • Do void setToken(String token, String type, String refresh, long by); void setToken(AccessToken token);

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

• Entity class declaration • Easy to remember the type of args • Type-Safe void setToken(AccessToken token); Make libraries STABLE

Slide 9

Slide 9 text

Make libraries STABLE • Multi-thread compatibility • Synchronization • Immutable entity • Thread pool and callback lifecycle • Singleton implementation

Slide 10

Slide 10 text

Make libraries STABLE • Multi-thread compatibility • Synchronization • “synchronized” block • Synchronization utils(CyclicBarrier, …) • Atomicity(AtomicInteger, …) • “volatile” field

Slide 11

Slide 11 text

Make libraries STABLE • Multi-thread compatibility • Immutable entity • Immutable entity is thread safe

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Make libraries STABLE • Multi-thread compatibility • Singleton implementation • Be aware of “Lazy Initialization”

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Make libraries STABLE • Multi-thread compatibility • Singleton implementation • “synchronized” block • Double checked locking • Initialization on demand holder

Slide 16

Slide 16 text

private static Singleton sInstance; public static synchronized Singleton getInstance() { if (sInstance == null) { sInstance = new Singleton(); } return sInstance; } Case Study Multi-thread compatibility

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

static class Holder { public static final Singleton SINGLETON = new Singleton(); } public static getInstance() { return Holder.SINGLETON; } Case Study Multi-thread compatibility

Slide 19

Slide 19 text

Make libraries STABLE • Lifecycle management • Object lifetime alignment

Slide 20

Slide 20 text

Make libraries STABLE • Lifecycle management • Object lifetime alignment • Lifecycle methods of various “Context” • onCreate/onDestroy • onStart/onStop, onResume/onPause

Slide 21

Slide 21 text

Make libraries STABLE • Lifecycle management • Object lifetime alignment • Naming convention • add/remove, register/unregister • start/finish, initialize/destroy

Slide 22

Slide 22 text

Make libraries FLEXIBLE

Slide 23

Slide 23 text

Make libraries FLEXIBLE • Annotations vs Listeners • Customizable resources • Split package by domain

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Make libraries FLEXIBLE • Listeners ✓ Faster than annotations(runtime) ✓ Simple architecture ✗ Client should maintain the lifetime

Slide 26

Slide 26 text

Make libraries FLEXIBLE • Annotations and Listeners • Do NOT call methods of dead object

Slide 27

Slide 27 text

• Customizable resources • If the library has UI resources… • Theme should be customizable • What about layout resources? Make libraries FLEXIBLE

Slide 28

Slide 28 text

• Customizable resources • At least you need to… • Define ID resources that the library uses • Otherwise layout may not be customized Make libraries FLEXIBLE

Slide 29

Slide 29 text

Make libraries FLEXIBLE • Split package by domain • Avoid exceeding 65k method limit • Less effort to strip out codes not used

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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