mercredi 6 juillet 2016

What is the name and the purpose of the design pattern with Stub, Proxy, SmartProxy classes inside of an interface?

What is please the name and the purpose of the design pattern used in the following interface mindroid/os/IServiceManager.java?

Why are there 3 classes defined inside the interface?

public interface IServiceManager extends IInterface {
    public static abstract class Stub extends Binder implements IServiceManager {

        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        public static IServiceManager asInterface(IBinder binder) {
            if (binder == null) {
                return null;
            }
            return new IServiceManager.Stub.SmartProxy(binder);
        }

        public IBinder asBinder() {
            return this;
        }

        private static class Proxy implements IServiceManager {
            private final IBinder mRemote;

            Proxy(IBinder remote) {
                mRemote = remote;
            }

            public IBinder asBinder() {
                return mRemote;
            }
        }

        private static class SmartProxy implements IServiceManager {
            private final IBinder mRemote;
            private final IServiceManager mStub;
            private final IServiceManager mProxy;

            SmartProxy(IBinder remote) {
                mRemote = remote;
                mStub = (IServiceManager) remote.queryLocalInterface(DESCRIPTOR);
                mProxy = new IServiceManager.Stub.Proxy(remote);
            }

            public IBinder asBinder() {
                return mRemote;
            }
        }
    }
}

I have difficulties to understand and use the above code.

The only thing I have noticed is the check if the caller is at the same thread and if not - a message is sent via handler to the Proxy class.

Aucun commentaire:

Enregistrer un commentaire