最近看了一本关于设计模式的书,看到这个状态模式,正好相对应我做项目的模块登录与分享的模块,用起来真的也是方便了不少。也有了一种恍然大悟的感觉,就象以前不经意看到的一篇博客,然后过了很长时间再回头去看,哦,原来是这样。没有使用状态模式之前就是每次去点击功能按钮都要经过if else 去判断,麻烦了不少,今天你你肯定会有收获。
这个图可能画的比较潦草,但是你仔细看的话,肯定就很容易就明白。下面我就跟大家一起分析一下这个迷一样的图。
一.大体的流程
- 实现用户状态的接口
- 两个类,一个未登录的类与一个登录的类都去实现这种接口,不同的类实现方法不相同
- 定义一个控制登录状态的类,去控制用户的登录的状态从而不同的操作
- 我们定义了两个功能,被是分享功能还有个就是注销的功能,大家肯定都能想到分享功能肯定是登录后才能做操作的,注销就是注销哦。
二.下边开始对应流程贴代码
做一个强大CV战士,^_^。
1.实现用户状态的接口
public interface UserState<T> {
/**
* 转发的函数
*/
void forworl(T t);
}
2. 两个类,一个未登录的类与一个登录的类都去实现这种接口,不同的类实现方法不相同
未登录的类
public class LogOutState implements UserState<Context> {
@Override
public void forworl(Context context) {
//未登录他就跳转页面跳转到登录的页面吗,符合正常的逻辑
gotoActivity(context);
}
/**
* 登录的activity的跳转的方法
*
*/
private void gotoActivity(Context con) {
Intent intent = new Intent(con, LoginActivity.class);
con.startActivity(intent);
}
}
登录的类
public class LoginState implements UserState {
@Override
public void forworl(Object o) {
Context con = (Context) o;
Toast.makeText(con, "转发成功", Toast.LENGTH_SHORT).show();
}
}
3. 定义一个控制登录状态的类,去控制用户的登录的状态从而不同的操作
public class LoginContext {
/**
* 默认的登录状态为未登录的状态
*/
private UserState mState = new LogOutState();
static LoginContext mLoginContext = new LoginContext();
public LoginContext() {
}
public static LoginContext getmLoginContext() {
return mLoginContext;
}
public void setmState(UserState mState) {
this.mState = mState;
}
/**
* 分享的方法
*/
public void forward(Context context) {
mState.forworl(context);
}
}
4. 我们定义了两个功能,被是分享功能还有个就是注销的功能,大家肯定都能想到分享功能肯定是登录后才能做操作的,注销就是注销哦。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.forworl_btn)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginContext.getmLoginContext().forward(MainActivity.this);
LoginContext.getmLoginContext().setmState(new LoginState());
}
});
findViewById(R.id.logout_btn)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LoginContext.getmLoginContext().setmState(new LogOutState());
}
});
}
}
MainActivity的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="zl.com.statedemo.MainActivity">
<Button
android:id="@+id/forworl_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="转发"/>
<Button
android:id="@+id/logout_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注销"/>
</LinearLayout>
登录的LoginActivity
public class LoginActivity extends AppCompatActivity {
private EditText id_editText;
private EditText pwd_editText;
private Button login_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
id_editText = (EditText) findViewById(R.id.login_id);
pwd_editText = (EditText) findViewById(R.id.login_pwd);
login_btn = (Button) findViewById(R.id.login_btn);
login_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String userId = id_editText.getText().toString().trim();
String userPwd = pwd_editText.getText().toString().trim();
if (!userId.equals("") && !userPwd.equals("")) {
finish();
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
}
}
});
}
}
LoginActivity的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="zl.com.statedemo.LoginActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="20sp"/>
<EditText
android:id="@+id/login_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="20sp"
/>
<EditText
android:id="@+id/login_pwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:password="true"/>
</LinearLayout>
<Button
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"/>
</LinearLayout>
好了就是这样的,懂了的朋友给个star吧。