• 【Java】【设计模式 Design Pattern】装饰器模式 Decorator


    解决的问题:

    对象的扩展问题:

    package cn.echo42.decorator;
    
    /**
     * @author DaiZhiZhou
     * @file Netty
     * @create 2020-07-29 16:49
     */
    public class Client {
    
        public static void main(String[] args) {
            Phone huaweiP30 = new Huawei();
            huaweiP30.function();
            System.out.println(huaweiP30.price());
    
            huaweiP30 = new MediaPlayerPhoneDecorator(huaweiP30);
            huaweiP30.function();
            System.out.println(huaweiP30.price());
    
            huaweiP30 = new ShotPhoneDecorator(huaweiP30);
            huaweiP30.function();
            System.out.println(huaweiP30.price());
    
            // 单件购买也可以
            Phone phone = new MediaPlayerPhoneDecorator();
            System.out.println(phone);
        }
    
        interface Phone {
            int price();
            void function();
        }
    
        static class Huawei implements Phone {
            @Override
            public int price() {
                return 1000;
            }
    
            @Override
            public void function() {
                System.out.println("华为手机的基础功能");
            }
        }
    
        interface PhoneDecorator extends Phone{
    
        }
    
        static class MediaPlayerPhoneDecorator implements PhoneDecorator {
    
            private Phone phone;
    
            public MediaPlayerPhoneDecorator() {
            }
    
            public MediaPlayerPhoneDecorator(Phone phone) {
                this.phone = phone;
            }
    
            public void setPhone(Phone phone) {
                this.phone = phone;
            }
    
            @Override
            public int price() {
                return 200 + this.phone.price();
            }
    
            @Override
            public void function() {
                phone.function();
                System.out.println("增强了MP3播放功能");
            }
        }
    
        static class ShotPhoneDecorator implements PhoneDecorator {
    
            private Phone phone;
    
            public ShotPhoneDecorator(Phone phone) {
                this.phone = phone;
            }
    
    
            public ShotPhoneDecorator() {
            }
    
            public void setPhone(Phone phone) {
                this.phone = phone;
            }
    
            @Override
            public int price() {
                return this.phone.price();
            }
    
            @Override
            public void function() {
                phone.function();
                System.out.println("增强了拍摄功能");
            }
        }
    }

    测试结果:

    华为手机的基础功能
    1000
    华为手机的基础功能
    增强了MP3播放功能
    1200
    华为手机的基础功能
    增强了MP3播放功能
    增强了拍摄功能
    1200
    cn.echo42.decorator.Client$MediaPlayerPhoneDecorator@677327b6
    
    Process finished with exit code 0
  • 相关阅读:
    [React Testing] Intro to Shallow Rendering
    人物-IT-马化腾:马化腾
    人物-IT-雷军:雷军
    Less:Less(CSS预处理语言)
    文学-谚语-英文谚语:英文谚语
    文明-墓-太阳墓:太阳墓
    地理-撒哈拉之眼:撒哈拉之眼
    生物-海底人:海底人
    地理-蓝洞:蓝洞
    文明-根达亚文明:根达亚文明
  • 原文地址:https://www.cnblogs.com/mindzone/p/13398416.html
Copyright © 2020-2023  润新知