您的位置:首页 > 移动开发 > Android开发

Android 常见的设计模式

2017-01-16 16:06 183 查看

Application 单例模式

public class User {
private static User user;

private User(){
}

public synchronized static User getInstance(){
if (user == null) {
user=new User();
}
return user;
}
}
//synchronized 代码同步


必须防止外部可以调用构造函数进行实例化,因此构造函数必须私有化。

单例使用volatile修饰

必须定义一个静态函数获得该单例

使用synchronized 进行同步处理,并且双重判断是否为null,我们看到synchronized (Singleton.class)里面又进行了是否为null的判断,这是因为一个线程进入了该代码,如果另一个线程在等待,这时候前一个线程创建了一个实例出来完毕后,另一个线程获得锁进入该同步代码,实例已经存在,没必要再次创建,因此这个判断是否是null还是必须的。

public class Singleton {
private static volatile Singleton instance = null;

private Singleton(){
}

public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
}
}


Builder模式

我们在Builder类里定义了一份与Person类一模一样的变量,通过一系列的成员函数进行设置属性值,但是返回值都是this,也就是都是Builder对象,最后提供了一个build函数用于创建Person对象,返回的是Person对象,对应的构造函数在Person类中进行定义,也就是构造函数的入参是Builder对象,然后依次对自己的成员变量进行赋值,对应的值都是Builder对象中的值。此外Builder类中的成员函数返回Builder对象自身的另一个作用就是让它支持链式调用,使代码可读性大大增强。

AlertDialog.Builder builder=new AlertDialog.Builder(this);
AlertDialog dialog=builder.setTitle("标题")
.setIcon(android.R.drawable.ic_dialog_alert)
.setView(R.layout.myview)
.setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.setNegativeButton(R.string.negative, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
})
.create();
dialog.show();


观察者模式,各种lisener

定义对象间的一种一对多的依赖关系,当一个对象的状态发送改变时,所有依赖于它的对象都能得到通知并被自动更新。

Android的广播机制,其本质也是观察者模式。

LocalBroadcastManager localBroadcastManager=LocalBroadcastManager.getInstance(this);
localBroadcastManager.registerReceiver(BroadcastReceiver receiver, IntentFilter filter);
localBroadcastManager.unregisterReceiver(BroadcastReceiver receiver);
localBroadcastManager.sendBroadcast(Intent intent)


调用registerReceiver方法注册广播,调用unregisterReceiver方法取消注册,之后直接使用sendBroadcast发送广播,发送广播之后,注册的广播会收到对应的广播信息,这就是典型的观察者模式。

class A 中:

/**
* 视图刷新监听接口
*/
1、    public interface onRefreshLisener{

public void pullDownRefresh(); //调用接口时执行此方法
public void onLoadMore();
}
2、   private onRefreshLisener mOnRefreshLisener;

/**
* 添加视图刷新的监听
* by ppa
* @param lisener
*/
3、    public void setmOnRefreshLisener(onRefreshLisener lisener){
this.mOnRefreshLisener=lisener;
}

4、方法调用
public void methodA(){
if(mOnRefreshLisener!=null){
mOnRefreshLisener.onLoadMore();
}
}


class B 中:

listview.setmOnRefreshLisener(new onRefreshLisener ())


策略模式

策略模式定义了一些列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变换。(添加新功能时,实现新的接口即可)

首先定义个策略接口

public interface Strategy {
void travel();
}


根据不同的出行方式实现对应的接口

public class WalkStrategy implements Strategy{

@Override
public void travel() {
System.out.println("walk");
}
}

public class SubwayStrategy implements Strategy{

@Override
public void travel() {
System.out.println("subway");
}
}


还需要一个包装策略的类,并调用策略接口中的方法

public class TravelContext {
Strategy strategy;

public Strategy getStrategy() {
return strategy;
}

public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}

public void travel() {
if (strategy != null) {
strategy.travel();
}
}
}


测试代码

public class Main {
public static void main(String[] args) {
TravelContext travelContext=new TravelContext();
travelContext.setStrategy(new PlaneStrategy());
travelContext.travel();
travelContext.setStrategy(new WalkStrategy());
travelContext.travel();
travelContext.setStrategy(new SubwayStrategy());
travelContext.travel();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  设计模式