模拟Context调用

Context
package com.jk.test.context;

import com.jk.test.sharedpreferences.SharedPreferences;

public abstract class Context {
	public abstract SharedPreferences getSharedPreferences(String name,
            int mode);
}

package com.jk.test.context;


import java.util.HashMap;


import com.jk.test.sharedpreferences.SharedPreferences;
import com.jk.test.sharedpreferences.SharedPreferencesImpl;


public class ContextImpl extends Context {
	private static final HashMap<String, SharedPreferencesImpl> sSharedPrefs =
            new HashMap<String, SharedPreferencesImpl>();
	
	public ContextImpl() {
		super();
		sSharedPrefs.put("name", new SharedPreferencesImpl(1));
	}


	@Override
	public SharedPreferences getSharedPreferences(String name, int mode) {
		SharedPreferencesImpl sp;
		
		sp = sSharedPrefs.get(name);
        if (sp == null) {
            sp = new SharedPreferencesImpl(mode);
            sSharedPrefs.put(name, sp);
            return sp;
        }
        
		return sp;
	}


}

preferencemanager
package com.jk.test.preferencemanager;

import com.jk.test.context.Context;
import com.jk.test.sharedpreferences.SharedPreferences;

public class PreferenceManager {
	public static SharedPreferences getDefaultSharedPreferences(Context context){
		
		
		return context.getSharedPreferences("name", 0);
	}
}

sharedpreferences
package com.jk.test.sharedpreferences;

public interface SharedPreferences {
	
}
package com.jk.test.sharedpreferences;


public class SharedPreferencesImpl implements SharedPreferences {


	public SharedPreferencesImpl(int mode) {
	}


	@Override
	public String toString() {
		
		return "= " + SharedPreferencesImpl.class.getName().hashCode();
	}


	//1914154876
	//1914154876
}
package com.jk.test;


import com.jk.test.context.Context;
import com.jk.test.context.ContextImpl;
import com.jk.test.preferencemanager.PreferenceManager;
import com.jk.test.sharedpreferences.SharedPreferences;


public class Test {


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Context c = new ContextImpl();
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
		System.out.println(prefs);
	}


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值