迭代器设计模式之构建通用底部的BottomTabNavigationBar

1.模式定义:

提供一种方法顺序访问一个容器中的各个元素,而又不需要暴露该对象的内部显示(如List,Objcet数组,set)。

首先看下简单的使用

一般写法:比如百度网盘登录可以用微信和qq,假设qq使用的是List保存数据,和微信使用的是数组保存数据

用户信息

public class UserInfo {
    public String userName;
    public String userPwd;
    public String userId;
    public String userSex;

    public UserInfo(String userName, String userPwd, String userId, String userSex) {
        this.userName = userName;
        this.userPwd = userPwd;
        this.userId = userId;
        this.userSex = userSex;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "userName='" + userName + '\'' +
                ", userPwd='" + userPwd + '\'' +
                ", userId='" + userId + '\'' +
                ", userSex='" + userSex + '\'' +
                '}';
    }
}

QQUserSystem用户系统

public class QQUserSystem {
    private List<UserInfo> userInfos;

    public QQUserSystem() {
        userInfos = new ArrayList<>();
        userInfos.add(new UserInfo("Peakmain","1234","001","男"));
        userInfos.add(new UserInfo("Treasure","1234","002","女"));
        userInfos.add(new UserInfo("Jack","abcd","003","男"));
    }

    public List<UserInfo> getUserInfos() {
        return userInfos;
    }
}

微信用户系统

    public class WXUserSystem {
    private UserInfo[] userInfos;

    public WXUserSystem() {
        userInfos = new UserInfo[3];
        userInfos[0] = new UserInfo("奋斗@加油", "1234", "001", "男");
        userInfos[1] = new UserInfo("芊芊", "1234", "002", "女");
        userInfos[2] = new UserInfo("太想爱你", "1234", "003", "男");
    }

    public UserInfo[] getUserInfos() {
        return userInfos;
    }
}

测试

public class Client {
    public static void main(String[] args) {
        UserInfo userInfo=queryWXUserInfo("爱我的你","1234");
        if(userInfo==null){
            userInfo=queryQQUserInfo("爱我的你","1234");
        }
        if(userInfo==null){
            System.out.println("查无此人");
        }
    }

    private static UserInfo queryQQUserInfo(String name, String pwd) {
        QQUserSystem userSystem=new QQUserSystem();
        List<UserInfo> userInfos = userSystem.getUserInfos();
        for (UserInfo userInfo : userInfos) {
            if(userInfo.userName.equals(name)&&userInfo.userPwd.equals(pwd)){
                return userInfo;
            }
        }
        return null;
    }

    /**
     * 查询微信用户信息
     */
    private static UserInfo queryWXUserInfo(String name, String password) {
        WXUserSystem userSystem=new WXUserSystem();
        UserInfo[] userInfos = userSystem.getUserInfos();
        for (UserInfo userInfo : userInfos) {
            if(userInfo.userName.equals(name)&&userInfo.userPwd.equals(password)){
                return userInfo;
            }
        }
        return null;
    }
}

这时候我们将用户信息以 List userInfos或者数组 UserInfo[] userInfos暴露在外面,这时候我们可以使用迭代器

责任链模式构建用户系统

迭代器接口

public interface Iterator<T> {
    /**
     * 获取下一个
     */
    T next();

    /**
     * 是否有下一个
     */
    boolean hasNext();
}

QQIterator迭代器

public class QQIterator implements Iterator<UserInfo> {
    List<UserInfo> userInfos;
    int index = 0;//下标

    public QQIterator(List<UserInfo> userInfos) {
        this.userInfos = userInfos;
    }

    @Override
    public UserInfo next() {
        return userInfos.get(index++);
    }

    @Override
    public boolean hasNext() {
        return index<userInfos.size();
    }
}

微信迭代器

public class WXIterator implements Iterator<UserInfo> {
   UserInfo[] userInfos;
    int index=0;

    public WXIterator(UserInfo[] userInfos) {
        this.userInfos = userInfos;
    }

    @Override
    public UserInfo next() {
        return userInfos[index++];
    }

