大话设计模式-装饰模式-2020-09-17-周四
2020-09-22 本文已影响0人
勇往直前888
定义
动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活。
结构图

-
如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。
-
如果只有一个ConcreteDecorator类,那么就没必要建立单独的Decorator类,而可以把Decorator和ConcreteDecorator的责任合并成一个类。
-
装饰模式就是为已有功能动态添加更多功能的一种方式。
-
把类中的装饰功能从类中搬移去除,这样可以简化原有的类。有效的把类的核心职责和装饰功能区分开了。
换装的例子
- 结构图

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

- Person类(ConcreteComponent)
核心功能只有一个类,所以不需要父类Component,直接用具体的子类就可以了。
class Person {
private String name;
protected String description;
Person() {}
Person(String name) {
this.name = name;
}
public void show() {
description = name + "的装扮:\n";
Log.v("装饰模式", name + "的装扮:");
}
}
- Finery服饰类(Decorator)
由于有多个具体的服饰类,所以要引入一个公共的父类。
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;
}
}
}
- 具体服饰类(ConcreteDecorator)
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