首页 > 其他分享 >软件设计实验11

软件设计实验11

时间:2022-10-16 10:44:49浏览次数:32  
标签:11 receiveCall cout 软件设计 void Phone 实验 public JarPhone

实验11:装饰模式

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

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

直接放源码:

#include<iostream>
#include<string>
using namespace std;
class Phone{
public:virtual void receiveCall()=0;
};

class SimplePhone :public Phone{
public:void receiveCall(){
        cout << "接受来电,电话响了" << endl;
    }
};

class PhoneDecorator :public Phone{
private:Phone *pho;
public:PhoneDecorator(Phone *p){
        if (p != NULL){
            pho = p;
        }
        else{
            pho = new SimplePhone();

        }
    }
    void receiveCall(){
        pho->receiveCall();
    }
};
class JarPhone:public PhoneDecorator{
private:Phone *p;
public: JarPhone(Phone *p):PhoneDecorator(p) {
        // TODO Auto-generated constructor stub
        this->p = p;
    }
    void receiveCall(){
        p->receiveCall();
        cout<<"接受来电,手机震动"<<endl;
    }

};
class ComplexPhone :public PhoneDecorator{
private:Phone *p;
public: ComplexPhone(Phone *p) :PhoneDecorator(p) {
        // TODO Auto-generated constructor stub
        this->p = p;
    }
    void receiveCall(){
        p->receiveCall();
        cout << "接受来电,灯光闪烁" << endl;
    }

};
int main(){
    Phone *p = new SimplePhone();
    p->receiveCall();
    cout<<"Simple"<<endl;
    Phone *p1 = new JarPhone(p);
    p1->receiveCall();
    cout<<"JarPhone"<<endl;
    Phone *p2 = new ComplexPhone(p1);
    p2->receiveCall();
    cout<<"ComplexPhone"<<endl;
}

  

标签:11,receiveCall,cout,软件设计,void,Phone,实验,public,JarPhone
From: https://www.cnblogs.com/lyf3701/p/16795744.html

相关文章

  • 电力系统机组组合优化调度(IEEE14节点、IEEE30节点、IEEE118节点)
           目录​​1概述​​​​2知识点学习​​​​3运行结果​​​​3.1算例1——IEEE14节点​​​​3.2算例2——IEEE30节点​​​​ 3.3算例3——IEE......
  • 实验二——类与对象(2)
    task2:array<int,5>x3<x1>;语句在devc的环境下可能会报错,改成array<int,5>x3(x1)可正确编译。task3:to_string的功能是将数字常量转换为字符串,还有就是要注意,静态变量类外......
  • 《剑指offer》day11
    删除链表的节点题目描述思路基本操作代码实现/***Definitionforsingly-linkedlist.*publicclassListNode{*intval;*ListNodenext;*......
  • 实验4:开源控制器实践——OpenDaylight
    一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。二、实验环境Ubuntu20.04Desktopamd64三、实验要......
  • Leetcode简单题背后的数学规律 | LCP 11. 期望个数统计
    最近签到打卡,每日额外再刷两道题攒积分。遇到一个简单题LCP11.期望个数统计,挺有意思的,记录一下分析过程并重温概率学知识。题目给定n个数的数组scores,小A和小B负责......
  • 实验4:开源控制器实践——OpenDaylight
     一、实验目的能够独立完成OpenDaylight控制器的安装配置;能够使用Postman工具调用OpenDaylightAPI接口下发流表。 二、实验环境Ubuntu20.04Desktopamd64三、实......
  • 实验1 C语言开发环境使用和编程初体验
    #include<stdio.h>intmain(){printf("O\n");printf("<H>\n");printf("II\n");return0;}#include<stdio.h>intmain(){printf("O\n");pri......
  • Visual Studio扩展开发——清理实验环境
    清理实验环境如果您正在开发多个扩展,或者只是使用不同版本的扩展代码来探索结果,那么您的实验环境可能会停止按应有的方式工作。在这种情况下,您应该运行重置脚本。它称为重......
  • 实验4:开源控制器实践——OpenDaylight
    一是Mininet拓扑生成并连接控制器的结果二是Mininet中ping测试截图,并体现个人信息,其余文字请勿赘述;进阶要求为选做,有完成的同学请提交所整理的API文档(选主要的即可,例......
  • P1186 玛丽卡
    #include<bits/stdc++.h>usingnamespacestd;constintinf=0x3f3f3f3f;intn,m;intd1[10001];intd2[10001];intv[10001];intd[10001];intpt[10001];intto......