Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class WidgetComponent {
- public static interface Owner {
- WidgetComponent getWidgetComponent();
- }
- public static WidgetComponent get(Object o) {
- if (o instanceof Owner) {
- return ((Owner)o).getWidgetComponent();
- }
- return null;
- }
- private String m_foo;
- public WidgetComponent() {
- m_foo = "bar";
- }
- public String getFoo() {
- return m_foo;
- }
- }
- public class Thing implements WidgetComponent.Owner {
- private m_widgetComp;
- public Thing() {
- m_widgetComp = new WidgetComponent();
- }
- @Override
- public WidgetComponent getWidgetComponent() {
- return m_widgetComp;
- }
- }
- public class Main {
- public static void main(String[] args) {
- // access component from concrete class
- Thing thing = new Thing();
- System.out.println(thing.getWidgetComponent().getFoo());
- // access component from abstract class
- // (kind of like instanceof for inheritance trees)
- Object o = (Object)thing;
- WidgetComponent widgetComp = WidgetComponent.get(o);
- if (widgetComp != null) {
- System.out.println(widgetComp.getFoo());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement