android源码中的设计模式举例

单例模式

package android.app;

@SystemService(Context.ACTIVITY_SERVICE)
public class ActivityManager {
    public static IActivityManager getService() {
        return IActivityManagerSingleton.get();
    }
    private static final Singleton IActivityManagerSingleton =
            new Singleton() {
                @Override
                protected IActivityManager create() {
                    final IBinder b = ServiceManager.getService(Context.ACTIVITY_SERVICE);
                    final IActivityManager am = IActivityManager.Stub.asInterface(b);
                    return am;
                }
    };
}

package android.util;
public abstract class Singleton {
private T mInstance;

protected abstract T create();

public final T get() {
    synchronized (this) {
        if (mInstance == null) {
            mInstance = create();
        }
        return mInstance;
    }
  }
}

你可能感兴趣的:(Android,设计模式)