    @Override
    public boolean hasNext() {
        return index<userInfos.length;
    }
}

Aggregate获取迭代器

public interface Aggregate<T> {
    //获得迭代器
    Iterator<T>  iterator();
}

修改qq用户系统

public class QQUserSystem  implements Aggregate<UserInfo>{
    private List<UserInfo> userInfos;

    public QQUserSystem() {
        userInfos = new ArrayList<>();
        userInfos.add(new UserInfo("Peakmain","1234","001","男"));
        userInfos.add(new UserInfo("Treasure","1234","002","女"));
        userInfos.add(new UserInfo("Jack","abcd","003","男"));
    }

     //将暴露的List隐藏,显示迭代器
    @Override
    public Iterator<UserInfo> iterator() {
        return new QQIterator(userInfos);
    }
}

修改微信用户系统

public class WXUserSystem implements Aggregate<UserInfo>{
    private UserInfo[] userInfos;

    public WXUserSystem() {
        userInfos = new UserInfo[3];
        userInfos[0] = new UserInfo("奋斗@加油", "1234", "001", "男");
        userInfos[1] = new UserInfo("芊芊", "1234", "002", "女");
        userInfos[2] = new UserInfo("太想爱你", "1234", "003", "男");
    }
    @Override
    public Iterator<UserInfo> iterator() {
        return new WXIterator(userInfos);
    }
}

测试

public class Client {
    public static void main(String[] args) {
        QQUserSystem qqUserSystem=new QQUserSystem();
        WXUserSystem wxUserSystem=new WXUserSystem();
        UserInfo userInfo=queryUserInfo("爱我的你","1234",qqUserSystem.iterator());
        if(userInfo==null){
            userInfo=queryUserInfo("爱我的你","1234",wxUserSystem.iterator());
        }
        if(userInfo==null){
            System.out.println("查无此人");
        }
    }

    private static UserInfo queryUserInfo(String name, String pwd, Iterator<UserInfo> iterator) {
        if(iterator.hasNext()){
            UserInfo userInfo = iterator.next();
            if(userInfo.userName.equals(name)&&userInfo.userPwd.equals(pwd)){
                return userInfo;
            }
        }
        return null;
    }
}
构建通用的底部BottomTabNavigationBar

迭代器接口

public interface TabIterator {
    boolean hasNext();
    BottomTabItem next();
}

列表显示的迭代器

public class TabListIterator<T extends BottomTabItem> implements TabIterator {
    private List<T> mTabItems;
    private int index;

    public TabListIterator() {
        this.mTabItems = new ArrayList<>();
    }

    @Override
    public boolean hasNext() {
        return index < mTabItems.size();
    }

    @Override
    public BottomTabItem next() {
        return mTabItems.get(index++);
    }

    public void addItem(T item) {
        mTabItems.add(item);
    }
}

底部item的基类

public abstract class BottomTabItem {

    private int mLayoutId;
    private View mTabItemView;
    private Context mContext;

    public BottomTabItem(int layoutId, Context context) {
        this.mLayoutId = layoutId;
        this.mContext = context;
    }

    /**
     * 获取底部条目的显示
     */
    public View getTabView() {
        if (mTabItemView == null) {
            mTabItemView = LayoutInflater.from(mContext).inflate(mLayoutId, null);
            initLayout();
        }
        return mTabItemView;
    }

    /**
     * 初始化显示
     */
    public abstract void initLayout();

    protected <T extends View> T findViewById(int id) {
        return (T) mTabItemView.findViewById(id);
    }

    /**
     * 是否选择当前条目
     */
    protected abstract void setSelected(boolean selected);
}

MainBottomTabItem实现类

public class MainBottomTabItem extends BottomTabItem {
    private Builder builder;

    private MainBottomTabItem(Context context) {
        super(R.layout.tab_main_bottom_item, context);
    }

    public MainBottomTabItem(Builder builder) {
        this(builder.mContext);
        this.builder=builder;
    }

