当前位置:操作系统 > 安卓/Android >>

Android[高级教程] 设计模式之七 单例模式

单例模式也是我最喜欢的模式,因为不管你创建多少个对象,当前对象在内存中只存在一份实例,而且像Android中对数据库的操作,对HTTP的请求都可以用单例模式来实现,而且效率也会提升不少。这里我们就对《西游记》里的主人公唐僧来实现单例模式呢:

[java] <pre name="code" class="java">public class Tangseng { 
 
    private static Tangseng tangseng; 
 
    Tangseng() { 
        // TODO Auto-generated constructor stub  
    } 
 
    public Tangseng getInstance() { 
 
        if (tangseng == null) { 
            tangseng = new Tangseng(); 
        } 
 
        return tangseng; 
 
    } 
 

<pre name="code" class="java">public class Tangseng {

 private static Tangseng tangseng;

 Tangseng() {
  // TODO Auto-generated constructor stub
 }

 public Tangseng getInstance() {

  if (tangseng == null) {
   tangseng = new Tangseng();
  }

  return tangseng;

 }

}

 

 

就简单的几行代码,我们就实现了单例模式,以后调用的时候不管你new多少个对象,内存中只有一个。

在Android中调用一下看一下呢,


[java] public class XiyoujiActivity extends Activity { 
 
    private ListView listView; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        listView = (ListView) findViewById(R.id.listView1); 
        ArrayList<String> lists = new ArrayList<String>(); 
 
        lists.add(new Tangseng().getInstance().toString()); 
        lists.add(new Tangseng().getInstance().toString()); 
 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                android.R.layout.易做图_list_item_1, lists); 
 
        listView.setAdapter(adapter); 
 
    } 

public class XiyoujiActivity extends Activity {

 private ListView listView;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  listView = (ListView) findViewById(R.id.listView1);
  ArrayList<String> lists = new ArrayList<String>();

  lists.add(new Tangseng().getInstance().toString());
  lists.add(new Tangseng().getInstance().toString());

  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.易做图_list_item_1, lists);

  listView.setAdapter(adapter);

 }
}显示结果:
   
\


大家从图上应该能看出来,不管我new了多少个唐僧的类,它的内存地址是同一个,这就是单例模式的特性,因为一般我们都将实现单例模式的类静态化,静态化是什么意思呢?静态化就是将方法变成静态方法,JAVA中对于静态方法是可以直接调用的,不需要new出来的。接下来我们再重构一下:


[java] public class Tangseng { 
 
    private static Tangseng tangseng; 
 
    private Tangseng() { 
        // TODO Auto-generated constructor stub  
    } 
 
    public static Tangseng getInstance() { 
 
        if (tangseng == null) { 
            tangseng = new Tangseng(); 
        } 
 
        return tangseng; 
 
    } 
 

public class Tangseng {

 private static Tangseng tangseng;

 private Tangseng() {
  // TODO Auto-generated constructor stub
 }

 public static Tangseng getInstance() {

  if (tangseng == null) {
   tangseng = new Tangseng();
  }

  return tangseng;

 }

}其实也没多大改动,只是在getInstance方法前面加了个static 名称,这样这个方法就实现了静态方法,静态方法有什么好处呢?在Android中对于每个对象的生命周期都有很严格的规定,例如当前的Activity被另一个Activity覆盖,如果Android运行内存不够的话,前面的Activity就会被回收掉,但如果这个方法实现了static,一般是不会被回收的,这样我们就提高的代码的运行效率。接下来就我们再改一下调用方式呢:


[java] public class XiyoujiActivity extends Activity { 
 
    private ListView listView; 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
 
        listView = (ListView) findViewById(R.id.listView1); 
        ArrayList<String> lists = new ArrayList<String>(); 
 
        lists.add(Tangseng.getInstance().toString()); 
        lists.add(Tangseng.getInstance().toString()); 
 
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
                android.R.layout.易做图_list_item_1, lists); 
 
        listView.setAdapter(adapter); 
 
    } 

public class XiyoujiActivity extends Activity {

 private ListView listView;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  listView = (ListView) findViewById(R.id.listView1);
  ArrayList<String> lists = new ArrayList<String>();

  lists.add(T

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,