大话设计模式-装饰模式-2020-09-17-周四

2020-09-22  本文已影响0人  勇往直前888

定义

动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。

结构图

装饰模式

设计模式之六(装饰模式)

大话设计模式第六章之:装饰者模式

换装的例子

image.png

换装需要有一个人类用于指定是谁换装、一个服装类为具体服装类的父类、以及服装类下的各种具体服装的类。

image.png
class Person {
    private String name;
    protected String description;

    Person() {}

    Person(String name) {
        this.name = name;
    }

    public void show() {
        description = name + "的装扮:\n";
        Log.v("装饰模式", name + "的装扮:");
    }
}
class Finery extends Person {
    protected Person component;

    public void decorate(Person component) {
        this.component = component;
    }

    @Override
    public void show() {
        if (null != component) {
            component.show();
            description = component.description;
        }
    }
}
class BigTrouser extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "垮裤====";
        Log.v("装饰模式", "垮裤");
    }
}

class LeatherShoe extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "皮鞋====";
        Log.v("装饰模式", "皮鞋");
    }
}

class Sneaker extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "破球鞋====";
        Log.v("装饰模式", "破球鞋");
    }
}

class Suit extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "西装====";
        Log.v("装饰模式", "西装");
    }
}

class Tie extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "领带====";
        Log.v("装饰模式", "领带");
    }
}

class TShirt extends Finery {
    @Override
    public void show() {
        super.show();
        description = super.description;
        description += "大T恤====";
        Log.v("装饰模式", "大T恤");
    }
}
public void onShowButtonClick(View view) {
        CheckBox checkBoxTShirt = (CheckBox)findViewById(R.id.checkBoxTShirt);
        CheckBox checkBoxTrouser = (CheckBox)findViewById(R.id.checkBoxTrouser);
        CheckBox checkBoxTie = (CheckBox)findViewById(R.id.checkBoxTie);
        CheckBox checkBoxSuit = (CheckBox)findViewById(R.id.checkBoxSuit);
        CheckBox checkBoxSneaker = (CheckBox)findViewById(R.id.checkBoxSneaker);
        CheckBox checkBoxLeatherShoe = (CheckBox)findViewById(R.id.checkBoxLeatherShoe);
        EditText personName = (EditText)findViewById(R.id.editTextPersonName);
        TextView descriptionTextView = (TextView)findViewById(R.id.textViewDescription);

        /* 人物创建 */
        String name = personName.getText().toString();
        Person personXXX = new Person(name);

        TShirt tShirt = new TShirt();
        BigTrouser trouser = new BigTrouser();
        Tie tie = new Tie();
        Suit suit = new Suit();
        Sneaker sneaker = new Sneaker();
        LeatherShoe leatherShoe = new LeatherShoe();

        /* 装饰过程 */
        Person current = personXXX;
        if (checkBoxTShirt.isChecked()) {
            tShirt.decorate(current);
            current = tShirt;
        }
        if (checkBoxTrouser.isChecked()) {
            trouser.decorate(current);
            current = trouser;
        }
        if (checkBoxTie.isChecked()) {
            tie.decorate(current);
            current = tie;
        }
        if (checkBoxSuit.isChecked()) {
            suit.decorate(current);
            current = suit;
        }
        if (checkBoxSneaker.isChecked()) {
            sneaker.decorate(current);
            current = sneaker;
        }
        if (checkBoxLeatherShoe.isChecked()) {
            leatherShoe.decorate(current);
            current = leatherShoe;
        }

        /* 展示 */
        current.show();
        descriptionTextView.setText(current.description);
    }

Demo地址

https://gitee.com/zhangxusong888/Android/tree/master/design_pattern

上一篇 下一篇

猜你喜欢

热点阅读