在设计模式中有一种设计模式是状态模式,这种模式的意思就是对象的行为动作会根据其自身状态的改变而改变,在Android开发中我们经常会遇到一些UI组件需要根据不同的状态来进行变化,下面我就记录一下状态模式的简单设计与实现。
我这里以UI空间需要根据蓝牙的连接状态改变而改变来做示例。
首先我们需要定义一个状态接口,该接口定义了蓝牙的几种状态
public interface State {
/**
* 蓝牙连接,读取数据中
*/
public void connectLoading();
/**
* 蓝牙连接,数据读取完成
*/
public void connectCommplete();
/**
* 蓝牙未连接
*/
public void disConnect();
/**
* 搜索中
*/
public void searching();
}
根据以上接口接下来我们需要实现蓝牙的几种状态:
第一种是:蓝牙连接,读取数据中的状态
public class ConnectLoadingState implements State {
private BluetoothState bluetoothState;
public ConnectLoadingState(BluetoothState bluetoothState) {
this.bluetoothState = bluetoothState;
}
@Override
public void connectLoading() {
bluetoothState.getOne().setVisibility(View.GONE);
bluetoothState.getTwo().setVisibility(View.GONE);
bluetoothState.getThree().setVisibility(View.VISIBLE);
}
@Override
public void connectCommplete() {
}
@Override
public void disConnect() {
}
@Override
public void searching() {
}
第二种是:蓝牙连接,数据读取完成的状态
public class ConnectCompleteState implements State {
private BluetoothState bluetoothState;
public ConnectCompleteState(BluetoothState bluetoothState) {
this.bluetoothState = bluetoothState;
}
@Override
public void connectLoading() {
}
@Override
public void connectCommplete() {
bluetoothState.getOne().setVisibility(View.VISIBLE);
bluetoothState.getTwo().setVisibility(View.GONE);
bluetoothState.getThree().setVisibility(View.GONE);
}
@Override
public void disConnect() {
}
@Override
public void searching() {
}
}
第三种是:蓝牙未连接的状态
public class DisConnectState implements State {
private BluetoothState bluetoothState;
public DisConnectState(BluetoothState bluetoothState) {
this.bluetoothState = bluetoothState;
}
@Override
public void connectLoading() {
}
@Override
public void connectCommplete() {
}
@Override
public void disConnect() {
bluetoothState.getOne().setVisibility(View.GONE);
bluetoothState.getTwo().setVisibility(View.GONE);
bluetoothState.getThree().setVisibility(View.GONE);
}
@Override
public void searching() {
}
第四种是:搜索中的状态
public class SearchingState implements State {
private BluetoothState bluetoothState;
public SearchingState(BluetoothState bluetoothState) {
this.bluetoothState = bluetoothState;
}
@Override
public void connectLoading() {
}
@Override
public void connectCommplete() {
}
@Override
public void disConnect() {
}
@Override
public void searching() {
bluetoothState.getOne().setVisibility(View.GONE);
bluetoothState.getTwo().setVisibility(View.VISIBLE);
bluetoothState.getThree().setVisibility(View.GONE);
}
最后我们需要定义一个蓝牙状态的控制类
public class BluetoothState {
private State mState;
ConnectLoadingState connectLoadingState;
ConnectCompleteState connectCompleteState;
DisConnectState disConnectState;
SearchingState searchingState;
View one, two, three;
public BluetoothState(View one, View two, View three) {
this.connectLoadingState = new ConnectLoadingState(this);
this.connectCompleteState = new ConnectCompleteState(this);
this.disConnectState = new DisConnectState(this);
this.searchingState = new SearchingState(this);
this.one = one;
this.two = two;
this.three = three;
this.mState = disConnectState;
}
public void connectLoading() {
mState.connectLoading();
}
public void connectCommplete() {
mState.connectCommplete();
}
public void disConnect() {
mState.disConnect();
}
public void searching() {
mState.searching();
}
public View getOne() {
return one;
}
public View getTwo() {
return two;
}
public View getThree() {
return three;
}
在控制类中,我们初始化蓝牙的所有状态类,在外部使用的时候直接调用控制类中的相关方法即可改变状态,从而根据状态改变我们的UI界面。
使用这种方式来对界面UI的状态进行控制可以很好的解耦项目提高扩展性,当我们的业务逻辑越来越复杂的时候,这种状态控制的优势就会非常明显。
网友评论