菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

软件设计-Tutorial11

[实验任务一]:手机功能的升级

用装饰模式模拟手机功能的升级过程:简单的手机(SimplePhone)在接收来电时,会发出声音提醒主人;而JarPhone除了声音还能振动;更高级的手机(ComplexPhone)除了声音、振动外,还有灯光闪烁提示。

类图:

复制代码
```mermaid
classDiagram
    class Phone {
        <<interface>>
        +receiveCall()
    }
    class SimplePhone {
        +receiveCall()
    }
    class PhoneDecorator {
        <<abstract>>
        #phone: Phone
        +PhoneDecorator(phone: Phone)
        +receiveCall()
    }
    class JarPhone {
        +JarPhone(phone: Phone)
        +receiveCall()
    }
    class ComplexPhone {
        +ComplexPhone(phone: Phone)
        +receiveCall()
    }
    class PhoneTest {
        +main(args: String[])
    }

    Phone <|.. SimplePhone : implements
    Phone <|.. PhoneDecorator : implements
    PhoneDecorator <|-- JarPhone : extends
    PhoneDecorator <|-- ComplexPhone : extends
    PhoneDecorator o-- Phone : has
    PhoneTest ..> Phone : uses
    PhoneTest ..> SimplePhone : uses
    PhoneTest ..> JarPhone : uses
    PhoneTest ..> ComplexPhone : uses
```
复制代码
package Tutorial11;

public interface Phone {
    void receiveCall();  // 接收来电
}
复制代码
package Tutorial11;

abstract public class PhoneDecorator implements Phone{
    protected Phone phone;  // 被装饰的手机

    public PhoneDecorator(Phone phone) {
        this.phone = phone;
    }

    @Override
    public void receiveCall() {
        phone.receiveCall();  // 调用被装饰对象的功能
    }
}
复制代码
package Tutorial11;

public class SimplePhone implements Phone{
    @Override
    public void receiveCall() {
        System.out.println("手机响铃:来电提醒!");
    }
}
复制代码
package Tutorial11;

public class JarPhone extends PhoneDecorator{
    public JarPhone(Phone phone) {
        super(phone);
    }

    @Override
    public void receiveCall() {
        super.receiveCall();  // 调用父类的功能(响铃)
        System.out.println("手机震动:振动提醒!");
    }
}
复制代码
复制代码
package Tutorial11;

public class ComplexPhone extends PhoneDecorator{
    public ComplexPhone(Phone phone) {
        super(phone);
    }

    @Override
    public void receiveCall() {
        super.receiveCall();  // 调用父类的功能(响铃和振动)
        System.out.println("手机闪光:灯光闪烁提醒!");
    }
}
复制代码
复制代码
package Tutorial11;

public class PhoneTest {
    public static void main(String[] args) {
        // 创建基础手机(SimplePhone)
        Phone simplePhone = new SimplePhone();
        simplePhone.receiveCall();  // 只有响铃提醒

        System.out.println("---------- 升级为JarPhone ----------");

        // 创建升级后的手机(JarPhone,具有响铃和振动功能)
        Phone jarPhone = new JarPhone(simplePhone);
        jarPhone.receiveCall();  // 响铃 + 振动提醒

        System.out.println("---------- 升级为ComplexPhone ----------");

        // 创建最终版本的手机(ComplexPhone,具有响铃、振动和闪光功能)
        Phone complexPhone = new ComplexPhone(jarPhone);
        complexPhone.receiveCall();  // 响铃 + 振动 + 闪光提醒
    }
}
复制代码

 

posted on   hhmzd233  阅读(2)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2023-11-06 3.增删改查的实现(1)
点击右上角即可分享
微信分享提示