状态模式LoginContext(登陆/未登录)

登陆LoginContext

1 使用

LoginContext.getLoginContext().setUserState(new LoginedState());

LoginContext.getLoginContext().forward(context);//调此转发函数

2 具体代码

package com.ycjr.navpad_new.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class LoginContext {
    UserState userState = new LogoutState();
    static LoginContext loginContext = new LoginContext();
    private LoginContext(){}
    public static LoginContext getLoginContext(){
        return loginContext;
    }
    public void setUserState(UserState userState){
        this.userState = userState;
    }
    //转发
    public void forward(Context context){
        userState.forwaord(context);
    }
    //评论
    public void command(Context context){
        userState.command(context);
    }
    

    public interface UserState{
        void forwaord(Context context);//转发
        void command(Context context);//评论
    }
    //1 登陆状态
    public class LoginedState implements UserState{
        @Override
        public void forwaord(Context context) {
            Toast.makeText(context,"转发",Toast.LENGTH_LONG).show();
        }

        @Override
        public void command(Context context) {
            Toast.makeText(context,"评论",Toast.LENGTH_LONG).show();
        }
    }
    
    //2 未登陆状态
    public class LogoutState implements UserState{
        @Override
        public void forwaord(Context context) {
            gotoLoginActivity(context);
        }

        @Override
        public void command(Context context) {
            gotoLoginActivity(context);
        }
        private void gotoLoginActivity(Context context){
            Intent intent = new Intent(context,LoginActivity.class);
            context.startActivity(intent);
        }
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值