确保这个类只有一个对象的场景,避免产生多个对象消耗过多的资源,或者某种类型的对象只应该只有一个。
public class Singleton {
private static Singleton instance;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
public class Singleton {
private static Singleton mInstance = null;
private Singleton() {
}
public void doSomething() {
System.out.println("do something");
}
public static Singleton getsInstance() {
if (mInstance == null) {
synchronized (Singleton.class) {
if (mInstance == null) {
mInstance = new Singleton();
}
}
}
return mInstance;
}
}
缺点:第一次加载时反应稍慢,偶尔会发生失败。
3.静态内部类单例模式
public class Singleton {
private Singleton() {
}
public static Singleton getInstance() {
return SingletonHolder.mInstance;
}
/** * 静态内部类 */
private static class SingletonHolder {
private static final Singleton mInstance = new Singleton();
}
}
public enum SingletonEnum {
INSTANCE;
public void doSomething() {
System.out.println("do something");
}
}
/** * 在readRSovle方法中,将mInstance对象返回, * 而不是重新生成一个新的对象。 * 对于枚举,不会出现这种情况。 * @return * @throws ObjectStreamException */
private Object readResolve() throws ObjectStreamException {
return mInstance;
}
public class SingleManager {
private static Map<String, Object> objectMap = new HashMap<String, Object>();
private SingleManager() {
}
/** * 添加到容器中 * * @param key * @param instance */
public static void registerService(String key, Object instance) {
if (!objectMap.containsKey(key)) {
objectMap.put(key, instance);
}
}
/** * 从容器中取出单例 * * @param key * @param instance * @return */
public static Object getService(String key, Object instance) {
return objectMap.get(key);
}
}
优点:
6.小结
ViewPager+Fragment
public class FragmentsManager {
public static final int NEWS = 0;
public static final int MUSIC = 1;
public static final int BOOK = 2;
public static final int ABOUT = 3;
public static FragmentsManager mInstance;
public static Fragment mFragment;
public static final int FRAGMENT_SIZE = 4;
public static Map<Integer, Fragment> fragmentMap = new HashMap<Integer, Fragment>();
private FragmentsManager() {
}
public static FragmentsManager getInstance() {
if (mInstance == null) {
synchronized (FragmentsManager.class) {
if (mInstance == null) {
mInstance = new FragmentsManager();
}
}
}
return mInstance;
}
/** * 采用工厂类进行创建Fragment,保证唯一性 * * @param index * @return */
public static Fragment createFragment(int index) {
mFragment = fragmentMap.get(index);
if (mFragment == null) {
switch (index) {
case NEWS:
mFragment = new NewsFragment();
break;
case MUSIC:
mFragment = new MusicFragment();
break;
case BOOK:
mFragment = new BookFragment();
break;
case ABOUT:
mFragment = new AboutFragment();
break;
default:
break;
}
fragmentMap.put(index, mFragment);
}
return mFragment;
}
}
3. MainActivity
public class MainActivity extends AppCompatActivity {
private ViewPager vp_content;
private AboutFragment aboutFragment;
private BookFragment bookFragment;
private MusicFragment musicFragment;
private NewsFragment newsFragment;
private FragmentsManager fragmentsManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initVariables();
initView();
initData();
}
private void initVariables() {
fragmentsManager = FragmentsManager.getInstance();
}
private void initView() {
vp_content = (ViewPager) findViewById(R.id.vp_content);
}
private void initData() {
vp_content.setAdapter(new fragmentsAdapter(getSupportFragmentManager()));
}
class fragmentsAdapter extends FragmentStatePagerAdapter {
public fragmentsAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentsManager.createFragment(position);
}
@Override
public int getCount() {
return FragmentsManager.FRAGMENT_SIZE;
}
}
}
4. NewsFragment等:
public class AboutFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
TextView textView = new TextView(getContext());
textView.setText("关于");
return textView;
}
}