设计模式

定义:装饰模式是在不必改变原类文件和使用继承的情况下,动态的扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象。

装饰器模式是为已有功能添加更多功能的一种方式,就增加功能来说,装饰器模式比通过生成子类更为灵活。该模式通过将装饰的功能放在单独的类中,并让这些类包含了需要进行装饰的对象,使得客户端代码能够有选择性和顺序性来装饰对象,这能够将客户端中装饰功能代码移到单独的类中,使客户端的核心职责与装饰功能区分开来的同时不需要在客户端中编写重复的装饰逻辑。以下是装饰器模式的结构类图

可以看出,Component是一个对象接口,它的实现主要有两种,一个是需要被装饰的对象ConcreteComponent,一个是装饰抽象类Decorator,ConcreteDecoratorA和B是具体的装饰对象用来给Component添加功能,来看下以上类图的代码实现

Component接口

public interface Component {
    void decoration();
}

ConcreteComponent类

public class ConcreteComponent implements Component {
    @Override
    public void decoration() {
        System.out.println("未装饰的操作");
    }
}

Decorator抽象类

public abstract class Decorator implements Component {

    protected Component component;

    public Decorator(Component component) {
        super();
        this.component = component;
    }

    @Override
    public void decoration() {
        if (component != null) {
            component.decoration();
        }
    }
}

两个具体的抽象类

public class ConcreteDecoratorA extends Decorator {

    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    @Override
    public void decoration() {
        System.out.println("装饰A之前");
        super.decoration();
        System.out.println("装饰A之后");
    }

    public void decorationA() {
        System.out.println("装饰A之后扩展的方法");
    }
}
public class ConcreteDecoratorB extends Decorator {

    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    @Override
    public void decoration() {
        System.out.println("装饰B之前");
        super.decoration();
        System.out.println("装饰B之后");
    }

    public void decorationB() {
        System.out.println("装饰B之后扩展的方法");
    }
}

测试类的调用和输出结果

Component component = new ConcreteComponent();
component.decoration();
System.out.println("----------------");
component = new ConcreteDecoratorA(component);
component.decoration();
((ConcreteDecoratorA) component).decorationA();
System.out.println("----------------");
component = new ConcreteDecoratorB(component);
component.decoration();
((ConcreteDecoratorB) component).decorationB();

通过以上的类图和代码,使我们了解了装饰模式的大致结构。下面通过实现一个简单的角色打扮功能来加深对该模式的理解,这个功能即客户端能有选择性、顺序性的对人物进行穿着打扮,代码如下

人物接口

public interface Person {
    void show();
}

待打扮的男人类

public class Man implements Person {
    @Override
    public void show() {
        System.out.print("打扮的男人");
    }
}

用来穿着的服饰抽象类

public abstract class Finery implements Person {

    protected Person person;

    public Finery(Person person) {
        super();
        this.person = person;
    }

    @Override
    public void show() {
        if (person != null) {
            person.show();
        }
    }
}

具体的服饰类(代码只展示一种,其他类似)

public class Jeans extends Finery {

    public Jeans(Person person) {
        super(person);
    }

    @Override
    public void show() {
        System.out.print("牛仔裤 ");
        person.show();
    }
}

测试类的调用和输出

Person man = new Man();

man = new Suit(man);
man = new LeatherShoes(man);
man.show();
System.out.println();
man = new Man();
man = new Shirt(man);
man = new Jeans(man);
man = new SportsShoes(man);
man.show();

以上就是整个功能是实现代码,可以通过点击以下链接获取代码和类图,java中的IO就是用装饰器模式实现,可以参考左潇龙的博客

代码和类图

原文地址:https://www.cnblogs.com/guohaien/p/10431479.html