    @Override
    public void initLayout() {
        TextView tabText = findViewById(R.id.tab_text);
        ImageView tabIcon = findViewById(R.id.tab_icon);
        if (!TextUtils.isEmpty(builder.mText)) {
            tabText.setText(builder.mText);
        }
        if (builder.mIcon != 0) {
            tabIcon.setImageResource(builder.mIcon);
        }
    }

    @Override
    protected void setSelected(boolean selected) {
        TextView tabText = findViewById(R.id.tab_text);
        ImageView tabIcon = findViewById(R.id.tab_icon);
        tabIcon.setSelected(selected);
        tabText.setSelected(selected);
    }

    public static class Builder {
        private Context mContext;
        private String mText;
        private int mIcon;

        public Builder setText(String text) {
            this.mText = text;
            return this;
        }

        public Builder setIcon(int icon) {
            this.mIcon = icon;
            return this;
        }


        public Builder(Context context) {
            this.mContext = context;
        }

        public MainBottomTabItem create() {
            return new MainBottomTabItem(this);
        }


    }
}

每个item的集合

public class TabBottomNavigation extends LinearLayout {
    private List<BottomTabItem> mTabItems;
    private int mCurrentPosition = 0;

    public TabBottomNavigation(Context context) {
        this(context, null);
    }

    public TabBottomNavigation(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabBottomNavigation(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        mTabItems = new ArrayList<>();
    }

    public void addTabItem(TabIterator tabIterator) {
        mTabItems.clear();
        int index=0;
        while (tabIterator.hasNext()) {
            BottomTabItem tabItem = tabIterator.next();
            View tabView = tabItem.getTabView();
            addView(tabView);
            //设置weight
            LinearLayout.LayoutParams params = (LayoutParams) tabView.getLayoutParams();
            params.weight = 1;
            params.gravity = Gravity.CENTER;
            tabView.setLayoutParams(params);
            //设置点击事件
            setItemClickListener(tabView,index++);
            mTabItems.add(tabItem);
        }
        //设置默认选中的
        mTabItems.get(0).setSelected(true);

    }

    public void setItemClickListener( View tabView, final int position) {
        tabView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCurrentPosition != position) {
                 //原本选中的设置为没有选中
                    mTabItems.get(mCurrentPosition).setSelected(false);
                    mCurrentPosition=position;
                    //设置当前的为选中状态
                    mTabItems.get(mCurrentPosition).setSelected(true);
                }
            }
        });
    }
}

测试

public class MainActivity extends AppCompatActivity {
    private TabBottomNavigation mTabBottomNavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTabBottomNavigation = (TabBottomNavigation) findViewById(R.id.tab_bottom);
        TabListIterator<MainBottomTabItem> listIterator = new TabListIterator<>();
        listIterator.addItem(new MainBottomTabItem.Builder(this)
                .setIcon(R.drawable.main_tab_item).setText("text1").create());
        listIterator.addItem(new MainBottomTabItem.Builder(this)
                .setIcon(R.drawable.main_tab_item).setText("text2").create());
        listIterator.addItem(new MainBottomTabItem.Builder(this)
                .setIcon(R.drawable.main_tab_item).setText("text3").create());
        listIterator.addItem(new MainBottomTabItem.Builder(this)
                .setIcon(R.drawable.main_tab_item).setText("text4").create());

        mTabBottomNavigation.addTabItem(listIterator);
                /*   List<BottomTabItem> bottomTabItems=new ArrayList<>();
        bottomTabItems.add(new MainBottomTabItem.Builder(this).setText("text1").setIcon(R.drawable.main_tab_item).create());
        bottomTabItems.add(new MainBottomTabItem.Builder(this).setText("text2").setIcon(R.drawable.main_tab_item).create());
        bottomTabItems.add(new MainBottomTabItem.Builder(this).setText("text3").setIcon(R.drawable.main_tab_item).create());
        bottomTabItems.add(new MainBottomTabItem.Builder(this).setText("text4").setIcon(R.drawable.main_tab_item).create());

        mTabBottom.addTabItem(bottomTabItems*/
    }
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值