Android设计模式---装饰设计模式

一.核心思想:

装饰设计模式也称包装设计模式,在不使用的继承的方式下, 扩展对象的功能。

二. 简单实现

定义接口Person , 两个实现类PersonA, PersonB.

public interface Person {
    void dressed();
}

public class PersionA implements Person {
    @Override
    public void dressed() {
        Log.d("66", "A穿衣服");
    }
}

public class PersionB implements Person {
    @Override
    public void dressed() {
        Log.d("66", "B穿衣服");
    }
}

装饰类DecoratePerson, DecoratePerson可以扩展很多功能, 同时不需要改变PersonA和PersonB的内容

public class DecoratePerson implements Person {

    private Person mPerson;

    public DecoratePerson(Person person){
        this.mPerson = person;
    }

    @Override
    public void dressed() {
        Log.d("66", "喷点香水");
        mPerson.dressed();
        Log.d("66", "照一下镜子");
    }
}

调用

public void test(){
        start(new DecoratePerson(new PersionA()));
        start(new DecoratePerson(new PersionB()));
    }

    public void start(Person persion){
        persion.dressed();
    }

结果

2019-03-08 17:17:53.635 6133-6133/com.liys.modeldecorate D/66: 喷点香水
2019-03-08 17:17:53.635 6133-6133/com.liys.modeldecorate D/66: A穿衣服
2019-03-08 17:17:53.635 6133-6133/com.liys.modeldecorate D/66: 照一下镜子
2019-03-08 17:17:53.635 6133-6133/com.liys.modeldecorate D/66: 喷点香水
2019-03-08 17:17:53.635 6133-6133/com.liys.modeldecorate D/66: B穿衣服
2019-03-08 17:17:53.635 6133-6133/com.liys.modeldecorate D/66: 照一下镜子
3. 实际应用

思考: RecyclerView 不支持头部和底部布局View对象的添加的,但是 ListView 是支持的。如果我们要给RecyclerView 添加头部和尾部怎么办呢?

下面代码仅做参考: 不是特别完善

public class WrapAdapter extends Adapter {

    private final int TYPE_HEAD = 100000;
    private final int TYPE_FOOT = 200000;
    private Adapter mAdapter;
    private View mHeadView;
    private View mFootView;
    private int mCount;


    public WrapAdapter(@NonNull Adapter adapter){
        this.mAdapter = adapter;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        Log.d("66","viewType="+viewType);
        if(viewType == TYPE_HEAD) {
            return new LiysViewHolder(mHeadView);
        }
        if(viewType>=getItemCount()-1 && mFootView!=null) {
            return new LiysViewHolder(mFootView);
        }
        return mAdapter.onCreateViewHolder(viewGroup, viewType);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        int mAdapterPosition = position;
        if(mHeadView != null){ //有头的时候
            if(position==0){
                return;
            }else{
                mAdapterPosition = position-1;
            }
        }
        if(mFootView != null){ //有尾的时候
            if(position>=mCount-1){ //最后一项
                return;
            }
        }
        mAdapter.onBindViewHolder(viewHolder,mAdapterPosition);
    }


    @Override
    public int getItemViewType(int position) {
        if(isHeadView(position)){
            return TYPE_HEAD;
        }
        if(isFootView(position)){
            return TYPE_FOOT;
        }
        return position;
    }

    @Override
    public int getItemCount() {
        mCount = mAdapter.getItemCount();
        if(mHeadView != null){
            mCount++;
        }
        if(mFootView != null){
            mCount++;
        }
        return mCount;
    }

    //是否头部
    private boolean isHeadView(int position){
        return position==0 && mHeadView != null;
    }

    //是否尾部
    private boolean isFootView(int position){
        return position>=(getItemCount()-1) && mFootView != null;
    }

    public void addHeadView(View headView){
        this.mHeadView = headView;
        this.notifyDataSetChanged();
    }
    public void addFootView(View footView){
        this.mFootView = footView;
        this.notifyDataSetChanged();
    }

    public void removeHeadView(){
        this.mHeadView = null;
        this.notifyDataSetChanged();
    }
    public void removeFootView(){
        this.mFootView = null;
        this.notifyDataSetChanged();
    }

    class LiysViewHolder extends RecyclerView.ViewHolder {

        public LiysViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}

你可能感兴趣的:(Android设计模式---装饰设计模式)