装饰模式:
抽象构件(Component)角色:
给出一个抽象接口,以规范准备接收附加责任的对象。
具体构件(Concrete Component)角色:
实现组件对象接口,通常就是被装饰器装饰的对象。
装饰(Decorator)角色:
持有一个构件(Component)对象的实例,并定义一个与抽象构件接口一致的接口。
具体装饰(Concrete Decorator)角色:
负责给构件对象"贴上"附加的责任。
优点:
- 装饰模式与继承关系的目的都是要扩展对象的功能,但是装饰模式可以提供比
继承更多的灵活性。 - 通过使用不同的其体装饰类以及这些装饰类的排列组合,设计师可以创造出很多不同行为的组合。
缺点:
- 会产生很多细粒度对象
本质:动态组合
装饰模式的功能:
能够实现动态地为对象添加功能,是从一个对象外部来给对象增加功能,相当于是改变了对象的外观。
对象组合:
Favor Compositionover Inheritance.装饰模式的思考起点就是这个规则
装饰器和组件类的关系:
原装饰器是用来装饰组件的,装饰器定要实现和组件类一致的接口, 保证他们是同一个类型,并具有同一个外观,这样组合完成的装饰才能够递归调用下去
被装饰的对象接口 可以给继承自此类的子对象动态的添加功能:
/**
* 被装饰的对象接口 可以给继承自此类的子对象动态的添加职责
*/
public abstract class Component {
public abstract void Operation();
}
/**
* 具体的对象 可以给其动态的添加职责
*/
public class ConcreteComponent extends Component {
@Override
public void Operation() {
System.out.println("具体对象的操作");
}
}
/**
* 具有装饰功能的类 可以给Component的子类动态的添加职责
*/
public class Decorator extends Component {
protected Component component;
public Decorator() {
}
public Decorator(Component component) {
this.component = component;
}
public void setComponent(Component component) {
this.component = component;
}
@Override
public void Operation() {
if (component != null) {
component.Operation();
}
}
}
/**
* 具体的装饰类 给Component的子类添加A职责
*/
public class ConcreteDecoratorA extends Decorator {
public ConcreteDecoratorA() {
super();
}
public ConcreteDecoratorA(Component component) {
super(component);
}
@Override
public void Operation() {
super.Operation();
System.out.print("具体装饰对象A的操作->");
initA();
}
private void initA() {
System.out.println("A类独有的方法,区别于B类");
}
}
/**
* 具体的装饰类 给Component的子类添加B职责
*/
public class ConcreteDecoratorB extends Decorator {
public ConcreteDecoratorB() {
}
public ConcreteDecoratorB(Component component) {
super(component);
}
@Override
public void Operation() {
super.Operation();
System.out.print("具体装饰对象B的操作->");
initB();
}
private void initB() {
System.out.println("B类独有的方法,区别于A类");
}
}
public class Main {
public static void main(String[] args) {
//第一种创建方法
//concreteComponent 是被装饰的对象 下面将为其添加AB职责
ConcreteComponent concreteComponent = new ConcreteComponent();
ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA();
ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB();
concreteDecoratorA.setComponent(concreteComponent);
concreteDecoratorB.setComponent(concreteDecoratorA);
concreteDecoratorB.Operation();
//第二种创建方法
ConcreteDecoratorB b =
new ConcreteDecoratorB(
new ConcreteDecoratorA(
new ConcreteComponent()));
b.Operation();
}
}
下面是穿衣的例子:
public abstract class Component {
public abstract void Show();
}
public class Finery extends Component {
protected Component component;
public Finery() {
super();
}
public void Decorate(Component component) {
this.component = component;
}
@Override
public void Show() {
if(component!=null)
component.Show();
}
}
public class Person extends Component {
private String name;
public Person() {
super();
}
public Person(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public void Show() {
System.out.println("装扮的{0}"+this.getName());
}
}
public class Sneakers extends Finery {
@Override
public void Show() {
System.out.println("破球鞋");
super.Show();
}
}
public class BigTrouser extends Finery {
@Override
public void Show() {
System.out.println("垮裤");
super.Show();
}
}
public class TShirts extends Finery {
@Override
public void Show() {
System.out.println("T恤");
super.Show();
}
}
public class Main1 {
public static void main(String[] args) {
Person xc=new Person("小菜");
System.out.println("第一种装扮:");
Sneakers pqx = new Sneakers();
BigTrouser kk=new BigTrouser();
TShirts dtx =new TShirts();
pqx.Decorate(xc);
kk.Decorate(pqx);
dtx.Decorate(kk);
dtx.Show();
}
}