结构型(四) - 装饰器模式

一、概念

装饰器模式(Decorator Pattern):允许向一个现有的对象添加新的功能,同时又不改变其结构。这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

与代理模式的区别:装饰器和代理模式很像,他们最大的区别在于使用场景。代理模式中,代理类附加的是跟原始类无关的功能。而在装饰器模式中,装饰器类附加的是跟原始类相关的增强功能。

二、实现

系统中有一个通知功能,通知分为紧急、重要、一般等几个等级,以前发通知就发一段文字。现在加需求,发通知的时候,除了显示通知文字,还要显示一张图片或者一个动画,也可能既有图片也有动画。

分析,在不影响之前通知功能的前提下,我们需要再加几个装饰类来增加通知的显示功能。
1、通知抽象类

public abstract class Notification {
    protected String notice;

    protected void setNotice(String notice){
         this.notice = notice;
     }

    protected String getNotice(){
         return notice;
     }

    protected void sendNotice(){
         System.out.println(notice);
     }
}

2、通知实现类,下面是一个紧急通知

public class UrgencyNotification extends Notification {
    private String notice;

    @Override
    public void setNotice(String msg) {
        this.notice = msg;
    }

    @Override
    public String getNotice() {
        return notice;
    }

    @Override
    protected void sendNotice() {
        System.out.println("紧急消息:" + notice);
    }
}

3、测试类

public class Client {
    public static void main(String[] args) {
        Notification urgencyNotification = new UrgencyNotification();
        urgencyNotification.setNotice("要地震啦");
        urgencyNotification.sendNotice();
    }
}

4、运行结果
装饰模式运行结果1.png

5、加入图片和动画的装饰类,首先实现一个NotificationDecorator类,来统一实现setNotice、getNotice等方法,这样后面子类直接调用就行。

public class NotificationDecorator extends Notification {
    private Notification notification;

    public NotificationDecorator(Notification notification){
        this.notification = notification;
    }

    @Override
    protected void setNotice(String notice) {
        notification.setNotice(notice);
    }

    @Override
    protected String getNotice() {
        return notification.getNotice();
    }

    @Override
    protected void sendNotice() {
        notification.sendNotice();
    }
}

6、图片装饰类

public class ImageNotificationDecorator extends NotificationDecorator {
    private Notification notification;
    public ImageNotificationDecorator(Notification notice) {
        super(notice);
        this.notification = notice;
        notification.setNotice(notification.getNotice() + " && 图片AAA");
    }
}

7、动画装饰类

public class AnimationNotificationDecorator extends NotificationDecorator {
    private Notification notification;

    public AnimationNotificationDecorator(Notification notice) {
        super(notice);
        this.notification = notice;
        notification.setNotice(notification.getNotice() + " && 动画BBB");
    }
}

8、调用实现,在原来文字通知的基础上加上一个图片,后面是叠加一个动画装饰器,既显示文字又显示图片还是显示动画,即装饰器可相互嵌套,为原始功能增加多个功能。

public class Client {
    public static void main(String[] args) {
        Notification urgencyNotification = new UrgencyNotification();
        urgencyNotification.setNotice("要地震啦");
        urgencyNotification.sendNotice();

        System.out.println("---------------------");
        ImageNotificationDecorator imageDecorator = new ImageNotificationDecorator(urgencyNotification);
        imageDecorator.sendNotice();

        System.out.println("---------------------");
        AnimationNotificationDecorator animationDecorator = new AnimationNotificationDecorator(imageDecorator);
        animationDecorator.sendNotice();
    }
}

9、运行结果
装饰模式运行结果2.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值