Android: Generic Java UserData facility
Java UserDataHost/UserData is modeled after native C++
SupportsUserData/Data class to allow users to attach random class/data
to a 'host' class by key. Considering the limitation of Java disallowing
multiple inheritance, UserData itself is designed as interface, and
UserDataHost a final class that a host class defines as member variable.
Host class Foo exposes UserDataHost so that the classes to be attached to
Foo can be managed without Foo actually knowing about them. An object of
FooBar can be retrieved from UserHostData of Foo by:
public class Foo {
// Defines the container.
private final UserDataHost mUserDataHost = new UserDataHost();
public UserDataHost getUserDataHost() {
return mUserDataHost;
}
}
public class FooBar implements UserDataHost.UserData {
public FooBar from(UserDataHost host) {
FooBar foobar = host.getUserData(FooBar.class);
// Instantiate FooBar upon the first access.
return foobar != null ? foobar : host.setUserData(FooBar.class, new FooBar());
}
}
Foo foo = new Foo();
...
FooBar bar = FooBar.from(foo.getUserDataHost());
...
The design doc is here: https://goo.gl/2uWDja
The method |destroy| is defined in Userdata interface to have the lifecycle
entirely under control. Note that it is not *yet* possible to make it
work for WebView.
Change-Id: Iaae038a2b6577b07328917724c610c4efda9bb2e
Reviewed-on: https://chromium-review.googlesource.com/1179507Reviewed-by:
Tommy Nyquist <nyquist@chromium.org>
Commit-Queue: Jinsuk Kim <jinsukkim@chromium.org>
Cr-Commit-Position: refs/heads/master@{#586174}
Showing
Please register or sign in to comment