[컴][자바] singleton 예제

java 에서 싱글턴 구현하기, singleton example, 싱글튼 예제, Java singleton example, how to implement


Singleton

public class FavoriteListAdapter extends BaseAdapter{

ArrayList<ChartItem> favoriteList;
private volatile static FavoriteListAdapter instance;

/** Returns singleton class instance */
public static FavoriteListAdapter getInstance() {
if (instance == null) {
synchronized (FavoriteListAdapter.class) {
if (instance == null) {
instance = new FavoriteListAdapter();
}
}
}
return instance;
}

protected FavoriteListAdapter() {
}

public synchronized void init(Context context){

// namh
mDiskCache = new DiskLruImageCache(context, "muchart",
DISK_CACHE_SIZE,
CompressFormat.JPEG,
70);

mExecutorService = Executors.newFixedThreadPool(5);
mEmptyImage = getRoundedCornerBitmap(
BitmapFactory.decodeResource(context.getResources(), R.drawable.noimage)
);
}
...
}

MainActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Sigleton ImageLoader
ImageLoader.getInstance().init(this);

댓글 없음:

댓글 쓰기